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

File PropAddAction.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

10
38
2
1
100
70
7
0.18
19
2
3.5

Classes

Class Line # Actions
PropAddAction 37 38 0% 7 3
0.9494%
 

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.ArrayList;
23    import java.util.List;
24   
25    import javax.servlet.http.HttpServletResponse;
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.objects.meta.MetaClass;
34    import com.xpn.xwiki.objects.meta.PropertyMetaClass;
35    import com.xpn.xwiki.util.Util;
36   
 
37    public class PropAddAction extends XWikiAction
38    {
 
39  59 toggle @Override
40    public boolean action(XWikiContext context) throws XWikiException
41    {
42    // CSRF prevention
43  59 if (!csrfTokenCheck(context)) {
44  0 return false;
45    }
46   
47  59 XWiki xwiki = context.getWiki();
48  59 XWikiResponse response = context.getResponse();
49  59 XWikiDocument doc = context.getDoc();
50  59 XWikiForm form = context.getForm();
51   
52  59 String propName = ((PropAddForm) form).getPropName();
53   
54  59 if (!Util.isValidXMLElementName(propName)) {
55  1 context.put("message", "action.addClassProperty.error.invalidName");
56  1 response.setStatus(HttpServletResponse.SC_BAD_REQUEST,
57    localizePlainOrKey("action.addClassProperty.error.invalidName"));
58  1 return true;
59    }
60   
61  58 String propType = ((PropAddForm) form).getPropType();
62  58 BaseClass bclass = doc.getXClass();
63  58 bclass.setName(doc.getFullName());
64  58 if (bclass.get(propName) != null) {
65  9 context.put("message", "action.addClassProperty.error.alreadyExists");
66  9 List<String> parameters = new ArrayList<String>();
67  9 parameters.add(propName);
68  9 context.put("messageParameters", parameters);
69  9 response.setStatus(HttpServletResponse.SC_BAD_REQUEST,
70    localizePlainOrKey("action.addClassProperty.error.alreadyExists", parameters.toArray()));
71  9 return true;
72    } else {
73  49 MetaClass mclass = xwiki.getMetaclass();
74  49 PropertyMetaClass pmclass = (PropertyMetaClass) mclass.get(propType);
75  49 if (pmclass != null) {
76  49 PropertyClass pclass = (PropertyClass) pmclass.newObject(context);
77  49 pclass.setObject(bclass);
78  49 pclass.setName(propName);
79  49 pclass.setPrettyName(propName);
80  49 bclass.put(propName, pclass);
81  49 doc.setAuthorReference(context.getUserReference());
82  49 if (doc.isNew()) {
83  7 doc.setCreatorReference(context.getUserReference());
84    }
85  49 doc.setMetaDataDirty(true);
86  49 xwiki.saveDocument(doc, localizePlainOrKey("core.comment.addClassProperty"), true, context);
87    }
88    }
89    // forward to edit
90  49 String redirect = Utils.getRedirect("edit", "editor=class", context);
91  49 sendRedirect(response, redirect);
92  49 return false;
93    }
94   
 
95  10 toggle @Override
96    public String render(XWikiContext context) throws XWikiException
97    {
98  10 return "exception";
99    }
100    }