1 16 37 package com.opensourcestrategies.crmsfa.leads; 38 39 import java.util.Map ; 40 import java.util.List ; 41 import java.util.Locale ; 42 import java.util.Iterator ; 43 44 import org.ofbiz.base.util.Debug; 45 import org.ofbiz.base.util.UtilMisc; 46 import org.ofbiz.base.util.UtilDateTime; 47 import org.ofbiz.entity.GenericDelegator; 48 import org.ofbiz.entity.GenericEntityException; 49 import org.ofbiz.entity.GenericValue; 50 import org.ofbiz.entity.util.EntityUtil; 51 import org.ofbiz.service.DispatchContext; 52 import org.ofbiz.service.GenericServiceException; 53 import org.ofbiz.service.LocalDispatcher; 54 import org.ofbiz.service.ServiceUtil; 55 import org.ofbiz.service.ModelService; 56 import org.ofbiz.security.Security; 57 58 import com.opensourcestrategies.crmsfa.party.PartyHelper; 59 import com.opensourcestrategies.crmsfa.security.CrmsfaSecurity; 60 import com.opensourcestrategies.crmsfa.util.UtilCommon; 61 62 67 68 public class LeadsServices { 69 70 public static final String module = LeadsServices.class.getName(); 71 72 public static Map createLead(DispatchContext dctx, Map context) { 73 GenericDelegator delegator = dctx.getDelegator(); 74 LocalDispatcher dispatcher = dctx.getDispatcher(); 75 Security security = dctx.getSecurity(); 76 GenericValue userLogin = (GenericValue) context.get("userLogin"); 77 Locale locale = (Locale ) context.get("locale"); 78 79 if (!security.hasPermission("CRMSFA_LEAD_CREATE", userLogin)) { 80 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 81 } 82 83 String leadPartyId = null; 85 try { 86 87 String statusId = "PTYLEAD_ASSIGNED"; 90 91 Map input = UtilMisc.toMap("firstName", context.get("firstName"), "lastName", context.get("lastName")); 93 input.put("firstNameLocal", context.get("firstNameLocal")); 94 input.put("lastNameLocal", context.get("lastNameLocal")); 95 input.put("personalTitle", context.get("personalTitle")); 96 input.put("preferredCurrencyUomId", context.get("currencyUomId")); 97 input.put("description", context.get("description")); 98 input.put("birthDate", context.get("birthDate")); 99 input.put("statusId", statusId); Map serviceResults = dispatcher.runSync("createPerson", input); 101 if (ServiceUtil.isError(serviceResults)) { 102 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateLeadFail", locale, module); 103 } 104 leadPartyId = (String ) serviceResults.get("partyId"); 105 106 serviceResults = dispatcher.runSync("createPartyRole", UtilMisc.toMap("partyId", leadPartyId, "roleTypeId", "PROSPECT", "userLogin", userLogin)); 108 if (ServiceUtil.isError(serviceResults)) { 109 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateLeadFail", locale, module); 110 } 111 112 GenericValue partyData = delegator.makeValue("PartySupplementalData", UtilMisc.toMap("partyId", leadPartyId)); 114 partyData.setNonPKFields(context); 115 partyData.create(); 116 117 PartyHelper.createNewPartyToRelationship(userLogin.getString("partyId"), leadPartyId, "PROSPECT", "RESPONSIBLE_FOR", 119 "LEAD_OWNER", PartyHelper.TEAM_MEMBER_ROLES, true, userLogin, delegator, dispatcher); 120 121 String dataSourceId = (String ) context.get("dataSourceId"); 123 if (dataSourceId != null) { 124 serviceResults = dispatcher.runSync("crmsfa.addLeadDataSource", 125 UtilMisc.toMap("partyId", leadPartyId, "dataSourceId", dataSourceId, "userLogin", userLogin)); 126 if (ServiceUtil.isError(serviceResults)) { 127 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateLeadFail", locale, module); 128 } 129 } 130 131 } catch (GenericServiceException e) { 132 return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateLeadFail", locale, module); 133 } catch (GenericEntityException e) { 134 return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateLeadFail", locale, module); 135 } 136 137 Map results = ServiceUtil.returnSuccess(); 139 results.put("partyId", leadPartyId); 140 return results; 141 } 142 143 public static Map updateLead(DispatchContext dctx, Map context) { 144 GenericDelegator delegator = dctx.getDelegator(); 145 LocalDispatcher dispatcher = dctx.getDispatcher(); 146 Security security = dctx.getSecurity(); 147 GenericValue userLogin = (GenericValue) context.get("userLogin"); 148 Locale locale = (Locale ) context.get("locale"); 149 150 String leadPartyId = (String ) context.get("partyId"); 151 152 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_LEAD", "_UPDATE", userLogin, leadPartyId)) { 154 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 155 } 156 try { 157 GenericValue party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", leadPartyId)); 159 if (party == null) { 160 return UtilCommon.createAndLogServiceError("CrmErrorUpdateLeadFail", locale, module); 161 } 162 163 String statusId = (String ) context.get("statusId"); 165 if ((statusId != null) && (!statusId.equals(party.getString("statusId")))) { 166 Map serviceResults = dispatcher.runSync("setPartyStatus", UtilMisc.toMap("partyId", leadPartyId, "statusId", statusId, "userLogin", userLogin)); 167 if (ServiceUtil.isError(serviceResults)) { 168 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateLeadFail", locale, module); 169 } 170 } 171 172 Map input = UtilMisc.toMap("partyId", leadPartyId, "firstName", context.get("firstName"), "lastName", context.get("lastName")); 174 input.put("firstNameLocal", context.get("firstNameLocal")); 175 input.put("lastNameLocal", context.get("lastNameLocal")); 176 input.put("personalTitle", context.get("personalTitle")); 177 input.put("preferredCurrencyUomId", context.get("currencyUomId")); 178 input.put("description", context.get("description")); 179 input.put("birthDate", context.get("birthDate")); 180 input.put("userLogin", userLogin); 181 Map serviceResults = dispatcher.runSync("updatePerson", input); 182 if (ServiceUtil.isError(serviceResults)) { 183 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateLeadFail", locale, module); 184 } 185 186 GenericValue partyData = delegator.findByPrimaryKey("PartySupplementalData", UtilMisc.toMap("partyId", leadPartyId)); 188 if (partyData == null) { 189 partyData = delegator.makeValue("PartySupplementalData", UtilMisc.toMap("partyId", leadPartyId)); 191 partyData.create(); 192 } 193 partyData.setNonPKFields(context); 194 partyData.store(); 195 196 } catch (GenericServiceException e) { 197 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateLeadFail", locale, module); 198 } catch (GenericEntityException e) { 199 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateLeadFail", locale, module); 200 } 201 return ServiceUtil.returnSuccess(); 202 } 203 204 public static Map convertLead(DispatchContext dctx, Map context) { 205 GenericDelegator delegator = dctx.getDelegator(); 206 LocalDispatcher dispatcher = dctx.getDispatcher(); 207 Security security = dctx.getSecurity(); 208 GenericValue userLogin = (GenericValue) context.get("userLogin"); 209 Locale locale = (Locale ) context.get("locale"); 210 211 String leadPartyId = (String ) context.get("leadPartyId"); 212 String accountPartyId = (String ) context.get("accountPartyId"); 213 214 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_LEAD", "_UPDATE", userLogin, leadPartyId)) { 216 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 217 } 218 219 Map input = null; try { 221 GenericValue lead = delegator.findByPrimaryKey("PartySummaryCRMView", UtilMisc.toMap("partyId", leadPartyId)); 222 223 Map serviceResults = dispatcher.runSync("createPartyRole", UtilMisc.toMap("partyId", leadPartyId, "roleTypeId", "CONTACT", "userLogin", userLogin)); 225 if (ServiceUtil.isError(serviceResults)) { 226 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorConvertLeadFail", locale, module); 227 } 228 229 serviceResults = dispatcher.runSync("setPartyStatus", UtilMisc.toMap("partyId", leadPartyId, "statusId", "PTYLEAD_CONVERTED", "userLogin", userLogin)); 231 if (ServiceUtil.isError(serviceResults)) { 232 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorConvertLeadFail", locale, module); 233 } 234 235 if (accountPartyId == null) { 237 input = UtilMisc.toMap("groupName", lead.getString("companyName"), "description", lead.getString("description"), "userLogin", userLogin); 238 input.put("parentPartyId", lead.getString("parentPartyId")); 239 input.put("annualRevenue", lead.getDouble("annualRevenue")); 240 input.put("currencyUomId", lead.getString("currencyUomId")); 241 input.put("numberEmployees", lead.getLong("numberEmployees")); 242 input.put("industryEnumId", lead.getString("industryEnumId")); 243 input.put("ownershipEnumId", lead.getString("ownershipEnumId")); 244 input.put("sicCode", lead.getString("sicCode")); 245 input.put("tickerSymbol", lead.getString("tickerSymbol")); 246 serviceResults = dispatcher.runSync("crmsfa.createAccount", input); 247 if (ServiceUtil.isError(serviceResults)) { 248 return serviceResults; 249 } 250 accountPartyId = (String ) serviceResults.get("partyId"); 251 252 List dataSources = delegator.findByAnd("PartyDataSource", UtilMisc.toMap("partyId", leadPartyId)); 254 for (Iterator iter = dataSources.iterator(); iter.hasNext(); ) { 255 GenericValue dataSource = (GenericValue) iter.next(); 256 serviceResults = dispatcher.runSync("createPartyDataSource", UtilMisc.toMap("partyId", accountPartyId, 257 "dataSourceId", dataSource.getString("dataSourceId"), "userLogin", userLogin)); 258 if (ServiceUtil.isError(serviceResults)) { 259 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorConvertLeadFail", locale, module); 260 } 261 } 262 263 serviceResults = dispatcher.runSync("copyPartyContactMechs", UtilMisc.toMap("partyIdFrom", leadPartyId, "partyIdTo", accountPartyId, "userLogin", userLogin)); 265 if (ServiceUtil.isError(serviceResults)) { 266 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorConvertLeadFail", locale, module); 267 } 268 } 269 270 lead.set("parentPartyId", null); 272 lead.set("annualRevenue", null); 273 lead.set("currencyUomId", null); 274 lead.set("numberEmployees", null); 275 lead.set("industryEnumId", null); 276 lead.set("ownershipEnumId", null); 277 lead.set("sicCode", null); 278 lead.set("tickerSymbol", null); 279 lead.store(); 280 281 input = UtilMisc.toMap("contactPartyId", leadPartyId, "accountPartyId", accountPartyId, "userLogin", userLogin); 283 serviceResults = dispatcher.runSync("crmsfa.assignContactToAccount", input); 284 if (ServiceUtil.isError(serviceResults)) { 285 return serviceResults; 286 } 287 288 List partyRelationships = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", leadPartyId, "roleTypeIdFrom", "PROSPECT")); 290 PartyHelper.expirePartyRelationships(partyRelationships, UtilDateTime.nowTimestamp(), dispatcher, userLogin); 291 292 PartyHelper.createNewPartyToRelationship(userLogin.getString("partyId"), leadPartyId, "CONTACT", "RESPONSIBLE_FOR", "CONTACT_OWNER", 294 PartyHelper.TEAM_MEMBER_ROLES, true, userLogin, delegator, dispatcher); 295 296 List associations = EntityUtil.filterByDate(delegator.findByAnd("WorkEffortPartyAssignment", UtilMisc.toMap("partyId", leadPartyId))); 298 for (Iterator iter = associations.iterator(); iter.hasNext(); ) { 299 GenericValue wepa = (GenericValue) iter.next(); 300 ModelService service = dctx.getModelService("assignPartyToWorkEffort"); 301 input = service.makeValid(wepa, "IN"); 302 input.put("userLogin", userLogin); 303 304 wepa.set("thruDate", UtilDateTime.nowTimestamp()); 306 wepa.store(); 307 308 input.put("partyId", accountPartyId); 310 input.put("fromDate", null); 311 input.put("thruDate", null); 312 input.put("roleTypeId", "ACCOUNT"); 313 serviceResults = dispatcher.runSync("assignPartyToWorkEffort", input); 314 if (ServiceUtil.isError(serviceResults)) { 315 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorConvertLeadFail", locale, module); 316 } 317 318 input.put("partyId", leadPartyId); 320 input.put("fromDate", null); 321 input.put("thruDate", null); 322 input.put("roleTypeId", "CONTACT"); 323 serviceResults = dispatcher.runSync("assignPartyToWorkEffort", input); 324 if (ServiceUtil.isError(serviceResults)) { 325 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorConvertLeadFail", locale, module); 326 } 327 } 328 329 List oppRoles = delegator.findByAnd("SalesOpportunityRole", UtilMisc.toMap("partyId", leadPartyId, "roleTypeId", "PROSPECT")); 331 for (Iterator iter = oppRoles.iterator(); iter.hasNext(); ) { 332 GenericValue oppRole = (GenericValue) iter.next(); 333 334 input = UtilMisc.toMap("partyId", leadPartyId, "salesOpportunityId", oppRole.get("salesOpportunityId"), "roleTypeId", "CONTACT"); 336 GenericValue contactOppRole = delegator.makeValue("SalesOpportunityRole", input); 337 contactOppRole.create(); 338 339 input = UtilMisc.toMap("partyId", accountPartyId, "salesOpportunityId", oppRole.get("salesOpportunityId"), "roleTypeId", "ACCOUNT"); 341 GenericValue accountOppRole = delegator.makeValue("SalesOpportunityRole", input); 342 accountOppRole.create(); 343 344 oppRole.remove(); 346 } 347 348 serviceResults = dispatcher.runSync("setPartyStatus", UtilMisc.toMap("partyId", leadPartyId, "statusId", "PTYLEAD_CONVERTED", "userLogin", userLogin)); 350 if (ServiceUtil.isError(serviceResults)) { 351 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorConvertLeadFail", locale, module); 352 } 353 } catch (GenericServiceException e) { 354 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateLeadFail", locale, module); 355 } catch (GenericEntityException e) { 356 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateLeadFail", locale, module); 357 } 358 Map results = ServiceUtil.returnSuccess(); 360 results.put("partyId", leadPartyId); 361 return results; 362 } 363 364 public static Map reassignLeadResponsibleParty(DispatchContext dctx, Map context) { 365 GenericDelegator delegator = dctx.getDelegator(); 366 LocalDispatcher dispatcher = dctx.getDispatcher(); 367 Security security = dctx.getSecurity(); 368 GenericValue userLogin = (GenericValue) context.get("userLogin"); 369 Locale locale = (Locale ) context.get("locale"); 370 371 String leadPartyId = (String ) context.get("leadPartyId"); 372 String newPartyId = (String ) context.get("newPartyId"); 373 374 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_LEAD", "_REASSIGN", userLogin, leadPartyId)) { 376 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 377 } 378 try { 379 boolean result = PartyHelper.createNewPartyToRelationship(newPartyId, leadPartyId, "PROSPECT", "RESPONSIBLE_FOR", 381 "LEAD_OWNER", PartyHelper.TEAM_MEMBER_ROLES, true, userLogin, delegator, dispatcher); 382 if (result == false) { 383 return UtilCommon.createAndLogServiceError("CrmErrorReassignFail", locale, module); 384 } 385 } catch (GenericServiceException e) { 386 return UtilCommon.createAndLogServiceError(e, "CrmErrorReassignFail", locale, module); 387 } catch (GenericEntityException e) { 388 return UtilCommon.createAndLogServiceError(e, "CrmErrorReassignFail", locale, module); 389 } 390 return ServiceUtil.returnSuccess(); 391 } 392 393 399 public static Map deleteLead(DispatchContext dctx, Map context) { 400 GenericDelegator delegator = dctx.getDelegator(); 401 Security security = dctx.getSecurity(); 402 GenericValue userLogin = (GenericValue) context.get("userLogin"); 403 Locale locale = (Locale ) context.get("locale"); 404 405 String leadPartyId = (String ) context.get("leadPartyId"); 406 407 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_LEAD", "_DELETE", userLogin, leadPartyId)) { 409 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 410 } 411 412 try { 413 GenericValue lead = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", leadPartyId)); 415 if (lead == null) { 416 return UtilCommon.createAndLogServiceError("Lead [" + leadPartyId + "] not found.", 417 "CrmErrorDeleteLeadFail", locale, module); 418 } 419 String statusId = lead.getString("statusId"); 420 if (statusId == null || !(statusId.equals("PTYLEAD_NEW") || statusId.equals("PTYLEAD_ASSIGNED") || statusId.equals("PTYLEAD_QUALIFIED"))) { 421 return UtilCommon.createAndLogServiceError("Lead [" + leadPartyId + "] cannot be deleted. Only new, assigned or qualified leads may be deleted.", 422 "CrmErrorDeleteLeadFail", locale, module); 423 } 424 425 PartyHelper.deleteCrmParty(leadPartyId, delegator); 427 428 } catch (GenericEntityException e) { 429 return UtilCommon.createAndLogServiceError(e, "CrmErrorDeleteLeadFail", locale, module); 430 } 431 return ServiceUtil.returnSuccess(); 432 } 433 } 434 | Popular Tags |