Clover Coverage Report - Atlassian Core
Coverage timestamp: Sun Nov 30 2008 18:33:35 CST
46   214   23   5.11
18   154   0.5   9
9     2.56  
1    
 
 
  GroupUtils       Line # 21 46 23 74% 0.739726
 
  (7)
 
1    /*
2    * Created by IntelliJ IDEA.
3    * User: Administrator
4    * Date: Oct 23, 2002
5    * Time: 1:08:11 PM
6    * To change this template use Options | File Templates.
7    */
8    package com.atlassian.core.user;
9   
10    import com.opensymphony.user.*;
11    import org.apache.log4j.Category;
12   
13    import java.util.*;
14   
15   
16    /**
17    * A utility class for operating on groups.
18    *
19    * @see com.atlassian.core.user.UserUtils
20    */
 
21    public class GroupUtils
22    {
23    private static final Category log = Category.getInstance(GroupUtils.class);
24   
25    final static Comparator alphaGroupComparator = new Comparator()
26    {
 
27  17 toggle public int compare(Object a, Object b)
28    {
29  17 if (a == null)
30    {
31  0 if (b == null)
32    {
33  0 return 0;
34    }
35    else
36    {
37  0 return -1;
38    }
39    }
40    else
41    {
42  17 if (b == null)
43    {
44  0 return 1;
45    }
46    else
47    {
48  17 return alphaStringComparator.compare(((Group) a).getName(), ((Group) b).getName());
49    }
50    }
51    }
52    };
53   
54    final static Comparator alphaStringComparator = new Comparator()
55    {
 
56  25 toggle public int compare(Object o1, Object o2)
57    {
58  25 String a = (String) o1;
59  25 String b = (String) o2;
60   
61  25 if (a == null)
62    {
63  0 if (b == null)
64    {
65  0 return 0;
66    }
67    else
68    {
69  0 return -1;
70    }
71    }
72    else
73    {
74  25 if (b == null)
75    {
76  0 return 1;
77    }
78    else
79    {
80  25 return a.compareToIgnoreCase(b);
81    }
82    }
83    }
84    };
85   
86    /**
87    * This method gets a group from OSUser - however if the group does not exist, it tries to create
88    * it and then return it (mitigates the need for a setup file).
89    */
 
90  1 toggle public static Group getGroupSafely(String name) throws ImmutableException
91    {
92  1 Group group = null;
93   
94  1 try
95    {
96  1 group = UserManager.getInstance().getGroup(name);
97    }
98    catch (EntityNotFoundException e)
99    {
100  1 try
101    {
102  1 group = UserManager.getInstance().createGroup(name);
103    }
104    catch (DuplicateEntityException e1) // should never happen - touch wood ;)
105    {
106  0 e.printStackTrace(System.err);
107    }
108    }
109   
110  1 return group;
111    }
112   
113    /**
114    * Get a group from the underlying user manager.
115    * @return The Group from the underlying UserManager, or null if the group doesn't exist
116    */
 
117  6 toggle public static Group getGroup(String name)
118    {
119  6 if (name == null)
120    {
121  1 return null;
122    }
123    else
124    {
125  5 if (GroupUtils.existsGroup(name))
126    {
127  4 try
128    {
129  4 return UserManager.getInstance().getGroup(name);
130    }
131    catch (EntityNotFoundException e)
132    {
133  0 log.error("Error getting group: " + name, e);
134  0 return null;
135    }
136    }
137    else
138    {
139  1 return null;
140    }
141    }
142    }
143   
144    /**
145    * A simple method to tell if a group already exists or not
146    */
 
147  9 toggle public static boolean existsGroup(String name)
148    {
149  9 try
150    {
151  9 UserManager.getInstance().getGroup(name);
152    }
153    catch (EntityNotFoundException e)
154    {
155  3 return false;
156    }
157   
158  6 return true;
159    }
160   
161    /**
162    * @return A collection of all groups (sorted by name)
163    */
 
164  4 toggle public static Collection getGroups()
165    {
166  4 List groups = UserManager.getInstance().getGroups();
167  4 sortGroups(groups);
168  4 return groups;
169    }
170   
171    /**
172    * Sort group names in alphabetical (*not* ascii) order. This means that
173    * upper case and lower case characters are sorted together.
174    * @param groups
175    */
 
176  5 toggle public static void sortGroups(List groups)
177    {
178  5 Collections.sort(groups, alphaGroupComparator);
179    }
180   
181    /**
182    * Sort group names in alphabetical (*not* ascii) order. This means that
183    * upper case and lower case characters are sorted together.
184    * @param groups
185    */
 
186  1 toggle public static void sortGroupNames(List groups)
187    {
188  1 Collections.sort(groups, alphaStringComparator);
189    }
190   
191    /**
192    * This will remove a group, and remove any users from that group
193    */
 
194  1 toggle public static void removeGroup(Group group) throws Exception
195    {
196  1 Collection users = new HashSet(group.getUsers());
197   
198  2 for (Iterator iterator = users.iterator(); iterator.hasNext();)
199    {
200  1 try
201    {
202  1 User u = UserUtils.getUser((String) iterator.next());
203  1 u.removeFromGroup(group);
204    }
205    catch (EntityNotFoundException e)
206    {
207    // don't care! :)
208    }
209    }
210   
211  1 group.remove();
212    }
213   
214    }