📄 partyservices.java
字号:
/* * $Id: PartyServices.java 7119 2006-03-29 19:22:22Z sichen $ * * Copyright (c) 2001, 2002, 2003 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 org.ofbiz.party.party;import java.sql.Timestamp;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Locale;import java.util.Map;import javolution.util.FastList;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilDateTime;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.condition.EntityCondition;import org.ofbiz.entity.condition.EntityConditionList;import org.ofbiz.entity.condition.EntityExpr;import org.ofbiz.entity.condition.EntityFieldValue;import org.ofbiz.entity.condition.EntityFunction;import org.ofbiz.entity.condition.EntityOperator;import org.ofbiz.entity.model.DynamicViewEntity;import org.ofbiz.entity.model.ModelKeyMap;import org.ofbiz.entity.util.EntityFindOptions;import org.ofbiz.entity.util.EntityListIterator;import org.ofbiz.entity.util.EntityTypeUtil;import org.ofbiz.entity.util.EntityUtil;import org.ofbiz.security.Security;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.ModelService;import org.ofbiz.service.ServiceUtil;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.GenericServiceException;/** * Services for Party/Person/Group maintenance * * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 7119 $ * @since 2.0 */public class PartyServices { public static final String module = PartyServices.class.getName(); public static final String resource = "PartyUiLabels"; /** * Deletes a Party. * @param ctx The DispatchContext that this service is operating in. * @param context Map containing the input parameters. * @return Map with the result of the service, the output parameters. */ public static Map deleteParty(DispatchContext ctx, Map context) { Locale locale = (Locale) context.get("locale"); /* * pretty serious operation, would delete: * - Party * - PartyRole * - PartyRelationship: from and to * - PartyDataObject * - Person or PartyGroup * - PartyContactMech, but not ContactMech itself * - PartyContactMechPurpose * - Order? * * We may want to not allow this, but rather have some sort of delete flag for it if it's REALLY that big of a deal... */ String errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_delete_party_not_implemented", locale); return ServiceUtil.returnError(errMsg); } /** * Creates a Person. * If no partyId is specified a numeric partyId is retrieved from the Party sequence. * @param ctx The DispatchContext that this service is operating in. * @param context Map containing the input parameters. * @return Map with the result of the service, the output parameters. */ public static Map createPerson(DispatchContext ctx, Map context) { Map result = new HashMap(); GenericDelegator delegator = ctx.getDelegator(); Timestamp now = UtilDateTime.nowTimestamp(); List toBeStored = new LinkedList(); Locale locale = (Locale) context.get("locale"); // in most cases userLogin will be null, but get anyway so we can keep track of that info if it is available GenericValue userLogin = (GenericValue) context.get("userLogin"); String partyId = (String) context.get("partyId"); // if specified partyId starts with a number, return an error if (partyId != null && partyId.length() > 0 && Character.isDigit(partyId.charAt(0))) { return ServiceUtil.returnError(UtilProperties.getMessage(resource, "party.id_is_digit", locale)); } // partyId might be empty, so check it and get next seq party id if empty if (partyId == null || partyId.length() == 0) { try { partyId = delegator.getNextSeqId("Party"); } catch (IllegalArgumentException e) { return ServiceUtil.returnError(UtilProperties.getMessage(resource, "party.id_generation_failure", locale)); } } // check to see if party object exists, if so make sure it is PERSON type party GenericValue party = null; try { party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId)); } catch (GenericEntityException e) { Debug.logWarning(e.getMessage(), module); } if (party != null) { if (!"PERSON".equals(party.getString("partyTypeId"))) { return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.create.party_exists_not_person_type", locale)); } } else { // create a party if one doesn't already exist with an initial status from the input String statusId = (String) context.get("statusId"); Map newPartyMap = UtilMisc.toMap("partyId", partyId, "partyTypeId", "PERSON", "createdDate", now, "lastModifiedDate", now, "statusId", statusId); if (userLogin != null) { newPartyMap.put("createdByUserLogin", userLogin.get("userLoginId")); newPartyMap.put("lastModifiedByUserLogin", userLogin.get("userLoginId")); } party = delegator.makeValue("Party", newPartyMap); toBeStored.add(party); } GenericValue person = null; try { person = delegator.findByPrimaryKey("Person", UtilMisc.toMap("partyId", partyId)); } catch (GenericEntityException e) { Debug.logWarning(e.getMessage(), module); } if (person != null) { return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.create.person_exists", locale)); } person = delegator.makeValue("Person", UtilMisc.toMap("partyId", partyId)); person.setNonPKFields(context); toBeStored.add(person); try { delegator.storeAll(toBeStored); } catch (GenericEntityException e) { Debug.logWarning(e.getMessage(), module); return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.create.db_error", new Object[] { e.getMessage() }, locale)); } result.put("partyId", partyId); result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); return result; } /** * Sets a party status. * <b>security check</b>: userLogin must have permission PARTYMGR_STS_UPDATE and the status change must be defined in StatusValidChange. */ public static Map setPartyStatus(DispatchContext ctx, Map context) { Map result = new HashMap(); GenericDelegator delegator = ctx.getDelegator(); Security security = ctx.getSecurity(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); String partyId = (String) context.get("partyId"); String statusId = (String) context.get("statusId"); Timestamp statusDate = (Timestamp) context.get("statusDate"); if (statusDate == null) statusDate = UtilDateTime.nowTimestamp(); // userLogin must have PARTYMGR_STS_UPDATE. Also, we aren't letting userLogin with same partyId change his own status. if (!security.hasEntityPermission("PARTYMGR", "_STS_UPDATE", userLogin)) { String errorMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.no_permission_to_operation", locale) + "."; Debug.logWarning(errorMsg, module); return ServiceUtil.returnError(errorMsg); } try { GenericValue party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId)); // check that status is defined as a valid change GenericValue statusValidChange = delegator.findByPrimaryKey("StatusValidChange", UtilMisc.toMap("statusId", party.getString("statusId"), "statusIdTo", statusId)); if (statusValidChange == null) { String errorMsg = "Cannot change party status from " + party.getString("statusId") + " to " + statusId; Debug.logWarning(errorMsg, module); return ServiceUtil.returnError(errorMsg); } // record the oldStatusId and change the party status String oldStatusId = party.getString("statusId"); party.set("statusId", statusId); party.store(); // record this status change in PartyStatus table GenericValue partyStatus = delegator.makeValue("PartyStatus", UtilMisc.toMap("partyId", partyId, "statusId", statusId, "statusDate", statusDate)); partyStatus.create(); Map results = ServiceUtil.returnSuccess(); results.put("oldStatusId", oldStatusId); return results; } catch (GenericEntityException e) { Debug.logError(e, e.getMessage(), module); return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.update.write_failure", new Object[] { e.getMessage() }, locale)); } } /** * Updates a Person. * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_GRP_UPDATE permission. * @param ctx The DispatchContext that this service is operating in. * @param context Map containing the input parameters. * @return Map with the result of the service, the output parameters. */ public static Map updatePerson(DispatchContext ctx, Map context) { Map result = new HashMap(); GenericDelegator delegator = ctx.getDelegator(); Security security = ctx.getSecurity(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_GRP_UPDATE"); if (result.size() > 0) return result; GenericValue person = null; GenericValue party = null; try { person = delegator.findByPrimaryKey("Person", UtilMisc.toMap("partyId", partyId)); party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId)); } catch (GenericEntityException e) { Debug.logWarning(e, module); return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.update.read_failure", new Object[] { e.getMessage() }, locale)); } if (person == null || party == null) { return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.update.not_found", locale)); } person.setNonPKFields(context); party.setNonPKFields(context); try { person.store(); party.store(); } catch (GenericEntityException e) { Debug.logWarning(e.getMessage(), module); return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.update.write_failure", new Object[] { e.getMessage() }, locale)); } result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); result.put(ModelService.SUCCESS_MESSAGE, UtilProperties.getMessage(resource, "person.update.success", locale)); return result; } /** * Creates a PartyGroup. * If no partyId is specified a numeric partyId is retrieved from the Party sequence. * @param ctx The DispatchContext that this service is operating in. * @param context Map containing the input parameters. * @return Map with the result of the service, the output parameters. */ public static Map createPartyGroup(DispatchContext ctx, Map context) { Map result = new HashMap(); GenericDelegator delegator = ctx.getDelegator(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Timestamp now = UtilDateTime.nowTimestamp(); String partyId = (String) context.get("partyId"); Locale locale = (Locale) context.get("locale"); String errMsg = null; // partyId might be empty, so check it and get next seq party id if empty if (partyId == null || partyId.length() == 0) { try { partyId = delegator.getNextSeqId("Party"); } catch (IllegalArgumentException e) { errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_create_party_group_generation_failure", locale); return ServiceUtil.returnError(errMsg); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -