1 16 17 18 41 package com.opensourcestrategies.crmsfa.activities; 42 43 import java.util.HashMap ; 44 import java.util.Map ; 45 import java.util.List ; 46 import java.util.ArrayList ; 47 import java.util.Locale ; 48 import java.util.Iterator ; 49 import java.io.ObjectInputStream.GetField; 50 import java.sql.Timestamp ; 51 52 import org.ofbiz.base.util.Debug; 53 import org.ofbiz.base.util.UtilMisc; 54 import org.ofbiz.base.util.UtilDateTime; 55 import org.ofbiz.entity.GenericDelegator; 56 import org.ofbiz.entity.GenericEntityException; 57 import org.ofbiz.entity.GenericValue; 58 import org.ofbiz.entity.condition.EntityConditionList; 59 import org.ofbiz.entity.condition.EntityExpr; 60 import org.ofbiz.entity.condition.EntityOperator; 61 import org.ofbiz.entity.util.EntityUtil; 62 import org.ofbiz.service.DispatchContext; 63 import org.ofbiz.service.GenericServiceException; 64 import org.ofbiz.service.LocalDispatcher; 65 import org.ofbiz.service.ServiceUtil; 66 import org.ofbiz.service.ModelService; 67 import org.ofbiz.service.mail.MimeMessageWrapper; 68 import org.ofbiz.security.Security; 69 70 import com.opensourcestrategies.crmsfa.party.PartyHelper; 71 import com.opensourcestrategies.crmsfa.security.CrmsfaSecurity; 72 import com.opensourcestrategies.crmsfa.util.UtilCommon; 73 import com.opensourcestrategies.crmsfa.opportunities.UtilOpportunity; 74 import com.opensourcestrategies.crmsfa.cases.UtilCase; 75 import com.opensourcestrategies.crmsfa.activities.UtilActivity; 76 77 83 84 public class ActivitiesServices { 85 86 public static final String module = ActivitiesServices.class.getName(); 87 88 public static Map sendEmail(DispatchContext dctx, Map context) { 89 return sendOrSaveEmailHelper(dctx, context, true, "CrmErrorSendEmailFail"); 90 } 91 92 public static Map saveEmail(DispatchContext dctx, Map context) { 93 return sendOrSaveEmailHelper(dctx, context, false, "CrmErrorSaveEmailFail"); 94 } 95 96 103 private static Map sendOrSaveEmailHelper(DispatchContext dctx, Map context, boolean sending, String errorLabel) { 104 GenericDelegator delegator = dctx.getDelegator(); 105 LocalDispatcher dispatcher = dctx.getDispatcher(); 106 Security security = dctx.getSecurity(); 107 GenericValue userLogin = (GenericValue) context.get("userLogin"); 108 Locale locale = (Locale ) context.get("locale"); 109 110 String toEmail = (String ) context.get("toEmail"); 112 String internalPartyId = (String ) context.get("internalPartyId"); 113 114 String communicationEventId = (String ) context.get("communicationEventId"); 116 String workEffortId = (String ) context.get("workEffortId"); 117 boolean existing = ((communicationEventId == null) || communicationEventId.equals("") ? false : true); 118 119 try { 120 Map serviceResults = validateWorkEffortAssociations(dctx, context); 122 if (ServiceUtil.isError(serviceResults)) { 123 return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); 124 } 125 126 List candidates = EntityUtil.filterByDate(delegator.findByAnd("PartyAndContactMech", UtilMisc.toMap("infoString", toEmail, "partyId", internalPartyId))); 128 if (candidates.size() == 0) { 129 return UtilCommon.createAndLogServiceError("Could not find email [" + toEmail + "] for party with ID [" + internalPartyId + "].", errorLabel, locale, module); 130 } 131 String contactMechIdTo = ((GenericValue) candidates.get(0)).getString("contactMechId"); 133 134 List toContactMechs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMech", 136 UtilMisc.toMap("contactMechId", contactMechIdTo), UtilMisc.toList("fromDate DESC"))); 137 String partyIdTo = ((GenericValue) toContactMechs.get(0)).getString("partyId"); 138 String roleTypeIdTo = PartyHelper.getFirstValidRoleTypeId(partyIdTo, PartyHelper.CLIENT_PARTY_ROLES, delegator); 139 140 147 148 String serviceName = (existing ? "updateCommunicationEvent" : "createCommunicationEvent"); 150 ModelService service = dctx.getModelService(serviceName); 151 Map input = service.makeValid(context, "IN"); 152 if (existing) input.put("communicationEventId", communicationEventId); 153 if (!existing) input.put("entryDate", UtilDateTime.nowTimestamp()); 154 input.put("contactMechIdTo", contactMechIdTo); 155 input.put("contactMechTypeId", "EMAIL_ADDRESS"); 156 input.put("communicationEventTypeId", "EMAIL_COMMUNICATION"); 157 input.put("statusId", "COM_PENDING"); 158 input.put("partyIdTo", partyIdTo); 159 input.put("roleTypeIdTo", roleTypeIdTo); 160 input.put("partyIdFrom", userLogin.getString("partyId")); 161 input.put("roleTypeIdFrom", PartyHelper.getFirstValidRoleTypeId(userLogin.getString("partyId"), PartyHelper.TEAM_MEMBER_ROLES, delegator)); 162 serviceResults = dispatcher.runSync(serviceName, input); 163 if (ServiceUtil.isError(serviceResults)) { 164 return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); 165 } 166 167 if (!existing) communicationEventId = (String ) serviceResults.get("communicationEventId"); 169 170 if (sending) { 171 input = UtilMisc.toMap("communicationEventId", communicationEventId, "statusId", "COM_IN_PROGRESS", "userLogin", userLogin); 173 input.put("communicationEventTypeId", "EMAIL_COMMUNICATION"); input.put("datetimeEnded", UtilDateTime.nowTimestamp()); 175 serviceResults = dispatcher.runSync("updateCommunicationEvent", input); 176 if (ServiceUtil.isError(serviceResults)) { 177 return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); 178 } 179 180 input = UtilMisc.toMap("workEffortTypeId", "TASK", "currentStatusId", "TASK_COMPLETED", "userLogin", userLogin); 182 if (existing) input.put("workEffortId", workEffortId); 183 input.put("actualStartDate", context.get("datetimeStarted")); 184 input.put("actualCompletionDate", UtilDateTime.nowTimestamp()); 185 input.put("workEffortName", context.get("subject")); 186 input.put("workEffortPurposeTypeId", "WEPT_TASK_EMAIL"); 187 serviceName = (existing ? "updateWorkEffort" : "createWorkEffort"); 188 serviceResults = dispatcher.runSync(serviceName, input); 189 if (ServiceUtil.isError(serviceResults)) { 190 return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); 191 } 192 } else { 193 input = UtilMisc.toMap("workEffortTypeId", "TASK", "currentStatusId", "TASK_STARTED", "userLogin", userLogin); 195 if (existing) input.put("workEffortId", workEffortId); 196 input.put("actualStartDate", context.get("datetimeStarted")); 197 input.put("workEffortName", context.get("subject")); 198 input.put("workEffortPurposeTypeId", "WEPT_TASK_EMAIL"); 199 serviceResults = dispatcher.runSync(existing ? "updateWorkEffort" : "createWorkEffort", input); 200 if (ServiceUtil.isError(serviceResults)) { 201 return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); 202 } 203 } 204 205 if (!existing) workEffortId = (String ) serviceResults.get("workEffortId"); 207 208 input = UtilMisc.toMap("userLogin", userLogin, "communicationEventId", communicationEventId, "workEffortId", workEffortId); 210 serviceResults = dispatcher.runSync("createCommunicationEventWorkEff", input); 211 if (ServiceUtil.isError(serviceResults)) { 212 return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); 213 } 214 215 if (existing) { 217 UtilActivity.removeAllAssociationsForWorkEffort(workEffortId, delegator); 218 } 219 220 return createWorkEffortPartyAssociations(dctx, context, workEffortId, errorLabel, !existing); 222 223 } catch (GenericEntityException e) { 224 return UtilCommon.createAndLogServiceError(e, errorLabel, locale, module); 225 } catch (GenericServiceException e) { 226 return UtilCommon.createAndLogServiceError(e, errorLabel, locale, module); 227 } 228 } 229 230 234 private static void associateCommunicationEventWorkEffortAndParty(String partyIdTo, GenericValue communicationEvent, GenericDelegator delegator, LocalDispatcher dispatcher, GenericValue userLogin, Locale locale, DispatchContext dctx, Map context) 235 throws GenericEntityException, GenericServiceException { 236 if(partyIdTo != null) { 239 String roleTypeId = PartyHelper.getFirstValidTeamMemberRoleTypeId(partyIdTo, delegator); 240 241 if (roleTypeId != null) { 242 Map serviceResults = null; 243 Map input = null; 244 String communicationEventId = communicationEvent.getString("communicationEventId"); 245 246 input = UtilMisc.toMap("workEffortTypeId", "TASK", "currentStatusId", "TASK_SCHEDULED", "userLogin", userLogin); 249 input.put("actualStartDate", communicationEvent.getTimestamp("datetimeStarted")); 250 input.put("actualCompletionDate", communicationEvent.getTimestamp("datetimeEnded")); 251 input.put("workEffortName", communicationEvent.getString("subject")); 252 input.put("workEffortPurposeTypeId", "WEPT_TASK_EMAIL"); 253 serviceResults = dispatcher.runSync("createWorkEffort", input); 254 if (ServiceUtil.isError(serviceResults)) { 255 Debug.logError(ServiceUtil.getErrorMessage(serviceResults), module); 256 throw new GenericServiceException(ServiceUtil.getErrorMessage(serviceResults)); 257 } 258 259 String workEffortId = (String ) serviceResults.get("workEffortId"); 261 262 input = UtilMisc.toMap("userLogin", userLogin, "communicationEventId", communicationEventId, "workEffortId", workEffortId); 264 serviceResults = dispatcher.runSync("createCommunicationEventWorkEff", input); 265 if (ServiceUtil.isError(serviceResults)) { 266 Debug.logError(ServiceUtil.getErrorMessage(serviceResults), module); 267 throw new GenericServiceException(ServiceUtil.getErrorMessage(serviceResults)); 268 } 269 270 context.put("internalPartyId", communicationEvent.getString("partyIdFrom")); 272 createWorkEffortPartyAssociations(dctx, context, workEffortId, "CrmErrorProcessIncomingEmailFail", false); 273 input = UtilMisc.toMap("partyId", partyIdTo, "workEffortId", workEffortId, "roleTypeId", roleTypeId, "statusId", "PRTYASGN_ASSIGNED", "userLogin", userLogin); 274 serviceResults = dispatcher.runSync("assignPartyToWorkEffort", input); 275 if (ServiceUtil.isError(serviceResults)) { 276 Debug.logError(ServiceUtil.getErrorMessage(serviceResults), module); 277 throw new GenericServiceException(ServiceUtil.getErrorMessage(serviceResults)); 278 } 279 } else { 280 Debug.logWarning("No valid team member roles found for partyId [" + partyIdTo + "], so no activities will be created for it", module); 281 } 282 } 283 } 284 285 288 public static Map processIncomingEmail(DispatchContext dctx, Map context) { 289 GenericDelegator delegator = dctx.getDelegator(); 290 LocalDispatcher dispatcher = dctx.getDispatcher(); 291 GenericValue userLogin = (GenericValue) context.get("userLogin"); 292 Locale locale = (Locale ) context.get("locale"); 293 MimeMessageWrapper wrapper = (MimeMessageWrapper) context.get("messageWrapper"); 294 Map serviceResults = null; 295 Map input = null; 296 297 try { 298 if (wrapper == null) { 299 Debug.logError("Null message wrapper when trying to store email: " + wrapper.getMessage(), module); 300 return ServiceUtil.returnError("Null message wrapper"); 301 } 302 if (userLogin == null) { 303 Debug.logWarning("Null userLogin in when trying to process incoming email, using system", module); 304 userLogin = delegator.findByPrimaryKeyCache("UserLogin", UtilMisc.toMap("userLoginId", "system")); 305 } 306 307 input = UtilMisc.toMap("messageWrapper", wrapper, "userLogin", userLogin); 309 serviceResults = dispatcher.runSync("storeIncomingEmail", input); 310 if (ServiceUtil.isError(serviceResults)) { 311 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorProcessIncomingEmailFail", locale, module); 312 } 313 314 String communicationEventId = (String ) serviceResults.get("communicationEventId"); 316 317 GenericValue communicationEvent = delegator.findByPrimaryKey("CommunicationEvent", UtilMisc.toMap("communicationEventId", communicationEventId)); 319 320 String partyIdTo = communicationEvent.getString("partyIdTo"); 321 322 associateCommunicationEventWorkEffortAndParty(partyIdTo, communicationEvent, delegator, dispatcher, userLogin, locale, dctx, context); 324 325 List commEventRoles = delegator.findByAnd("CommunicationEventRole", UtilMisc.toMap("communicationEventId", communicationEventId)); 327 Iterator itr = commEventRoles.iterator(); 328 329 while (itr.hasNext()) { 330 GenericValue commEventRole = (GenericValue) itr.next(); 331 332 associateCommunicationEventWorkEffortAndParty(commEventRole.getString("partyId"), communicationEvent, delegator, dispatcher, userLogin, locale, dctx, context); 333 } 334 } catch (GenericEntityException e) { 335 return UtilCommon.createAndLogServiceError(e, "CrmErrorProcessIncomingEmailFail", locale, module); 336 } catch (GenericServiceException e) { 337 return UtilCommon.createAndLogServiceError(e, "CrmErrorProcessIncomingEmailFail", locale, module); 338 } 339 340 return ServiceUtil.returnSuccess(); 341 } 342 343 344 345 346 347 public static Map createActivity(DispatchContext dctx, Map context) { 348 GenericDelegator delegator = dctx.getDelegator(); 349 LocalDispatcher dispatcher = dctx.getDispatcher(); 350 Security security = dctx.getSecurity(); 351 GenericValue userLogin = (GenericValue) context.get("userLogin"); 352 Locale locale = (Locale ) context.get("locale"); 353 354 try { 355 Timestamp estimatedStartDate = (Timestamp ) context.get("estimatedStartDate"); 357 Timestamp estimatedCompletionDate = UtilCommon.getEndTimestamp(estimatedStartDate, (String ) context.get("duration"), locale); 358 359 String forceIfConflicts = (String ) context.get("forceIfConflicts"); 361 if (forceIfConflicts == null || forceIfConflicts.equals("N")) { 362 List events = UtilActivity.getActivityConflicts(userLogin, estimatedStartDate, estimatedCompletionDate); 363 if (events.size() > 0) { 364 StringBuffer msg = new StringBuffer ("You have one or more conflicting events during the specified time. "); 365 msg.append("<a class=\"messageLink\" HREF=\"/crmsfa/control/myHome?calendarView=day&start="); 366 msg.append(UtilDateTime.getDayStart(estimatedStartDate).getTime()); 367 msg.append("\">Click here to view them.</a>"); 368 return UtilCommon.createAndLogServiceError(msg.toString(), "CrmErrorCreateActivityFail", locale, module); 369 } 370 } 371 372 Map serviceResults = validateWorkEffortAssociations(dctx, context); 374 if (ServiceUtil.isError(serviceResults)) { 375 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateActivityFail", locale, module); 376 } 377 378 ModelService service = dctx.getModelService("createWorkEffort"); 380 Map input = service.makeValid(context, "IN"); 381 input.put("estimatedCompletionDate", estimatedCompletionDate); 382 serviceResults = dispatcher.runSync("createWorkEffort", input); 383 if (ServiceUtil.isError(serviceResults)) { 384 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateActivityFail", locale, module); 385 } 386 String workEffortId = (String ) serviceResults.get("workEffortId"); 387 388 serviceResults = createWorkEffortPartyAssociations(dctx, context, workEffortId, "CrmErrorCreateActivityFail", true); 390 if (ServiceUtil.isError(serviceResults)) { 391 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorCreateActivityFail", locale, module); 392 } 393 394 Map results = ServiceUtil.returnSuccess(); 395 results.put("workEffortId", workEffortId); 396 return results; 397 } catch (IllegalArgumentException e) { 398 return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateActivityFail", locale, module); 399 } catch (GenericEntityException e) { 400 return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateActivityFail", locale, module); 401 } catch (GenericServiceException e) { 402 return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateActivityFail", locale, module); 403 } 404 } 405 406 public static Map updateActivity(DispatchContext dctx, Map context) { 407 GenericDelegator delegator = dctx.getDelegator(); 408 LocalDispatcher dispatcher = dctx.getDispatcher(); 409 Security security = dctx.getSecurity(); 410 GenericValue userLogin = (GenericValue) context.get("userLogin"); 411 Locale locale = (Locale ) context.get("locale"); 412 413 String workEffortId = (String ) context.get("workEffortId"); 414 try { 415 if (!CrmsfaSecurity.hasActivityPermission(security, "_UPDATE", userLogin, workEffortId)) { 417 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 418 } 419 420 Timestamp estimatedStartDate = (Timestamp ) context.get("estimatedStartDate"); 422 Timestamp estimatedCompletionDate = UtilCommon.getEndTimestamp(estimatedStartDate, (String ) context.get("duration"), locale); 423 424 String forceIfConflicts = (String ) context.get("forceIfConflicts"); 426 if (forceIfConflicts == null || forceIfConflicts.equals("N")) { 427 List events = UtilActivity.getActivityConflicts(userLogin, estimatedStartDate, estimatedCompletionDate, workEffortId); 428 if (events.size() > 0) { 429 StringBuffer msg = new StringBuffer ("You have one or more conflicting events during the specified time. "); 430 msg.append("<a class=\"messageLink\" HREF=\"/crmsfa/control/myHome?calendarView=day&start="); 431 msg.append(UtilDateTime.getDayStart(estimatedStartDate).getTime()); 432 msg.append("\">Click here to view them.</a>"); 433 return UtilCommon.createAndLogServiceError(msg.toString(), "CrmErrorCreateActivityFail", locale, module); 434 } 435 } 436 437 Map serviceResults = validateWorkEffortAssociations(dctx, context); 439 if (ServiceUtil.isError(serviceResults)) { 440 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateActivityFail", locale, module); 441 } 442 443 ModelService service = dctx.getModelService("updateWorkEffort"); 445 Map input = service.makeValid(context, "IN"); 446 input.put("estimatedCompletionDate", estimatedCompletionDate); 447 serviceResults = dispatcher.runSync("updateWorkEffort", input); 448 if (ServiceUtil.isError(serviceResults)) { 449 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateActivityFail", locale, module); 450 } 451 452 UtilActivity.removeAllAssociationsForWorkEffort(workEffortId, delegator); 454 455 return createWorkEffortPartyAssociations(dctx, context, workEffortId, "CrmErrorUpdateActivityFail", false); 457 458 } catch (IllegalArgumentException e) { 459 return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateActivityFail", locale, module); 460 } catch (GenericEntityException e) { 461 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateActivityFail", locale, module); 462 } catch (GenericServiceException e) { 463 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateActivityFail", locale, module); 464 } 465 } 466 467 public static Map updateActivityWithoutAssoc(DispatchContext dctx, Map context) { 468 GenericDelegator delegator = dctx.getDelegator(); 469 LocalDispatcher dispatcher = dctx.getDispatcher(); 470 Security security = dctx.getSecurity(); 471 GenericValue userLogin = (GenericValue) context.get("userLogin"); 472 Locale locale = (Locale ) context.get("locale"); 473 474 String workEffortId = (String ) context.get("workEffortId"); 475 try { 476 if (!CrmsfaSecurity.hasActivityPermission(security, "_UPDATE", userLogin, workEffortId)) { 478 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 479 } 480 481 ModelService service = dctx.getModelService("updateWorkEffort"); 483 Map input = service.makeValid(context, "IN"); 484 Map serviceResults = dispatcher.runSync("updateWorkEffort", input); 485 if (ServiceUtil.isError(serviceResults)) { 486 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateActivityFail", locale, module); 487 } 488 489 return ServiceUtil.returnSuccess(); 490 } catch (GenericServiceException e) { 491 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateActivityFail", locale, module); 492 } 493 } 494 495 500 public static Map updateActivityCommEvent(DispatchContext dctx, Map context) { 501 GenericDelegator delegator = dctx.getDelegator(); 502 LocalDispatcher dispatcher = dctx.getDispatcher(); 503 Security security = dctx.getSecurity(); 504 GenericValue userLogin = (GenericValue) context.get("userLogin"); 505 Locale locale = (Locale ) context.get("locale"); 506 507 String workEffortId = (String ) context.get("workEffortId"); 508 Map input = null; 509 Map serviceResults = null; 510 try { 511 GenericValue workEffort = delegator.findByPrimaryKey("WorkEffort", UtilMisc.toMap("workEffortId", workEffortId)); 513 514 String workEffortStatus = workEffort.getString("currentStatusId"); 515 516 List commEvents = null; 517 521 if("TASK_STARTED".equals(workEffortStatus)) { 522 commEvents = delegator.findByAnd("WorkEffortCommunicationEventView", UtilMisc.toMap("workEffortId", workEffortId, "statusId", "COM_ENTERED")); 523 524 if (commEvents.size() == 0) { 525 return ServiceUtil.returnSuccess(); 526 } 527 for (Iterator iter = commEvents.iterator(); iter.hasNext(); ) { 528 GenericValue commEvent = (GenericValue) iter.next(); 529 530 input = UtilMisc.toMap("communicationEventId", commEvent.getString("communicationEventId"), "statusId", "COM_PENDING", "userLogin", userLogin); 531 serviceResults = dispatcher.runSync("updateCommunicationEvent", input); 532 if (ServiceUtil.isError(serviceResults)) { 533 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateActivityCommEventFail", locale, module); 534 } 535 } 536 } else if("TASK_COMPLETED".equals(workEffortStatus)) { 537 commEvents = delegator.findByAnd("WorkEffortCommunicationEventView", UtilMisc.toMap("workEffortId", workEffortId, "statusId", "COM_PENDING")); 538 539 if (commEvents.size() == 0) { 540 return ServiceUtil.returnSuccess(); 541 } 542 for (Iterator iter = commEvents.iterator(); iter.hasNext(); ) { 543 GenericValue commEvent = (GenericValue) iter.next(); 544 545 input = UtilMisc.toMap("communicationEventId", commEvent.getString("communicationEventId"), "statusId", "COM_COMPLETE", "userLogin", userLogin); 546 serviceResults = dispatcher.runSync("updateCommunicationEvent", input); 547 if (ServiceUtil.isError(serviceResults)) { 548 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateActivityCommEventFail", locale, module); 549 } 550 } 551 } 552 553 return ServiceUtil.returnSuccess(); 554 } catch (GenericEntityException e) { 555 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateActivityCommEventFail", locale, module); 556 } catch (GenericServiceException e) { 557 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateActivityCommEventFail", locale, module); 558 } 559 } 560 561 public static Map logTask(DispatchContext dctx, Map context) { 562 GenericDelegator delegator = dctx.getDelegator(); 563 LocalDispatcher dispatcher = dctx.getDispatcher(); 564 Security security = dctx.getSecurity(); 565 GenericValue userLogin = (GenericValue) context.get("userLogin"); 566 Locale locale = (Locale ) context.get("locale"); 567 String workEffortPurposeTypeId = (String ) context.get("workEffortPurposeTypeId"); 568 569 try { 570 Timestamp actualStartDate = (Timestamp ) context.get("actualStartDate"); 572 if (actualStartDate == null) actualStartDate = UtilDateTime.nowTimestamp(); 573 Timestamp actualCompletionDate = UtilCommon.getEndTimestamp(actualStartDate, (String ) context.get("duration"), locale); 574 575 Map serviceResults = validateWorkEffortAssociations(dctx, context); 577 if (ServiceUtil.isError(serviceResults)) { 578 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorLogTaskFail", locale, module); 579 } 580 581 584 ModelService service = dctx.getModelService("createWorkEffort"); 586 Map input = service.makeValid(context, "IN"); 587 input.put("actualCompletionDate", actualCompletionDate); 588 input.put("workEffortTypeId", "TASK"); 589 input.put("currentStatusId", "TASK_COMPLETED"); 590 serviceResults = dispatcher.runSync("createWorkEffort", input); 591 if (ServiceUtil.isError(serviceResults)) { 592 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorLogTaskFail", locale, module); 593 } 594 String workEffortId = (String ) serviceResults.get("workEffortId"); 595 596 serviceResults = createWorkEffortPartyAssociations(dctx, context, workEffortId, "CrmErrorLogTaskFail", true); 598 if (ServiceUtil.isError(serviceResults)) { 599 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorLogTaskFail", locale, module); 600 } 601 602 boolean outbound = false; 604 String partyIdTo = userLogin.getString("partyId"); 605 String partyIdFrom = (String ) context.get("internalPartyId"); 606 607 if (context.get("outbound").equals("Y")) { 609 outbound = true; 610 partyIdTo = (String ) context.get("internalPartyId"); 611 partyIdFrom = userLogin.getString("partyId"); 612 } 613 614 service = dctx.getModelService("createCommunicationEvent"); 616 input = service.makeValid(context, "IN"); 617 input.put("subject", context.get("workEffortName")); 618 input.put("entryDate", UtilDateTime.nowTimestamp()); 619 input.put("datetimeStarted", actualStartDate); 620 input.put("datetimeEnded", actualCompletionDate); 621 if ("WEPT_TASK_EMAIL".equals(workEffortPurposeTypeId)) { 622 input.put("contactMechTypeId", "EMAIL_ADDRESS"); 623 input.put("communicationEventTypeId", "EMAIL_COMMUNICATION"); 624 } else if ("WEPT_TASK_PHONE_CALL".equals(workEffortPurposeTypeId)) { 625 input.put("contactMechTypeId", "TELECOM_NUMBER"); 626 input.put("communicationEventTypeId", "PHONE_COMMUNICATION"); 627 } else { 628 Debug.logWarning("Work effort purpose type [" + workEffortPurposeTypeId + "] not known, not able to set communication event and contact mech types", module); 629 } 630 input.put("statusId", "COM_COMPLETE"); 631 input.put("partyIdTo", partyIdTo); 632 input.put("partyIdFrom", partyIdFrom); 633 if (outbound) { 634 if (partyIdTo != null) input.put("roleTypeIdTo", PartyHelper.getFirstValidInternalPartyRoleTypeId(partyIdTo, delegator)); 635 input.put("roleTypeIdFrom", PartyHelper.getFirstValidTeamMemberRoleTypeId(partyIdFrom, delegator)); 636 } else { 637 if (partyIdFrom != null) input.put("roleTypeIdFrom", PartyHelper.getFirstValidInternalPartyRoleTypeId(partyIdFrom, delegator)); 638 input.put("roleTypeIdTo", PartyHelper.getFirstValidTeamMemberRoleTypeId(partyIdTo, delegator)); 639 } 640 serviceResults = dispatcher.runSync("createCommunicationEvent", input); 641 if (ServiceUtil.isError(serviceResults)) { 642 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorLogTaskFail", locale, module); 643 } 644 String communicationEventId = (String ) serviceResults.get("communicationEventId"); 645 646 input = UtilMisc.toMap("userLogin", userLogin, "communicationEventId", communicationEventId, "workEffortId", workEffortId); 648 serviceResults = dispatcher.runSync("createCommunicationEventWorkEff", input); 649 if (ServiceUtil.isError(serviceResults)) { 650 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorLogTaskFail", locale, module); 651 } 652 653 Map results = ServiceUtil.returnSuccess(); 654 results.put("workEffortId", workEffortId); 655 return results; 656 } catch (IllegalArgumentException e) { 657 return UtilCommon.createAndLogServiceError(e, "CrmErrorLogTaskFail", locale, module); 658 } catch (GenericEntityException e) { 659 return UtilCommon.createAndLogServiceError(e, "CrmErrorLogTaskFail", locale, module); 660 } catch (GenericServiceException e) { 661 return UtilCommon.createAndLogServiceError(e, "CrmErrorLogTaskFail", locale, module); 662 } 663 } 664 665 666 667 668 669 public static Map addWorkEffortPartyAssignment(DispatchContext dctx, Map context) { 670 GenericDelegator delegator = dctx.getDelegator(); 671 LocalDispatcher dispatcher = dctx.getDispatcher(); 672 Security security = dctx.getSecurity(); 673 GenericValue userLogin = (GenericValue) context.get("userLogin"); 674 Locale locale = (Locale ) context.get("locale"); 675 676 String workEffortId = (String ) context.get("workEffortId"); 677 String partyId = (String ) context.get("partyId"); 678 try { 679 if (!CrmsfaSecurity.hasActivityPermission(security, "_UPDATE", userLogin, workEffortId)) { 681 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 682 } 683 684 List oldAssocs = EntityUtil.filterByDate(delegator.findByAnd("WorkEffortPartyAssignment", 686 UtilMisc.toMap("workEffortId", workEffortId, "partyId", partyId))); 687 if (oldAssocs.size() == 0) { 688 689 Map input = UtilMisc.toMap("workEffortId", workEffortId, "partyId", partyId, "roleTypeId", "CAL_ATTENDEE", "statusId", "PRTYASGN_ASSIGNED", 691 "availabilityStatusId", "WEPA_AV_AVAILABLE"); 692 input.put("userLogin", userLogin); 693 Map serviceResults = dispatcher.runSync("assignPartyToWorkEffort", input); 694 if (ServiceUtil.isError(serviceResults)) { 695 return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorUpdateActivityFail", locale, module); 696 } 697 } 698 } catch (GenericEntityException e) { 699 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateActivityFail", locale, module); 700 } catch (GenericServiceException e) { 701 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateActivityFail", locale, module); 702 } 703 return ServiceUtil.returnSuccess(); 704 } 705 706 public static Map removeWorkEffortPartyAssignment(DispatchContext dctx, Map context) { 707 GenericDelegator delegator = dctx.getDelegator(); 708 LocalDispatcher dispatcher = dctx.getDispatcher(); 709 Security security = dctx.getSecurity(); 710 GenericValue userLogin = (GenericValue) context.get("userLogin"); 711 Locale locale = (Locale ) context.get("locale"); 712 713 String workEffortId = (String ) context.get("workEffortId"); 714 String partyId = (String ) context.get("partyId"); 715 try { 716 if (!CrmsfaSecurity.hasActivityPermission(security, "_UPDATE", userLogin, workEffortId)) { 718 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 719 } 720 721 List associations = EntityUtil.filterByDate(delegator.findByAnd("WorkEffortPartyAssignment", 723 UtilMisc.toMap("workEffortId", workEffortId, "partyId", partyId, "roleTypeId", "CAL_ATTENDEE"))); 724 if (associations.size() == 0) { 725 return UtilCommon.createAndLogServiceError("Cannot remove party with ID [" + partyId + "]", "CrmErrorUpdateActivityFail", locale, module); 726 } 727 for (Iterator iter = associations.iterator(); iter.hasNext(); ) { 728 GenericValue assoc = (GenericValue) iter.next(); 729 assoc.set("thruDate", UtilDateTime.nowTimestamp()); 730 assoc.store(); 731 } 732 } catch (GenericEntityException e) { 733 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateActivityFail", locale, module); 734 } 735 return ServiceUtil.returnSuccess(); 736 } 737 738 public static Map updateWorkEffortPartyAssignment(DispatchContext dctx, Map context) { 739 GenericDelegator delegator = dctx.getDelegator(); 740 LocalDispatcher dispatcher = dctx.getDispatcher(); 741 Security security = dctx.getSecurity(); 742 GenericValue userLogin = (GenericValue) context.get("userLogin"); 743 Locale locale = (Locale ) context.get("locale"); 744 745 String workEffortId = (String ) context.get("workEffortId"); 746 String partyId = (String ) context.get("partyId"); 747 try { 748 if (!CrmsfaSecurity.hasActivityPermission(security, "_UPDATE", userLogin, workEffortId)) { 750 return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); 751 } 752 753 List associations = EntityUtil.filterByDate(delegator.findByAnd("WorkEffortPartyAssignment", 755 UtilMisc.toMap("workEffortId", workEffortId, "partyId", partyId))); 756 if (associations.size() > 0) { 757 GenericValue assoc = (GenericValue) associations.get(0); 758 assoc.setAllFields(context, false, null, null); 760 assoc.store(); 761 } 762 } catch (GenericEntityException e) { 763 return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateActivityFail", locale, module); 764 } 765 return ServiceUtil.returnSuccess(); 766 } 767 768 769 770 771 772 public static Map findActivities(DispatchContext dctx, Map context) { 773 GenericDelegator delegator = dctx.getDelegator(); 774 LocalDispatcher dispatcher = dctx.getDispatcher(); 775 Locale locale = (Locale ) context.get("locale"); 776 String partyId = (String ) context.get("partyId"); 777 String salesOpportunityId = (String ) context.get("salesOpportunityId"); 778 String custRequestId = (String ) context.get("custRequestId"); 779 List pendingOrderByFields = (List ) context.get("pendingOrderByFields"); 780 List completedOrderByFields = (List ) context.get("completedOrderByFields"); 781 782 Map results = ServiceUtil.returnSuccess(); 783 784 String entityName = null; 786 EntityExpr keyCondition = null; 787 788 if ((partyId != null) && !(partyId.equals(""))) { 789 entityName = "WorkEffortAndPartyAssign"; 790 keyCondition = new EntityExpr("partyId", EntityOperator.EQUALS, partyId); 791 } else if ((salesOpportunityId != null) && !(salesOpportunityId.equals(""))) { 792 entityName = "WorkEffortAndSalesOpportunity"; 793 keyCondition = new EntityExpr("salesOpportunityId", EntityOperator.EQUALS, salesOpportunityId); 794 } else if ((custRequestId != null) && !(custRequestId.equals(""))) { 795 entityName = "WorkEffortCustRequestView"; 796 keyCondition = new EntityExpr("custRequestId", EntityOperator.EQUALS, custRequestId); 797 } 798 799 if ((entityName == null) || (keyCondition == null)) { 800 return UtilCommon.createAndLogServiceError("No parameters specified for crmsfa.findActivities", "CrmErrorFindActivitiesFail", locale, module); 801 } 802 803 try { 804 List fieldsToSelect = UtilMisc.toList("workEffortId", "workEffortTypeId", "workEffortName", "currentStatusId", "estimatedStartDate", "estimatedCompletionDate"); 805 fieldsToSelect.add("workEffortPurposeTypeId"); 806 fieldsToSelect.add("actualStartDate"); 807 fieldsToSelect.add("actualCompletionDate"); 808 809 List pendingActivitiesCondList = UtilMisc.toList(keyCondition); 810 for (Iterator iter = UtilActivity.ACT_STATUSES_COMPLETED.iterator(); iter.hasNext(); ) { 811 pendingActivitiesCondList.add(new EntityExpr("currentStatusId", EntityOperator.NOT_EQUAL, (String ) iter.next())); 812 } 813 if (entityName.equals("WorkEffortAndPartyAssign")) { 814 pendingActivitiesCondList.add(EntityUtil.getFilterByDateExpr()); 815 } 816 EntityConditionList pendingActivitiesCond = new EntityConditionList(pendingActivitiesCondList, EntityOperator.AND); 817 List pendingActivities = delegator.findByCondition(entityName, pendingActivitiesCond, fieldsToSelect, pendingOrderByFields); 818 819 List completedActivitiesCondList = UtilMisc.toList(keyCondition); 820 for (Iterator iter = UtilActivity.ACT_STATUSES_PENDING.iterator(); iter.hasNext(); ) { 821 completedActivitiesCondList.add(new EntityExpr("currentStatusId", EntityOperator.NOT_EQUAL, (String ) iter.next())); 822 } 823 if (entityName.equals("WorkEffortAndPartyAssign")) { 824 completedActivitiesCondList.add(EntityUtil.getFilterByDateExpr()); 825 } 826 EntityConditionList completedActivitiesCond = new EntityConditionList(completedActivitiesCondList, EntityOperator.AND); 827 List completedActivities = delegator.findByCondition(entityName, completedActivitiesCond, fieldsToSelect, completedOrderByFields); 828 829 results.put("pendingActivities", pendingActivities); 830 results.put("completedActivities", completedActivities); 831 } catch (GenericEntityException e) { 832 return UtilCommon.createAndLogServiceError(e, "CrmErrorFindActivitiesFail", locale, module); 833 } 834 return results; 835 } 836 837 838 839 840 841 848 private static Map createWorkEffortPartyAssociations(DispatchContext dctx, Map context, String workEffortId, String errorLabel, boolean reassign) 849 throws GenericEntityException, GenericServiceException { 850 GenericDelegator delegator = dctx.getDelegator(); 851 LocalDispatcher dispatcher = dctx.getDispatcher(); 852 GenericValue userLogin = (GenericValue) context.get("userLogin"); 853 Locale locale = (Locale ) context.get("locale"); 854 Map input = null; 855 Map serviceResults = null; 856 857 String internalPartyId = (String ) context.get("internalPartyId"); 859 String salesOpportunityId = (String ) context.get("salesOpportunityId"); 860 String custRequestId = (String ) context.get("custRequestId"); 861 862 868 List partyAssociationIds = new ArrayList (); 869 if (internalPartyId != null) partyAssociationIds.add(internalPartyId); 870 if (salesOpportunityId != null) { 871 partyAssociationIds.addAll(UtilOpportunity.getOpportunityAccountPartyIds(delegator, salesOpportunityId)); 872 partyAssociationIds.addAll(UtilOpportunity.getOpportunityContactPartyIds(delegator, salesOpportunityId)); 873 } 874 if (custRequestId != null) { 875 List parties = UtilCase.getCaseAccountsAndContacts(delegator, custRequestId); 876 for (Iterator iter = parties.iterator(); iter.hasNext(); ) { 877 partyAssociationIds.add(((GenericValue) iter.next()).getString("partyId")); 878 } 879 } 880 List partyAssocRoleTypeIds = new ArrayList (); 882 for (Iterator iter = partyAssociationIds.iterator(); iter.hasNext(); ) { 883 String partyId = (String ) iter.next(); 884 String roleTypeId = PartyHelper.getFirstValidRoleTypeId(partyId, PartyHelper.CLIENT_PARTY_ROLES, delegator); 885 if (roleTypeId == null) { 886 return UtilCommon.createAndLogServiceError("Unknown recepient, opportunity or case with ID [" + partyId + "].", errorLabel, locale, module); 887 } 888 partyAssocRoleTypeIds.add(roleTypeId); 889 } 890 891 894 895 if (reassign) { 896 List oldOwners = EntityUtil.filterByDate(delegator.findByAnd("WorkEffortPartyAssignment", UtilMisc.toMap("workEffortId", workEffortId, "roleTypeId", "CAL_OWNER"))); 898 for (Iterator iter = oldOwners.iterator(); iter.hasNext(); ) { 899 GenericValue old = (GenericValue) iter.next(); 900 old.set("thruDate", UtilDateTime.nowTimestamp()); 901 old.store(); 902 } 903 904 input = UtilMisc.toMap("partyId", userLogin.getString("partyId"), "roleTypeId", "CAL_OWNER"); 906 List partyRoles = delegator.findByAnd("PartyRole", input); 907 if (partyRoles.size() == 0) { 908 input.put("userLogin", userLogin); 909 serviceResults = dispatcher.runSync("createPartyRole", input); 910 if (ServiceUtil.isError(serviceResults)) { 911 return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); 912 } 913 } 914 915 input.put("workEffortId", workEffortId); 917 input.put("userLogin", userLogin); 918 input.put("roleTypeId", "CAL_OWNER"); 919 input.put("statusId", "PRTYASGN_ASSIGNED"); 920 input.put("availabilityStatusId", context.get("availabilityStatusId")); serviceResults = dispatcher.runSync("assignPartyToWorkEffort", input); 922 if (ServiceUtil.isError(serviceResults)) { 923 return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); 924 } 925 } 926 927 if (salesOpportunityId != null) { 929 input = UtilMisc.toMap("salesOpportunityId", salesOpportunityId, "workEffortId", workEffortId); 930 GenericValue map = delegator.findByPrimaryKey("SalesOpportunityWorkEffort", input); 931 if (map == null) { 932 map = delegator.makeValue("SalesOpportunityWorkEffort", input); 933 map.create(); 935 } 936 } 937 938 if (custRequestId != null) { 940 serviceResults = dispatcher.runSync("createWorkEffortRequest", 941 UtilMisc.toMap("workEffortId", workEffortId, "custRequestId", custRequestId, "userLogin", userLogin)); 942 if (ServiceUtil.isError(serviceResults)) { 943 return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); 944 } 945 } 946 947 if (partyAssociationIds != null) { 949 Iterator roleIter = partyAssocRoleTypeIds.iterator(); 950 Iterator partyIter = partyAssociationIds.iterator(); 951 while (partyIter.hasNext()) { 952 String partyId = (String ) partyIter.next(); 953 String roleTypeId = (String ) roleIter.next(); 954 955 List oldAssocs = EntityUtil.filterByDate(delegator.findByAnd("WorkEffortPartyAssignment", 957 UtilMisc.toMap("workEffortId", workEffortId, "roleTypeId", roleTypeId, "partyId", partyId))); 958 if (oldAssocs.size() > 0) continue; 959 960 input = UtilMisc.toMap("workEffortId", workEffortId, "partyId", partyId, "roleTypeId", roleTypeId, "statusId", "PRTYASGN_ASSIGNED"); 962 input.put("userLogin", userLogin); 963 serviceResults = dispatcher.runSync("assignPartyToWorkEffort", input); 964 if (ServiceUtil.isError(serviceResults)) { 965 return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); 966 } 967 } 968 } 969 return ServiceUtil.returnSuccess(); 970 } 971 972 975 private static Map validateWorkEffortAssociations(DispatchContext dctx, Map context) 976 throws GenericEntityException, GenericServiceException { 977 GenericDelegator delegator = dctx.getDelegator(); 978 GenericValue userLogin = (GenericValue) context.get("userLogin"); 979 Security security = dctx.getSecurity(); 980 981 String internalPartyId = (String ) context.get("internalPartyId"); 983 String salesOpportunityId = (String ) context.get("salesOpportunityId"); 984 String custRequestId = (String ) context.get("custRequestId"); 985 986 if (internalPartyId != null) { 988 String module = CrmsfaSecurity.getSecurityModuleOfInternalParty(internalPartyId, delegator); 989 if (!CrmsfaSecurity.hasPartyRelationSecurity(security, module, "_UPDATE", userLogin, internalPartyId)) { 990 return ServiceUtil.returnError("Cannot perform this operation because you do not have permission to update the account, contact or lead with ID [" + internalPartyId + "]"); 991 } 992 } 993 994 if (salesOpportunityId != null) { 996 if (!CrmsfaSecurity.hasOpportunityPermission(security, "_UPDATE", userLogin, salesOpportunityId)) { 997 return ServiceUtil.returnError("Cannot perform this operation because you do not have permission to update the opportunity with ID [" + salesOpportunityId + "]"); 998 } 999 } 1000 1001 if (custRequestId != null) { 1003 if (!CrmsfaSecurity.hasCasePermission(security, "_UPDATE", userLogin, custRequestId)) { 1004 return ServiceUtil.returnError("Cannot perform this operation because you do not have permission to update the case with ID [" + custRequestId + "]"); 1005 } 1006 } 1007 1008 return ServiceUtil.returnSuccess(); 1010 } 1011} 1012 | Popular Tags |