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

File LevelsClass.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

14
66
14
1
207
159
24
0.36
4.71
14
1.71

Classes

Class Line # Actions
LevelsClass 47 66 0% 24 19
0.7978723679.8%
 

Contributing tests

This file is covered by 14 tests. .

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.objects.classes;
21   
22    import java.util.ArrayList;
23    import java.util.Collections;
24    import java.util.HashMap;
25    import java.util.List;
26    import java.util.Map;
27   
28    import org.apache.commons.lang3.StringUtils;
29    import org.apache.ecs.xhtml.input;
30    import org.apache.ecs.xhtml.option;
31    import org.apache.ecs.xhtml.select;
32    import org.dom4j.Element;
33    import org.xwiki.model.reference.EntityReference;
34    import org.xwiki.xml.XMLUtils;
35   
36    import com.xpn.xwiki.XWikiContext;
37    import com.xpn.xwiki.XWikiException;
38    import com.xpn.xwiki.doc.merge.MergeConfiguration;
39    import com.xpn.xwiki.doc.merge.MergeResult;
40    import com.xpn.xwiki.internal.xml.XMLAttributeValueFilter;
41    import com.xpn.xwiki.objects.BaseCollection;
42    import com.xpn.xwiki.objects.BaseProperty;
43    import com.xpn.xwiki.objects.StringProperty;
44    import com.xpn.xwiki.objects.meta.PropertyMetaClass;
45    import com.xpn.xwiki.web.XWikiRequest;
46   
 
47    public class LevelsClass extends ListClass
48    {
49    private static final String XCLASSNAME = "levelslist";
50   
 
51  484 toggle public LevelsClass(PropertyMetaClass wclass)
52    {
53  484 super(XCLASSNAME, "Levels List", wclass);
54  484 setSize(6);
55    }
56   
 
57  484 toggle public LevelsClass()
58    {
59  484 this(null);
60    }
61   
 
62  2 toggle @Override
63    public List<String> getList(XWikiContext context)
64    {
65  2 List<String> list;
66  2 try {
67  2 list = context.getWiki().getRightService().listAllLevels(context);
68    } catch (XWikiException e) {
69    // TODO add log exception
70  0 list = new ArrayList<String>();
71    }
72   
73  2 XWikiRequest req = context.getRequest();
74  2 if (("editrights".equals(req.get("xpage")) || "rights".equals(req.get("editor")))
75    && (!"1".equals(req.get("global")))) {
76  0 list.remove("admin");
77  0 list.remove("programming");
78  0 list.remove("delete");
79  0 list.remove("register");
80    }
81  2 return list;
82    }
83   
 
84  0 toggle @Override
85    public Map<String, ListItem> getMap(XWikiContext context)
86    {
87  0 return new HashMap<String, ListItem>();
88    }
89   
 
90  358 toggle @Override
91    public BaseProperty newProperty()
92    {
93  358 BaseProperty property = new StringProperty();
94  358 property.setName(getName());
95  358 return property;
96    }
97   
 
98  355 toggle @Override
99    public BaseProperty fromString(String value)
100    {
101  355 BaseProperty prop = newProperty();
102  355 prop.setValue(value);
103  355 return prop;
104    }
105   
 
106  1 toggle @Override
107    public BaseProperty fromStringArray(String[] strings)
108    {
109  1 List<String> list = new ArrayList<String>();
110  2 for (int i = 0; i < strings.length; i++) {
111  1 if (!strings[i].trim().equals("")) {
112  1 list.add(strings[i]);
113    }
114    }
115  1 BaseProperty prop = newProperty();
116  1 fromList(prop, list);
117  1 return prop;
118    }
119   
 
120  11 toggle public String getText(String value, XWikiContext context)
121    {
122  11 return value;
123    }
124   
 
125  244 toggle public static List<String> getListFromString(String value)
126    {
127  244 return getListFromString(value, ",", false);
128    }
129   
 
130  1 toggle @Override
131    public void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context)
132    {
133  1 select select = new select(prefix + name, 1);
134  1 select.setAttributeFilter(new XMLAttributeValueFilter());
135  1 select.setMultiple(isMultiSelect());
136  1 select.setSize(getSize());
137  1 select.setName(prefix + name);
138  1 select.setID(prefix + name);
139  1 select.setDisabled(isDisabled());
140   
141  1 List<String> list = getList(context);
142   
143  1 BaseProperty prop = (BaseProperty) object.safeget(name);
144  1 List<String> selectlist = toList(prop);
145   
146    // Add options from Set
147  1 for (String value : list) {
148  11 String display = getText(value, context);
149  11 option option = new option(display, value);
150  11 option.setAttributeFilter(new XMLAttributeValueFilter());
151  11 option.addElement(XMLUtils.escape(display));
152    // If we don't have this option in the list then add it
153  11 if (!list.contains(value)) {
154  0 list.add(value);
155    }
156  11 if (selectlist.contains(value)) {
157  1 option.setSelected(true);
158    }
159  11 select.addElement(option);
160    }
161   
162  1 buffer.append(select.toString());
163  1 input in = new input();
164  1 in.setAttributeFilter(new XMLAttributeValueFilter());
165  1 in.setType("hidden");
166  1 in.setName(prefix + name);
167  1 in.setDisabled(isDisabled());
168  1 buffer.append(in.toString());
169    }
170   
 
171  0 toggle @Override
172    public BaseProperty newPropertyfromXML(Element ppcel)
173    {
174  0 String value = ppcel.getText();
175  0 return fromString(value);
176    }
177   
 
178  1 toggle @Override
179    public List<String> toList(BaseProperty<?> property)
180    {
181  1 List<String> selectlist;
182   
183  1 if (property == null) {
184  0 selectlist = Collections.emptyList();
185    } else {
186  1 selectlist = getListFromString((String) property.getValue());
187    }
188   
189  1 return selectlist;
190    }
191   
 
192  1 toggle @Override
193    public void fromList(BaseProperty<?> property, List<String> list)
194    {
195  1 property.setValue(list != null ? StringUtils.join(list, ',') : null);
196    }
197   
 
198  0 toggle @Override
199    public <T extends EntityReference> void mergeProperty(BaseProperty<T> currentProperty,
200    BaseProperty<T> previousProperty, BaseProperty<T> newProperty, MergeConfiguration configuration,
201    XWikiContext xcontext, MergeResult mergeResult)
202    {
203    // always a not ordered list
204  0 mergeNotOrderedListProperty(currentProperty, previousProperty, newProperty, configuration, xcontext,
205    mergeResult);
206    }
207    }