1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.web

File PropUpdateAction.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
66% 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  24 toggle public boolean propUpdate(XWikiContext context) throws XWikiException
38    {
39  24 XWiki xwiki = context.getWiki();
40  24 XWikiDocument doc = context.getDoc();
41  24 XWikiForm form = context.getForm();
42   
43    // Prepare new class
44  24 BaseClass bclass = doc.getXClass();
45  24 BaseClass bclass2 = bclass.clone();
46  24 bclass2.setFields(new HashMap());
47   
48    // Prepare a Map for field renames
49  24 Map<String, String> fieldsToRename = new HashMap<String, String>();
50   
51  24 for (PropertyClass originalProperty : (Collection<PropertyClass>) bclass.getFieldList()) {
52  41 PropertyClass newProperty = originalProperty.clone();
53  41 String name = newProperty.getName();
54  41 Map<String, ?> map = ((EditForm) form).getObject(name);
55  41 newProperty.getXClass(context).fromMap(map, newProperty);
56  41 String newName = newProperty.getName();
57   
58  41 if (!Util.isValidXMLElementName(newName)) {
59  0 context.put("message", "propertynamenotcorrect");
60  0 return true;
61    }
62   
63  41 if (newName.indexOf(" ") != -1) {
64  0 newName = newName.replaceAll(" ", "");
65  0 newProperty.setName(newName);
66    }
67  41 bclass2.addField(newName, newProperty);
68  41 if (!newName.equals(name)) {
69  0 fieldsToRename.put(name, newName);
70  0 bclass2.addPropertyForRemoval(originalProperty);
71    }
72    }
73   
74  24 doc.setXClass(bclass2);
75  24 doc.renameProperties(bclass.getName(), fieldsToRename);
76  24 doc.setMetaDataDirty(true);
77  24 if (doc.isNew()) {
78  0 doc.setCreator(context.getUser());
79    }
80  24 doc.setAuthor(context.getUser());
81  24 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  24 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  24 return false;
99    }
100   
 
101  16 toggle @Override
102    public boolean action(XWikiContext context) throws XWikiException
103    {
104    // CSRF prevention
105  16 if (!csrfTokenCheck(context)) {
106  0 return false;
107    }
108   
109  16 try {
110  16 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  16 String redirect = Utils.getRedirect("view", context);
119  16 sendRedirect(context.getResponse(), redirect);
120  16 return false;
121    }
122   
 
123  0 toggle @Override
124    public String render(XWikiContext context) throws XWikiException
125    {
126  0 return "exception";
127    }
128    }