001: /*
002: * $Id: PartyRoleServices.java,v 1.2 2004/02/26 09:10:48 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.party.party;
025:
026: import java.util.HashMap;
027: import java.util.Map;
028: import java.util.Locale;
029:
030: import org.ofbiz.base.util.Debug;
031: import org.ofbiz.base.util.UtilMisc;
032: import org.ofbiz.base.util.UtilProperties;
033: import org.ofbiz.entity.GenericDelegator;
034: import org.ofbiz.entity.GenericEntityException;
035: import org.ofbiz.entity.GenericValue;
036: import org.ofbiz.security.Security;
037: import org.ofbiz.service.DispatchContext;
038: import org.ofbiz.service.ModelService;
039: import org.ofbiz.service.ServiceUtil;
040:
041: /**
042: * Services for Party Role maintenance
043: *
044: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
045: * @version $Revision: 1.2 $
046: * @since 2.0
047: */
048: public class PartyRoleServices {
049:
050: public static final String module = PartyRoleServices.class
051: .getName();
052: public static final String resource = "PartyUiLabels";
053:
054: /**
055: * Creates a PartyRole
056: *@param ctx The DispatchContext that this service is operating in
057: *@param context Map containing the input parameters
058: *@return Map with the result of the service, the output parameters
059: */
060: public static Map createPartyRole(DispatchContext ctx, Map context) {
061: Map result = new HashMap();
062: GenericDelegator delegator = ctx.getDelegator();
063: Security security = ctx.getSecurity();
064: GenericValue userLogin = (GenericValue) context
065: .get("userLogin");
066:
067: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
068: security, context, result, "PARTYMGR", "_CREATE");
069: Locale locale = (Locale) context.get("locale");
070: String errMsg = null;
071:
072: if (result.size() > 0)
073: return result;
074:
075: GenericValue partyRole = delegator.makeValue("PartyRole",
076: UtilMisc.toMap("partyId", partyId, "roleTypeId",
077: context.get("roleTypeId")));
078:
079: try {
080: if (delegator.findByPrimaryKey(partyRole.getPrimaryKey()) != null) {
081: errMsg = UtilProperties
082: .getMessage(
083: resource,
084: "partyroleservices.could_not_create_party_role_exists",
085: locale);
086: return ServiceUtil.returnError(errMsg);
087: }
088: } catch (GenericEntityException e) {
089: Debug.logWarning(e, module);
090: Map messageMap = UtilMisc.toMap("errMessage", e
091: .getMessage());
092: errMsg = UtilProperties
093: .getMessage(
094: resource,
095: "partyroleservices.could_not_create_party_role_read",
096: messageMap, locale);
097: return ServiceUtil.returnError(errMsg);
098: }
099:
100: try {
101: partyRole.create();
102: } catch (GenericEntityException e) {
103: Debug.logWarning(e.getMessage(), module);
104: Map messageMap = UtilMisc.toMap("errMessage", e
105: .getMessage());
106: errMsg = UtilProperties
107: .getMessage(
108: resource,
109: "partyroleservices.could_not_create_party_role_write",
110: messageMap, locale);
111: return ServiceUtil.returnError(errMsg);
112: // return ServiceUtil.returnError("Could create party role (write failure): " + e.getMessage());
113: }
114:
115: result.put(ModelService.RESPONSE_MESSAGE,
116: ModelService.RESPOND_SUCCESS);
117: return result;
118: }
119:
120: /**
121: * Deletes a PartyRole
122: *@param ctx The DispatchContext that this service is operating in
123: *@param context Map containing the input parameters
124: *@return Map with the result of the service, the output parameters
125: */
126: public static Map deletePartyRole(DispatchContext ctx, Map context) {
127: Map result = new HashMap();
128: GenericDelegator delegator = ctx.getDelegator();
129: Security security = ctx.getSecurity();
130: GenericValue userLogin = (GenericValue) context
131: .get("userLogin");
132:
133: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
134: security, context, result, "PARTYMGR", "_CREATE");
135: Locale locale = (Locale) context.get("locale");
136: String errMsg = null;
137:
138: if (result.size() > 0)
139: return result;
140:
141: GenericValue partyRole = null;
142:
143: try {
144: partyRole = delegator.findByPrimaryKey("PartyRole",
145: UtilMisc.toMap("partyId", partyId, "roleTypeId",
146: context.get("roleTypeId")));
147: } catch (GenericEntityException e) {
148: Debug.logWarning(e, module);
149: Map messageMap = UtilMisc.toMap("errMessage", e
150: .getMessage());
151: errMsg = UtilProperties
152: .getMessage(
153: resource,
154: "partyroleservices.could_not_delete_party_role_read",
155: messageMap, locale);
156: return ServiceUtil.returnError(errMsg);
157: }
158:
159: if (partyRole == null) {
160: errMsg = UtilProperties
161: .getMessage(
162: resource,
163: "partyroleservices.could_not_delete_party_role_not_found",
164: locale);
165: return ServiceUtil.returnError(errMsg);
166: }
167:
168: try {
169: partyRole.remove();
170: } catch (GenericEntityException e) {
171: Debug.logWarning(e.getMessage(), module);
172: Map messageMap = UtilMisc.toMap("errMessage", e
173: .getMessage());
174: errMsg = UtilProperties
175: .getMessage(
176: resource,
177: "partyroleservices.could_not_delete_party_role_write",
178: messageMap, locale);
179: return ServiceUtil.returnError(errMsg);
180: }
181:
182: result.put(ModelService.RESPONSE_MESSAGE,
183: ModelService.RESPOND_SUCCESS);
184: return result;
185: }
186: }
|