1 16 17 18 41 package com.opensourcestrategies.crmsfa.accounts; 42 43 import java.util.Map ; 44 import java.util.List ; 45 import java.util.Locale ; 46 import java.sql.Timestamp ; 47 48 import javolution.util.FastMap; 49 50 import org.ofbiz.base.util.Debug; 51 import org.ofbiz.base.util.UtilMisc; 52 import org.ofbiz.base.util.UtilDateTime; 53 import org.ofbiz.entity.GenericDelegator; 54 import org.ofbiz.entity.GenericEntityException; 55 import org.ofbiz.entity.GenericValue; 56 import org.ofbiz.service.DispatchContext; 57 import org.ofbiz.service.GenericServiceException; 58 import org.ofbiz.service.LocalDispatcher; 59 import org.ofbiz.service.ServiceUtil; 60 import org.ofbiz.security.Security; 61 62 import com.opensourcestrategies.crmsfa.party.PartyHelper; 63 import com.opensourcestrategies.crmsfa.security.CrmsfaSecurity; 64 import com.opensourcestrategies.crmsfa.util.UtilCommon; 65 66 72 73 public class AccountsServices { 74 75 public static final String module = AccountsServices.class.getName(); 76 77 public static Map createAccount(DispatchContext dctx, Map context) { 78 GenericDelegator delegator = dctx.getDelegator(); 79 LocalDispatcher dispatcher = dctx.getDispatcher(); 80 Security security = dctx.getSecurity(); 81 GenericValue userLogin = (GenericValue) context.get("userLogin"); 82 Locale locale = (Locale ) context.get("locale"); 83 84 if (!security.hasPermission("CRMSFA_ACCOUNT_CREATE", userLogin)) { 85 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 86 } 87 88 String accountPartyId = null; 90 try { 91 Map input = UtilMisc.toMap("groupName", context.get("groupName"), "groupNameLocal", context.get("groupNameLocal"), 93 "officeSiteName", context.get("officeSiteName"), "description", context.get("description")); 94 Map serviceResults = dispatcher.runSync("createPartyGroup", input); 95 if (ServiceUtil.isError(serviceResults)) { 96 return serviceResults; 97 } 98 accountPartyId = (String ) serviceResults.get("partyId"); 99 100 serviceResults = dispatcher.runSync("createPartyRole", UtilMisc.toMap("partyId", accountPartyId, "roleTypeId", "ACCOUNT", "userLogin", userLogin)); 102 if (ServiceUtil.isError(serviceResults)) { 103 return serviceResults; 104 } 105 106 GenericValue partyData = delegator.makeValue("PartySupplementalData", UtilMisc.toMap("partyId", accountPartyId)); 108 partyData.setNonPKFields(context); 109 partyData.create(); 110 111 createResponsibleAccountRelationshipForParty(userLogin.getString("partyId"), accountPartyId, userLogin, delegator, dispatcher); 113 114 String initialTeamPartyId = (String ) context.get("initialTeamPartyId"); 116 if (initialTeamPartyId != null) { 117 serviceResults = dispatcher.runSync("crmsfa.assignTeamToAccount", UtilMisc.toMap("accountPartyId", accountPartyId, 118 "teamPartyId", initialTeamPartyId, "userLogin", userLogin)); 119 if (ServiceUtil.isError(serviceResults)) { 120 return serviceResults; 121 } 122 } 123 } catch (GenericServiceException e) { 124 return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateAccountFail", locale, module); 125 } catch (GenericEntityException e) { 126 return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateAccountFail", locale, module); 127 } 128 129 Map results = ServiceUtil.returnSuccess(); 131 results.put("partyId", accountPartyId); 132 return results; 133 } 134 135 public static Map updateAccount(DispatchContext dctx, Map context) { 136 GenericDelegator delegator = dctx.getDelegator(); 137 LocalDispatcher dispatcher = dctx.getDispatcher(); 138 Security security = dctx.getSecurity(); 139 GenericValue userLogin = (GenericValue) context.get("userLogin"); 140 Locale locale = (Locale ) context.get("locale"); 141 142 String accountPartyId = (String ) context.get("partyId"); 143 144 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_ACCOUNT", "_UPDATE", userLogin, accountPartyId)) { 146 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 147 } 148 try { 149 Map input = UtilMisc.toMap("groupName", context.get("groupName"), "groupNameLocal", context.get("groupNameLocal"), 151 "officeSiteName", context.get("officeSiteName"), "description", context.get("description")); 152 input.put("partyId", accountPartyId); 153 input.put("userLogin", userLogin); 154 Map serviceResults = dispatcher.runSync("updatePartyGroup", input); 155 if (ServiceUtil.isError(serviceResults)) { 156 return serviceResults; 157 } 158 159 GenericValue partyData = delegator.findByPrimaryKey("PartySupplementalData", UtilMisc.toMap("partyId", accountPartyId)); 161 if (partyData == null) { 162 partyData = delegator.makeValue("PartySupplementalData", UtilMisc.toMap("partyId", accountPartyId)); 164 partyData.create(); 165 } 166 partyData.setNonPKFields(context); 167 partyData.store(); 168 169 } catch (GenericServiceException e) { 170 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateAccountFail", locale, module); 171 } catch (GenericEntityException e) { 172 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateAccountFail", locale, module); 173 } 174 return ServiceUtil.returnSuccess(); 175 } 176 177 178 public static Map deactivateAccount(DispatchContext dctx, Map context) { 179 GenericDelegator delegator = dctx.getDelegator(); 180 LocalDispatcher dispatcher = dctx.getDispatcher(); 181 Security security = dctx.getSecurity(); 182 GenericValue userLogin = (GenericValue) context.get("userLogin"); 183 Locale locale = (Locale ) context.get("locale"); 184 185 String accountPartyId = (String ) context.get("partyId"); 187 188 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_ACCOUNT", "_DEACTIVATE", userLogin, accountPartyId)) { 190 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 191 } 192 193 Timestamp expireDate = (Timestamp ) context.get("expireDate"); 195 if (expireDate == null) { 196 expireDate = UtilDateTime.nowTimestamp(); 197 } 198 199 try { 201 List partyRelationships = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", accountPartyId, "roleTypeIdFrom", "ACCOUNT")); 202 PartyHelper.expirePartyRelationships(partyRelationships, expireDate, dispatcher, userLogin); 203 } catch (GenericEntityException e) { 204 return UtilCommon.createAndLogServiceError(e, "CrmErrorDeactivateAccountFail", locale, module); 205 } catch (GenericServiceException e) { 206 return UtilCommon.createAndLogServiceError(e, "CrmErrorDeactivateAccountFail", locale, module); 207 } 208 return ServiceUtil.returnSuccess(); 209 } 210 211 public static Map reassignAccountResponsibleParty(DispatchContext dctx, Map context) { 212 GenericDelegator delegator = dctx.getDelegator(); 213 LocalDispatcher dispatcher = dctx.getDispatcher(); 214 Security security = dctx.getSecurity(); 215 GenericValue userLogin = (GenericValue) context.get("userLogin"); 216 Locale locale = (Locale ) context.get("locale"); 217 218 String accountPartyId = (String ) context.get("accountPartyId"); 219 String newPartyId = (String ) context.get("newPartyId"); 220 221 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_ACCOUNT", "_REASSIGN", userLogin, accountPartyId)) { 223 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 224 } 225 try { 226 boolean result = createResponsibleAccountRelationshipForParty(newPartyId, accountPartyId, userLogin, delegator, dispatcher); 228 if (result == false) { 229 return UtilCommon.createAndLogServiceError("CrmErrorReassignFail", locale, module); 230 } 231 } catch (GenericServiceException e) { 232 return UtilCommon.createAndLogServiceError(e, "CrmErrorReassignFail", locale, module); 233 } catch (GenericEntityException e) { 234 return UtilCommon.createAndLogServiceError(e, "CrmErrorReassignFail", locale, module); 235 } 236 return ServiceUtil.returnSuccess(); 237 } 238 239 240 241 242 243 247 public static boolean createResponsibleAccountRelationshipForParty(String partyId, String accountPartyId, 248 GenericValue userLogin, GenericDelegator delegator, LocalDispatcher dispatcher) 249 throws GenericServiceException, GenericEntityException { 250 return PartyHelper.createNewPartyToRelationship(partyId, accountPartyId, "ACCOUNT", "RESPONSIBLE_FOR", 251 "ACCOUNT_OWNER", PartyHelper.TEAM_MEMBER_ROLES, true, userLogin, delegator, dispatcher); 252 } 253 254 } 255 | Popular Tags |