1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wiki.internal.descriptor.document

File XWikiServerXwikiDocumentInitializer.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

4
24
3
1
127
72
6
0.25
8
3
2

Classes

Class Line # Actions
XWikiServerXwikiDocumentInitializer 50 24 0% 6 4
0.8709677587.1%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.wiki.internal.descriptor.document;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Provider;
25    import javax.inject.Singleton;
26   
27    import org.slf4j.Logger;
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.container.Container;
30    import org.xwiki.container.Request;
31    import org.xwiki.container.servlet.ServletRequest;
32   
33    import com.xpn.xwiki.XWiki;
34    import com.xpn.xwiki.XWikiContext;
35    import com.xpn.xwiki.XWikiException;
36    import com.xpn.xwiki.doc.XWikiDocument;
37    import com.xpn.xwiki.internal.mandatory.AbstractMandatoryDocumentInitializer;
38    import com.xpn.xwiki.objects.BaseObject;
39    import com.xpn.xwiki.user.api.XWikiRightService;
40   
41    /**
42    * Initialize main wiki descriptor with what come from the first request.
43    *
44    * @version $Id: 7681f936f8f24a6991e3aeb1d3ffee689af7c744 $
45    * @since 8.0RC1
46    */
47    @Component
48    @Named("XWiki.XWikiServerXwiki")
49    @Singleton
 
50    public class XWikiServerXwikiDocumentInitializer extends AbstractMandatoryDocumentInitializer
51    {
52    /**
53    * The name of the mandatory document.
54    */
55    public static final String DOCUMENT_NAME = "XWikiServerXwiki";
56   
57    /**
58    * Used to access the XWiki model.
59    */
60    @Inject
61    private Provider<XWikiContext> contextProvider;
62   
63    @Inject
64    private Logger logger;
65   
66    @Inject
67    private Container container;
68   
69    /**
70    * Default constructor.
71    */
 
72  60 toggle public XWikiServerXwikiDocumentInitializer()
73    {
74  60 super(XWiki.SYSTEM_SPACE, DOCUMENT_NAME);
75    }
76   
 
77  60 toggle @Override
78    public boolean isMainWikiOnly()
79    {
80    // Initialize it only for the main wiki.
81  60 return true;
82    }
83   
 
84  88 toggle @Override
85    public boolean updateDocument(XWikiDocument document)
86    {
87  88 boolean needsUpdate = false;
88   
89    // Add a descriptor if none already exist
90  88 if (document.getXObject(XWikiServerClassDocumentInitializer.SERVER_CLASS) == null) {
91  34 XWikiContext xcontext = this.contextProvider.get();
92   
93  34 try {
94  34 BaseObject xobject = document.newXObject(XWikiServerClassDocumentInitializer.SERVER_CLASS, xcontext);
95   
96  34 xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_DESCRIPTION, "Main wiki");
97  34 xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_HOMEPAGE, "Main.WebHome");
98  34 xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_LANGUAGE, "en");
99  34 xobject.setIntValue(XWikiServerClassDocumentInitializer.FIELD_SECURE, 0);
100  34 xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_STATE, "active");
101  34 xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_VISIBILITY, "public");
102  34 xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_OWNER,
103    XWikiRightService.SUPERADMIN_USER_FULLNAME);
104  34 xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME, "Home");
105   
106  34 Request request = this.container.getRequest();
107  34 if (request instanceof ServletRequest) {
108  0 ServletRequest servletRequest = (ServletRequest) request;
109  0 xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER,
110    servletRequest.getHttpServletRequest().getServerName());
111    } else {
112  34 xobject.setStringValue(XWikiServerClassDocumentInitializer.FIELD_SERVER, "localhost");
113    }
114   
115  34 needsUpdate = true;
116    } catch (XWikiException e) {
117  0 this.logger.error("Faied to initialize main wiki descriptor", e);
118    }
119    }
120   
121    // Note: We don't set a title since there's a sheet computing a proper title.
122  88 needsUpdate |= setDocumentFields(document, "");
123   
124  88 return needsUpdate;
125    }
126   
127    }