001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.role;
018:
019: import java.sql.Timestamp;
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.ofbiz.base.util.Debug;
026: import org.ofbiz.entity.GenericDelegator;
027: import org.ofbiz.entity.GenericEntityException;
028: import org.ofbiz.entity.GenericValue;
029:
030: import com.sourcetap.sfa.replication.GenericReplicator;
031:
032: /**
033: * DOCUMENT ME!
034: *
035: */
036: public class RoleHelper {
037: public static final String module = RoleHelper.class.getName();
038:
039: /**
040: * DOCUMENT ME!
041: *
042: * @param entity
043: * @param entityId
044: * @param contactId
045: * @param roleId
046: * @param userName
047: * @param delegator
048: */
049: public static void storeRolesForEntity(String entity,
050: String entityId, String contactId, String roleId,
051: String userName, GenericDelegator delegator) {
052: try {
053: String rolePath = getRolePath(contactId, roleId, delegator);
054: HashMap accessMap = new HashMap();
055: List entityExists = null;
056: accessMap.put("entity", entity);
057: accessMap.put("entityId", entityId);
058: accessMap.put("partyId", rolePath);
059: accessMap.put("partyEntityType", "Role");
060: entityExists = delegator.findByAnd("EntityAccess",
061: accessMap, null);
062:
063: if ((entityExists == null) || (entityExists.size() <= 0)) {
064: GenericValue entityAccess = new GenericValue(delegator
065: .getModelEntity("EntityAccess"));
066: entityAccess.setDelegator(delegator);
067: entityAccess.set("entityAccessId", GenericReplicator
068: .getNextSeqId("EntityAccess", delegator));
069: entityAccess.set("entity", entity);
070: entityAccess.set("entityId", entityId);
071: entityAccess.set("partyId", rolePath);
072: entityAccess.set("partyEntityType", "Role");
073: entityAccess.set("modifiedBy", userName);
074: entityAccess.set("modifiedDate", new Timestamp(
075: new java.util.Date().getTime()));
076: entityAccess.set("createdBy", userName);
077: entityAccess.set("createdDate", new Timestamp(
078: new java.util.Date().getTime()));
079: delegator.create(entityAccess);
080: }
081: } catch (GenericEntityException gnee) {
082: gnee.printStackTrace();
083: Debug.logError(gnee, module);
084: }
085: }
086:
087: /**
088: * DOCUMENT ME!
089: *
090: * @param partyId
091: * @param roleId
092: * @param delegator
093: *
094: * @return
095: */
096: public static List getParentRolesForParty(String partyId,
097: String roleId, GenericDelegator delegator) {
098: ArrayList returnList = new ArrayList();
099:
100: try {
101: //find all roles
102: ArrayList orderBy = new ArrayList();
103: orderBy.add("roleParentId");
104:
105: List roles = delegator.findAllCache("Role", orderBy);
106: HashMap roleMap = new HashMap();
107:
108: if ((roles != null) && (roles.size() > 0)) {
109: GenericValue[] roleValues = (GenericValue[]) roles
110: .toArray(new GenericValue[0]);
111: GenericValue roleValue = null;
112:
113: for (int i = 0; i < roleValues.length; i++) {
114: roleValue = roleValues[i];
115: roleMap.put(roleValue.getString("roleId"),
116: roleValue.getString("roleParentId"));
117: }
118: }
119:
120: List entityExists = null;
121:
122: if ((roleId != null) || (roleId.trim().length() > 0)) {
123: //store every role above current users role in Entity_Access
124: String roleParentId = "";
125: boolean moreParents = true;
126: int i = 0;
127:
128: while (moreParents) {
129: if (i > 0) {
130: roleParentId = (String) roleMap.get(roleId);
131: } else {
132: roleParentId = roleId;
133: }
134:
135: if (roleParentId == null) {
136: moreParents = false;
137: } else {
138: returnList.add(roleParentId);
139: }
140:
141: roleId = roleParentId;
142: i++;
143: }
144: } else {
145: return new ArrayList();
146: }
147: } catch (GenericEntityException gnee) {
148: gnee.printStackTrace();
149: Debug.logError(gnee, module);
150: }
151:
152: return returnList;
153: }
154:
155: /**
156: * DOCUMENT ME!
157: *
158: * @param partyId
159: * @param roleId
160: * @param delegator
161: *
162: * @return
163: */
164: public static String getRolePath(String partyId, String roleId,
165: GenericDelegator delegator) {
166: ArrayList roleList = (ArrayList) getParentRolesForParty(
167: partyId, roleId, delegator);
168: StringBuffer returnString = new StringBuffer();
169:
170: for (int i = 0; i < roleList.size(); i++) {
171: returnString.append((String) roleList.get(i));
172:
173: if (i < (roleList.size() - 1)) {
174: returnString.append(", ");
175: }
176: }
177:
178: return returnString.toString();
179: }
180:
181: /**
182: * DOCUMENT ME!
183: *
184: * @param partyId
185: * @param roleId
186: * @param delegator
187: *
188: * @return
189: */
190: public static List getSubordinateRolesForParty(String partyId,
191: String roleId, GenericDelegator delegator) {
192:
193: ArrayList orderBy = new ArrayList();
194: orderBy.add("roleId");
195:
196: List roles = null;
197: ArrayList returnList = new ArrayList();
198:
199: try {
200: roles = delegator.findAllCache("Role", orderBy);
201:
202: HashMap roleMap = new HashMap();
203:
204: if ((roles != null) && (roles.size() > 0)) {
205: GenericValue[] roleValues = (GenericValue[]) roles
206: .toArray(new GenericValue[0]);
207: GenericValue roleValue = null;
208:
209: for (int i = 0; i < roleValues.length; i++) {
210: roleValue = roleValues[i];
211: roleMap.put(roleValue.getString("roleId"),
212: roleValue.getString("roleParentId"));
213: }
214: }
215:
216: ArrayList childNodes = new ArrayList();
217: returnList.add(roleId);
218:
219: Iterator iter = null;
220: String testRoleId = roleId;
221: boolean noMoreChildren = true;
222:
223: while (noMoreChildren) {
224: iter = roleMap.keySet().iterator();
225:
226: while (iter.hasNext()) {
227: String key = (String) iter.next();
228:
229: if ((key != null) && (roleMap.get(key) != null)
230: && roleMap.get(key).equals(testRoleId)) {
231: if (!childNodes.contains(key)) {
232: childNodes.add(key);
233: returnList.add(key);
234: }
235: }
236: }
237:
238: if (childNodes.size() == 0) {
239: noMoreChildren = false;
240: } else {
241: int remove = childNodes.size() - 1;
242: testRoleId = (String) childNodes.get(remove);
243: childNodes.remove(remove);
244: }
245: }
246: } catch (GenericEntityException gee) {
247: gee.printStackTrace();
248: Debug.logError(gee, module);
249: } catch (Exception gee) {
250: gee.printStackTrace();
251: Debug.logError(gee, module);
252: }
253:
254: return returnList;
255: }
256:
257: /**
258: * DOCUMENT ME!
259: *
260: * @param entity
261: * @param entityId
262: * @param contactId
263: * @param roleId
264: * @param userName
265: * @param delegator
266: */
267: public static void removeRolesForEntity(String entity,
268: String entityId, String contactId, String roleId,
269: String userName, GenericDelegator delegator) {
270: try {
271: String rolePath = getRolePath(contactId, roleId, delegator);
272: HashMap accessMap = new HashMap();
273: accessMap.put("entity", entity);
274: accessMap.put("entityId", entityId);
275: accessMap.put("partyId", rolePath);
276: accessMap.put("partyEntityType", "Role");
277: delegator.removeByAnd("EntityAccess", accessMap);
278: } catch (GenericEntityException gnee) {
279: gnee.printStackTrace();
280: Debug.logError(gnee, module);
281: }
282: }
283: }
|