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

File GroupsClass.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

12
37
15
1
214
128
23
0.62
2.47
15
1.53

Classes

Class Line # Actions
GroupsClass 48 37 0% 23 34
0.4687546.9%
 

Contributing tests

This file is covered by 25 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.Arrays;
24    import java.util.Collections;
25    import java.util.HashMap;
26    import java.util.List;
27    import java.util.Map;
28   
29    import org.apache.commons.lang3.StringUtils;
30    import org.dom4j.Element;
31    import org.slf4j.Logger;
32    import org.slf4j.LoggerFactory;
33    import org.xwiki.model.reference.EntityReference;
34   
35    import com.xpn.xwiki.XWikiContext;
36    import com.xpn.xwiki.XWikiException;
37    import com.xpn.xwiki.doc.merge.MergeConfiguration;
38    import com.xpn.xwiki.doc.merge.MergeResult;
39    import com.xpn.xwiki.objects.BaseProperty;
40    import com.xpn.xwiki.objects.LargeStringProperty;
41    import com.xpn.xwiki.objects.meta.PropertyMetaClass;
42   
43    /**
44    * Defines an XClass property type whose value is a list of group references.
45    *
46    * @version $Id: 074f891c7611af8518f168b7a37a4f6c5815efe7 $
47    */
 
48    public class GroupsClass extends ListClass
49    {
50    /** Logging helper object. */
51    private static final Logger LOGGER = LoggerFactory.getLogger(GroupsClass.class);
52   
53    /**
54    * The meta property that specifies if the list box that is used to select the groups should be filled with all the
55    * available groups. This property should not be set when the number of groups is very large.
56    */
57    private static final String META_PROPERTY_USES_LIST = "usesList";
58   
59    /**
60    * Creates a new Groups List property that is described by the given meta class.
61    *
62    * @param metaClass the meta class that defines the list of meta properties associated with this property type
63    */
 
64  484 toggle public GroupsClass(PropertyMetaClass metaClass)
65    {
66  484 super("groupslist", "Groups List", metaClass);
67   
68  484 setSize(20);
69  484 setDisplayType("input");
70  484 setPicker(true);
71    }
72   
73    /**
74    * Default constructor.
75    */
 
76  484 toggle public GroupsClass()
77    {
78  484 this(null);
79    }
80   
 
81  1 toggle @Override
82    public List<String> getList(XWikiContext context)
83    {
84  1 try {
85  1 return (List<String>) context.getWiki().getGroupService(context)
86    .getAllMatchedGroups(null, false, 0, 0, null, context);
87    } catch (XWikiException e) {
88  0 LOGGER.warn("Failed to retrieve the list of groups.", e);
89  0 return Collections.emptyList();
90    }
91    }
92   
 
93  0 toggle @Override
94    public Map<String, ListItem> getMap(XWikiContext context)
95    {
96  0 return new HashMap<String, ListItem>();
97    }
98   
99    /**
100    * @return {@code true} if the list box that is used to select the groups should be filled with all the available
101    * groups, {@code false} otherwise
102    * @deprecated since 4.3M2 this meta property is not used anymore because we changed the default displayer
103    */
 
104  0 toggle @Deprecated
105    public boolean isUsesList()
106    {
107  0 return getIntValue(META_PROPERTY_USES_LIST) == 1;
108    }
109   
110    /**
111    * Sets whether to list all the available groups in the list box used to select the groups. This property should not
112    * be set when the number of groups is very large.
113    *
114    * @param usesList {@code true} to fill the list box that is used to select the groups with all the available
115    * groups, {@code false} otherwise
116    * @deprecated since 4.3M2 this meta property is not used anymore because we changed the default displayer
117    */
 
118  0 toggle @Deprecated
119    public void setUsesList(boolean usesList)
120    {
121  0 setIntValue(META_PROPERTY_USES_LIST, usesList ? 1 : 0);
122    }
123   
 
124  244 toggle @Override
125    public BaseProperty newProperty()
126    {
127  244 BaseProperty property = new LargeStringProperty();
128  244 property.setName(getName());
129  244 return property;
130    }
131   
 
132  241 toggle @Override
133    public BaseProperty fromString(String value)
134    {
135  241 BaseProperty prop = newProperty();
136  241 prop.setValue(value);
137  241 return prop;
138    }
139   
 
140  1 toggle @Override
141    public BaseProperty fromStringArray(String[] strings)
142    {
143  1 List<String> list = Arrays.asList(strings);
144   
145  1 BaseProperty prop = newProperty();
146  1 fromList(prop, list);
147  1 return prop;
148    }
149   
150    /**
151    * @param value a group string reference
152    * @param context the XWiki context
153    * @return the name of the specified group (the document name component from the given reference)
154    */
 
155  0 toggle public String getText(String value, XWikiContext context)
156    {
157  0 if (value.indexOf(":") != -1) {
158  0 return value;
159    }
160  0 return value.substring(value.lastIndexOf(".") + 1);
161    }
162   
163    /**
164    * Splits the given string into a list of group names.
165    *
166    * @param value a comma separate list of group names
167    * @return the list of group names
168    */
 
169  353 toggle public static List<String> getListFromString(String value)
170    {
171  353 return getListFromString(value, ",", false);
172    }
173   
 
174  0 toggle @Override
175    public BaseProperty newPropertyfromXML(Element ppcel)
176    {
177  0 String value = ppcel.getText();
178  0 return fromString(value);
179    }
180   
 
181  0 toggle @Override
182    public List<String> toList(BaseProperty<?> property)
183    {
184  0 List<String> selectlist;
185   
186  0 if (property == null) {
187  0 selectlist = new ArrayList<String>();
188    } else {
189  0 selectlist = getListFromString((String) property.getValue());
190    }
191   
192  0 return selectlist;
193    }
194   
 
195  1 toggle @Override
196    public void fromList(BaseProperty<?> property, List<String> list)
197    {
198  1 if (isMultiSelect()) {
199  1 property.setValue(list != null ? StringUtils.join(list, ',') : null);
200    } else {
201  0 property.setValue(list != null && !list.isEmpty() ? list.get(0) : null);
202    }
203    }
204   
 
205  0 toggle @Override
206    public <T extends EntityReference> void mergeProperty(BaseProperty<T> currentProperty,
207    BaseProperty<T> previousProperty, BaseProperty<T> newProperty, MergeConfiguration configuration,
208    XWikiContext xcontext, MergeResult mergeResult)
209    {
210    // always a not ordered list
211  0 mergeNotOrderedListProperty(currentProperty, previousProperty, newProperty, configuration, xcontext,
212    mergeResult);
213    }
214    }