📄 partyhelper.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.party;import java.util.Map;import java.util.List;import java.util.Iterator;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.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.security.Security;/** * Party Helper methods which are designed to provide a consistent set of APIs that can be reused by * higher level services. * * @author <a href="mailto:leon@opensourcestrategies.com">Leon Torres</a> * @version $Rev: 94 $ */public class PartyHelper { public static final String module = PartyHelper.class.getName(); public static List TEAM_MEMBER_ROLES = UtilMisc.toList("ACCOUNT_MANAGER", "ACCOUNT_REP", "CUST_SERVICE_REP"); public static List CLIENT_PARTY_ROLES = UtilMisc.toList("ACCOUNT", "CONTACT", "PROSPECT"); /** * A helper method which finds the first valid roleTypeId for a partyId, using a List of possible roleTypeIds * * @param partyId * @param possibleRoleTypeIds a List of roleTypeIds * @param delegator * @return the first roleTypeId from possibleRoleTypeIds which is actually found in PartyRole for the given partyId * @throws GenericEntityException */ public static String getFirstValidRoleTypeId(String partyId, List possibleRoleTypeIds, GenericDelegator delegator) throws GenericEntityException { List partyRoles = delegator.findByAndCache("PartyRole", UtilMisc.toMap("partyId", partyId)); // iterate across all possible roleTypeIds from the parameter Iterator iterValid = possibleRoleTypeIds.iterator(); while (iterValid.hasNext()) { String possibleRoleTypeId = (String) iterValid.next(); // try to look for each one in the list of PartyRoles Iterator partyRolesIter = partyRoles.iterator(); while (partyRolesIter.hasNext()) { GenericValue partyRole = (GenericValue) partyRolesIter.next(); if (possibleRoleTypeId.equals(partyRole.getString("roleTypeId"))) { return possibleRoleTypeId; } } } return null; } /** * As above, but pass in the list of internal party roles, such as ACCOUNT, CONTACT, PROSPECT */ public static String getFirstValidInternalPartyRoleTypeId(String partyId, GenericDelegator delegator) throws GenericEntityException { return getFirstValidRoleTypeId(partyId, CLIENT_PARTY_ROLES, delegator); } /** * As above, but pass in the list of team member roles such as ACCOUNT_REP, etc. */ public static String getFirstValidTeamMemberRoleTypeId(String partyId, GenericDelegator delegator) throws GenericEntityException { return getFirstValidRoleTypeId(partyId, TEAM_MEMBER_ROLES, delegator); } /** * A helper method for creating a PartyRelationship entity from partyIdTo to partyIdFrom with specified partyRelationshipTypeId, roleTypeIdFrom, * a List of valid roles for the to-party, and a flag to expire any existing relationships between the two parties of the same * type. The idea is that several services would do validation and then use this method to do all the work. * * @param partyIdTo * @param partyIdFrom * @param roleTypeIdFrom * @param partyRelationshipTypeId * @param securityGroupId * @param validToPartyRoles List of roleTypeIds which are valid for the partyIdTo in the create relationship. It will cycle * through until the first of these roles is actually associated with partyIdTo and then create a PartyRelationship using that * roleTypeId. If none of these are associated with partyIdTo, then it will return false * @param fromDate * @param expireExistingRelationships If set to true, will look for all existing PartyRelationships of partyIdFrom, partyRelationshipTypeId * and expire all of them as of the passed in fromDate * @return false if no relationship was created or true if operation succeeds */ public static boolean createNewPartyToRelationship(String partyIdTo, String partyIdFrom, String roleTypeIdFrom, String partyRelationshipTypeId, String securityGroupId, List validToPartyRoles, Timestamp fromDate, boolean expireExistingRelationships, GenericValue userLogin, GenericDelegator delegator, LocalDispatcher dispatcher) throws GenericEntityException, GenericServiceException { // get the first valid roleTypeIdTo from a list of possible roles for the partyIdTo // this will be the role we use as roleTypeIdTo in PartyRelationship. String roleTypeIdTo = getFirstValidRoleTypeId(partyIdTo, validToPartyRoles, delegator); // if no matching roles were found, then no relationship created if (roleTypeIdTo == null) return false; /* * if expireExistingRelationships is true, then find all existing PartyRelationships with partyIdFrom and partyRelationshipTypeId which * are not expired on the fromDate and then expire them */ if (expireExistingRelationships == true) { List partyRelationships = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", partyIdFrom, "partyRelationshipTypeId", partyRelationshipTypeId)); expirePartyRelationships(partyRelationships, fromDate, dispatcher, userLogin); } // call createPartyRelationship service to create PartyRelationship using parameters and the role we just found Map input = UtilMisc.toMap("partyIdTo", partyIdTo, "roleTypeIdTo", roleTypeIdTo, "partyIdFrom", partyIdFrom, "roleTypeIdFrom", roleTypeIdFrom); input.put("partyRelationshipTypeId", partyRelationshipTypeId); input.put("securityGroupId", securityGroupId); input.put("fromDate", fromDate); input.put("userLogin", userLogin); Map serviceResult = dispatcher.runSync("createPartyRelationship", input); // on success return true return true; } /** * Same as above except uses a default of now for the timestamp */ public static boolean createNewPartyToRelationship(String partyIdTo, String partyIdFrom, String roleTypeIdFrom, String partyRelationshipTypeId, String securityGroupId, List validToPartyRoles, boolean expireExistingRelationships, GenericValue userLogin, GenericDelegator delegator, LocalDispatcher dispatcher) throws GenericEntityException, GenericServiceException { return createNewPartyToRelationship(partyIdTo, partyIdFrom, roleTypeIdFrom, partyRelationshipTypeId, securityGroupId, validToPartyRoles, UtilDateTime.nowTimestamp(), expireExistingRelationships, userLogin, delegator, dispatcher);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -