📄 utilopportunity.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. *//* * 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.opportunities;import java.util.Map;import java.util.List;import java.util.Iterator;import java.util.ArrayList;import java.util.Locale;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.common.period.PeriodWorker;import org.ofbiz.entity.*;import org.ofbiz.entity.condition.*;import org.ofbiz.entity.util.*;/** * Opportunity utility methods. * * @author <a href="mailto:leon@opensourcestrategies.com">Leon Torres</a> */public class UtilOpportunity { public static final String module = UtilOpportunity.class.getName(); /** * Helper method to get the principal account for an opportunity. This is a simplification of the * datamodel and should only be calld for non-critical uses. Returns null if no account was found, * which would be the case if there were a lead party Id instead. */ public static String getOpportunityAccountPartyId(GenericValue opportunity) throws GenericEntityException { List candidates = opportunity.getRelatedByAnd("SalesOpportunityRole", UtilMisc.toMap("roleTypeId", "ACCOUNT")); if (candidates.size() == 0) return null; // we have two out of three primary keys, so the result is guaranteed to be the one with our partyId GenericValue salesOpportunityRole = (GenericValue) candidates.get(0); return salesOpportunityRole.getString("partyId"); } /** * Helper method to get the principal lead for an opportunity. This is a simplification of the * datamodel and should only be calld for non-critical uses. Returns null if no lead was found, * which would be the case if there were an account party Id instead. */ public static String getOpportunityLeadPartyId(GenericValue opportunity) throws GenericEntityException { List candidates = opportunity.getRelatedByAnd("SalesOpportunityRole", UtilMisc.toMap("roleTypeId", "PROSPECT")); if (candidates.size() == 0) return null; // we have two out of three primary keys, so the result is guaranteed to be the one with our partyId GenericValue salesOpportunityRole = (GenericValue) candidates.get(0); return salesOpportunityRole.getString("partyId"); } /** Helper method to get the principal lead or account partyId of an opportunity. Use this to get one or the other. */ public static String getOpportunityAccountOrLeadPartyId(GenericValue opportunity) throws GenericEntityException { List candidates = opportunity.getRelatedByAnd("SalesOpportunityRole", UtilMisc.toMap("roleTypeId", "ACCOUNT")); if (candidates.size() > 0) return ((GenericValue) candidates.get(0)).getString("partyId"); candidates = opportunity.getRelatedByAnd("SalesOpportunityRole", UtilMisc.toMap("roleTypeId", "PROSPECT")); if (candidates.size() > 0) return ((GenericValue) candidates.get(0)).getString("partyId"); return null; } /** * Helper method to get all account party Id's for an opportunity. This is a more serious version of the above * for use in critical logic, such as security or in complex methods that should use the whole list from the beginning. */ public static List getOpportunityAccountPartyIds(GenericDelegator delegator, String salesOpportunityId) throws GenericEntityException { return getOpportunityPartiesByRole(delegator, salesOpportunityId, "ACCOUNT"); } /** Helper method to get all lead party Id's for an opportunity. See comments for getOpportunityAccountPartyIds(). */ public static List getOpportunityLeadPartyIds(GenericDelegator delegator, String salesOpportunityId) throws GenericEntityException { return getOpportunityPartiesByRole(delegator, salesOpportunityId, "PROSPECT"); } /** Helper method to get all contact party Id's for an opportunity. */ public static List getOpportunityContactPartyIds(GenericDelegator delegator, String salesOpportunityId) throws GenericEntityException { return getOpportunityPartiesByRole(delegator, salesOpportunityId, "CONTACT"); } /** Helper method to get all party Id's of a given role for an opportunity. It's better to use one of the more specific methods above. */ public static List getOpportunityPartiesByRole(GenericDelegator delegator, String salesOpportunityId, String roleTypeId) throws GenericEntityException { List maps = delegator.findByAnd("SalesOpportunityRole", UtilMisc.toMap("roleTypeId", roleTypeId, "salesOpportunityId", salesOpportunityId)); List results = new ArrayList(); for (Iterator iter = maps.iterator(); iter.hasNext(); ) { GenericValue map = (GenericValue) iter.next(); results.add(map.getString("partyId")); } return results; } /** * Helper method to make a sales opportunity history, which should be done whenever an opp is created, updated or deleted. * @return The created SalesOpportunityHistory */ public static GenericValue createSalesOpportunityHistory(GenericValue opportunity, GenericDelegator delegator, Map context) throws GenericEntityException { GenericValue userLogin = (GenericValue) context.get("userLogin"); String historyId = delegator.getNextSeqId("SalesOpportunityHistory"); GenericValue history = delegator.makeValue("SalesOpportunityHistory", UtilMisc.toMap("salesOpportunityHistoryId", historyId)); // we assume the opportunity has all fields set as desired already, especially the probability history.setNonPKFields(opportunity.getAllFields()); history.set("changeNote", context.get("changeNote")); history.set("modifiedByUserLogin", userLogin.getString("userLoginId")); history.set("modifiedTimestamp", UtilDateTime.nowTimestamp()); history.create(); return history;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -