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

File UsersClass.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

16
46
15
1
230
140
26
0.57
3.07
15
1.73

Classes

Class Line # Actions
UsersClass 47 46 0% 26 35
0.5454545654.5%
 

Contributing tests

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