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

File DeletedAttachment.java

 

Coverage histogram

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

Code metrics

8
38
11
1
216
101
19
0.5
3.45
11
1.73

Classes

Class Line # Actions
DeletedAttachment 43 38 0% 19 57
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.api;
21   
22    import java.util.Calendar;
23    import java.util.Date;
24   
25    import org.slf4j.Logger;
26    import org.slf4j.LoggerFactory;
27   
28    import com.xpn.xwiki.XWikiContext;
29    import com.xpn.xwiki.XWikiException;
30    import com.xpn.xwiki.doc.XWikiDocument;
31    import com.xpn.xwiki.util.Programming;
32   
33    /**
34    * Information about a deleted attachment in the recycle bin. Note that this does not hold much information about the
35    * real attachment, but only meta-information relevant to the trash: original document and filename, deleter, deletion
36    * date. The attachment can be accessed using {@link #getAttachment()}.
37    * <p>
38    * This object is immutable, since entries in the trash can not be modified.
39    *
40    * @version $Id: 07ec5ab11b8f5ba9acd95617584f2098c4228ca0 $
41    * @since 2.2M1
42    */
 
43    public class DeletedAttachment extends Api
44    {
45    /** Logging helper object. */
46    private static final Logger LOGGER = LoggerFactory.getLogger(DeletedAttachment.class);
47   
48    /** The internal object wrapped by this API. */
49    private final com.xpn.xwiki.doc.DeletedAttachment deletedAttachment;
50   
51    /**
52    * Simple constructor, initializes a new API object with the current {@link com.xpn.xwiki.XWikiContext context} and
53    * the specified protected {@link com.xpn.xwiki.doc.DeletedAttachment deleted attachment} object.
54    *
55    * @param deletedAttachment the internal object wrapped by this API
56    * @param context the current request context
57    */
 
58  0 toggle public DeletedAttachment(com.xpn.xwiki.doc.DeletedAttachment deletedAttachment, XWikiContext context)
59    {
60  0 super(context);
61  0 this.deletedAttachment = deletedAttachment;
62    }
63   
64    /**
65    * Retrieve the internal entry index, used to uniquely identify this entity in the trash. This is needed because a
66    * file can be attached and deleted multiple times, so the document name and filename are not enough to uniquely
67    * identify a deleted attachment.
68    *
69    * @return internal identifier of the corresponding trash entry
70    */
 
71  0 toggle public long getId()
72    {
73  0 return this.deletedAttachment.getId();
74    }
75   
76    /**
77    * Retrieve the original name of this attachment.
78    *
79    * @return the original filename, for example {@code MyPhoto.png}
80    */
 
81  0 toggle public String getFilename()
82    {
83  0 return this.deletedAttachment.getFilename();
84    }
85   
86    /**
87    * Retrieve the name of the document this attachment belonged to.
88    *
89    * @return the name of the owner document, in the {@code Space.Document} format
90    */
 
91  0 toggle public String getDocName()
92    {
93  0 return this.deletedAttachment.getDocName();
94    }
95   
96    /**
97    * Retrieve the name of the user who deleted this attachment.
98    *
99    * @return the user who deleted the attachment, as its document name (e.g. {@code XWiki.Admin})
100    */
 
101  0 toggle public String getDeleter()
102    {
103  0 return this.deletedAttachment.getDeleter();
104    }
105   
106    /**
107    * Retrieve the date and time this attachment has been deleted.
108    *
109    * @return the date of the deletion
110    */
 
111  0 toggle public Date getDate()
112    {
113  0 return this.deletedAttachment.getDate();
114    }
115   
116    /**
117    * Access to the real attachment object.
118    *
119    * @return the attachment as it was before being deleted, and as it currently is in the recycle bin
120    */
 
121  0 toggle public Attachment getAttachment()
122    {
123  0 try {
124  0 Document doc = this.context.getWiki().getDocument(getDocName(), this.context).newDocument(this.context);
125  0 return new Attachment(doc, this.deletedAttachment.restoreAttachment(null, this.context), this.context);
126    } catch (XWikiException ex) {
127  0 LOGGER.warn("Failed to parse deleted attachment: " + ex.getMessage(), ex);
128  0 return null;
129    }
130    }
131   
132    /**
133    * Privileged access to the internal object wrapped by this API.
134    *
135    * @return original deleted attachment if the current user has programming rights, else {@code null}.
136    */
 
137  0 toggle @Programming
138    public com.xpn.xwiki.doc.DeletedAttachment getDeletedAttachment()
139    {
140  0 if (hasProgrammingRights()) {
141  0 return this.deletedAttachment;
142    } else {
143  0 return null;
144    }
145    }
146   
147    /**
148    * Check if the current user has the right to restore the attachment.
149    *
150    * @return {@code true} if the current user can restore this document, {@code false} otherwise
151    */
 
152  0 toggle public boolean canRestore()
153    {
154    // FIXME Temporary disabled until this action is implemented.
155    // As a workaround, the attachment can be downloaded and re-attached.
156  0 return false;
157    }
158   
159    /**
160    * Check if the current user has the right to permanently delete the attachment from the trash.
161    *
162    * @return {@code true} if the current user can purge this document, {@code false} otherwise
163    * @xwiki.xwikicfg xwiki.store.recyclebin.adminWaitDays How many days should an administrator wait before being able
164    * to permanently delete this document from the recycle bin. 0 by default.
165    * @xwiki.xwikicfg xwiki.store.recyclebin.waitDays How many days should a normal user with "delete" right wait
166    * before being able to permanently delete this document from the recycle bin. 7 by default.
167    */
 
168  0 toggle public boolean canDelete()
169    {
170  0 try {
171  0 XWikiDocument doc = new XWikiDocument();
172  0 doc.setFullName(getDocName(), this.context);
173  0 if (!hasAdminRights()
174    && !getXWikiContext().getWiki().getRightService().checkAccess("delete", doc, this.context)) {
175  0 return false;
176    }
177  0 String waitdays;
178  0 if (hasAdminRights()) {
179  0 waitdays = getXWikiContext().getWiki().Param("xwiki.store.recyclebin.adminWaitDays", "0");
180    } else {
181  0 waitdays = getXWikiContext().getWiki().Param("xwiki.store.recyclebin.waitDays", "7");
182    }
183  0 int seconds = (int) (Double.parseDouble(waitdays) * 24 * 60 * 60 + 0.5);
184  0 Calendar cal = Calendar.getInstance();
185  0 cal.setTime(getDate());
186  0 cal.add(Calendar.SECOND, seconds);
187  0 return cal.before(Calendar.getInstance());
188    } catch (Exception ex) {
189    // Public APIs should not throw exceptions
190  0 LOGGER.warn("Exception while checking if entry [" + getId() + "] can be removed from the recycle bin", ex);
191  0 return false;
192    }
193    }
194   
195    /**
196    * Permanently delete this attachment from the trash. Throws an access denied exception if the user does not have
197    * the right to perform this action, which will trigger the generic Access Denied message. Any other failures will
198    * be silently ignored.
199    *
200    * @throws XWikiException if the user does not have the right to perform this action
201    */
 
202  0 toggle public void delete() throws XWikiException
203    {
204  0 if (this.canDelete()) {
205  0 try {
206  0 this.context.getWiki().getAttachmentRecycleBinStore().deleteFromRecycleBin(getId(), this.context, true);
207    } catch (Exception ex) {
208  0 LOGGER.warn("Failed to purge deleted attachment", ex);
209    }
210    } else {
211  0 java.lang.Object[] args = { this.getFilename(), this.getDocName() };
212  0 throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, XWikiException.ERROR_XWIKI_ACCESS_DENIED,
213    "Cannot permanently delete attachment {0}@{1} from the trash", null, args);
214    }
215    }
216    }