📄 partyservices.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 */package com.opensourcestrategies.crmsfa.party;import java.util.Map;import java.util.List;import java.util.Locale;import java.util.Iterator;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.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.security.Security;import com.opensourcestrategies.crmsfa.party.PartyHelper;import com.opensourcestrategies.crmsfa.security.CrmsfaSecurity;import com.opensourcestrategies.crmsfa.util.UtilCommon;/** * Services common to CRM parties such as accounts/contacts/leads. The service documentation is in services_party.xml. * * @author <a href="mailto:leon@opensourcestrategies.com">Leon Torres</a> */public class PartyServices { public static final String module = PartyServices.class.getName(); /** Merging function for two unique GenericValues. */ private static void mergeTwoValues(String entityName, Map fromKeys, Map toKeys, GenericDelegator delegator) throws GenericEntityException { GenericValue from = delegator.findByPrimaryKey(entityName, fromKeys); GenericValue to = delegator.findByPrimaryKey(entityName, toKeys); if (from == null || to == null) return; from.setNonPKFields(to.getAllFields()); to.setNonPKFields(from.getAllFields()); to.store(); } public static Map validateMergeCrmParties(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Security security = dctx.getSecurity(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); String partyIdFrom = (String) context.get("partyIdFrom"); String partyIdTo = (String) context.get("partyIdTo"); try { // ensure that merging parties are the same type (ACCOUNT, CONTACT, PROSPECT) String fromPartyType = PartyHelper.getFirstValidInternalPartyRoleTypeId(partyIdFrom, delegator); String toPartyType = PartyHelper.getFirstValidInternalPartyRoleTypeId(partyIdTo, delegator); if ((fromPartyType == null) || !fromPartyType.equals(toPartyType)) { return UtilCommon.createAndLogServiceError("Cannot merge party ["+partyIdFrom+"] of type ["+fromPartyType+"] with party ["+partyIdTo+"] of type ["+toPartyType+"] because they are not the same type.", "CrmErrorMergePartiesFail", locale, module); } if (partyIdFrom.equals(partyIdTo)) { return UtilCommon.createAndLogServiceError("Cannot merge party ["+partyIdFrom+"] to itself!", "CrmErrorMergeParties", locale, module); } // convert ACCOUNT/CONTACT/PROSPECT to ACCOUNT/CONTACT/LEAD String partyTypeCrm = (fromPartyType.equals("PROSPECT") ? "LEAD" : fromPartyType); // make sure userLogin has CRMSFA_${partyTypeCrm}_UPDATE permission for both parties TODO: and delete, check security config if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_" + partyTypeCrm, "_UPDATE", userLogin, partyIdFrom) || !CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_" + partyTypeCrm, "_UPDATE", userLogin, partyIdTo)) { return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); } } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorMergePartiesFail", locale, module); } return ServiceUtil.returnSuccess(); } // TODO: avoid merging parties with particular statuses. implement with an array public static Map mergeCrmParties(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Security security = dctx.getSecurity(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); String partyIdFrom = (String) context.get("partyIdFrom"); String partyIdTo = (String) context.get("partyIdTo"); try { // validate again Map serviceResults = dispatcher.runSync("crmsfa.validateMergeCrmParties", UtilMisc.toMap("userLogin", userLogin, "partyIdFrom", partyIdFrom, "partyIdTo", partyIdTo)); if (ServiceUtil.isError(serviceResults)) { return serviceResults; } // merge the party objects mergeTwoValues("PartySupplementalData", UtilMisc.toMap("partyId", partyIdFrom), UtilMisc.toMap("partyId", partyIdTo), delegator); mergeTwoValues("Person", UtilMisc.toMap("partyId", partyIdFrom), UtilMisc.toMap("partyId", partyIdTo), delegator); mergeTwoValues("PartyGroup", UtilMisc.toMap("partyId", partyIdFrom), UtilMisc.toMap("partyId", partyIdTo), delegator); mergeTwoValues("Party", UtilMisc.toMap("partyId", partyIdFrom), UtilMisc.toMap("partyId", partyIdTo), delegator); // merge relationships by changing partyIdFrom to partyIdTo in PartyRelationship for valid ones (active and not equal) List relationships = EntityUtil.filterByDate(delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", partyIdFrom))); for (Iterator iter = relationships.iterator(); iter.hasNext(); ) { GenericValue relationship = (GenericValue) iter.next(); List toRelationships = EntityUtil.filterByDate(delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", partyIdTo, "partyIdTo", relationship.get("partyIdTo"), "roleTypeIdFrom", relationship.get("roleTypeIdFrom"), "roleTypeIdTo", relationship.get("roleTypeIdTo")))); if (toRelationships.size() > 0) continue; GenericValue newRelationship = delegator.makeValue("PartyRelationship", relationship.getAllFields()); newRelationship.set("partyIdFrom", partyIdTo); newRelationship.create(); relationship.remove(); } relationships = EntityUtil.filterByDate(delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdTo", partyIdFrom))); for (Iterator iter = relationships.iterator(); iter.hasNext(); ) { GenericValue relationship = (GenericValue) iter.next(); List toRelationships = EntityUtil.filterByDate(delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdTo", partyIdTo, "partyIdFrom", relationship.get("partyIdFrom"), "roleTypeIdFrom", relationship.get("roleTypeIdFrom"), "roleTypeIdTo", relationship.get("roleTypeIdTo")))); if (toRelationships.size() > 0) continue; GenericValue newRelationship = delegator.makeValue("PartyRelationship", relationship.getAllFields());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -