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

File AbstractMandatoryDocumentInitializer.java

 

Coverage histogram

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

Code metrics

24
48
8
1
226
115
22
0.46
6
8
2.75

Classes

Class Line # Actions
AbstractMandatoryDocumentInitializer 48 48 0% 22 3
0.962596.2%
 

Contributing tests

This file is covered by 8 tests. .

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   
25    import org.apache.commons.lang3.StringUtils;
26    import org.xwiki.model.EntityType;
27    import org.xwiki.model.reference.DocumentReference;
28    import org.xwiki.model.reference.DocumentReferenceResolver;
29    import org.xwiki.model.reference.EntityReference;
30    import org.xwiki.model.reference.LocalDocumentReference;
31    import org.xwiki.model.reference.WikiReference;
32    import org.xwiki.sheet.SheetBinder;
33    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
34   
35    import com.xpn.xwiki.XWiki;
36    import com.xpn.xwiki.doc.MandatoryDocumentInitializer;
37    import com.xpn.xwiki.doc.XWikiDocument;
38    import com.xpn.xwiki.objects.classes.BaseClass;
39    import com.xpn.xwiki.objects.classes.BooleanClass;
40    import com.xpn.xwiki.user.api.XWikiRightService;
41   
42    /**
43    * Base class for standard class providers.
44    *
45    * @version $Id: a393420590eaaec2c5c415a465a44731c07874bf $
46    * @since 4.3M1
47    */
 
