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

File PropUpdateAction.java

 

Coverage histogram

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

Code metrics

14
48
3
1
128
90
11
0.23
16
3
3.67

Classes

Class Line # Actions
PropUpdateAction 35 48 0% 11 25
0.6153846461.5%
 

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 java.util.Collection;
23    import java.util.HashMap;
24    import java.util.List;
25    import java.util.Map;
26   
27    import com.xpn.xwiki.XWiki;
28    import com.xpn.xwiki.XWikiContext;
29    import com.xpn.xwiki.XWikiException;
30    import com.xpn.xwiki.doc.XWikiDocument;
31    import com.xpn.xwiki.objects.classes.BaseClass;
32    import com.xpn.xwiki.objects.classes.PropertyClass;
33    import com.xpn.xwiki.util.Util;
34   
 
35    public class PropUpdateAction extends XWikiAction
36    {
 
37  2 toggle public boolean propUpdate(XWikiContext context) throws XWikiException
38    {
39  2 XWiki xwiki = context.getWiki();
40  2 XWikiDocument doc = context.getDoc();
41  2 XWikiForm form = context.getForm();
42   
43    // Prepare new class
44  2 BaseClass bclass = doc.getXClass();
45  2 BaseClass bclass2 = bclass.clone();
46  2 bclass2.setFields(new HashMap());
47   
48    // Prepare a Map for field renames
49  2 Map<String, String> fieldsToRename = new HashMap<String, String>();
50   
51  2 for (PropertyClass originalProperty : (Collection<PropertyClass>) bclass.getFieldList()) {
52  3 PropertyClass newProperty = originalProperty.clone();
53  3 String name = newProperty.getName();
54  3 Map<String, ?> map = ((EditForm) form).getObject(name);
55  3 newProperty.getXClass(context).fromMap(map, newProperty);
56  3 String newName = newProperty.getName();
57   
58  3 if (!Util.isValidXMLElementName(newName)) {
59  0 context.put("message", "propertynamenotcorrect");
60  0 return true;
61    }
62   
63  3 if (newName.indexOf(" ") != -1) {
64  0 newName = newName.replaceAll(" ", "");
65  0 newProperty.setName(newName);
66    }
67  3 bclass2.addField(newName, newProperty);
68  3 if (!newName.equals(name)) {
69  0 fieldsToRename.put(name, newName);
70  0 bclass2.addPropertyForRemoval(originalProperty);
71    }
72    }
73   
74  2 doc.setXClass(bclass2);
75  2 doc.renameProperties(bclass.getName(), fieldsToRename);
76  2 doc.setMetaDataDirty(true);
77  2 if (doc.isNew()) {
78  0 doc.setCreator(context.getUser());
79    }
80  2 doc.setAuthor(context.getUser());
81  2 xwiki.saveDocument(doc, localizePlainOrKey("core.comment.updateClassProperty"), true,
82    context);
83   
84    // We need to load all documents that use this property and rename it
85  2 if (fieldsToRename.size() > 0) {
86  0 List<String> list =
87    xwiki.getStore().searchDocumentsNames(
88    ", BaseObject as obj where obj.name=doc.fullName and obj.className='"
89    + Utils.SQLFilter(bclass.getName()) + "' and doc.fullName <> '"
90    + Utils.SQLFilter(bclass.getName()) + "'", context);
91  0 for (String docName : list) {
92  0 XWikiDocument doc2 = xwiki.getDocument(docName, context);
93  0 doc2.renameProperties(bclass.getName(), fieldsToRename);
94  0 xwiki.saveDocument(doc2, localizePlainOrKey("core.comment.updateClassPropertyName"), true, context);
95    }
96    }
97   
98  2 return false;
99    }
100   
 
101  2 toggle @Override
102    public boolean action(XWikiContext context) throws XWikiException
103    {
104    // CSRF prevention
105  2 if (!csrfTokenCheck(context)) {
106  0 return false;
107    }
108   
109  2 try {
110  2 if (propUpdate(context)) {
111  0 return true;
112    }
113    } catch (XWikiException ex) {
114  0 context.put("exception", ex);
115  0 return true;
116    }
117    // forward to view
118  2 String redirect = Utils.getRedirect("view", context);
119  2 sendRedirect(context.getResponse(), redirect);
120  2 return false;
121    }
122   
 
123  0 toggle @Override
124    public String render(XWikiContext context) throws XWikiException
125    {
126  0 return "exception";
127    }
128    }