📄 activitiesservices.java
字号:
/* * Copyright (C) 2006 Open Source Strategies, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *//* Copyright (c) 2005-2006 Open Source Strategies, Inc. *//* * $Id:$ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package com.opensourcestrategies.crmsfa.activities;import java.util.HashMap;import java.util.Map;import java.util.List;import java.util.ArrayList;import java.util.Locale;import java.util.Iterator;import java.io.ObjectInputStream.GetField;import java.sql.Timestamp;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.condition.EntityConditionList;import org.ofbiz.entity.condition.EntityExpr;import org.ofbiz.entity.condition.EntityOperator;import org.ofbiz.entity.util.EntityUtil;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ServiceUtil;import org.ofbiz.service.ModelService;import org.ofbiz.service.mail.MimeMessageWrapper;import org.ofbiz.security.Security;import com.opensourcestrategies.crmsfa.party.PartyHelper;import com.opensourcestrategies.crmsfa.security.CrmsfaSecurity;import com.opensourcestrategies.crmsfa.util.UtilCommon;import com.opensourcestrategies.crmsfa.opportunities.UtilOpportunity;import com.opensourcestrategies.crmsfa.cases.UtilCase;import com.opensourcestrategies.crmsfa.activities.UtilActivity;/** * Activities services. The service documentation is in services_activities.xml. * * @author <a href="mailto:leon@opensourcestrategies.com">Leon Torres</a> * @version $Rev: 248 $ */public class ActivitiesServices { public static final String module = ActivitiesServices.class.getName(); public static Map sendEmail(DispatchContext dctx, Map context) { return sendOrSaveEmailHelper(dctx, context, true, "CrmErrorSendEmailFail"); } public static Map saveEmail(DispatchContext dctx, Map context) { return sendOrSaveEmailHelper(dctx, context, false, "CrmErrorSaveEmailFail"); } /** * Saving and sending are very complex services that are nearly identical in most ways. * There are four things that break the identity in minor ways that can be handled with * booleans. The four things are: Send new email, send existing email, save new email, * and send existing email. Instead of creating four separate methods several hundred * lines each, we do everything here. */ private static Map sendOrSaveEmailHelper(DispatchContext dctx, Map context, boolean sending, String errorLabel) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Security security = dctx.getSecurity(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); // use the toEmail and internalPartyId to find the contactMechIdTo String toEmail = (String) context.get("toEmail"); String internalPartyId = (String) context.get("internalPartyId"); // if the email exists already, these will be set String communicationEventId = (String) context.get("communicationEventId"); String workEffortId = (String) context.get("workEffortId"); boolean existing = ((communicationEventId == null) || communicationEventId.equals("") ? false : true); try { // validate the associations Map serviceResults = validateWorkEffortAssociations(dctx, context); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); } // search for contactMechIdTo using the passed in email and party List candidates = EntityUtil.filterByDate(delegator.findByAnd("PartyAndContactMech", UtilMisc.toMap("infoString", toEmail, "partyId", internalPartyId))); if (candidates.size() == 0) { return UtilCommon.createAndLogServiceError("Could not find email [" + toEmail + "] for party with ID [" + internalPartyId + "].", errorLabel, locale, module); } // use the first one in the list String contactMechIdTo = ((GenericValue) candidates.get(0)).getString("contactMechId"); // also get the partyIdTo and roleTypeIdTo of this contact mech List toContactMechs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMech", UtilMisc.toMap("contactMechId", contactMechIdTo), UtilMisc.toList("fromDate DESC"))); String partyIdTo = ((GenericValue) toContactMechs.get(0)).getString("partyId"); String roleTypeIdTo = PartyHelper.getFirstValidRoleTypeId(partyIdTo, PartyHelper.CLIENT_PARTY_ROLES, delegator); /* * We're done with validation, now we begin the complex task of creating comm events, workefforts, and updating them * so that the email is sent or saved properly. The most verbose way of doing this requires four different methods with * a lot of redundant code. But here we use two booleans "existing" and "sending" to decide what to do. This is much * more compact but can be prone to subtle state errors. On the other hand, changes to the form input require only one * change here instead of four changes. */ // create a PENDING comm event or update the existing one; set the comm event data with the form input String serviceName = (existing ? "updateCommunicationEvent" : "createCommunicationEvent"); ModelService service = dctx.getModelService(serviceName); Map input = service.makeValid(context, "IN"); if (existing) input.put("communicationEventId", communicationEventId); if (!existing) input.put("entryDate", UtilDateTime.nowTimestamp()); input.put("contactMechIdTo", contactMechIdTo); input.put("contactMechTypeId", "EMAIL_ADDRESS"); input.put("communicationEventTypeId", "EMAIL_COMMUNICATION"); input.put("statusId", "COM_PENDING"); input.put("partyIdTo", partyIdTo); input.put("roleTypeIdTo", roleTypeIdTo); input.put("partyIdFrom", userLogin.getString("partyId")); input.put("roleTypeIdFrom", PartyHelper.getFirstValidRoleTypeId(userLogin.getString("partyId"), PartyHelper.TEAM_MEMBER_ROLES, delegator)); serviceResults = dispatcher.runSync(serviceName, input); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); } // get the communication event id if the comm event was created if (!existing) communicationEventId = (String) serviceResults.get("communicationEventId"); if (sending) { // Update communication event to status IN_PROGRESS: this causes it to be sent by OFBiz synchronously input = UtilMisc.toMap("communicationEventId", communicationEventId, "statusId", "COM_IN_PROGRESS", "userLogin", userLogin); input.put("communicationEventTypeId", "EMAIL_COMMUNICATION"); // this also needs to be specified to force the ECA that sends email to pass its conditions input.put("datetimeEnded", UtilDateTime.nowTimestamp()); serviceResults = dispatcher.runSync("updateCommunicationEvent", input); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); } // now update or create a work effort to record this email as a completed task input = UtilMisc.toMap("workEffortTypeId", "TASK", "currentStatusId", "TASK_COMPLETED", "userLogin", userLogin); if (existing) input.put("workEffortId", workEffortId); input.put("actualStartDate", context.get("datetimeStarted")); input.put("actualCompletionDate", UtilDateTime.nowTimestamp()); input.put("workEffortName", context.get("subject")); input.put("workEffortPurposeTypeId", "WEPT_TASK_EMAIL"); serviceName = (existing ? "updateWorkEffort" : "createWorkEffort"); serviceResults = dispatcher.runSync(serviceName, input); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); } } else { // Create or update a scheduled (TASK_STARTED) TASK WorkEffort to save this email input = UtilMisc.toMap("workEffortTypeId", "TASK", "currentStatusId", "TASK_STARTED", "userLogin", userLogin); if (existing) input.put("workEffortId", workEffortId); input.put("actualStartDate", context.get("datetimeStarted")); input.put("workEffortName", context.get("subject")); input.put("workEffortPurposeTypeId", "WEPT_TASK_EMAIL"); serviceResults = dispatcher.runSync(existing ? "updateWorkEffort" : "createWorkEffort", input); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); } } // get the work effort ID from the serviceResults if a workEffort was created (note that the last service run in this case is always createWorkEffort) if (!existing) workEffortId = (String) serviceResults.get("workEffortId"); // create an association between the task and comm event (safe even if existing) input = UtilMisc.toMap("userLogin", userLogin, "communicationEventId", communicationEventId, "workEffortId", workEffortId); serviceResults = dispatcher.runSync("createCommunicationEventWorkEff", input); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, errorLabel, locale, module); } // we need to zap all associations now if there are any previously saved ones if (existing) { UtilActivity.removeAllAssociationsForWorkEffort(workEffortId, delegator); } // create the associations and finish return createWorkEffortPartyAssociations(dctx, context, workEffortId, errorLabel, !existing); } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, errorLabel, locale, module); } catch (GenericServiceException e) { return UtilCommon.createAndLogServiceError(e, errorLabel, locale, module); } } /** * If the party is a member of the team, then Creates a Work Effort, associates it with the Communication Event, * and assigns it to the Party with Work Effort */ private static void associateCommunicationEventWorkEffortAndParty(String partyIdTo, GenericValue communicationEvent, GenericDelegator delegator, LocalDispatcher dispatcher, GenericValue userLogin, Locale locale, DispatchContext dctx, Map context) throws GenericEntityException, GenericServiceException { // create an association between the WorkEffort and the CommunicationEvent using the partyIdTo field provided // partyIdTo is not null and the party has a CRM/SFA team member role (ie, is a CRM/SFA user) if(partyIdTo != null) { String roleTypeId = PartyHelper.getFirstValidTeamMemberRoleTypeId(partyIdTo, delegator); if (roleTypeId != null) { Map serviceResults = null; Map input = null; String communicationEventId = communicationEvent.getString("communicationEventId"); //Create Workeffort from CommunicationEvent with task status as scheduled (TASK_SCHEDULED) instead of complete (TASK_COMPLETED) //so that it shows up in Pending Activities rather than Activities History input = UtilMisc.toMap("workEffortTypeId", "TASK", "currentStatusId", "TASK_SCHEDULED", "userLogin", userLogin); input.put("actualStartDate", communicationEvent.getTimestamp("datetimeStarted"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -