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

File PropAddAction.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% 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 9
0.8282%
 

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