1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.doc

File AbstractMandatoryDocumentInitializer.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
56% of files have more coverage

Code metrics

22
39
10
1
204
110
24
0.62
3.9
10
2.4

Classes

Class Line # Actions
AbstractMandatoryDocumentInitializer 45 39 0% 24 18
0.7464788674.6%
 

Contributing tests

This file is covered by 18 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.doc;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24   
25    import org.apache.commons.lang3.StringUtils;
26    import org.xwiki.component.phase.Initializable;
27    import org.xwiki.component.phase.InitializationException;
28    import org.xwiki.model.EntityType;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.model.reference.DocumentReferenceResolver;
31    import org.xwiki.model.reference.EntityReference;
32    import org.xwiki.model.reference.LocalDocumentReference;
33    import org.xwiki.model.reference.WikiReference;
34    import org.xwiki.sheet.SheetBinder;
35    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
36   
37    import com.xpn.xwiki.user.api.XWikiRightService;
38   
39    /**
40    * Base class for standard mandatory document initializers.
41    *
42    * @version $Id: ecd913168d2013e2fb01f00ce66c152cb3114924 $
43    * @since 9.0RC1
44    */
 
45    public abstract class AbstractMandatoryDocumentInitializer implements MandatoryDocumentInitializer, Initializable
46    {
47    /**
48    * Used to associate a document with a document sheet.
49    */
50    @Inject
51    @Named("document")
52    protected SheetBinder documentSheetBinder;
53   
54    /**
55    * Used to get the main wiki.
56    */
57    @Inject
58    protected WikiDescriptorManager wikiDescriptorManager;
59   
60    @Inject
61    protected DocumentReferenceResolver<EntityReference> resolver;
62   
63    /**
64    * @see #getDocumentReference()
65    */
66    private EntityReference reference;
67   
68    private String title;
69   
70    /**
71    * @param reference the reference of the document to update. Can be either local or absolute depending if the
72    * document is associated to a specific wiki or not
73    */
 
74  2093 toggle public AbstractMandatoryDocumentInitializer(EntityReference reference)
75    {
76  2093 this(reference, null);
77    }
78   
79    /**
80    * @param reference the reference of the document to update. Can be either local or absolute depending if the
81    * document is associated to a specific wiki or not
82    * @param title the title of the document
83    */
 
84  2095 toggle public AbstractMandatoryDocumentInitializer(EntityReference reference, String title)
85    {
86  2095 this.reference = reference;
87  2095 this.title = title;
88    }
89   
 
90  2095 toggle @Override
91    public void initialize() throws InitializationException
92    {
93    // If a local reference was specified but isMainWikiOnly() is true, then convert to a main wiki reference.
94  2095 if (this.reference != null && this.reference.extractReference(EntityType.WIKI) == null && isMainWikiOnly()) {
95  243 synchronized (this) {
96  243 if (this.reference.extractReference(EntityType.WIKI) == null) {
97    // Convert to main wiki reference
98  243 EntityReference mainWikiEntityReference = this.resolver.resolve(this.reference,
99    new WikiReference(this.wikiDescriptorManager.getMainWikiId()));
100   
101  243 this.reference = mainWikiEntityReference;
102    }
103    }
104    }
105    }
106   
 
107  4551 toggle @Override
108    public EntityReference getDocumentReference()
109    {
110  4551 return this.reference;
111    }
112   
113    // Override
114   
 
115  4249 toggle @Override
116    public boolean updateDocument(XWikiDocument document)
117    {
118  4249 return updateDocumentFields(document, getTitle());
119    }
120   
 
121  0 toggle protected boolean updateTitle(XWikiDocument document)
122    {
123  0 if (StringUtils.isEmpty(document.getTitle())) {
124  0 String returnedTitle = getTitle();
125   
126  0 if (!StringUtils.isEmpty(returnedTitle)) {
127  0 document.setTitle(getTitle());
128    }
129   
130  0 return true;
131    }
132   
133  0 return false;
134    }
135   
 
136  4249 toggle protected String getTitle()
137    {
138  4249 return this.title;
139    }
140   
141    /**
142    * @return true if the passed reference should be resolved to the main wiki instead of the local one. The default is
143    * {@code false}. This is ignored if the passed reference already contains the wiki information.
144    */
 
145  1841 toggle protected boolean isMainWikiOnly()
146    {
147  1841 return false;
148    }
149   
150    // Helpers
151   
152    /**
153    * Set the fields of the document passed as parameter. Can generate content for both XWiki Syntax 1.0 and XWiki
154    * Syntax 2.0. If new documents are set to be created in XWiki Syntax 1.0 then generate XWiki 1.0 Syntax otherwise
155    * generate XWiki Syntax 2.0.
156    *
157    * @param document the document
158    * @param title the page title to set (if null or blank the title won't be set)
159    * @return true if the document has been modified, false otherwise
160    */
 
161  4252 toggle protected boolean updateDocumentFields(XWikiDocument document, String title)
162    {
163  4252 boolean needsUpdate = false;
164   
165  4252 if (document.getCreatorReference() == null) {
166  1033 document.setCreator(XWikiRightService.SUPERADMIN_USER);
167  1033 needsUpdate = true;
168    }
169  4252 if (document.getAuthorReference() == null) {
170  1033 document.setAuthorReference(document.getCreatorReference());
171  1033 needsUpdate = true;
172    }
173   
174  4252 if (document.getParentReference() == null) {
175    // Use the current document's space homepage and default document name.
176  1033 EntityReference spaceReference = getDocumentReference().extractReference(EntityType.SPACE);
177  1033 DocumentReference fullReference = this.resolver.resolve(null, spaceReference);
178  1033 EntityReference localReference = new LocalDocumentReference(fullReference);
179  1033 document.setParentReference(localReference);
180  1033 needsUpdate = true;
181    }
182   
183  4252 if (StringUtils.isNotEmpty(title) && StringUtils.isBlank(document.getTitle())) {
184  5 document.setTitle(title);
185  5 needsUpdate = true;
186    }
187   
188  4252 if (!document.isHidden()) {
189  1033 document.setHidden(true);
190  1033 needsUpdate = true;
191    }
192   
193  4252 return needsUpdate;
194    }
195   
196    /**
197    * @param value the {@link Boolean} value to convert.
198    * @return the converted <code>int</code> value.
199    */
 
200  0 toggle protected int intFromBoolean(Boolean value)
201    {
202  0 return value == null ? -1 : (value.booleanValue() ? 1 : 0);
203    }
204    }