48    public abstract class AbstractMandatoryDocumentInitializer implements MandatoryDocumentInitializer
49    {
50    /**
51    * Used to associate a document with a document sheet.
52    */
53    @Inject
54    @Named("document")
55    protected SheetBinder documentSheetBinder;
56   
57    /**
58    * Used to get the main wiki.
59    */
60    @Inject
61    protected WikiDescriptorManager wikiDescriptorManager;
62   
63    @Inject
64    protected DocumentReferenceResolver<EntityReference> resolver;
65   
66    /**
67    * @see #getDocumentReference()
68    */
69    private EntityReference reference;
70   
71    /**
72    * @param reference the reference of the document to update. Can be either local or absolute depending if the
73    * document is associated to a specific wiki or not
74    */
 
75  1339 toggle public AbstractMandatoryDocumentInitializer(EntityReference reference)
76    {
77  1339 this.reference = reference;
78    }
79   
80    /**
81    * @param spaceName the space name of the document
82    * @param documentName the document name of the document
83    */
 
84  852 toggle public AbstractMandatoryDocumentInitializer(String spaceName, String documentName)
85    {
86  852 this(new EntityReference(documentName, EntityType.DOCUMENT, new EntityReference(spaceName, EntityType.SPACE)));
87    }
88   
 
89  2092 toggle @Override
90    public EntityReference getDocumentReference()
91    {
92    // If a local reference was specified but isMainWikiOnly() is true, then convert to a main wiki reference.
93  2092 if (this.reference != null && this.reference.extractReference(EntityType.WIKI) == null && isMainWikiOnly()) {
94  125 synchronized (this) {
95  125 if (this.reference.extractReference(EntityType.WIKI) == null) {
96    // Convert to main wiki reference
97  125 EntityReference mainWikiEntityReference =
98    this.resolver.resolve(this.reference,
99    new WikiReference(this.wikiDescriptorManager.getMainWikiId()));
100   
101  125 this.reference = mainWikiEntityReference;
102    }
103    }
104    }
105   
106  2092 return this.reference;
107    }
108   
109    /**
110    * @return true if the passed reference should be resolved to the main wiki instead of the local one. The default is
111    * {@code false}. This is ignored if the passed reference already contains the wiki information.
112    */
 
113  1862 toggle protected boolean isMainWikiOnly()
114    {
115  1862 return false;
116    }
117   
118    /**
119    * Set the fields of the class document passed as parameter. Can generate content for both XWiki Syntax 1.0 and
120    * XWiki Syntax 2.0. If new documents are set to be created in XWiki Syntax 1.0 then generate XWiki 1.0 Syntax
121    * otherwise generate XWiki Syntax 2.0.
122    *
123    * @param document the document
124    * @param title the page title to set
125    * @return true if the document has been modified, false otherwise
126    */
 
127  1893 toggle protected boolean setClassDocumentFields(XWikiDocument document, String title)
128    {
129  1893 boolean needsUpdate = false;
130   
131    // Set the parent since it is different from the current document's space homepage.
132  1893 if (document.getParentReference() == null) {
133  789 needsUpdate = true;
134  789 document.setParentReference(new LocalDocumentReference(XWiki.SYSTEM_SPACE, "XWikiClasses"));
135    }
136   
137  1893 needsUpdate |= setDocumentFields(document, title);
138   
139    // Use ClassSheet to display the class document if no other sheet is explicitly specified.
140  1893 if (this.documentSheetBinder.getSheets(document).isEmpty()) {
141  750 String wikiName = document.getDocumentReference().getWikiReference().getName();
142  750 DocumentReference sheet = new DocumentReference(wikiName, XWiki.SYSTEM_SPACE, "ClassSheet");
143  750 needsUpdate |= this.documentSheetBinder.bind(document, sheet);
144    }
145   
146  1893 return needsUpdate;
147    }
148   
149    /**
150    * Set the fields of the document passed as parameter. Can generate content for both XWiki Syntax 1.0 and XWiki
151    * Syntax 2.0. If new documents are set to be created in XWiki Syntax 1.0 then generate XWiki 1.0 Syntax otherwise
152    * generate XWiki Syntax 2.0.
153    *
154    * @param document the document
155    * @param title the page title to set (if null or blank the title won't be set)
156    * @return true if the document has been modified, false otherwise
157    */
 
158  1984 toggle protected boolean setDocumentFields(XWikiDocument document, String title)
159    {
160  1984 boolean needsUpdate = false;
161   
162  1984 if (document.getCreatorReference() == null) {
163  826 needsUpdate = true;
164  826 document.setCreator(XWikiRightService.SUPERADMIN_USER);
165    }
166  1984 if (document.getAuthorReference() == null) {
167  826 needsUpdate = true;
168  826 document.setAuthorReference(document.getCreatorReference());
169    }
170   
171  1984 if (document.getParentReference() == null) {
172  37 needsUpdate = true;
173    // Use the current document's space homepage and default document name.
174  37 EntityReference spaceReference = getDocumentReference().extractReference(EntityType.SPACE);
175  37 DocumentReference fullReference = this.resolver.resolve(null, spaceReference);
176  37 EntityReference localReference = new LocalDocumentReference(fullReference);
177  37 document.setParentReference(localReference);
178    }
179   
180  1984 if (StringUtils.isBlank(document.getTitle())) {
181  880 needsUpdate = true;
182  880 document.setTitle(title);
183    }
184   
185  1984 if (!document.isHidden()) {
186  813 needsUpdate = true;
187  813 document.setHidden(true);
188    }
189   
190  1984 return needsUpdate;
191    }
192   
193    /**
194    * Set the default value of a boolean field of a XWiki class.
195    *
196    * @param baseClass the XWiki class.
197    * @param fieldName the name of the field.
198    * @param value the default value.
199    * @return true if <code>baseClass</code> modified.
200    */
 
201  89 toggle protected boolean updateBooleanClassDefaultValue(BaseClass baseClass, String fieldName, Boolean value)
202    {
203  89 boolean needsUpdate = false;
204   
205  89 BooleanClass bc = (BooleanClass) baseClass.get(fieldName);
206   
207  89 int old = bc.getDefaultValue();
208  89 int intvalue = intFromBoolean(value);
209   
210  89 if (intvalue != old) {
211  35 bc.setDefaultValue(intvalue);
212  35 needsUpdate = true;
213    }
214   
215  89 return needsUpdate;
216    }
217   
218    /**
219    * @param value the {@link Boolean} value to convert.
220    * @return the converted <code>int</code> value.
221    */
 
222  89 toggle protected int intFromBoolean(Boolean value)
223    {
224  89 return value == null ? -1 : (value.booleanValue() ? 1 : 0);
225    }
226    }