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

File XWikiDeletedDocument.java

 

Coverage histogram

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

Code metrics

10
48
27
1
388
173
35
0.73
1.78
27
1.3

Classes

Class Line # Actions
XWikiDeletedDocument 45 48 0% 35 16
0.811764781.2%
 

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.io.IOException;
23    import java.util.Date;
24    import java.util.Locale;
25   
26    import org.apache.commons.lang3.StringUtils;
27    import org.xwiki.localization.LocaleUtils;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.model.reference.DocumentReferenceResolver;
30   
31    import com.xpn.xwiki.XWikiContext;
32    import com.xpn.xwiki.XWikiException;
33    import com.xpn.xwiki.internal.store.hibernate.XWikiHibernateDeletedDocumentContent;
34    import com.xpn.xwiki.util.AbstractSimpleClass;
35    import com.xpn.xwiki.util.Util;
36    import com.xpn.xwiki.web.Utils;
37   
38    /**
39    * Archive of deleted document, stored in {@link com.xpn.xwiki.store.XWikiRecycleBinStoreInterface}. Immutable, because
40    * we don't need modify deleted document.
41    *
42    * @version $Id: 8868c6e289e723895801f435bc9de77d754e75cc $
43    * @since 1.2M1
44    */
 
45    public class XWikiDeletedDocument extends AbstractSimpleClass
46    {
47    /**
48    * Synthetic id.
49    */
50    private long id;
51   
52    /**
53    * @see XWikiDocument#getFullName()
54    */
55    private String fullName;
56   
57    /**
58    * @see XWikiDocument#getLocale()
59    */
60    private Locale locale;
61   
62    /**
63    * date of delete action.
64    */
65    private Date date;
66   
67    /**
68    * @see XWikiDeletedDocument#getDeleter()
69    */
70    private String deleter;
71   
72    private String xmlStore;
73   
74    private XWikiDeletedDocumentContent content;
75   
76    private String batchId;
77   
78    /**
79    * Default constructor. Used only in hibernate.
80    */
 
81  291 toggle protected XWikiDeletedDocument()
82    {
83    }
84   
85    /**
86    * @param fullName the local reference of the document
87    * @param locale the locale of the document
88    * @param storeType - the way to store the document
89    * @param deleter - user which delete document
90    * @param deleteDate - date of delete action
91    * @throws XWikiException if any error
92    * @since 9.0RC1
93    */
 
94  287 toggle private XWikiDeletedDocument(String fullName, Locale locale, String storeType, String deleter, Date deleteDate)
95    throws XWikiException
96    {
97  287 this.fullName = fullName;
98  287 this.locale = locale;
99  287 this.deleter = deleter;
100  287 this.date = deleteDate;
101  287 this.xmlStore = storeType;
102    }
103   
104    /**
105    * @param doc - deleted document
106    * @param deleter - user which delete document
107    * @param deleteDate - date of delete action
108    * @param context - used for environment
109    * @throws XWikiException if any error
110    */
 
111  0 toggle public XWikiDeletedDocument(XWikiDocument doc, String deleter, Date deleteDate, XWikiContext context)
112    throws XWikiException
113    {
114  0 this(doc.getFullName(), doc.getLocale(), null, deleter, deleteDate);
115   
116  0 setDocument(doc, context);
117    }
118   
119    /**
120    * @param fullName the local reference of the document
121    * @param locale the locale of the document
122    * @param storeType the way to store the document
123    * @param deleter the user who delete document
124    * @param deleteDate date of delete action
125    * @param content the stored deleted document
126    * @throws XWikiException if any error
127    * @since 9.0RC1
128    */
 
129  287 toggle public XWikiDeletedDocument(String fullName, Locale locale, String storeType, String deleter, Date deleteDate,
130    XWikiDeletedDocumentContent content) throws XWikiException
131    {
132  287 this(fullName, locale, storeType, deleter, deleteDate);
133   
134  287 this.content = content;
135    }
136   
137    /**
138    * @param fullName the local reference of the document
139    * @param locale the locale of the document
140    * @param storeType the way to store the document
141    * @param deleter the user who delete document
142    * @param deleteDate date of delete action
143    * @param content the stored deleted document
144    * @param batchId the id of the batch deletion
145    * @throws XWikiException if any error
146    * @since 9.4RC1
147    */
 
148  287 toggle public XWikiDeletedDocument(String fullName, Locale locale, String storeType, String deleter, Date deleteDate,
149    XWikiDeletedDocumentContent content, String batchId) throws XWikiException
150    {
151  287 this(fullName, locale, storeType, deleter, deleteDate, content);
152   
153  287 this.batchId = batchId;
154    }
155   
156    /**
157    * @return the synthetic id of this deleted document. unique only for document.
158    */
 
159  1007 toggle public long getId()
160    {
161  1007 return this.id;
162    }
163   
164    /**
165    * @param id - the synthetic id to set. used only in Hibernate.
166    */
 
167  502 toggle protected void setId(long id)
168    {
169  502 this.id = id;
170    }
171   
172    /**
173    * @return {@link XWikiDocument#getFullName()}
174    */
 
175  2013 toggle public String getFullName()
176    {
177  2013 return this.fullName;
178    }
179   
180    /**
181    * @param docFullName - {@link XWikiDocument#getFullName()} to set
182    */
 
183  215 toggle protected void setFullName(String docFullName)
184    {
185  215 this.fullName = docFullName;
186    }
187   
188    /**
189    * @return the document reference for the deleted document, including any locale information
190    * @since 9.4RC1
191    */
 
192  308 toggle public DocumentReference getDocumentReference()
193    {
194  308 DocumentReference documentReference = getDocumentReferenceResolver().resolve(getFullName());
195   
196  308 Locale localeValue = getLocale();
197  308 if (localeValue != null) {
198  308 documentReference = new DocumentReference(documentReference, localeValue);
199    }
200   
201  308 return documentReference;
202    }
203   
 
204  308 toggle private static DocumentReferenceResolver<String> getDocumentReferenceResolver()
205    {
206  308 return Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "currentmixed");
207    }
208   
209    /**
210    * @return {@link XWikiDocument#getLanguage()}
211    * @deprecated since 8.0M1, use {@link #getLocale()} instead
212    */
 
