1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
|
|
| 82% |
Uncovered Elements: 9 (50) |
Complexity: 7 |
Complexity Density: 0.18 |
|
37 |
|
public class PropAddAction extends XWikiAction |
38 |
|
{ |
|
|
| 80.9% |
Uncovered Elements: 9 (47) |
Complexity: 6 |
Complexity Density: 0.16 |
|
39 |
6 |
@Override... |
40 |
|
public boolean action(XWikiContext context) throws XWikiException |
41 |
|
{ |
42 |
|
|
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 |
|
|
90 |
4 |
String redirect = Utils.getRedirect("edit", "editor=class", context); |
91 |
4 |
sendRedirect(response, redirect); |
92 |
4 |
return false; |
93 |
|
} |
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
95 |
2 |
@Override... |
96 |
|
public String render(XWikiContext context) throws XWikiException |
97 |
|
{ |
98 |
2 |
return "exception"; |
99 |
|
} |
100 |
|
} |