1 16 17 18 39 40 package com.opensourcestrategies.crmsfa.teams; 41 42 import java.util.Map ; 43 import java.util.Locale ; 44 import java.util.List ; 45 import java.util.Iterator ; 46 47 import org.ofbiz.base.util.Debug; 48 import org.ofbiz.base.util.UtilMisc; 49 import org.ofbiz.base.util.UtilDateTime; 50 import org.ofbiz.entity.GenericDelegator; 51 import org.ofbiz.entity.GenericEntityException; 52 import org.ofbiz.entity.GenericValue; 53 import org.ofbiz.entity.util.EntityUtil; 54 import org.ofbiz.service.DispatchContext; 55 import org.ofbiz.service.GenericServiceException; 56 import org.ofbiz.service.LocalDispatcher; 57 import org.ofbiz.service.ServiceUtil; 58 import org.ofbiz.security.Security; 59 60 import com.opensourcestrategies.crmsfa.security.CrmsfaSecurity; 61 import com.opensourcestrategies.crmsfa.party.PartyHelper; 62 import com.opensourcestrategies.crmsfa.util.UtilCommon; 63 64 70 71 public class TeamServices { 72 73 public static final String module = TeamServices.class.getName(); 74 75 public static Map assignTeamToAccount(DispatchContext dctx, Map context) { 77 GenericDelegator delegator = dctx.getDelegator(); 78 LocalDispatcher dispatcher = dctx.getDispatcher(); 79 Security security = dctx.getSecurity(); 80 Locale locale = (Locale ) context.get("locale"); 81 GenericValue userLogin = (GenericValue) context.get("userLogin"); 82 83 String accountPartyId = (String ) context.get("accountPartyId"); 84 String teamPartyId = (String ) context.get("teamPartyId"); 85 86 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_TEAM", "_ASSIGN", userLogin, accountPartyId)) { 88 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 89 } 90 try { 91 PartyHelper.copyToPartyRelationships(teamPartyId, "ACCOUNT_TEAM", accountPartyId, "ACCOUNT", userLogin, delegator, dispatcher); 93 } catch (GenericServiceException e) { 94 return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module); 95 } catch (GenericEntityException e) { 96 return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module); 97 } 98 return ServiceUtil.returnSuccess(); 99 } 100 101 public static Map addTeamMember(DispatchContext dctx, Map context) { 102 GenericDelegator delegator = dctx.getDelegator(); 103 LocalDispatcher dispatcher = dctx.getDispatcher(); 104 Security security = dctx.getSecurity(); 105 Locale locale = (Locale ) context.get("locale"); 106 GenericValue userLogin = (GenericValue) context.get("userLogin"); 107 108 String teamMemberPartyId = (String ) context.get("teamMemberPartyId"); 109 String accountTeamPartyId = (String ) context.get("accountTeamPartyId"); 110 String securityGroupId = (String ) context.get("securityGroupId"); 111 112 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_TEAM", "_ASSIGN", userLogin, accountTeamPartyId)) { 114 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 115 } 116 try { 117 String roleTypeIdFrom = PartyHelper.getFirstValidRoleTypeId(accountTeamPartyId, UtilMisc.toList("ACCOUNT", "ACCOUNT_TEAM"), delegator); 119 120 String roleTypeIdTo = PartyHelper.getFirstValidRoleTypeId(teamMemberPartyId, UtilMisc.toList("ACCOUNT_MANAGER", "ACCOUNT_REP", "CUS_SERVICE_REP"), delegator); 122 123 List relationships = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", accountTeamPartyId, "partyRelationshipTypeId", "ASSIGNED_TO", 125 "roleTypeIdFrom", roleTypeIdFrom, "partyIdTo", teamMemberPartyId, "roleTypeIdFrom", roleTypeIdFrom)); 126 List activeRelations = EntityUtil.filterByDate(relationships, UtilDateTime.nowTimestamp()); if (activeRelations.size() > 0) { 128 return UtilCommon.createAndLogServiceError("CrmErrorAlreadyMember", locale, module); 129 } 130 131 Map input = UtilMisc.toMap("partyIdFrom", accountTeamPartyId,"roleTypeIdFrom", roleTypeIdFrom , "partyIdTo", teamMemberPartyId, "roleTypeIdTo", roleTypeIdTo); 133 input.put("partyRelationshipTypeId", "ASSIGNED_TO"); 134 input.put("securityGroupId", securityGroupId); 135 input.put("fromDate", UtilDateTime.nowTimestamp()); 136 input.put("userLogin", userLogin); 137 Map serviceResults = dispatcher.runSync("createPartyRelationship", input); 138 if (ServiceUtil.isError(serviceResults)) { 139 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorAssignFail", locale, module); 140 } 141 } catch (GenericServiceException e) { 142 return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module); 143 } catch (GenericEntityException e) { 144 return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module); 145 } 146 return ServiceUtil.returnSuccess(); 147 } 148 149 public static Map removeTeamMember(DispatchContext dctx, Map context) { 150 GenericDelegator delegator = dctx.getDelegator(); 151 LocalDispatcher dispatcher = dctx.getDispatcher(); 152 Security security = dctx.getSecurity(); 153 Locale locale = (Locale ) context.get("locale"); 154 GenericValue userLogin = (GenericValue) context.get("userLogin"); 155 156 String teamMemberPartyId = (String ) context.get("teamMemberPartyId"); 157 String accountTeamPartyId = (String ) context.get("accountTeamPartyId"); 158 159 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_TEAM", "_REMOVE", userLogin, accountTeamPartyId)) { 161 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 162 } 163 try { 164 String roleTypeIdFrom = PartyHelper.getFirstValidRoleTypeId(accountTeamPartyId, UtilMisc.toList("ACCOUNT", "ACCOUNT_TEAM"), delegator); 166 167 List relationships = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", accountTeamPartyId, 169 "roleTypeIdFrom", roleTypeIdFrom, "partyIdTo", teamMemberPartyId)); 170 171 PartyHelper.expirePartyRelationships(relationships, UtilDateTime.nowTimestamp(), dispatcher, userLogin); 173 174 } catch (GenericServiceException e) { 175 return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module); 176 } catch (GenericEntityException e) { 177 return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module); 178 } 179 return ServiceUtil.returnSuccess(); 180 } 181 182 public static Map setTeamMemberSecurityGroup(DispatchContext dctx, Map context) { 183 GenericDelegator delegator = dctx.getDelegator(); 184 LocalDispatcher dispatcher = dctx.getDispatcher(); 185 Security security = dctx.getSecurity(); 186 Locale locale = (Locale ) context.get("locale"); 187 GenericValue userLogin = (GenericValue) context.get("userLogin"); 188 189 String teamMemberPartyId = (String ) context.get("teamMemberPartyId"); 190 String accountTeamPartyId = (String ) context.get("accountTeamPartyId"); 191 String securityGroupId = (String ) context.get("securityGroupId"); 192 193 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_TEAM", "_UPDATE", userLogin, accountTeamPartyId)) { 195 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 196 } 197 try { 198 List relationships = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", accountTeamPartyId, 200 "partyRelationshipTypeId", "ASSIGNED_TO", "partyIdTo", teamMemberPartyId)); 201 202 List activeRelations = EntityUtil.filterByDate(relationships, UtilDateTime.nowTimestamp()); 204 205 PartyHelper.expirePartyRelationships(activeRelations, UtilDateTime.nowTimestamp(), dispatcher, userLogin); 207 208 for (Iterator iter = activeRelations.iterator(); iter.hasNext(); ) { 210 GenericValue relationship = (GenericValue) iter.next(); 211 212 Map input = UtilMisc.toMap("partyIdFrom", accountTeamPartyId, "roleTypeIdFrom", relationship.getString("roleTypeIdFrom"), "partyIdTo", teamMemberPartyId, "roleTypeIdTo", relationship.getString("roleTypeIdTo")); 213 input.put("partyRelationshipTypeId", "ASSIGNED_TO"); 214 input.put("securityGroupId", securityGroupId); 215 input.put("fromDate", UtilDateTime.nowTimestamp()); 216 input.put("userLogin", userLogin); 217 Map serviceResults = dispatcher.runSync("createPartyRelationship", input); 218 if (ServiceUtil.isError(serviceResults)) { 219 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorAssignFail", locale, module); 220 } 221 } 222 } catch (GenericServiceException e) { 223 return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module); 224 } catch (GenericEntityException e) { 225 return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignFail", locale, module); 226 } 227 return ServiceUtil.returnSuccess(); 228 } 229 } 230 | Popular Tags |