/specialpurpose/shark/src/org/ofbiz/shark/user/GenericUserGroupMgr.java

https://github.com/thanhvc/ofbiz · Java · 449 lines · 384 code · 44 blank · 21 comment · 43 complexity · ca943128a41bdab65b09a7481ae91c47 MD5 · raw file

  1. /*******************************************************************************
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. *******************************************************************************/
  19. package org.ofbiz.shark.user;
  20. import java.util.List;
  21. import java.util.ArrayList;
  22. import java.util.Iterator;
  23. import org.ofbiz.entity.GenericValue;
  24. import org.ofbiz.entity.Delegator;
  25. import org.ofbiz.entity.GenericEntityException;
  26. import org.ofbiz.shark.container.SharkContainer;
  27. import org.ofbiz.base.util.UtilMisc;
  28. import org.ofbiz.base.util.Debug;
  29. import org.ofbiz.base.util.UtilValidate;
  30. import org.enhydra.shark.api.internal.usergroup.UserGroupManager;
  31. import org.enhydra.shark.api.internal.working.CallbackUtilities;
  32. import org.enhydra.shark.api.RootException;
  33. import org.enhydra.shark.api.UserTransaction;
  34. /**
  35. * Shark User/Group Manager
  36. */
  37. public class GenericUserGroupMgr implements UserGroupManager {
  38. public static final String module = GenericUserGroupMgr.class.getName();
  39. protected CallbackUtilities callBack = null;
  40. public void configure(CallbackUtilities callbackUtilities) throws RootException {
  41. this.callBack = callbackUtilities;
  42. }
  43. public List getAllGroupnames(UserTransaction trans) throws RootException {
  44. Delegator delegator = SharkContainer.getDelegator();
  45. List groupNames = new ArrayList();
  46. List groups = null;
  47. try {
  48. groups = delegator.findList(org.ofbiz.shark.SharkConstants.SharkGroup, null, null, null, null, false);
  49. } catch (GenericEntityException e) {
  50. Debug.logError(e, module);
  51. throw new RootException(e);
  52. }
  53. if (UtilValidate.isNotEmpty(groups)) {
  54. Iterator i = groups.iterator();
  55. while (i.hasNext()) {
  56. GenericValue v = (GenericValue) i.next();
  57. groupNames.add(v.getString(org.ofbiz.shark.SharkConstants.groupName));
  58. }
  59. }
  60. return groupNames;
  61. }
  62. public List getAllUsers(UserTransaction trans) throws RootException {
  63. Delegator delegator = SharkContainer.getDelegator();
  64. List userNames = new ArrayList();
  65. List users = null;
  66. try {
  67. users = delegator.findList(org.ofbiz.shark.SharkConstants.SharkUser, null, null, null, null, false);
  68. } catch (GenericEntityException e) {
  69. Debug.logError(e, module);
  70. throw new RootException(e);
  71. }
  72. if (UtilValidate.isNotEmpty(users)) {
  73. Iterator i = users.iterator();
  74. while (i.hasNext()) {
  75. GenericValue v = (GenericValue) i.next();
  76. userNames.add(v.getString(org.ofbiz.shark.SharkConstants.userName));
  77. }
  78. }
  79. return userNames;
  80. }
  81. public List getAllUsers(UserTransaction trans, String groupName) throws RootException {
  82. Delegator delegator = SharkContainer.getDelegator();
  83. List userNames = new ArrayList();
  84. List members = null;
  85. try {
  86. members = delegator.findByAnd(org.ofbiz.shark.SharkConstants.SharkGroupMember, UtilMisc.toMap(org.ofbiz.shark.SharkConstants.groupName, groupName));
  87. } catch (GenericEntityException e) {
  88. Debug.logError(e, module);
  89. throw new RootException(e);
  90. }
  91. if (UtilValidate.isNotEmpty(members)) {
  92. Iterator i = members.iterator();
  93. while (i.hasNext()) {
  94. GenericValue v = (GenericValue) i.next();
  95. userNames.add(v.getString(org.ofbiz.shark.SharkConstants.userName));
  96. }
  97. }
  98. return userNames;
  99. }
  100. public List getAllUsers(UserTransaction trans, List groupNames) throws RootException {
  101. List userNames = new ArrayList();
  102. if (UtilValidate.isNotEmpty(groupNames)) {
  103. Iterator i = groupNames.iterator();
  104. while (i.hasNext()) {
  105. String groupName = (String) i.next();
  106. userNames.addAll(getAllUsers(trans, groupName));
  107. }
  108. }
  109. return userNames;
  110. }
  111. public List getAllImmediateUsers(UserTransaction trans, String groupName) throws RootException {
  112. return this.getAllUsers(trans, groupName);
  113. }
  114. public List getAllSubgroups(UserTransaction trans, String groupName) throws RootException {
  115. Delegator delegator = SharkContainer.getDelegator();
  116. List subGroups = new ArrayList();
  117. List rollups = null;
  118. try {
  119. rollups = delegator.findByAnd(org.ofbiz.shark.SharkConstants.SharkGroupRollup, UtilMisc.toMap(org.ofbiz.shark.SharkConstants.groupName, groupName));
  120. } catch (GenericEntityException e) {
  121. Debug.logError(e, module);
  122. throw new RootException(e);
  123. }
  124. if (UtilValidate.isNotEmpty(rollups)) {
  125. Iterator i = rollups.iterator();
  126. while (i.hasNext()) {
  127. GenericValue v = (GenericValue) i.next();
  128. subGroups.add(v.getString("subGroupName"));
  129. }
  130. }
  131. return subGroups;
  132. }
  133. public List getAllSubgroups(UserTransaction trans, List groupNames) throws RootException {
  134. List subGroups = new ArrayList();
  135. if (UtilValidate.isNotEmpty(groupNames)) {
  136. Iterator i = groupNames.iterator();
  137. while (i.hasNext()) {
  138. String groupName = (String) i.next();
  139. subGroups.addAll(getAllSubgroups(trans, groupName));
  140. }
  141. }
  142. return subGroups;
  143. }
  144. public List getAllImmediateSubgroups(UserTransaction trans, String groupName) throws RootException {
  145. return this.getAllSubgroups(trans, groupName);
  146. }
  147. public void createGroup(UserTransaction trans, String groupName, String description) throws RootException {
  148. Delegator delegator = SharkContainer.getDelegator();
  149. GenericValue group = delegator.makeValue(org.ofbiz.shark.SharkConstants.SharkGroup);
  150. group.set(org.ofbiz.shark.SharkConstants.groupName, groupName);
  151. group.set(org.ofbiz.shark.SharkConstants.description, description);
  152. try {
  153. delegator.create(group);
  154. } catch (GenericEntityException e) {
  155. Debug.logError(e, module);
  156. throw new RootException(e);
  157. }
  158. }
  159. public void removeGroup(UserTransaction trans, String groupName) throws RootException {
  160. GenericValue group = getGroup(groupName);
  161. if (group != null) {
  162. try {
  163. group.remove();
  164. } catch (GenericEntityException e) {
  165. Debug.logError(e, module);
  166. throw new RootException(e);
  167. }
  168. }
  169. }
  170. public boolean doesGroupExist(UserTransaction trans, String groupName) throws RootException {
  171. GenericValue group = getGroup(groupName);
  172. if (group != null) {
  173. return true;
  174. }
  175. return false;
  176. }
  177. public boolean doesGroupBelongToGroup(UserTransaction trans, String groupName, String subGroupName) throws RootException {
  178. GenericValue rollup = getGroupRollup(groupName, subGroupName);
  179. if (rollup != null) {
  180. return true;
  181. }
  182. return false;
  183. }
  184. public void updateGroup(UserTransaction trans, String groupName, String description) throws RootException {
  185. GenericValue group = getGroup(groupName);
  186. if (group != null) {
  187. group.set(org.ofbiz.shark.SharkConstants.description, description);
  188. try {
  189. group.store();
  190. } catch (GenericEntityException e) {
  191. Debug.logError(e, module);
  192. throw new RootException(e);
  193. }
  194. }
  195. }
  196. public void addGroupToGroup(UserTransaction trans, String parentGroupName, String groupName) throws RootException {
  197. Delegator delegator = SharkContainer.getDelegator();
  198. GenericValue rollup = delegator.makeValue(org.ofbiz.shark.SharkConstants.SharkGroupRollup);
  199. rollup.set(org.ofbiz.shark.SharkConstants.parentGroupName, parentGroupName);
  200. rollup.set(org.ofbiz.shark.SharkConstants.groupName, groupName);
  201. try {
  202. delegator.create(rollup);
  203. } catch (GenericEntityException e) {
  204. Debug.logError(e, module);
  205. throw new RootException(e);
  206. }
  207. }
  208. public void removeGroupFromGroup(UserTransaction trans, String parentGroup, String group) throws RootException {
  209. GenericValue rollup = getGroupRollup(parentGroup, group);
  210. if (rollup != null) {
  211. try {
  212. rollup.remove();
  213. } catch (GenericEntityException e) {
  214. Debug.logError(e, module);
  215. throw new RootException(e);
  216. }
  217. }
  218. }
  219. public void removeGroupTree(UserTransaction trans, String s) throws RootException {
  220. Debug.logInfo("Call : removeGroupTree(UserTransaction trans, String s)", module);
  221. }
  222. public void removeUsersFromGroupTree(UserTransaction trans, String s) throws RootException {
  223. Debug.logInfo("Call : void removeUsersFromGroupTree(UserTransaction trans, String s)", module);
  224. }
  225. public void moveGroup(UserTransaction trans, String currentParentGroup, String newParentGroup, String groupName) throws RootException {
  226. this.removeGroupFromGroup(trans, currentParentGroup, groupName);
  227. this.addGroupToGroup(trans, newParentGroup, groupName);
  228. }
  229. public String getGroupDescription(UserTransaction trans, String groupName) throws RootException {
  230. GenericValue group = getGroup(groupName);
  231. if (group != null) {
  232. return group.getString(org.ofbiz.shark.SharkConstants.description);
  233. }
  234. return null;
  235. }
  236. public void addUserToGroup(UserTransaction trans, String groupName, String username) throws RootException {
  237. Delegator delegator = SharkContainer.getDelegator();
  238. GenericValue member = delegator.makeValue(org.ofbiz.shark.SharkConstants.SharkGroupMember);
  239. member.set(org.ofbiz.shark.SharkConstants.groupName, groupName);
  240. member.set(org.ofbiz.shark.SharkConstants.userName, username);
  241. try {
  242. delegator.create(member);
  243. } catch (GenericEntityException e) {
  244. Debug.logError(e, module);
  245. throw new RootException(e);
  246. }
  247. }
  248. public void removeUserFromGroup(UserTransaction trans, String groupName, String username) throws RootException {
  249. GenericValue member = getGroupMember(groupName, username);
  250. if (member != null) {
  251. try {
  252. member.remove();
  253. } catch (GenericEntityException e) {
  254. Debug.logError(e, module);
  255. throw new RootException(e);
  256. }
  257. }
  258. }
  259. public void moveUser(UserTransaction trans, String currentGroup, String newGroup, String username) throws RootException {
  260. this.removeUserFromGroup(trans, currentGroup, username);
  261. this.addUserToGroup(trans, newGroup, username);
  262. }
  263. public boolean doesUserBelongToGroup(UserTransaction trans, String groupName, String username) throws RootException {
  264. GenericValue member = getGroupMember(groupName, username);
  265. if (member != null) {
  266. return true;
  267. }
  268. return false;
  269. }
  270. public void createUser(UserTransaction trans, String groupName, String username, String password, String firstName, String lastName, String email) throws RootException {
  271. Delegator delegator = SharkContainer.getDelegator();
  272. GenericValue user = delegator.makeValue(org.ofbiz.shark.SharkConstants.SharkUser);
  273. user.set(org.ofbiz.shark.SharkConstants.userName, username);
  274. user.set(org.ofbiz.shark.SharkConstants.firstName, firstName);
  275. user.set(org.ofbiz.shark.SharkConstants.lastName, lastName);
  276. user.set(org.ofbiz.shark.SharkConstants.passwd, password);
  277. user.set(org.ofbiz.shark.SharkConstants.emailAddress, email);
  278. try {
  279. delegator.create(user);
  280. } catch (GenericEntityException e) {
  281. Debug.logError(e, module);
  282. throw new RootException(e);
  283. }
  284. if (groupName != null) {
  285. this.addUserToGroup(trans, groupName, username);
  286. }
  287. }
  288. public void updateUser(UserTransaction trans, String username, String firstName, String lastName, String email) throws RootException {
  289. GenericValue user = getUser(username);
  290. if (user != null) {
  291. user.set(org.ofbiz.shark.SharkConstants.firstName, firstName);
  292. user.set(org.ofbiz.shark.SharkConstants.lastName, firstName);
  293. user.set(org.ofbiz.shark.SharkConstants.emailAddress, email);
  294. try {
  295. user.store();
  296. } catch (GenericEntityException e) {
  297. Debug.logError(e, module);
  298. throw new RootException(e);
  299. }
  300. }
  301. }
  302. public void removeUser(UserTransaction trans, String username) throws RootException {
  303. GenericValue user = getUser(username);
  304. if (user != null) {
  305. try {
  306. user.remove();
  307. } catch (GenericEntityException e) {
  308. Debug.logError(e, module);
  309. throw new RootException(e);
  310. }
  311. }
  312. }
  313. public boolean doesUserExist(UserTransaction trans, String username) throws RootException {
  314. GenericValue user = getUser(username);
  315. if (user == null) {
  316. return false;
  317. }
  318. return true;
  319. }
  320. public void setPassword(UserTransaction trans, String username, String password) throws RootException {
  321. GenericValue user = getUser(username);
  322. if (user != null) {
  323. user.set(org.ofbiz.shark.SharkConstants.passwd, password);
  324. try {
  325. user.store();
  326. } catch (GenericEntityException e) {
  327. Debug.logError(e, module);
  328. throw new RootException(e);
  329. }
  330. }
  331. }
  332. public String getUserRealName(UserTransaction trans, String username) throws RootException {
  333. StringBuilder buf = new StringBuilder();
  334. GenericValue user = getUser(username);
  335. if (!UtilValidate.isEmpty(user.getString(org.ofbiz.shark.SharkConstants.firstName))) {
  336. buf.append(user.getString(org.ofbiz.shark.SharkConstants.firstName));
  337. }
  338. if (!UtilValidate.isEmpty(user.getString(org.ofbiz.shark.SharkConstants.lastName))) {
  339. if (buf.length() > 0) {
  340. buf.append(" ");
  341. }
  342. buf.append(user.getString(org.ofbiz.shark.SharkConstants.lastName));
  343. }
  344. return buf.toString();
  345. }
  346. public String getUserFirstName(UserTransaction trans, String username) throws RootException {
  347. GenericValue user = getUser(username);
  348. return user.getString(org.ofbiz.shark.SharkConstants.firstName);
  349. }
  350. public String getUserLastName(UserTransaction trans, String username) throws RootException {
  351. GenericValue user = getUser(username);
  352. return user.getString(org.ofbiz.shark.SharkConstants.lastName);
  353. }
  354. public String getUserEMailAddress(UserTransaction trans, String username) throws RootException {
  355. GenericValue user = getUser(username);
  356. if (user != null) {
  357. return user.getString(org.ofbiz.shark.SharkConstants.emailAddress);
  358. }
  359. return null;
  360. }
  361. private GenericValue getUser(String username) throws RootException {
  362. Delegator delegator = SharkContainer.getDelegator();
  363. GenericValue value = null;
  364. try {
  365. value = delegator.findByPrimaryKey(org.ofbiz.shark.SharkConstants.SharkUser, UtilMisc.toMap(org.ofbiz.shark.SharkConstants.userName, username));
  366. } catch (GenericEntityException e) {
  367. Debug.logError(e, module);
  368. throw new RootException(e);
  369. }
  370. return value;
  371. }
  372. private GenericValue getGroup(String groupName) throws RootException {
  373. Delegator delegator = SharkContainer.getDelegator();
  374. GenericValue value = null;
  375. try {
  376. value = delegator.findByPrimaryKey(org.ofbiz.shark.SharkConstants.SharkGroup, UtilMisc.toMap(org.ofbiz.shark.SharkConstants.groupName, groupName));
  377. } catch (GenericEntityException e) {
  378. Debug.logError(e, module);
  379. throw new RootException(e);
  380. }
  381. return value;
  382. }
  383. private GenericValue getGroupMember(String groupName, String username) throws RootException {
  384. Delegator delegator = SharkContainer.getDelegator();
  385. GenericValue member = null;
  386. try {
  387. member = delegator.findByPrimaryKey(org.ofbiz.shark.SharkConstants.SharkGroupMember, UtilMisc.toMap(org.ofbiz.shark.SharkConstants.groupName, groupName, org.ofbiz.shark.SharkConstants.userName, username));
  388. } catch (GenericEntityException e) {
  389. Debug.logError(e, module);
  390. throw new RootException(e);
  391. }
  392. return member;
  393. }
  394. private GenericValue getGroupRollup(String parentGroup, String group) throws RootException {
  395. Delegator delegator = SharkContainer.getDelegator();
  396. GenericValue rollup = null;
  397. try {
  398. rollup = delegator.findByPrimaryKey(org.ofbiz.shark.SharkConstants.SharkGroupRollup, UtilMisc.toMap(org.ofbiz.shark.SharkConstants.parentGroupName, parentGroup, org.ofbiz.shark.SharkConstants.groupName, group));
  399. } catch (GenericEntityException e) {
  400. Debug.logError(e, module);
  401. throw new RootException(e);
  402. }
  403. return rollup;
  404. }
  405. }