1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.mandatory

File XWikiAllGroupDocumentInitializer.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

10
22
2
1
119
71
8
0.36
11
2
4

Classes

Class Line # Actions
XWikiAllGroupDocumentInitializer 50 22 0% 8 1
0.970588297.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 com.xpn.xwiki.internal.mandatory;
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.model.reference.DocumentReference;
30    import org.xwiki.model.reference.EntityReference;
31    import org.xwiki.model.reference.LocalDocumentReference;
32    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
33   
34    import com.xpn.xwiki.XWiki;
35    import com.xpn.xwiki.XWikiContext;
36    import com.xpn.xwiki.XWikiException;
37    import com.xpn.xwiki.doc.MandatoryDocumentInitializer;
38    import com.xpn.xwiki.doc.XWikiDocument;
39    import com.xpn.xwiki.objects.BaseObject;
40   
41    /**
42    * Create the mandatory XWikiAllGroup document in the current wiki.
43    *
44    * @version $Id: f16e372cc93d33bdd9f590fa8e62e3185e2f5578 $
45    * @since 6.4.2
46    */
47    @Component
48    @Singleton
49    @Named("XWiki.XWikiAllGroup")
 
50    public class XWikiAllGroupDocumentInitializer implements MandatoryDocumentInitializer
51    {
52    private static final String DOCUMENT_NAME = "XWikiAllGroup";
53   
54    private static final String CLASS_NAME = "XWikiGroups";
55   
56    @Inject
57    private WikiDescriptorManager wikiDescriptorManager;
58   
59    @Inject
60    private Provider<XWikiContext> xcontextProvider;
61   
62    @Inject
63    private Logger logger;
64   
 
65  92 toggle @Override
66    public EntityReference getDocumentReference()
67    {
68  92 return new LocalDocumentReference(XWiki.SYSTEM_SPACE, DOCUMENT_NAME);
69    }
70   
 
71  92 toggle @Override
72    public boolean updateDocument(XWikiDocument document)
73    {
74  92 boolean needsUpdate = false;
75   
76    // Create the reference to the parent of the current document which is also the class of the object to create
77  92 LocalDocumentReference classReference = new LocalDocumentReference(XWiki.SYSTEM_SPACE, CLASS_NAME);
78   
79    // Ensure the document has a creator
80  92 if (document.getCreatorReference() == null) {
81  38 document.setCreatorReference(new DocumentReference(wikiDescriptorManager.getMainWikiId(),
82    XWiki.SYSTEM_SPACE, "superadmin"));
83  38 needsUpdate = true;
84    }
85   
86    // Ensure the document has an author
87  92 if (document.getAuthorReference() == null) {
88  38 document.setAuthorReference(document.getCreatorReference());
89  38 needsUpdate = true;
90    }
91   
92    // Ensure the document has a parent
93  92 if (document.getParentReference() == null) {
94  38 document.setParentReference(classReference);
95  38 needsUpdate = true;
96    }
97   
98    // Ensure the document is hidden, like every technical document
99  92 if (!document.isHidden()) {
100  38 document.setHidden(true);
101  38 needsUpdate = true;
102    }
103   
104    // Ensure the document has an XWikiGroups object
105  92 if (document.getXObject(classReference) == null) {
106  38 try {
107  38 BaseObject obj = document.newXObject(classReference, xcontextProvider.get());
108  38 obj.setStringValue("member", "");
109  38 needsUpdate = true;
110    } catch (XWikiException e) {
111  0 logger.error(
112    String.format("Impossible to add an object to the document XWiki.XWikiAllGroups in the wiki [%s].",
113    document.getDocumentReference().getWikiReference().getName()), e);
114    }
115    }
116   
117  92 return needsUpdate;
118    }
119    }