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

File LazyXWikiDocument.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

8
58
41
1
368
259
47
0.81
1.41
41
1.15

Classes

Class Line # Actions
LazyXWikiDocument 63 58 0% 47 107
0.00%
 

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.doc;
21   
22    import java.util.Date;
23    import java.util.List;
24    import java.util.Locale;
25    import java.util.Map;
26   
27    import org.dom4j.Document;
28    import org.suigeneris.jrcs.rcs.Version;
29    import org.xwiki.context.Execution;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.model.reference.EntityReference;
32    import org.xwiki.rendering.block.XDOM;
33    import org.xwiki.rendering.syntax.Syntax;
34   
35    import com.xpn.xwiki.XWikiContext;
36    import com.xpn.xwiki.XWikiException;
37    import com.xpn.xwiki.objects.BaseObject;
38    import com.xpn.xwiki.objects.classes.BaseClass;
39    import com.xpn.xwiki.store.XWikiStoreInterface;
40    import com.xpn.xwiki.web.Utils;
41   
42    /**
43    * Read only lazy loading document.
44    * <p>
45    * The following informations are taken into account:
46    * <ul>
47    * <li>document reference: the absolute reference of the document</li>
48    * <li>document language: if provided the proper language version of the document is loaded. If not the default one is
49    * loaded.</li>
50    * <li>document version: if provided the proper version of the document is loaded. Also make extra sure to bypass cache
51    * storage for remote observation use case.</li>
52    * </ul>
53    * <p>
54    * originalDocument remain the property of {@link LazyXWikiDocument} object and is not taken from the lazy loaded
55    * document since it depends on how this {@link XWikiDocument} object is used (its technical meaning is its state in the
56    * database before any modification but in the case of observation it's used as the previous version of the document).
57    * TODO: we should probably think about a separation of theses two notions in something more clear, something for the
58    * new model.
59    *
60    * @version $Id: e215d894834917f828fdedad1e54c9872379f377 $
61    * @since 2.0M4
62    */
 
