KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > party > party > PartyRoleServices


1 /*
2  * $Id: PartyRoleServices.java 6412 2005-12-22 19:26:15Z sichen $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.party.party;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Locale JavaDoc;
29
30 import org.ofbiz.base.util.Debug;
31 import org.ofbiz.base.util.UtilMisc;
32 import org.ofbiz.base.util.UtilProperties;
33 import org.ofbiz.entity.GenericDelegator;
34 import org.ofbiz.entity.GenericEntityException;
35 import org.ofbiz.entity.GenericValue;
36 import org.ofbiz.security.Security;
37 import org.ofbiz.service.DispatchContext;
38 import org.ofbiz.service.ModelService;
39 import org.ofbiz.service.ServiceUtil;
40
41 /**
42  * Services for Party Role maintenance
43  *
44  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
45  * @version $Rev: 6412 $
46  * @since 2.0
47  */

48 public class PartyRoleServices {
49     
50     public static final String JavaDoc module = PartyRoleServices.class.getName();
51     public static final String JavaDoc resource = "PartyUiLabels";
52
53     /**
54      * Creates a PartyRole
55      *@param ctx The DispatchContext that this service is operating in
56      *@param context Map containing the input parameters
57      *@return Map with the result of the service, the output parameters
58      */

59     public static Map JavaDoc createPartyRole(DispatchContext ctx, Map JavaDoc context) {
60         Map JavaDoc result = new HashMap JavaDoc();
61         GenericDelegator delegator = ctx.getDelegator();
62         Security security = ctx.getSecurity();
63         GenericValue userLogin = (GenericValue) context.get("userLogin");
64
65         // check permission PARTYMGR_ROLE_CREATE or use ServiceUtil.getPartyIdCheckSecurity to check permission
66
String JavaDoc partyId = null;
67         if (security.hasEntityPermission("PARTYMGR", "_ROLE_CREATE", userLogin)) {
68             partyId = (String JavaDoc) context.get("partyId");
69         } else {
70             partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_CREATE");
71         }
72         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
73         String JavaDoc errMsg = null;
74
75         if (result.size() > 0)
76             return result;
77
78         GenericValue partyRole = delegator.makeValue("PartyRole", UtilMisc.toMap("partyId", partyId, "roleTypeId", context.get("roleTypeId")));
79
80         try {
81             if (delegator.findByPrimaryKey(partyRole.getPrimaryKey()) != null) {
82                 errMsg = UtilProperties.getMessage(resource,"partyroleservices.could_not_create_party_role_exists", locale);
83                 return ServiceUtil.returnError(errMsg);
84             }
85         } catch (GenericEntityException e) {
86             Debug.logWarning(e, module);
87             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
88             errMsg = UtilProperties.getMessage(resource,"partyroleservices.could_not_create_party_role_read", messageMap, locale);
89             return ServiceUtil.returnError(errMsg);
90         }
91
92         try {
93             partyRole.create();
94         } catch (GenericEntityException e) {
95             Debug.logWarning(e.getMessage(), module);
96             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
97             errMsg = UtilProperties.getMessage(resource,"partyroleservices.could_not_create_party_role_write", messageMap, locale);
98             return ServiceUtil.returnError(errMsg);
99 // return ServiceUtil.returnError("Could create party role (write failure): " + e.getMessage());
100
}
101
102         result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
103         return result;
104     }
105
106     /**
107      * Deletes a PartyRole
108      *@param ctx The DispatchContext that this service is operating in
109      *@param context Map containing the input parameters
110      *@return Map with the result of the service, the output parameters
111      */

112     public static Map JavaDoc deletePartyRole(DispatchContext ctx, Map JavaDoc context) {
113         Map JavaDoc result = new HashMap JavaDoc();
114         GenericDelegator delegator = ctx.getDelegator();
115         Security security = ctx.getSecurity();
116         GenericValue userLogin = (GenericValue) context.get("userLogin");
117
118         // check permission PARTYMGR_ROLE_DELETE or use ServiceUtil.getPartyIdCheckSecurity to check permission
119
String JavaDoc partyId = null;
120         if (security.hasEntityPermission("PARTYMGR", "_ROLE_DELETE", userLogin)) {
121             partyId = (String JavaDoc) context.get("partyId");
122         } else {
123             partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_CREATE");
124         }
125         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
126         String JavaDoc errMsg = null;
127
128         if (result.size() > 0)
129             return result;
130
131         GenericValue partyRole = null;
132
133         try {
134             partyRole = delegator.findByPrimaryKey("PartyRole", UtilMisc.toMap("partyId", partyId, "roleTypeId", context.get("roleTypeId")));
135         } catch (GenericEntityException e) {
136             Debug.logWarning(e, module);
137             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
138             errMsg = UtilProperties.getMessage(resource,"partyroleservices.could_not_delete_party_role_read", messageMap, locale);
139             return ServiceUtil.returnError(errMsg);
140         }
141
142         if (partyRole == null) {
143             errMsg = UtilProperties.getMessage(resource,"partyroleservices.could_not_delete_party_role_not_found", locale);
144             return ServiceUtil.returnError(errMsg);
145         }
146
147         try {
148             partyRole.remove();
149         } catch (GenericEntityException e) {
150             Debug.logWarning(e.getMessage(), module);
151             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
152             errMsg = UtilProperties.getMessage(resource,"partyroleservices.could_not_delete_party_role_write", messageMap, locale);
153             return ServiceUtil.returnError(errMsg);
154         }
155
156         result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
157         return result;
158     }
159 }
160
Popular Tags