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

File DeletedAttachment.java

 

Coverage histogram

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

Code metrics

6
44
27
1
401
164
33
0.75
1.63
27
1.22

Classes

Class Line # Actions
DeletedAttachment 43 44 0% 33 20
0.7402597774%
 

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   
25    import org.apache.commons.lang3.StringUtils;
26    import org.xwiki.model.reference.AttachmentReference;
27    import org.xwiki.model.reference.DocumentReference;
28    import org.xwiki.model.reference.DocumentReferenceResolver;
29   
30    import com.xpn.xwiki.XWikiContext;
31    import com.xpn.xwiki.XWikiException;
32    import com.xpn.xwiki.internal.store.hibernate.HibernateDeletedAttachmentContent;
33    import com.xpn.xwiki.util.AbstractSimpleClass;
34    import com.xpn.xwiki.web.Utils;
35   
36    /**
37    * Archive of deleted attachment, stored in {@link com.xpn.xwiki.store.AttachmentRecycleBinStore}. Immutable, because
38    * deleted attachments should not be modified.
39    *
40    * @version $Id: c8daacd377284fdd83f5988c7deb293d73c4465a $
41    * @since 1.4M1
42    */
 
43    public class DeletedAttachment extends AbstractSimpleClass
44    {
45    /** Synthetic id, generated by Hibernate. This is used to address entries in the recycle bin. */
46    private long id;
47   
48    /** The ID of the document this attachment belonged to. */
49    private long docId;
50   
51    /** The reference of the document this attachment belonged to. */
52    private String docName;
53   
54    /** The name of the attachment. */
55    private String filename;
56   
57    /** Date of delete action. */
58    private Date date;
59   
60    /** The user who deleted the attachment, in the <tt>XWiki.UserName</tt> format. */
61    private String deleter;
62   
63    private String contentStore;
64   
65    private DeletedAttachmentContent content;
66   
67    /** Default constructor. Used only by hibernate when restoring objects from the database. */
 
68  77 toggle protected DeletedAttachment()
69    {
70    }
71   
72    /**
73    * A constructor with all the information about the deleted attachment.
74    *
75    * @param docId the ID of the document this attachment belonged to
76    * @param docName the reference of the document this attachment belonged to
77    * @param filename the name of the attachment
78    * @param storeType the way to store the document
79    * @param deleter User which deleted the attachment.
80    * @param deleteDate Date of delete action.
81    */
 
82  10 toggle private DeletedAttachment(long docId, String docName, String filename, String storeType, String deleter,
83    Date deleteDate)
84    {
85  10 this.docId = docId;
86  10 this.docName = docName;
87  10 this.filename = filename;
88  10 this.deleter = deleter;
89  10 this.date = deleteDate;
90  10 this.contentStore = storeType;
91    }
92   
93    /**
94    * A constructor with all the information about the deleted attachment.
95    *
96    * @param attachment Deleted attachment.
97    * @param deleter User which deleted the attachment.
98    * @param deleteDate Date of delete action.
99    * @param context The current context. Used for determining the encoding.
100    * @throws XWikiException If the attachment cannot be exported to XML.
101    */
 
102  0 toggle public DeletedAttachment(XWikiAttachment attachment, String deleter, Date deleteDate, XWikiContext context)
103    throws XWikiException
104    {
105  0 this(attachment.getDocId(), attachment.getDoc().getFullName(), attachment.getFilename(), null, deleter,
106    deleteDate);
107   
108  0 setAttachment(attachment, context);
109    }
110   
111    /**
112    * @param docId the ID of the document this attachment belonged to
113    * @param docName the reference of the document this attachment belonged to
114    * @param filename the name of the attachment
115    * @param storeType the way to store the document
116    * @param deleter the user who delete document
117    * @param deleteDate date of delete action
118    * @param content the stored deleted document
119    * @since 9.10RC1
120    */
 
121  10 toggle public DeletedAttachment(long docId, String docName, String filename, String storeType, String deleter,
122    Date deleteDate, DeletedAttachmentContent content)
123    {
124  10 this(docId, docName, filename, storeType, deleter, deleteDate);
125   
126  10 this.content = content;
127    }
128   
129    /**
130    * @param docId the ID of the document this attachment belonged to
131    * @param docName the reference of the document this attachment belonged to
132    * @param filename the name of the attachment
133    * @param storeType the way to store the document
134    * @param deleter the user who delete document
135    * @param deleteDate date of delete action
136    * @param content the stored deleted document
137    * @param id the synthetic id of this deleted attachment. Uniquely identifies an entry in the recycle bin.
138    * @since 9.10RC1
139    */
 
140  0 toggle public DeletedAttachment(long docId, String docName, String filename, String storeType, String deleter,
141    Date deleteDate, DeletedAttachmentContent content, long id)
142    {
143  0 this(docId, docName, filename, storeType, deleter, deleteDate, content);
144   
145  0 this.id = id;
146    }
147   
148    /**
149    * Getter for {@link #id}.
150    *
151    * @return The synthetic id of this deleted attachment. Uniquely identifies an entry in the recycle bin.
152    */
 
153  87 toggle public long getId()
154    {
155  87 return this.id;
156    }
157   
158    /**
159    * Setter for {@link #id}.
160    *
161    * @param id The synthetic id to set. Used only by hibernate.
162    */
 
163  11 toggle protected void setId(long id)
164    {
165  11 this.id = id;
166    }
167   
168    /**
169    * Getter for {@link #docId}.
170    *
171    * @return The id of the document this attachment belonged to.
172    */
 
173  20 toggle public long getDocId()
174    {
175  20 return this.docId;
176    }
177   
178    /**
179    * Setter for {@link #docId}.
180    *
181    * @param docId The id of the document to set. Used only by hibernate.
182    */
 
183  1 toggle protected void setDocId(long docId)
184    {
185  1 this.docId = docId;
186    }
187   
188    /**
189    * Getter for {@link #docName}.
190    *
191    * @return The name of the document this attachment belonged to.
192    */
 
193  21 toggle public String getDocName()
194    {
195  21 return this.docName;
196    }
197   
198    /**
199    * Setter for {@link #docName}.
200    *
201    * @param docName The document name to set. Used only by hibernate.
202    */
 
203  1 toggle protected void setDocName(String docName)
204    {
205  1 this.docName = docName;
206    }
207   
208    /**
209    * Getter for {@link #filename}.
210    *
211    * @return The name of the attachment.
212    */
 
213  21 toggle public String getFilename()
214    {
215  21 return this.filename;
216    }
217   
218    /**
219    * Setter for {@link #filename}.
220    *
221    * @param filename The attachment filename to set. Used only by hibernate.
222    */
 
223  1 toggle protected void setFilename(String filename)
224    {
225  1 this.filename = filename;
226    }
227   
228    /**
229    * @return the attachment reference for the deleted attachment
230    * @since 9.9RCA
231    */
 
232  1 toggle public AttachmentReference getAttachmentReference()
233    {
234  1 DocumentReference documentReference = getDocumentReferenceResolver().resolve(getDocName());
235   
236  1 return new AttachmentReference(getFilename(), documentReference);
237    }
238   
 
239  1 toggle private static DocumentReferenceResolver<String> getDocumentReferenceResolver()
240    {
241  1 return Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "currentmixed");
242    }
243   
244    /**
245    * Getter for {@link #date}.
246    *
247    * @return The date of the delete action.
248    */
 