63    public class LazyXWikiDocument extends XWikiDocument
64    {
65    /**
66    * The real document.
67    */
68    private XWikiDocument document;
69   
70    /**
71    * @deprecated use {@link #LazyXWikiDocument(DocumentReference)} instead
72    */
 
73  0 toggle @Deprecated
74    public LazyXWikiDocument()
75    {
76   
77    }
78   
 
79  0 toggle public LazyXWikiDocument(DocumentReference documentReference)
80    {
81  0 super(documentReference);
82    }
83   
84    /**
85    * Load and return the document in the database.
86    *
87    * @return the real document
88    */
 
89  0 toggle private XWikiDocument getDocument()
90    {
91  0 if (this.document == null) {
92    // get context
93  0 XWikiContext context =
94    (XWikiContext) Utils.getComponent(Execution.class).getContext().getProperty("xwikicontext");
95   
96  0 XWikiDocument doc = new XWikiDocument(getDocumentReference(), getLocale());
97   
98  0 String currentWiki = context.getWikiId();
99  0 try {
100    // Put context in document wiki
101  0 context.setWikiId(getDocumentReference().getWikiReference().getName());
102   
103  0 if (this.version == null) {
104  0 this.document = context.getWiki().getDocument(doc, context);
105    } else {
106    // Force bypassing the cache to make extra sure we get the last version of the document. This is
107    // safer for example when LazyXWikiDocument is used in the context of remote events. This is for
108    // properly emulate events, XWikiCacheStore is taking care itself of invalidating itself.
109  0 doc = context.getWiki().getNotCacheStore().loadXWikiDoc(doc, context);
110  0 if (doc.getRCSVersion().equals(this.version)) {
111    // It's the last version of the document
112  0 this.document = doc;
113    } else {
114    // It's not the last version of the document, ask versioning store.
115  0 try {
116  0 this.document =
117    context.getWiki().getVersioningStore()
118    .loadXWikiDoc(doc, this.version.toString(), context);
119    } catch (XWikiException e) {
120    // If the proper can't be found, return the last version of the document
121  0 this.document = doc;
122    }
123    }
124    }
125    } catch (XWikiException e) {
126  0 throw new RuntimeException("Failed to get document [" + this + "]", e);
127    } finally {
128  0 context.setWikiId(currentWiki);
129    }
130    }
131   
132  0 return this.document;
133    }
134   
 
135  0 toggle @Override
136    public Version getRCSVersion()
137    {
138  0 if (this.version == null) {
139  0 return getDocument().getRCSVersion();
140    } else {
141  0 return this.version;
142    }
143    }
144   
 
145  0 toggle @Override
146    public String getContent()
147    {
148  0 return getDocument().getContent();
149    }
150   
 
151  0 toggle @Override
152    public Map<DocumentReference, List<BaseObject>> getXObjects()
153    {
154  0 return getDocument().getXObjects();
155    }
156   
 
157  0 toggle @Override
158    public BaseClass getXClass()
159    {
160  0 return getDocument().getXClass();
161    }
162   
 
163  0 toggle @Override
164    public String getXClassXML()
165    {
166  0 return getDocument().getXClassXML();
167    }
168   
 
169  0 toggle @Override
170    public DocumentReference getAuthorReference()
171    {
172  0 return getDocument().getAuthorReference();
173    }
174   
 
175  0 toggle @Override
176    public DocumentReference getContentAuthorReference()
177    {
178  0 return getDocument().getContentAuthorReference();
179    }
180   
 
181  0 toggle @Override
182    public DocumentReference getCreatorReference()
183    {
184  0 return super.getCreatorReference();
185    }
186   
 
187  0 toggle @Override
188    public Date getDate()
189    {
190  0 return getDocument().getDate();
191    }
192   
 
193  0 toggle @Override
194    public Date getCreationDate()
195    {
196  0 return getDocument().getCreationDate();
197    }
198   
 
199  0 toggle @Override
200    public Date getContentUpdateDate()
201    {
202  0 return getDocument().getContentUpdateDate();
203    }
204   
 
205  0 toggle @Override
206    public String getMeta()
207    {
208  0 return getDocument().getMeta();
209    }
210   
 
211  0 toggle @Override
212    public String getTitle()
213    {
214  0 return getDocument().getTitle();
215    }
216   
 
217  0 toggle @Override
218    public String getFormat()
219    {
220  0 return getDocument().getFormat();
221    }
222   
 
223  0 toggle @Override
224    public Locale getDefaultLocale()
225    {
226  0 return getDocument().getDefaultLocale();
227    }
228   
 
229  0 toggle @Override
230    public int getTranslation()
231    {
232  0 return getDocument().getTranslation();
233    }
234   
 
235  0 toggle @Override
236    public String getCustomClass()
237    {
238  0 return getDocument().getCustomClass();
239    }
240   
 
241  0 toggle @Override
242    public EntityReference getRelativeParentReference()
243    {
244  0 return getDocument().getRelativeParentReference();
245    }
246   
 
247  0 toggle @Override
248    public DocumentReference getParentReference()
249    {
250  0 return getDocument().getParentReference();
251    }
252   
 
253  0 toggle @Override
254    public int getElements()
255    {
256  0 return getDocument().getElements();
257    }
258   
 
259  0 toggle @Override
260    public String getDefaultTemplate()
261    {
262  0 return getDocument().getDefaultTemplate();
263    }
264   
 
265  0 toggle @Override
266    public String getValidationScript()
267    {
268  0 return getDocument().getValidationScript();
269    }
270   
 
271  0 toggle @Override
272    public String getComment()
273    {
274  0 return getDocument().getComment();
275    }
276   
 
277  0 toggle @Override
278    public Syntax getSyntax()
279    {
280  0 return getDocument().getSyntax();
281    }
282   
 
283  0 toggle @Override
284    public Boolean isHidden()
285    {
286  0 return getDocument().isHidden();
287    }
288   
 
289  0 toggle @Override
290    public XWikiDocumentArchive getDocumentArchive()
291    {
292  0 return getDocument().getDocumentArchive();
293    }
294   
 
295  0 toggle @Override
296    public void loadArchive(XWikiContext context) throws XWikiException
297    {
298  0 getDocument().loadArchive(context);
299    }
300   
 
301  0 toggle @Override
302    public XWikiDocumentArchive getDocumentArchive(XWikiContext context) throws XWikiException
303    {
304  0 return getDocument().getDocumentArchive(context);
305    }
306   
 
307  0 toggle @Override
308    public XWikiStoreInterface getStore()
309    {
310  0 return getDocument().getStore();
311    }
312   
 
313  0 toggle @Override
314    public long getId()
315    {
316  0 return getDocument().getId();
317    }
318   
 
319  0 toggle @Override
320    public XWikiStoreInterface getStore(XWikiContext context)
321    {
322  0 return getDocument().getStore(context);
323    }
324   
 
325  0 toggle @Override
326    public XDOM getXDOM()
327    {
328  0 return getDocument().getXDOM();
329    }
330   
 
331  0 toggle @Override
332    public String getTags(XWikiContext context)
333    {
334  0 return getDocument().getTags(context);
335    }
336   
 
337  0 toggle @Override
338    public List<String> getTagsPossibleValues(XWikiContext context)
339    {
340  0 return getDocument().getTagsPossibleValues(context);
341    }
342   
 
343  0 toggle @Override
344    public boolean isFromCache()
345    {
346  0 return getDocument().isFromCache();
347    }
348   
 
349  0 toggle @Override
350    public boolean isMostRecent()
351    {
352  0 return getDocument().isMostRecent();
353    }
354   
 
355  0 toggle @Override
356    public Document toXMLDocument(boolean bWithObjects, boolean bWithRendering, boolean bWithAttachmentContent,
357    boolean bWithVersions, XWikiContext context) throws XWikiException
358    {
359  0 return getDocument()
360    .toXMLDocument(bWithObjects, bWithRendering, bWithAttachmentContent, bWithVersions, context);
361    }
362   
 
363  0 toggle @Override
364    public Object getWikiNode()
365    {
366  0 return getDocument().getWikiNode();
367    }
368    }