1 25 package org.ofbiz.shark.user; 26 27 import java.util.List ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 31 import org.ofbiz.entity.GenericValue; 32 import org.ofbiz.entity.GenericDelegator; 33 import org.ofbiz.entity.GenericEntityException; 34 import org.ofbiz.shark.container.SharkContainer; 35 import org.ofbiz.base.util.UtilMisc; 36 import org.ofbiz.base.util.Debug; 37 import org.ofbiz.base.util.UtilValidate; 38 39 import org.enhydra.shark.api.internal.usergroup.UserGroupManager; 40 import org.enhydra.shark.api.internal.working.CallbackUtilities; 41 import org.enhydra.shark.api.RootException; 42 import org.enhydra.shark.api.UserTransaction; 43 44 51 public class GenericUserGroupMgr implements UserGroupManager { 52 53 public static final String module = GenericUserGroupMgr.class.getName(); 54 protected CallbackUtilities callBack = null; 55 56 public void configure(CallbackUtilities callbackUtilities) throws RootException { 57 this.callBack = callbackUtilities; 58 } 59 60 public List getAllGroupnames(UserTransaction trans) throws RootException { 61 GenericDelegator delegator = SharkContainer.getDelegator(); 62 List groupNames = new ArrayList (); 63 List groups = null; 64 try { 65 groups = delegator.findAll("SharkGroup"); 66 } catch (GenericEntityException e) { 67 Debug.logError(e, module); 68 throw new RootException(e); 69 } 70 if (groups != null && groups.size() > 0) { 71 Iterator i = groups.iterator(); 72 while (i.hasNext()) { 73 GenericValue v = (GenericValue) i.next(); 74 groupNames.add(v.getString("groupName")); 75 } 76 } 77 return groupNames; 78 } 79 80 public List getAllUsers(UserTransaction trans) throws RootException { 81 GenericDelegator delegator = SharkContainer.getDelegator(); 82 List userNames = new ArrayList (); 83 List users = null; 84 try { 85 users = delegator.findAll("SharkUser"); 86 } catch (GenericEntityException e) { 87 Debug.logError(e, module); 88 throw new RootException(e); 89 } 90 if (users != null && users.size() > 0) { 91 Iterator i = users.iterator(); 92 while (i.hasNext()) { 93 GenericValue v = (GenericValue) i.next(); 94 userNames.add(v.getString("userName")); 95 } 96 } 97 return userNames; 98 } 99 100 public List getAllUsers(UserTransaction trans, String groupName) throws RootException { 101 GenericDelegator delegator = SharkContainer.getDelegator(); 102 List userNames = new ArrayList (); 103 List members = null; 104 try { 105 members = delegator.findByAnd("SharkGroupMember", UtilMisc.toMap("groupName", groupName)); 106 } catch (GenericEntityException e) { 107 Debug.logError(e, module); 108 throw new RootException(e); 109 } 110 if (members != null && members.size() > 0) { 111 Iterator i = members.iterator(); 112 while (i.hasNext()) { 113 GenericValue v = (GenericValue) i.next(); 114 userNames.add(v.getString("userName")); 115 } 116 } 117 return userNames; 118 } 119 120 public List getAllUsers(UserTransaction trans, List groupNames) throws RootException { 121 List userNames = new ArrayList (); 122 if (groupNames != null && groupNames.size() > 0) { 123 Iterator i = groupNames.iterator(); 124 while (i.hasNext()) { 125 String groupName = (String ) i.next(); 126 userNames.addAll(getAllUsers(trans, groupName)); 127 } 128 } 129 return userNames; 130 } 131 132 public List getAllImmediateUsers(UserTransaction trans, String groupName) throws RootException { 133 return this.getAllUsers(trans, groupName); 134 } 135 136 public List getAllSubgroups(UserTransaction trans, String groupName) throws RootException { 137 GenericDelegator delegator = SharkContainer.getDelegator(); 138 List subGroups = new ArrayList (); 139 List rollups = null; 140 try { 141 rollups = delegator.findByAnd("SharkGroupRollup", UtilMisc.toMap("groupName", groupName)); 142 } catch (GenericEntityException e) { 143 Debug.logError(e, module); 144 throw new RootException(e); 145 } 146 if (rollups != null && rollups.size() > 0) { 147 Iterator i = rollups.iterator(); 148 while (i.hasNext()) { 149 GenericValue v = (GenericValue) i.next(); 150 subGroups.add(v.getString("subGroupName")); 151 } 152 } 153 return subGroups; 154 } 155 156 public List getAllSubgroups(UserTransaction trans, List groupNames) throws RootException { 157 List subGroups = new ArrayList (); 158 if (groupNames != null && groupNames.size() > 0) { 159 Iterator i = groupNames.iterator(); 160 while (i.hasNext()) { 161 String groupName = (String ) i.next(); 162 subGroups.addAll(getAllSubgroups(trans, groupName)); 163 } 164 } 165 return subGroups; 166 } 167 168 public List getAllImmediateSubgroups(UserTransaction trans, String groupName) throws RootException { 169 return this.getAllSubgroups(trans, groupName); 170 } 171 172 public void createGroup(UserTransaction trans, String groupName, String description) throws RootException { 173 GenericDelegator delegator = SharkContainer.getDelegator(); 174 GenericValue group = delegator.makeValue("SharkGroup", null); 175 group.set("groupName", groupName); 176 group.set("description", description); 177 try { 178 delegator.create(group); 179 } catch (GenericEntityException e) { 180 Debug.logError(e, module); 181 throw new RootException(e); 182 } 183 } 184 185 public void removeGroup(UserTransaction trans, String groupName) throws RootException { 186 GenericValue group = getGroup(groupName); 187 if (group != null) { 188 try { 189 group.remove(); 190 } catch (GenericEntityException e) { 191 Debug.logError(e, module); 192 throw new RootException(e); 193 } 194 } 195 } 196 197 public boolean doesGroupExist(UserTransaction trans, String groupName) throws RootException { 198 GenericValue group = getGroup(groupName); 199 if (group != null) { 200 return true; 201 } 202 return false; 203 } 204 205 public boolean doesGroupBelongToGroup(UserTransaction trans, String groupName, String subGroupName) throws RootException { 206 GenericValue rollup = getGroupRollup(groupName, subGroupName); 207 if (rollup != null) { 208 return true; 209 } 210 return false; 211 } 212 213 public void updateGroup(UserTransaction trans, String groupName, String description) throws RootException { 214 GenericValue group = getGroup(groupName); 215 if (group != null) { 216 group.set("description", description); 217 try { 218 group.store(); 219 } catch (GenericEntityException e) { 220 Debug.logError(e, module); 221 throw new RootException(e); 222 } 223 } 224 } 225 226 public void addGroupToGroup(UserTransaction trans, String parentGroupName, String groupName) throws RootException { 227 GenericDelegator delegator = SharkContainer.getDelegator(); 228 GenericValue rollup = delegator.makeValue("SharkGroupRollup", null); 229 rollup.set("parentGroupName", parentGroupName); 230 rollup.set("groupName", groupName); 231 try { 232 delegator.create(rollup); 233 } catch (GenericEntityException e) { 234 Debug.logError(e, module); 235 throw new RootException(e); 236 } 237 } 238 239 public void removeGroupFromGroup(UserTransaction trans, String parentGroup, String group) throws RootException { 240 GenericValue rollup = getGroupRollup(parentGroup, group); 241 if (rollup != null) { 242 try { 243 rollup.remove(); 244 } catch (GenericEntityException e) { 245 Debug.logError(e, module); 246 throw new RootException(e); 247 } 248 } 249 } 250 251 public void removeGroupTree(UserTransaction trans, String s) throws RootException { 252 } 254 255 public void removeUsersFromGroupTree(UserTransaction trans, String s) throws RootException { 256 } 258 259 public void moveGroup(UserTransaction trans, String currentParentGroup, String newParentGroup, String groupName) throws RootException { 260 this.removeGroupFromGroup(trans, currentParentGroup, groupName); 261 this.addGroupToGroup(trans, newParentGroup, groupName); 262 } 263 264 public String getGroupDescription(UserTransaction trans, String groupName) throws RootException { 265 GenericValue group = getGroup(groupName); 266 if (group != null) { 267 return group.getString("description"); 268 } 269 return null; 270 } 271 272 public void addUserToGroup(UserTransaction trans, String groupName, String username) throws RootException { 273 GenericDelegator delegator = SharkContainer.getDelegator(); 274 GenericValue member = delegator.makeValue("SharkGroupMember", null); 275 member.set("groupName", groupName); 276 member.set("userName", username); 277 try { 278 delegator.create(member); 279 } catch (GenericEntityException e) { 280 Debug.logError(e, module); 281 throw new RootException(e); 282 } 283 } 284 285 public void removeUserFromGroup(UserTransaction trans, String groupName, String username) throws RootException { 286 GenericValue member = getGroupMember(groupName, username); 287 if (member != null) { 288 try { 289 member.remove(); 290 } catch (GenericEntityException e) { 291 Debug.logError(e, module); 292 throw new RootException(e); 293 } 294 } 295 } 296 297 public void moveUser(UserTransaction trans, String currentGroup, String newGroup, String username) throws RootException { 298 this.removeUserFromGroup(trans, currentGroup, username); 299 this.addUserToGroup(trans, newGroup, username); 300 } 301 302 public boolean doesUserBelongToGroup(UserTransaction trans, String groupName, String username) throws RootException { 303 GenericValue member = getGroupMember(groupName, username); 304 if (member != null) { 305 return true; 306 } 307 return false; 308 } 309 310 public void createUser(UserTransaction trans, String groupName, String username, String password, String firstName, String lastName, String email) throws RootException { 311 GenericDelegator delegator = SharkContainer.getDelegator(); 312 GenericValue user = delegator.makeValue("SharkUser", null); 313 user.set("userName", username); 314 user.set("firstName", firstName); 315 user.set("lastName", lastName); 316 user.set("passwd", password); 317 user.set("emailAddress", email); 318 try { 319 delegator.create(user); 320 } catch (GenericEntityException e) { 321 Debug.logError(e, module); 322 throw new RootException(e); 323 } 324 if (groupName != null) { 325 this.addUserToGroup(trans, groupName, username); 326 } 327 } 328 329 public void updateUser(UserTransaction trans, String username, String firstName, String lastName, String email) throws RootException { 330 GenericValue user = getUser(username); 331 if (user != null) { 332 user.set("firstName", firstName); 333 user.set("lastName", firstName); 334 user.set("emailAddress", email); 335 try { 336 user.store(); 337 } catch (GenericEntityException e) { 338 Debug.logError(e, module); 339 throw new RootException(e); 340 } 341 } 342 } 343 344 public void removeUser(UserTransaction trans, String username) throws RootException { 345 GenericValue user = getUser(username); 346 if (user != null) { 347 try { 348 user.remove(); 349 } catch (GenericEntityException e) { 350 Debug.logError(e, module); 351 throw new RootException(e); 352 } 353 } 354 } 355 356 public boolean doesUserExist(UserTransaction trans, String username) throws RootException { 357 GenericValue user = getUser(username); 358 if (user == null) { 359 return false; 360 } 361 return true; 362 } 363 364 public void setPassword(UserTransaction trans, String username, String password) throws RootException { 365 GenericValue user = getUser(username); 366 if (user != null) { 367 user.set("passwd", password); 368 try { 369 user.store(); 370 } catch (GenericEntityException e) { 371 Debug.logError(e, module); 372 throw new RootException(e); 373 } 374 } 375 } 376 377 public String getUserRealName(UserTransaction trans, String username) throws RootException { 378 StringBuffer buf = new StringBuffer (); 379 GenericValue user = getUser(username); 380 if (!UtilValidate.isEmpty(user.getString("firstName"))) { 381 buf.append(user.getString("firstName")); 382 383 } 384 if (!UtilValidate.isEmpty(user.getString("lastName"))) { 385 if (buf.length() > 0) { 386 buf.append(" "); 387 } 388 buf.append(user.getString("lastName")); 389 } 390 return buf.toString(); 391 } 392 393 public String getUserFirstName(UserTransaction trans, String username) throws RootException { 394 GenericValue user = getUser(username); 395 return user.getString("firstName"); 396 } 397 398 public String getUserLastName(UserTransaction trans, String username) throws RootException { 399 GenericValue user = getUser(username); 400 return user.getString("lastName"); 401 } 402 403 public String getUserEMailAddress(UserTransaction trans, String username) throws RootException { 404 GenericValue user = getUser(username); 405 if (user != null) { 406 return user.getString("emailAddress"); 407 } 408 return null; 409 } 410 411 private GenericValue getUser(String username) throws RootException { 412 GenericDelegator delegator = SharkContainer.getDelegator(); 413 GenericValue value = null; 414 try { 415 value = delegator.findByPrimaryKey("SharkUser", UtilMisc.toMap("userName", username)); 416 } catch (GenericEntityException e) { 417 Debug.logError(e, module); 418 throw new RootException(e); 419 } 420 return value; 421 } 422 423 private GenericValue getGroup(String groupName) throws RootException { 424 GenericDelegator delegator = SharkContainer.getDelegator(); 425 GenericValue value = null; 426 try { 427 value = delegator.findByPrimaryKey("SharkGroup", UtilMisc.toMap("groupName", groupName)); 428 } catch (GenericEntityException e) { 429 Debug.logError(e, module); 430 throw new RootException(e); 431 } 432 return value; 433 } 434 435 private GenericValue getGroupMember(String groupName, String username) throws RootException { 436 GenericDelegator delegator = SharkContainer.getDelegator(); 437 GenericValue member = null; 438 try { 439 member = delegator.findByPrimaryKey("SharkGroupMember", UtilMisc.toMap("groupName", groupName, "userName", username)); 440 } catch (GenericEntityException e) { 441 Debug.logError(e, module); 442 throw new RootException(e); 443 } 444 return member; 445 } 446 447 private GenericValue getGroupRollup(String parentGroup, String group) throws RootException { 448 GenericDelegator delegator = SharkContainer.getDelegator(); 449 GenericValue rollup = null; 450 try { 451 rollup = delegator.findByPrimaryKey("SharkGroupRollup", UtilMisc.toMap("parentGroupName", parentGroup, "groupName", group)); 452 } catch (GenericEntityException e) { 453 Debug.logError(e, module); 454 throw new RootException(e); 455 } 456 return rollup; 457 } 458 } 459 | Popular Tags |