📄 accountsservices.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.accounts;import java.util.Map;import java.util.List;import java.util.Locale;import java.sql.Timestamp;import javolution.util.FastMap;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.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ServiceUtil;import org.ofbiz.security.Security;import com.opensourcestrategies.crmsfa.party.PartyHelper;import com.opensourcestrategies.crmsfa.security.CrmsfaSecurity;import com.opensourcestrategies.crmsfa.util.UtilCommon;/** * Accounts services. The service documentation is in services_accounts.xml. * * @author <a href="mailto:leon@opensourcestrategies.com">Leon Torres</a> * @version $Rev: 88 $ */public class AccountsServices { public static final String module = AccountsServices.class.getName(); public static Map createAccount(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"); if (!security.hasPermission("CRMSFA_ACCOUNT_CREATE", userLogin)) { return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); } // the net result of creating an account is the generation of an Account partyId String accountPartyId = null; try { // create the Party and PartyGroup, which results in a partyId Map input = UtilMisc.toMap("groupName", context.get("groupName"), "groupNameLocal", context.get("groupNameLocal"), "officeSiteName", context.get("officeSiteName"), "description", context.get("description")); Map serviceResults = dispatcher.runSync("createPartyGroup", input); if (ServiceUtil.isError(serviceResults)) { return serviceResults; } accountPartyId = (String) serviceResults.get("partyId"); // create a PartyRole for the resulting Account partyId with roleTypeId = ACCOUNT serviceResults = dispatcher.runSync("createPartyRole", UtilMisc.toMap("partyId", accountPartyId, "roleTypeId", "ACCOUNT", "userLogin", userLogin)); if (ServiceUtil.isError(serviceResults)) { return serviceResults; } // create PartySupplementalData GenericValue partyData = delegator.makeValue("PartySupplementalData", UtilMisc.toMap("partyId", accountPartyId)); partyData.setNonPKFields(context); partyData.create(); // create a unique party relationship between the userLogin and the Account with partyRelationshipTypeId RESPONSIBLE_FOR createResponsibleAccountRelationshipForParty(userLogin.getString("partyId"), accountPartyId, userLogin, delegator, dispatcher); // if there's an initialTeamPartyId, assign the team to the account String initialTeamPartyId = (String) context.get("initialTeamPartyId"); if (initialTeamPartyId != null) { serviceResults = dispatcher.runSync("crmsfa.assignTeamToAccount", UtilMisc.toMap("accountPartyId", accountPartyId, "teamPartyId", initialTeamPartyId, "userLogin", userLogin)); if (ServiceUtil.isError(serviceResults)) { return serviceResults; } } } catch (GenericServiceException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateAccountFail", locale, module); } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorCreateAccountFail", locale, module); } // return the partyId of the newly created Account Map results = ServiceUtil.returnSuccess(); results.put("partyId", accountPartyId); return results; } public static Map updateAccount(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 accountPartyId = (String) context.get("partyId"); // make sure userLogin has CRMSFA_ACCOUNT_UPDATE permission for this account if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_ACCOUNT", "_UPDATE", userLogin, accountPartyId)) { return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); } try { // update the Party and PartyGroup Map input = UtilMisc.toMap("groupName", context.get("groupName"), "groupNameLocal", context.get("groupNameLocal"), "officeSiteName", context.get("officeSiteName"), "description", context.get("description")); input.put("partyId", accountPartyId); input.put("userLogin", userLogin); Map serviceResults = dispatcher.runSync("updatePartyGroup", input); if (ServiceUtil.isError(serviceResults)) { return serviceResults; } // update PartySupplementalData GenericValue partyData = delegator.findByPrimaryKey("PartySupplementalData", UtilMisc.toMap("partyId", accountPartyId)); if (partyData == null) { // create a new one partyData = delegator.makeValue("PartySupplementalData", UtilMisc.toMap("partyId", accountPartyId)); partyData.create(); } partyData.setNonPKFields(context); partyData.store(); } catch (GenericServiceException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateAccountFail", locale, module); } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateAccountFail", locale, module); } return ServiceUtil.returnSuccess(); } public static Map deactivateAccount(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"); // what account we're expiring String accountPartyId = (String) context.get("partyId"); // check that userLogin has CRMSFA_ACCOUNT_DEACTIVATE permission for this account if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_ACCOUNT", "_DEACTIVATE", userLogin, accountPartyId)) { return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); } // when to expire the account Timestamp expireDate = (Timestamp) context.get("expireDate"); if (expireDate == null) { expireDate = UtilDateTime.nowTimestamp(); } // in order to deactivate an acocunt, we expire all party relationships on the expire date try { List partyRelationships = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", accountPartyId, "roleTypeIdFrom", "ACCOUNT")); PartyHelper.expirePartyRelationships(partyRelationships, expireDate, dispatcher, userLogin); } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorDeactivateAccountFail", locale, module); } catch (GenericServiceException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorDeactivateAccountFail", locale, module); } return ServiceUtil.returnSuccess(); } public static Map reassignAccountResponsibleParty(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 accountPartyId = (String) context.get("accountPartyId"); String newPartyId = (String) context.get("newPartyId"); // ensure reassign permission on this account if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_ACCOUNT", "_REASSIGN", userLogin, accountPartyId)) { return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); } try { // reassign relationship using a helper method boolean result = createResponsibleAccountRelationshipForParty(newPartyId, accountPartyId, userLogin, delegator, dispatcher); if (result == false) { return UtilCommon.createAndLogServiceError("CrmErrorReassignFail", locale, module); } } catch (GenericServiceException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorReassignFail", locale, module); } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorReassignFail", locale, module); } return ServiceUtil.returnSuccess(); } /**************************************************************************/ /** Helper Methods ***/ /**************************************************************************/ /** * Creates an account relationship of a given type for the given party and removes all previous relationships of that type. * This method helps avoid semantic mistakes and typos from the repeated use of this code pattern. */ public static boolean createResponsibleAccountRelationshipForParty(String partyId, String accountPartyId, GenericValue userLogin, GenericDelegator delegator, LocalDispatcher dispatcher) throws GenericServiceException, GenericEntityException { return PartyHelper.createNewPartyToRelationship(partyId, accountPartyId, "ACCOUNT", "RESPONSIBLE_FOR", "ACCOUNT_OWNER", PartyHelper.TEAM_MEMBER_ROLES, true, userLogin, delegator, dispatcher); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -