KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > usermanager > GroupsTools


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
// GroupsTools
15
// NK 21.03.2001
16
//
17
//
18

19
20 package org.jahia.services.usermanager;
21
22 import org.apache.regexp.RE;
23 import org.jahia.exceptions.JahiaException;
24 import org.jahia.registries.ServicesRegistry;
25
26 import java.util.Vector JavaDoc;
27
28
29 /**
30  * Tools class to handle Application Role Groups
31  *
32  * @author NK
33  */

34
35
36 public class GroupsTools {
37     private static org.apache.log4j.Logger logger =
38             org.apache.log4j.Logger.getLogger (GroupsTools.class);
39
40     /**
41      * Pattern used to match Application Role Group Name:
42      *
43      *
44      * ex:
45      *
46      * In case of Application Role Group Names:
47      * appid + "_" + fieldid + "_" + role name
48      *
49      * 23_1_administrator :
50      * app id = 23,
51      * field id = 1,
52      * stored database group name = 23_1_administrator
53      * role name part = administrator
54      *
55      **/

56
57     /**
58      * Pattern used to match the identifier composed by : appid + "_" + fieldid + "_" + role name
59      * in case of groups that are applications' roles
60      */

61     private static String JavaDoc mRole_GroupName_Pattern = "^\\d+_\\d+_[\\w|_]+";
62
63     /** Pattern used to extract the first id part */
64     private static String JavaDoc mFirstIDPart = "(^\\d+)_.+";
65
66     /** Pattern used to extract the second id part */
67     private static String JavaDoc mSecondIDPart = "^\\d+_(\\d+)_.+";
68
69
70     /**
71      * Pattern used to extract the human readable role name part
72      * in case of Role Group Name : appid + "_" + fieldid + "_" + role name
73      */

74     private static String JavaDoc mRoleNamePart = "^\\d+_\\d+_([\\w|_]+$)";
75
76
77     /** The RExp used to match the Application Role Group Name Pattern */
78     private static org.apache.regexp.RE mRExpRoleGroupName = null;
79
80     /** The RExp used to extract the first id part */
81     private static org.apache.regexp.RE mRExpFirstIDPart = null;
82
83     /** The RExp used to extract the second id part */
84     private static org.apache.regexp.RE mRExpSecondIDPart = null;
85
86     /** The RExp used to extract the role name part in the Application role group name pattern */
87     private static org.apache.regexp.RE mRExpRoleNamePart = null;
88
89
90     static {
91
92         try {
93             mRExpRoleGroupName = new RE (mRole_GroupName_Pattern);
94             mRExpFirstIDPart = new RE (mFirstIDPart);
95             mRExpSecondIDPart = new RE (mSecondIDPart);
96             mRExpRoleNamePart = new RE (mRoleNamePart);
97         } catch (Throwable JavaDoc t) {
98             //System.out.println(" Reg Exp Exception " + t.getMessage() );
99
}
100
101     }
102
103
104
105     //--------------------------------------------------------------------------
106
/**
107      * Return the role name part in the Application Role Group Name
108      *
109      * @param String Application Role Group Ident
110      *
111      * @return String The Role Name Part or null if not matching
112      *
113      * @author NK
114      */

115     public static String JavaDoc getRoleNamePart (String JavaDoc appRoleGroupNameIdent) {
116
117         if (appRoleGroupNameIdent == null) {
118             return null;
119         }
120
121         mRExpRoleNamePart.match (appRoleGroupNameIdent);
122         return mRExpRoleNamePart.getParen (1);
123     }
124
125
126     //--------------------------------------------------------------------------
127
/**
128      * Return the first id part ( = the app id ) in the Application Role Group Name
129      *
130      * @param String Application Role Group Ident
131      *
132      * @return String The First ID part or null if not matching
133      *
134      * @author NK
135      */

136     public static String JavaDoc getAppIDPart (String JavaDoc appRoleGroupNameIdent) {
137
138         if (appRoleGroupNameIdent == null) {
139             return null;
140         }
141
142         mRExpFirstIDPart.match (appRoleGroupNameIdent);
143         return mRExpFirstIDPart.getParen (1);
144     }
145
146
147     //--------------------------------------------------------------------------
148
/**
149      * Return the second id Part ( = the field id ) in the Application Role Group Name
150      *
151      * @param String Application Role Group Ident
152      *
153      * @return String The Second ID part or null if not matching
154      *
155      * @author NK
156      */

157     public static String JavaDoc getFieldIDPart (String JavaDoc appRoleGroupNameIdent) {
158
159         if (appRoleGroupNameIdent == null) {
160             return null;
161         }
162
163         mRExpSecondIDPart.match (appRoleGroupNameIdent);
164         return mRExpSecondIDPart.getParen (1);
165     }
166
167
168     //-------------------------------------------------------------------------
169
/**
170      * Check if a group name is in fact an Application Role
171      */

172     public static boolean isRole (String JavaDoc grpName) {
173
174         if (grpName == null) {
175             return false;
176         }
177
178         return mRExpRoleGroupName.match (grpName);
179     }
180
181
182     //-------------------------------------------------------------------------
183
/**
184      * Return a vector of Jahia Groups or Application Role Groups
185      *
186      * @param boolean isRoleGroup if true, return only Application Role Groups
187      * otherwise return only Jahia Groups
188      */

189     public static Vector JavaDoc getGroups (boolean isRoleGroup) {
190
191
192         Vector JavaDoc allGroups = ServicesRegistry.getInstance ()
193                 .getJahiaGroupManagerService ()
194                 .getGroupList ();
195
196         if (allGroups == null) {
197             return null;
198         }
199
200         Vector JavaDoc reqGroups = new Vector JavaDoc ();
201
202         int size = allGroups.size ();
203         String JavaDoc grpKey = null;
204         JahiaGroup grp = null;
205
206         for (int i = 0; i < size; i++) {
207             grpKey = (String JavaDoc) allGroups.get (i);
208             grp = ServicesRegistry.getInstance ()
209                     .getJahiaGroupManagerService ()
210                     .lookupGroup (grpKey);
211             if (grp != null) {
212                 if (isRoleGroup && mRExpRoleGroupName.match (grp.getGroupname ())) {
213                     reqGroups.add (grp);
214                 } else if (!isRoleGroup && !mRExpRoleGroupName.match (grp.getGroupname ())) {
215                     reqGroups.add (grp);
216                 }
217             }
218         }
219         return reqGroups;
220     }
221
222
223     //-------------------------------------------------------------------------
224
/**
225      * Return a vector of Jahia Groups or Application Role Groups
226      *
227      * @param int the site id
228      * @param boolean isRoleGroup if true, return only Application Role Groups
229      * otherwise return only Jahia Groups
230      */

231     public static Vector JavaDoc getGroups (int siteID, boolean isRoleGroup)
232             throws JahiaException {
233
234         //logger.debug("started site=" + siteID);
235

236         Vector JavaDoc reqGroups = new Vector JavaDoc ();
237
238
239         Vector JavaDoc fieldIDs = fieldIDs = ServicesRegistry.getInstance ()
240                 .getJahiaFieldService ()
241                 .getAllFieldIDs (siteID);
242
243         //logger.debug("nbfields=" + fieldIDs.size());
244

245
246         if (fieldIDs == null) {
247             return reqGroups;
248         }
249
250         int nbFieldIDs = fieldIDs.size ();
251
252         Vector JavaDoc allGroups = null;
253         int id = siteID;
254         if (isRoleGroup) {
255             id = 0;
256         }
257         allGroups = ServicesRegistry.getInstance ()
258                 .getJahiaGroupManagerService ()
259                 .getGroupList (id);
260
261         //logger.debug("all grps=" + allGroups.size());
262

263         if (allGroups == null) {
264             return reqGroups;
265         }
266
267
268         int size = allGroups.size ();
269         String JavaDoc grpKey = null;
270         JahiaGroup grp = null;
271         int fieldID = 0;
272         Integer JavaDoc vFieldID = null;
273
274         for (int i = 0; i < size; i++) {
275             grpKey = (String JavaDoc) allGroups.get (i);
276             grp = ServicesRegistry.getInstance ()
277                     .getJahiaGroupManagerService ()
278                     .lookupGroup (grpKey);
279             if (grp != null) {
280                 if (isRoleGroup && mRExpRoleGroupName.match (grp.getGroupname ())) {
281
282                     //logger.debug("all grp=" + grp.getGroupname());
283

284                     // Need to check on field
285
try {
286                         fieldID = Integer.parseInt (getFieldIDPart (grp.getGroupname ()));
287
288                         //logger.debug("field part=" + fieldID);
289

290                         for (int j = 0; j < nbFieldIDs; j++) {
291                             vFieldID = (Integer JavaDoc) fieldIDs.get (j);
292                             if (fieldID == vFieldID.intValue ()) {
293                                 reqGroups.add (grp);
294                             }
295                         }
296                     } catch (Throwable JavaDoc t) {
297                         logger.error ("exception ", t);
298                     }
299                 } else if (!isRoleGroup && !mRExpRoleGroupName.match (grp.getGroupname ())) {
300                     reqGroups.add (grp);
301                 }
302             }
303         }
304         return reqGroups;
305     }
306
307
308 }
Popular Tags