213  584 toggle @Deprecated
214    public String getLanguage()
215    {
216  584 return getLocale().toString();
217    }
218   
219    /**
220    * @return {@link XWikiDocument#getLocale()}
221    * @since 8.0M1
222    */
 
223  1099 toggle public Locale getLocale()
224    {
225  1099 return this.locale != null ? this.locale : Locale.ROOT;
226    }
227   
228    /**
229    * @param locale - {@link XWikiDocument#getLanguage()} to set
230    * @deprecated since 8.0M1
231    */
 
232  215 toggle @Deprecated
233    protected void setLanguage(String locale)
234    {
235  215 this.locale = LocaleUtils.toLocale(Util.normalizeLanguage(locale), Locale.ROOT);
236    }
237   
238    /**
239    * @return the date of delete action
240    */
 
241  934 toggle public Date getDate()
242    {
243  934 return this.date;
244    }
245   
246    /**
247    * @param date - the date of delete action to set
248    */
 
249  215 toggle protected void setDate(Date date)
250    {
251  215 this.date = date;
252    }
253   
254    /**
255    * @return the user which has removed the document
256    */
 
257  871 toggle public String getDeleter()
258    {
259  871 return this.deleter;
260    }
261   
262    /**
263    * @param deleter - the user which has removed the document to set
264    */
 
265  215 toggle protected void setDeleter(String deleter)
266    {
267  215 this.deleter = deleter;
268    }
269   
270    /**
271    * @return the type of the store used for the content
272    * @since 9.0RC1
273    */
 
