1 16 17 18 39 package com.opensourcestrategies.crmsfa.contacts; 40 41 import java.util.Map ; 42 import java.util.List ; 43 import java.util.Locale ; 44 import java.sql.Date ; 45 import java.sql.Timestamp ; 46 47 import javolution.util.FastMap; 48 49 import org.ofbiz.base.util.Debug; 50 import org.ofbiz.base.util.UtilMisc; 51 import org.ofbiz.base.util.UtilProperties; 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.entity.condition.EntityConditionList; 57 import org.ofbiz.entity.condition.EntityExpr; 58 import org.ofbiz.entity.condition.EntityOperator; 59 import org.ofbiz.entity.util.EntityUtil; 60 import org.ofbiz.service.DispatchContext; 61 import org.ofbiz.service.GenericServiceException; 62 import org.ofbiz.service.LocalDispatcher; 63 import org.ofbiz.service.ServiceUtil; 64 import org.ofbiz.security.Security; 65 66 import com.opensourcestrategies.crmsfa.party.PartyHelper; 67 import com.opensourcestrategies.crmsfa.security.CrmsfaSecurity; 68 import com.opensourcestrategies.crmsfa.util.UtilCommon; 69 70 75 76 public class ContactsServices { 77 78 public static final String module = ContactsServices.class.getName(); 79 80 public static Map createContact(DispatchContext dctx, Map context) { 81 GenericDelegator delegator = dctx.getDelegator(); 82 LocalDispatcher dispatcher = dctx.getDispatcher(); 83 Security security = dctx.getSecurity(); 84 GenericValue userLogin = (GenericValue) context.get("userLogin"); 85 Locale locale = (Locale ) context.get("locale"); 86 87 if (!security.hasPermission("CRMSFA_CONTACT_CREATE", userLogin)) { 88 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 89 } 90 91 String contactPartyId = null; 93 try { 94 Map input = UtilMisc.toMap("firstName", context.get("firstName"), "lastName", context.get("lastName")); 96 input.put("firstNameLocal", context.get("firstNameLocal")); 97 input.put("lastNameLocal", context.get("lastNameLocal")); 98 input.put("personalTitle", context.get("personalTitle")); 99 input.put("preferredCurrencyUomId", context.get("preferredCurrencyUomId")); 100 input.put("description", context.get("description")); 101 input.put("birthDate", context.get("birthDate")); 102 Map serviceResults = dispatcher.runSync("createPerson", input); 103 if (ServiceUtil.isError(serviceResults)) { 104 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateContactFail", locale, module); 105 } 106 contactPartyId = (String ) serviceResults.get("partyId"); 107 108 serviceResults = dispatcher.runSync("createPartyRole", UtilMisc.toMap("partyId", contactPartyId, "roleTypeId", "CONTACT", "userLogin", userLogin)); 110 if (ServiceUtil.isError(serviceResults)) { 111 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateContactFail", locale, module); 112 } 113 114 GenericValue partyData = delegator.makeValue("PartySupplementalData", UtilMisc.toMap("partyId", contactPartyId)); 116 partyData.setNonPKFields(context); 117 partyData.create(); 118 119 createResponsibleContactRelationshipForParty(userLogin.getString("partyId"), contactPartyId, userLogin, delegator, dispatcher); 121 122 } catch (GenericServiceException e) { 123 return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateContactFail", locale, module); 124 } catch (GenericEntityException e) { 125 return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateContactFail", locale, module); 126 } 127 128 Map results = ServiceUtil.returnSuccess(); 130 results.put("partyId", contactPartyId); 131 results.put("contactPartyId", contactPartyId); 132 return results; 133 } 134 135 public static Map updateContact(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 contactPartyId = (String ) context.get("partyId"); 143 144 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_CONTACT", "_UPDATE", userLogin, contactPartyId)) { 146 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 147 } 148 try { 149 Map input = UtilMisc.toMap("partyId", contactPartyId, "firstName", context.get("firstName"), "lastName", context.get("lastName")); 151 input.put("firstNameLocal", context.get("firstNameLocal")); 152 input.put("lastNameLocal", context.get("lastNameLocal")); 153 input.put("personalTitle", context.get("personalTitle")); 154 input.put("preferredCurrencyUomId", context.get("preferredCurrencyUomId")); 155 input.put("description", context.get("description")); 156 input.put("birthDate", context.get("birthDate")); 157 input.put("userLogin", userLogin); 158 Map serviceResults = dispatcher.runSync("updatePerson", input); 159 if (ServiceUtil.isError(serviceResults)) { 160 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateContactFail", locale, module); 161 } 162 163 GenericValue partyData = delegator.findByPrimaryKey("PartySupplementalData", UtilMisc.toMap("partyId", contactPartyId)); 165 if (partyData == null) { 166 partyData = delegator.makeValue("PartySupplementalData", UtilMisc.toMap("partyId", contactPartyId)); 168 partyData.create(); 169 } 170 partyData.setNonPKFields(context); 171 partyData.store(); 172 173 } catch (GenericServiceException e) { 174 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateContactFail", locale, module); 175 } catch (GenericEntityException e) { 176 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateContactFail", locale, module); 177 } 178 return ServiceUtil.returnSuccess(); 179 } 180 181 public static Map assignContactToAccount(DispatchContext dctx, Map context) { 182 GenericDelegator delegator = dctx.getDelegator(); 183 LocalDispatcher dispatcher = dctx.getDispatcher(); 184 Security security = dctx.getSecurity(); 185 GenericValue userLogin = (GenericValue) context.get("userLogin"); 186 Locale locale = (Locale ) context.get("locale"); 187 188 String contactPartyId = (String ) context.get("contactPartyId"); 189 String accountPartyId = (String ) context.get("accountPartyId"); 190 191 try { 192 EntityConditionList searchConditions = new EntityConditionList(UtilMisc.toList( 194 new EntityExpr("partyIdFrom", EntityOperator.EQUALS, contactPartyId), 195 new EntityExpr("partyIdTo", EntityOperator.EQUALS, accountPartyId), 196 new EntityExpr("roleTypeIdFrom", EntityOperator.EQUALS, "CONTACT"), 197 new EntityExpr("roleTypeIdTo", EntityOperator.EQUALS, "ACCOUNT"), 198 new EntityExpr("partyRelationshipTypeId", EntityOperator.EQUALS, "CONTACT_REL_INV"), 199 EntityUtil.getFilterByDateExpr()), 200 EntityOperator.AND); 201 List existingRelationships = delegator.findByCondition("PartyRelationship", searchConditions, null, null); 202 if (existingRelationships.size() > 0) { 203 return UtilCommon.createAndLogServiceError("CrmErrorContactAlreadyAssociatedToAccount", locale, module); 204 } 205 206 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_ACCOUNT", "_UPDATE", userLogin, accountPartyId)) { 208 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 209 } 210 PartyHelper.createNewPartyToRelationship(accountPartyId, contactPartyId, "CONTACT", "CONTACT_REL_INV", 212 null, UtilMisc.toList("ACCOUNT"), false, userLogin, delegator, dispatcher); 213 214 } 215 catch (GenericServiceException e) { 216 return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignContactToAccountFail", locale, module); 217 } catch (GenericEntityException e) { 218 return UtilCommon.createAndLogServiceError(e, "CrmErrorAssignContactToAccountFail", locale, module); 219 } 220 return ServiceUtil.returnSuccess(); 221 } 222 223 public static Map reassignContactResponsibleParty(DispatchContext dctx, Map context) { 224 GenericDelegator delegator = dctx.getDelegator(); 225 LocalDispatcher dispatcher = dctx.getDispatcher(); 226 Security security = dctx.getSecurity(); 227 GenericValue userLogin = (GenericValue) context.get("userLogin"); 228 Locale locale = (Locale ) context.get("locale"); 229 230 String contactPartyId = (String ) context.get("contactPartyId"); 231 String newPartyId = (String ) context.get("newPartyId"); 232 233 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_CONTACT", "_REASSIGN", userLogin, contactPartyId)) { 235 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 236 } 237 try { 238 boolean result = createResponsibleContactRelationshipForParty(newPartyId, contactPartyId, userLogin, delegator, dispatcher); 240 if (result == false) { 241 return UtilCommon.createAndLogServiceError("CrmErrorReassignFail", locale, module); 242 } 243 } catch (GenericServiceException e) { 244 return UtilCommon.createAndLogServiceError(e, "CrmErrorReassignFail", locale, module); 245 } catch (GenericEntityException e) { 246 return UtilCommon.createAndLogServiceError(e, "CrmErrorReassignFail", locale, module); 247 } 248 return ServiceUtil.returnSuccess(); 249 } 250 251 public static Map removeContactFromAccount(DispatchContext dctx, Map context) { 252 GenericDelegator delegator = dctx.getDelegator(); 253 LocalDispatcher dispatcher = dctx.getDispatcher(); 254 Security security = dctx.getSecurity(); 255 GenericValue userLogin = (GenericValue) context.get("userLogin"); 256 Locale locale = (Locale ) context.get("locale"); 257 258 String contactPartyId = (String ) context.get("contactPartyId"); 259 String accountPartyId = (String ) context.get("accountPartyId"); 260 261 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_ACCOUNT", "_UPDATE", userLogin, accountPartyId)) { 263 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 264 } 265 try { 266 List relations = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdTo", accountPartyId, 268 "partyIdFrom", contactPartyId, "partyRelationshipTypeId", "CONTACT_REL_INV")); 269 PartyHelper.expirePartyRelationships(relations, UtilDateTime.nowTimestamp(), dispatcher, userLogin); 270 } catch (GenericServiceException e) { 271 return UtilCommon.createAndLogServiceError(e, "CrmErrorRemoveContactFail", locale, module); 272 } catch (GenericEntityException e) { 273 return UtilCommon.createAndLogServiceError(e, "CrmErrorRemoveContactFail", locale, module); 274 } 275 return ServiceUtil.returnSuccess(); 276 } 277 278 public static Map deactivateContact(DispatchContext dctx, Map context) { 279 GenericDelegator delegator = dctx.getDelegator(); 280 LocalDispatcher dispatcher = dctx.getDispatcher(); 281 Security security = dctx.getSecurity(); 282 GenericValue userLogin = (GenericValue) context.get("userLogin"); 283 Locale locale = (Locale ) context.get("locale"); 284 285 String contactPartyId = (String ) context.get("partyId"); 287 288 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_CONTACT", "_DEACTIVATE", userLogin, contactPartyId)) { 290 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 291 } 292 293 Timestamp expireDate = (Timestamp ) context.get("expireDate"); 295 if (expireDate == null) { 296 expireDate = UtilDateTime.nowTimestamp(); 297 } 298 299 try { 301 List partyRelationships = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", contactPartyId, "roleTypeIdFrom", "CONTACT")); 302 PartyHelper.expirePartyRelationships(partyRelationships, expireDate, dispatcher, userLogin); 303 } catch (GenericEntityException e) { 304 return UtilCommon.createAndLogServiceError(e, "CrmErrorDeactivateContactFail", locale, module); 305 } catch (GenericServiceException e) { 306 return UtilCommon.createAndLogServiceError(e, "CrmErrorDeactivateContactFail", locale, module); 307 } 308 return ServiceUtil.returnSuccess(); 309 } 310 311 312 313 314 315 316 320 public static boolean createResponsibleContactRelationshipForParty(String partyId, String contactPartyId, 321 GenericValue userLogin, GenericDelegator delegator, LocalDispatcher dispatcher) 322 throws GenericServiceException, GenericEntityException { 323 return PartyHelper.createNewPartyToRelationship(partyId, contactPartyId, "CONTACT", "RESPONSIBLE_FOR", 324 "CONTACT_OWNER", PartyHelper.TEAM_MEMBER_ROLES, true, userLogin, delegator, dispatcher); 325 } 326 } 327 | Popular Tags |