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

File DeletedDocument.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

8
36
11
1
194
102
19
0.53
3.27
11
1.73

Classes

Class Line # Actions
DeletedDocument 40 36 0% 19 24
0.5636363656.4%
 

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    import java.util.Locale;
25   
26    import org.slf4j.Logger;
27    import org.slf4j.LoggerFactory;
28   
29    import com.xpn.xwiki.XWikiContext;
30    import com.xpn.xwiki.XWikiException;
31    import com.xpn.xwiki.doc.XWikiDeletedDocument;
32    import com.xpn.xwiki.doc.XWikiDocument;
33    import com.xpn.xwiki.util.Programming;
34   
35    /**
36    * Information about a deleted document in the recycle bin.
37    *
38    * @version $Id: ed3915091daa0d0e419bd9632faf726f6366301b $
39    */
 
40    public class DeletedDocument extends Api
41    {
42    /** Logging helper object. */
43    private static final Logger LOGGER = LoggerFactory.getLogger(DeletedDocument.class);
44   
45    /**
46    * The internal object wrapped by this API.
47    */
48    private final XWikiDeletedDocument deletedDoc;
49   
50    /**
51    * Simple constructor, initializes a new API object with the current {@link com.xpn.xwiki.XWikiContext context} and
52    * the specified protected {@link com.xpn.xwiki.doc.XWikiDeletedDocument deleted document} object.
53    *
54    * @param deletedDoc the internal object wrapped by this API
55    * @param context the current request context
56    */
 
57  19 toggle public DeletedDocument(XWikiDeletedDocument deletedDoc, XWikiContext context)
58    {
59  19 super(context);
60  19 this.deletedDoc = deletedDoc;
61    }
62   
63    /**
64    * @return full name of document (ie: Main.WebHome)
65    */
 
66  52 toggle public String getFullName()
67    {
68  52 return this.deletedDoc.getFullName();
69    }
70   
71    /**
72    * @return locale of document
73    * @deprecated since 8.0M1, use {@link #getLocale()} instead
74    */
 
75  0 toggle @Deprecated
76    public String getLanguage()
77    {
78  0 return this.deletedDoc.getLanguage();
79    }
80   
81    /**
82    * @return locale of document
83    * @since 8.0M1
84    */
 
85  0 toggle public Locale getLocale()
86    {
87  0 return this.deletedDoc.getLocale();
88    }
89   
90    /**
91    * @return date of delete action
92    */
 
93  35 toggle public Date getDate()
94    {
95  35 return this.deletedDoc.getDate();
96    }
97   
98    /**
99    * @return user which delete document
100    */
 
101  17 toggle public String getDeleter()
102    {
103  17 return this.deletedDoc.getDeleter();
104    }
105   
106    /**
107    * @return id of deleted document. id is unique only for this document.
108    */
 
109  49 toggle public long getId()
110    {
111  49 return this.deletedDoc.getId();
112    }
113   
114    /**
115    * Check if the current user has the right to restore the document.
116    *
117    * @return {@code true} if the current user can restore this document, {@code false} otherwise
118    */
 
119  17 toggle public boolean canUndelete()
120    {
121  17 try {
122  17 return hasAdminRights() || hasAccessLevel("undelete", getFullName());
123    } catch (XWikiException ex) {
124    // Public APIs should not throw exceptions
125  0 LOGGER.warn(String.format("Exception while checking if entry [%s] can be restored from the recycle bin",
126    getId()), ex);
127  0 return false;
128    }
129    }
130   
131    /**
132    * @return {@code true} if the current user can permanently delete this document, {@code false} otherwise
133    * @xwiki.xwikicfg xwiki.store.recyclebin.adminWaitDays How many days should an administrator wait before being able
134    * to permanently delete this document from the recycle bin. 0 by default.
135    * @xwiki.xwikicfg xwiki.store.recyclebin.waitDays How many days should a normal user with "delete" right wait
136    * before being able to permanently delete this document from the recycle bin. 7 by default.
137    */
 
138  19 toggle public boolean canDelete()
139    {
140  19 try {
141  19 XWikiDocument doc = new XWikiDocument();
142  19 doc.setFullName(getFullName(), this.context);
143  19 if (!hasAdminRights()
144    && !getXWikiContext().getWiki().getRightService().checkAccess("delete", doc, this.context)) {
145  1 return false;
146    }
147  18 String waitdays;
148  18 if (hasAdminRights()) {
149  18 waitdays = getXWikiContext().getWiki().Param("xwiki.store.recyclebin.adminWaitDays", "0");
150    } else {
151  0 waitdays = getXWikiContext().getWiki().Param("xwiki.store.recyclebin.waitDays", "7");
152    }
153  18 int seconds = (int) (Double.parseDouble(waitdays) * 24 * 60 * 60 + 0.5);
154  18 Calendar cal = Calendar.getInstance();
155  18 cal.setTime(getDate());
156  18 cal.add(Calendar.SECOND, seconds);
157  18 return cal.before(Calendar.getInstance());
158    } catch (Exception ex) {
159    // Public APIs should not throw exceptions
160  0 LOGGER.warn(String.format("Exception while checking if entry [%s] can be removed from the recycle bin",
161    getId()), ex);
162  0 return false;
163    }
164    }
165   
166    /**
167    * @return original deleted document if user has programming rights, else {@code null}.
168    */
 
169  0 toggle @Programming
170    public XWikiDeletedDocument getDeletedDocument()
171    {
172  0 if (hasProgrammingRights()) {
173  0 return this.deletedDoc;
174    } else {
175  0 return null;
176    }
177    }
178   
179    /**
180    * @return the document as it is in the recycle bin if the user has admin rights, {@code null} otherwise
181    */
 
182  0 toggle public Document getDocument()
183    {
184  0 if (hasAdminRights()) {
185  0 try {
186  0 return new Document(this.deletedDoc.restoreDocument(null, this.context), this.context);
187    } catch (XWikiException e) {
188  0 LOGGER.warn("Failed to parse deleted document: " + e.getMessage());
189    }
190    }
191   
192  0 return null;
193    }
194    }