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

File UndeleteAction.java

 

Coverage histogram

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

Code metrics

10
21
1
1
86
46
7
0.33
21
1
7

Classes

Class Line # Actions
UndeleteAction 40 21 0% 7 3
0.9062590.6%
 

Contributing tests

This file is covered by 5 tests. .

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.web;
21   
22    import java.util.Locale;
23   
24    import org.apache.commons.lang3.StringUtils;
25    import org.xwiki.localization.LocaleUtils;
26    import org.xwiki.model.reference.DocumentReference;
27   
28    import com.xpn.xwiki.XWiki;
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   
34    /**
35    * Action for restoring documents from the recycle bin.
36    *
37    * @version $Id: 966b4d6c2b6602fc0ab3319e471000defe2d612f $
38    * @since 1.2M1
39    */
 
40    public class UndeleteAction extends XWikiAction
41    {
 
42  7 toggle @Override
43    public boolean action(XWikiContext context) throws XWikiException
44    {
45    // CSRF prevention
46  7 if (!csrfTokenCheck(context)) {
47  1 return false;
48    }
49   
50  6 XWiki xwiki = context.getWiki();
51  6 XWikiRequest request = context.getRequest();
52  6 XWikiResponse response = context.getResponse();
53  6 XWikiDocument doc = context.getDoc();
54  6 String deletedDocumentLanguage = null;
55   
56  6 if (xwiki.hasRecycleBin(context)) {
57  5 String sindex = request.getParameter("id");
58  5 long index = Long.parseLong(sindex);
59   
60    // See exactly what is it that we want to restore by looking at the language of the deleted document.
61    // FIXME: don`t use int type for index. Fix xwiki.getDeletedDocument to properly use long.
62  5 XWikiDeletedDocument deletedDocument =
63    xwiki.getDeletedDocument(StringUtils.EMPTY, StringUtils.EMPTY, (int) index, context);
64  5 if (deletedDocument != null) {
65  5 deletedDocumentLanguage = deletedDocument.getLanguage();
66   
67    // If the document (or the translation) that we want to restore does not exist, restore it.
68  5 DocumentReference translatedDocumentReference =
69    new DocumentReference(doc.getDocumentReference(), LocaleUtils.toLocale(deletedDocumentLanguage,
70    Locale.ROOT));
71  5 if (!xwiki.exists(translatedDocumentReference, context)) {
72  4 xwiki.restoreFromRecycleBin(doc, index, "Restored from recycle bin", context);
73    }
74    }
75    }
76   
77    // Redirect to the undeleted document. Make sure to redirect to the proper translation.
78  6 String queryString = null;
79  6 if (deletedDocumentLanguage != null && xwiki.isMultiLingual(context)) {
80  0 queryString = String.format("language=%s", deletedDocumentLanguage);
81    }
82  6 sendRedirect(response, doc.getURL("view", queryString, context));
83   
84  6 return false;
85    }
86    }