View Javadoc

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          public int compare(Object a, Object b)
28          {
29              if (a == null)
30              {
31                  if (b == null)
32                  {
33                      return 0;
34                  }
35                  else
36                  {
37                      return -1;
38                  }
39              }
40              else
41              {
42                  if (b == null)
43                  {
44                      return 1;
45                  }
46                  else
47                  {
48                      return alphaStringComparator.compare(((Group) a).getName(), ((Group) b).getName());
49                  }
50              }
51          }
52      };
53  
54      final static Comparator alphaStringComparator = new Comparator()
55      {
56          public int compare(Object o1, Object o2)
57          {
58              String a = (String) o1;
59              String b = (String) o2;
60  
61              if (a == null)
62              {
63                  if (b == null)
64                  {
65                      return 0;
66                  }
67                  else
68                  {
69                      return -1;
70                  }
71              }
72              else
73              {
74                  if (b == null)
75                  {
76                      return 1;
77                  }
78                  else
79                  {
80                      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      public static Group getGroupSafely(String name) throws ImmutableException
91      {
92          Group group = null;
93  
94          try
95          {
96              group = UserManager.getInstance().getGroup(name);
97          }
98          catch (EntityNotFoundException e)
99          {
100             try
101             {
102                 group = UserManager.getInstance().createGroup(name);
103             }
104             catch (DuplicateEntityException e1) // should never happen - touch wood ;)
105             {
106                 e.printStackTrace(System.err);
107             }
108         }
109 
110         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     public static Group getGroup(String name)
118     {
119         if (name == null)
120         {
121             return null;
122         }
123         else
124         {
125             if (GroupUtils.existsGroup(name))
126             {
127                 try
128                 {
129                     return UserManager.getInstance().getGroup(name);
130                 }
131                 catch (EntityNotFoundException e)
132                 {
133                     log.error("Error getting group: " + name, e);
134                     return null;
135                 }
136             }
137             else
138             {
139                 return null;
140             }
141         }
142     }
143 
144     /**
145      * A simple method to tell if a group already exists or not
146      */
147     public static boolean existsGroup(String name)
148     {
149         try
150         {
151             UserManager.getInstance().getGroup(name);
152         }
153         catch (EntityNotFoundException e)
154         {
155             return false;
156         }
157 
158         return true;
159     }
160 
161     /**
162      * @return A collection of all groups (sorted by name)
163      */
164     public static Collection getGroups()
165     {
166         List groups = UserManager.getInstance().getGroups();
167         sortGroups(groups);
168         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     public static void sortGroups(List groups)
177     {
178         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     public static void sortGroupNames(List groups)
187     {
188         Collections.sort(groups, alphaStringComparator);
189     }
190 
191     /**
192      * This will remove a group, and remove any users from that group
193      */
194     public static void removeGroup(Group group) throws Exception
195     {
196         Collection users = new HashSet(group.getUsers());
197 
198         for (Iterator iterator = users.iterator(); iterator.hasNext();)
199         {
200             try
201             {
202                 User u = UserUtils.getUser((String) iterator.next());
203                 u.removeFromGroup(group);
204             }
205             catch (EntityNotFoundException e)
206             {
207                 // don't care! :)
208             }
209         }
210 
211         group.remove();
212     }
213 
214 }