249  22 toggle public Date getDate()
250    {
251  22 return this.date;
252    }
253   
254    /**
255    * Setter for {@link #date}.
256    *
257    * @param date The date of the delete action to set. Used only by Hibernate.
258    */
 
259  1 toggle protected void setDate(Date date)
260    {
261  1 this.date = date;
262    }
263   
264    /**
265    * Getter for {@link #deleter}.
266    *
267    * @return the user who deleted the attachment, as its document name (e.g. {@code XWiki.Admin})
268    */
 
269  20 toggle public String getDeleter()
270    {
271  20 return this.deleter;
272    }
273   
274    /**
275    * Setter for {@link #deleter}.
276    *
277    * @param deleter The user which has removed the document to set. Used only by Hibernate.
278    */
 
279  1 toggle protected void setDeleter(String deleter)
280    {
281  1 this.deleter = deleter;
282    }
283   
284    /**
285    * @return the type of the store used for the content
286    * @since 9.10RC1
287    */
 
288  21 toggle public String getContentStore()
289    {
290  21 return this.contentStore;
291    }
292   
293    /**
294    * @param xmlStore the type of store (supported values are null/"hibernate" and "file")
295    * @since 9.10RC1
296    */
 
297  1 toggle public void setContentStore(String xmlStore)
298    {
299  1 this.contentStore = xmlStore;
300    }
301   
302    /**
303    * Getter for {@link #content}.
304    *
305    * @return XML serialization of {@link XWikiAttachment}
306    */
 
