1 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 ; 27 28 29 34 35 36 public class GroupsTools { 37 private static org.apache.log4j.Logger logger = 38 org.apache.log4j.Logger.getLogger (GroupsTools.class); 39 40 56 57 61 private static String mRole_GroupName_Pattern = "^\\d+_\\d+_[\\w|_]+"; 62 63 64 private static String mFirstIDPart = "(^\\d+)_.+"; 65 66 67 private static String mSecondIDPart = "^\\d+_(\\d+)_.+"; 68 69 70 74 private static String mRoleNamePart = "^\\d+_\\d+_([\\w|_]+$)"; 75 76 77 78 private static org.apache.regexp.RE mRExpRoleGroupName = null; 79 80 81 private static org.apache.regexp.RE mRExpFirstIDPart = null; 82 83 84 private static org.apache.regexp.RE mRExpSecondIDPart = null; 85 86 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 t) { 98 } 100 101 } 102 103 104 105 115 public static String getRoleNamePart (String appRoleGroupNameIdent) { 116 117 if (appRoleGroupNameIdent == null) { 118 return null; 119 } 120 121 mRExpRoleNamePart.match (appRoleGroupNameIdent); 122 return mRExpRoleNamePart.getParen (1); 123 } 124 125 126 136 public static String getAppIDPart (String appRoleGroupNameIdent) { 137 138 if (appRoleGroupNameIdent == null) { 139 return null; 140 } 141 142 mRExpFirstIDPart.match (appRoleGroupNameIdent); 143 return mRExpFirstIDPart.getParen (1); 144 } 145 146 147 157 public static String getFieldIDPart (String appRoleGroupNameIdent) { 158 159 if (appRoleGroupNameIdent == null) { 160 return null; 161 } 162 163 mRExpSecondIDPart.match (appRoleGroupNameIdent); 164 return mRExpSecondIDPart.getParen (1); 165 } 166 167 168 172 public static boolean isRole (String grpName) { 173 174 if (grpName == null) { 175 return false; 176 } 177 178 return mRExpRoleGroupName.match (grpName); 179 } 180 181 182 189 public static Vector getGroups (boolean isRoleGroup) { 190 191 192 Vector allGroups = ServicesRegistry.getInstance () 193 .getJahiaGroupManagerService () 194 .getGroupList (); 195 196 if (allGroups == null) { 197 return null; 198 } 199 200 Vector reqGroups = new Vector (); 201 202 int size = allGroups.size (); 203 String grpKey = null; 204 JahiaGroup grp = null; 205 206 for (int i = 0; i < size; i++) { 207 grpKey = (String ) 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 231 public static Vector getGroups (int siteID, boolean isRoleGroup) 232 throws JahiaException { 233 234 236 Vector reqGroups = new Vector (); 237 238 239 Vector fieldIDs = fieldIDs = ServicesRegistry.getInstance () 240 .getJahiaFieldService () 241 .getAllFieldIDs (siteID); 242 243 245 246 if (fieldIDs == null) { 247 return reqGroups; 248 } 249 250 int nbFieldIDs = fieldIDs.size (); 251 252 Vector allGroups = null; 253 int id = siteID; 254 if (isRoleGroup) { 255 id = 0; 256 } 257 allGroups = ServicesRegistry.getInstance () 258 .getJahiaGroupManagerService () 259 .getGroupList (id); 260 261 263 if (allGroups == null) { 264 return reqGroups; 265 } 266 267 268 int size = allGroups.size (); 269 String grpKey = null; 270 JahiaGroup grp = null; 271 int fieldID = 0; 272 Integer vFieldID = null; 273 274 for (int i = 0; i < size; i++) { 275 grpKey = (String ) 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 284 try { 286 fieldID = Integer.parseInt (getFieldIDPart (grp.getGroupname ())); 287 288 290 for (int j = 0; j < nbFieldIDs; j++) { 291 vFieldID = (Integer ) fieldIDs.get (j); 292 if (fieldID == vFieldID.intValue ()) { 293 reqGroups.add (grp); 294 } 295 } 296 } catch (Throwable 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 |