274  799 toggle public String getXmlStore()
275    {
276  799 return this.xmlStore;
277    }
278   
279    /**
280    * @param xmlStore the type of store (supported values are null/"hibernate" and "file")
281    * @since 9.0RC1
282    */
 
283  215 toggle protected void setXmlStore(String xmlStore)
284    {
285  215 this.xmlStore = xmlStore;
286    }
287   
288    /**
289    * Only used in Hibernate.
290    *
291    * @return xml serialization of {@link XWikiDocument}
292    */
 
293  584 toggle public String getXml()
294    {
295  584 if (this.content != null) {
296  0 try {
297  0 return this.content.getContentAsString();
298    } catch (Exception e) {
299  0 throw new RuntimeException(e);
300    }
301    }
302   
303    // Return empty String instead of null because this field is configured as not null at database level
304  584 return "";
305    }
306   
307    /**
308    * Only used in Hibernate.
309    *
310    * @param xml - xml serialization of {@link XWikiDocument}
311    */
 
312  215 toggle protected void setXml(String xml)
313    {
314  215 if (StringUtils.isNotEmpty(xml)) {
315  0 try {
316  0 this.content = new XWikiHibernateDeletedDocumentContent(xml);
317    } catch (IOException e) {
318  0 throw new RuntimeException(e);
319    }
320    }
321    }
322   
323    /**
324    * Export {@link XWikiDocument} to {@link XWikiDeletedDocument}.
325    *
326    * @param doc - the deleted document
327    * @param context - used in {@link XWikiDocument#toXML(XWikiContext)}
328    * @throws XWikiException in error in {@link XWikiDocument#toXML(XWikiContext)}
329    * @deprecated since 9.0RC1, use
330    * {@link XWikiDeletedDocument#XWikiDeletedDocument(String, Locale, String, String, Date, XWikiDeletedDocumentContent)
331    * instead}
332    */
 
333  0 toggle @Deprecated
334    protected void setDocument(XWikiDocument doc, XWikiContext context) throws XWikiException
335    {
336  0 this.content = new XWikiHibernateDeletedDocumentContent(doc);
337    }
338   
339    /**
340    * @return restored document
341    * @param doc optional object where to put the document data, if not <code>null</code>
342    * @param context the current {@link XWikiContext context}
343    * @throws XWikiException if error in {@link XWikiDocument#fromXML(String)}
344    */
 
345  94 toggle public XWikiDocument restoreDocument(XWikiDocument doc, XWikiContext context) throws XWikiException
346    {
347  94 if (this.content == null) {
348  1 throw new XWikiException(XWikiException.MODULE_XWIKI_DOC, XWikiException.ERROR_DOC_XML_PARSING,
349    "Cannot find any content for the deleted document [" + this.fullName + " (" + this.locale + ")]");
350    }
351   
352  93 try {
353  93 return this.content.getXWikiDocument(doc);
354    } catch (IOException e) {
355  0 throw new XWikiException(XWikiException.MODULE_XWIKI_DOC, XWikiException.ERROR_DOC_XML_PARSING,
356    "Error restoring document", e, null);
357    }
358    }
359   
360    /**
361    * @return restored document
362    * @param context the current {@link XWikiContext context}
363    * @throws XWikiException if error in {@link XWikiDocument#fromXML(String)}
364    * @since 9.0RC1
365    */
 
366  4 toggle public XWikiDocument restoreDocument(XWikiContext context) throws XWikiException
367    {
368  4 return restoreDocument(null, context);
369    }
370   
371    /**
372    * @param batchId batch operation ID to set
373    * @since 9.4RC1
374    */
 
375  215 toggle protected void setBatchId(String batchId)
376    {
377  215 this.batchId = batchId;
378    }
379   
380    /**
381    * @return the id of the operation that deleted multiple documents at the same time, including this one
382    * @since 9.4RC1
383    */
 
384  689 toggle public String getBatchId()
385    {
386  689 return batchId;
387    }
388    }