307  20 toggle public String getXml()
308    {
309  20 if (this.content != null) {
310  0 try {
311  0 return this.content.getContentAsString();
312    } catch (Exception e) {
313  0 throw new RuntimeException(e);
314    }
315    }
316   
317    // Return empty String instead of null because this field is configured as not null at database level
318  20 return "";
319    }
320   
321    /**
322    * Setter for {@link #content}.
323    *
324    * @param xml XML serialization of {@link XWikiAttachment}. Used only by Hibernate.
325    */
 
326  1 toggle protected void setXml(String xml)
327    {
328  1 if (StringUtils.isNotEmpty(xml)) {
329  0 try {
330  0 this.content = new HibernateDeletedAttachmentContent(xml);
331    } catch (IOException e) {
332  0 throw new RuntimeException(e);
333    }
334    }
335    }
336   
337    /**
338    * Export {@link XWikiAttachment} to {@link DeletedAttachment}.
339    *
340    * @param attachment the deleted attachment
341    * @param context the current context, used in the XML export
342    * @throws XWikiException if an exception occurs during the XML export
343    * @deprecated since 9.9RC1, use
344    * {@link #DeletedAttachment(long, String, String, String, String, Date, DeletedAttachmentContent)}
345    * instead
346    */
 
347  0 toggle @Deprecated
348    protected void setAttachment(XWikiAttachment attachment, XWikiContext context) throws XWikiException
349    {
350  0 this.content = new HibernateDeletedAttachmentContent(attachment);
351    }
352   
353    /**
354    * Restore a {@link XWikiAttachment} from a {@link DeletedAttachment}. Note that this method does not actually
355    * restore the attachment to its owner document, it simply re-composes an {@link XWikiAttachment} object from the
356    * saved data.
357    *
358    * @param attachment optional object where to put the attachment data, if not <code>null</code>
359    * @param context the current {@link XWikiContext context}
360    * @return restored attachment
361    * @throws XWikiException If an exception occurs while the Attachment is restored from the XML. See
362    * {@link XWikiAttachment#fromXML(String)}.
363    * @deprecated since 9.9RC1, use {@link #restoreAttachment(XWikiAttachment)} instead
364    */
 
365  0 toggle @Deprecated
366    public XWikiAttachment restoreAttachment(XWikiAttachment attachment, XWikiContext context) throws XWikiException
367    {
368  0 return restoreAttachment(attachment);
369    }
370   
371    /**
372    * Restore a {@link XWikiAttachment} from a {@link DeletedAttachment}. Note that this method does not actually
373    * restore the attachment to its owner document, it simply re-composes an {@link XWikiAttachment} object from the
374    * saved data.
375    *
376    * @param attachment optional object where to put the attachment data, if not <code>null</code>
377    * @return restored attachment
378    * @throws XWikiException If an exception occurs while the Attachment is restored from the XML. See
379    * {@link XWikiAttachment#fromXML(String)}.
380    */
 
381  1 toggle public XWikiAttachment restoreAttachment(XWikiAttachment attachment) throws XWikiException
382    {
383  1 try {
384  1 return this.content != null ? this.content.getXWikiAttachment(attachment) : null;
385    } catch (IOException e) {
386  0 throw new XWikiException(XWikiException.MODULE_XWIKI_DOC, XWikiException.ERROR_DOC_XML_PARSING,
387    "Error restoring document", e, null);
388    }
389    }
390   
391    /**
392    * @return restored attachment
393    * @throws XWikiException if error in {@link XWikiDocument#fromXML(String)}
394    * @since 9.10RC1
395    */
 
396  1 toggle public XWikiAttachment restoreAttachment() throws XWikiException
397    {
398  1 return restoreAttachment(null);
399    }
400   
401    }