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

File AdminAction.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

28
58
2
1
142
96
25
0.43
29
2
12.5

Classes

Class Line # Actions
AdminAction 35 58 0% 25 32
0.636363663.6%
 

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.web;
21   
22    import org.slf4j.Logger;
23    import org.slf4j.LoggerFactory;
24   
25    import com.xpn.xwiki.XWikiContext;
26    import com.xpn.xwiki.XWikiException;
27    import com.xpn.xwiki.doc.XWikiDocument;
28    import com.xpn.xwiki.doc.XWikiLock;
29   
30    /**
31    * Administration xwiki action.
32    *
33    * @version $Id: 2a7931f9db0a784aa47e432e7f1a115560b83bd5 $
34    */
 
35    public class AdminAction extends XWikiAction
36    {
37    /** The logger. */
38    private static final Logger LOGGER = LoggerFactory.getLogger(AdminAction.class);
39   
40    /**
41    * Default constructor.
42    */
 
43  8 toggle public AdminAction()
44    {
45  8 this.waitForXWikiInitialization = false;
46    }
47   
 
48  146 toggle @Override
49    public String render(XWikiContext context) throws XWikiException
50    {
51  146 XWikiRequest request = context.getRequest();
52  146 String content = request.getParameter("content");
53  146 XWikiDocument doc = context.getDoc();
54  146 XWikiForm form = context.getForm();
55   
56  146 synchronized (doc) {
57  146 XWikiDocument tdoc = (XWikiDocument) context.get("tdoc");
58  146 EditForm peform = (EditForm) form;
59  146 String parent = peform.getParent();
60  146 if (parent != null) {
61  0 doc.setParent(parent);
62    }
63  146 String creator = peform.getCreator();
64  146 if (creator != null) {
65  0 doc.setCreator(creator);
66    }
67  146 String defaultTemplate = peform.getDefaultTemplate();
68  146 if (defaultTemplate != null) {
69  0 doc.setDefaultTemplate(defaultTemplate);
70    }
71  146 String defaultLanguage = peform.getDefaultLanguage();
72  146 if ((defaultLanguage != null) && !defaultLanguage.equals("")) {
73  0 doc.setDefaultLanguage(defaultLanguage);
74    }
75  146 if (doc.getDefaultLanguage().equals("")) {
76  12 doc.setDefaultLanguage(context.getWiki().getLanguagePreference(context));
77    }
78   
79  146 String language = context.getWiki().getLanguagePreference(context);
80  146 String languagefromrequest = context.getRequest().getParameter("language");
81  146 String languagetoedit =
82  146 ((languagefromrequest == null) || (languagefromrequest.equals(""))) ? language : languagefromrequest;
83   
84  146 if ((languagetoedit == null) || (languagetoedit.equals("default"))) {
85  0 languagetoedit = "";
86    }
87  146 if (doc.isNew() || (doc.getDefaultLanguage().equals(languagetoedit))) {
88  146 languagetoedit = "";
89    }
90   
91  146 if (languagetoedit.equals("")) {
92    // In this case the created document is going to be the default document
93  146 tdoc = doc;
94  146 context.put("tdoc", doc);
95  146 if (doc.isNew()) {
96  4 doc.setDefaultLanguage(language);
97  4 doc.setLanguage("");
98    }
99    } else {
100    // If the translated doc object is the same as the doc object
101    // this means the translated doc did not exists so we need to create it
102  0 if ((tdoc == doc)) {
103  0 tdoc = new XWikiDocument(doc.getDocumentReference());
104  0 tdoc.setLanguage(languagetoedit);
105  0 tdoc.setContent(doc.getContent());
106  0 tdoc.setSyntax(doc.getSyntax());
107  0 tdoc.setAuthor(context.getUser());
108  0 tdoc.setStore(doc.getStore());
109  0 context.put("tdoc", tdoc);
110    }
111    }
112   
113  146 XWikiDocument tdoc2 = tdoc.clone();
114  146 if (content != null && !content.isEmpty()) {
115  0 tdoc2.setContent(content);
116    }
117  146 context.put("tdoc", tdoc2);
118  146 try {
119  146 tdoc2.readFromTemplate(peform, context);
120    } catch (XWikiException e) {
121  0 if (e.getCode() == XWikiException.ERROR_XWIKI_APP_DOCUMENT_NOT_EMPTY) {
122  0 context.put("exception", e);
123  0 return "docalreadyexists";
124    }
125    }
126   
127    /* Setup a lock */
128  146 try {
129  146 XWikiLock lock = tdoc.getLock(context);
130  146 if ((lock == null) || (lock.getUserName().equals(context.getUser())) || (peform.isLockForce())) {
131  146 tdoc.setLock(context.getUser(), context);
132    }
133    } catch (Exception e) {
134    // Lock should never make XWiki fail
135    // But we should log any related information
136  0 LOGGER.error("Exception while setting up lock", e);
137    }
138    }
139   
140  146 return "admin";
141    }
142    }