KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: PartyRelationshipServices.java 6449 2005-12-30 16:18:57Z 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.sql.Timestamp JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Locale JavaDoc;
30
31 import org.ofbiz.base.util.Debug;
32 import org.ofbiz.base.util.UtilDateTime;
33 import org.ofbiz.base.util.UtilMisc;
34 import org.ofbiz.base.util.UtilProperties;
35 import org.ofbiz.entity.GenericDelegator;
36 import org.ofbiz.entity.GenericEntityException;
37 import org.ofbiz.entity.GenericValue;
38 import org.ofbiz.security.Security;
39 import org.ofbiz.service.DispatchContext;
40 import org.ofbiz.service.ModelService;
41 import org.ofbiz.service.ServiceUtil;
42
43 /**
44  * Services for Party Relationship maintenance
45  *
46  * @author <a HREF="mailto:cworley@chris-n-april.com">Christopher Worley</a>
47  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
48  * @version $Rev: 6449 $
49  * @since 2.0
50  */

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

61     public static Map JavaDoc createPartyRelationship(DispatchContext ctx, Map JavaDoc context) {
62         Map JavaDoc result = new HashMap JavaDoc();
63         GenericDelegator delegator = ctx.getDelegator();
64         Security security = ctx.getSecurity();
65         GenericValue userLogin = (GenericValue) context.get("userLogin");
66
67         // check permission PARTYMGR_REL_CREATE or use ServiceUtil.getPartyIdCheckSecurity to check permission
68
if (!security.hasEntityPermission("PARTYMGR", "_REL_CREATE", userLogin)) {
69             // note: partyId isn't used after this, but it might in the future. for now, it is used only to check the security
70
String JavaDoc 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         String JavaDoc partyIdFrom = (String JavaDoc) context.get("partyIdFrom");
79         if (partyIdFrom == null) {
80             partyIdFrom = (String JavaDoc) userLogin.getString("partyId");
81         }
82
83         String JavaDoc partyIdTo = (String JavaDoc) context.get("partyIdTo");
84         if (partyIdTo == null) {
85 // Map messageMap = UtilMisc.toMap("errProductFeatures", e.getMessage());
86
errMsg = UtilProperties.getMessage(resource,"partyrelationshipservices.cannot_create_party_relationship_partyIdTo_null", locale);
87             return ServiceUtil.returnError(errMsg);
88         }
89
90         String JavaDoc roleTypeIdFrom = (String JavaDoc) context.get("roleTypeIdFrom");
91         if (roleTypeIdFrom == null) {
92             roleTypeIdFrom = "_NA_";
93         }
94
95         String JavaDoc roleTypeIdTo = (String JavaDoc) context.get("roleTypeIdTo");
96         if (roleTypeIdTo == null) {
97             roleTypeIdTo = "_NA_";
98         }
99
100         Timestamp JavaDoc fromDate = (Timestamp JavaDoc) context.get("fromDate");
101         if (fromDate == null) {
102             fromDate = UtilDateTime.nowTimestamp();
103         }
104
105         try {
106             if (delegator.findByPrimaryKey("PartyRole", UtilMisc.toMap("partyId", partyIdFrom, "roleTypeId", roleTypeIdFrom)) == null) {
107                 errMsg = UtilProperties.getMessage(resource,"partyrelationshipservices.cannot_create_party_relationship_partyIdFrom_not_in_role", locale);
108                 return ServiceUtil.returnError(errMsg);
109             }
110
111             if (delegator.findByPrimaryKey("PartyRole", UtilMisc.toMap("partyId", partyIdTo, "roleTypeId", roleTypeIdTo)) == null) {
112                 errMsg = UtilProperties.getMessage(resource,"partyrelationshipservices.cannot_create_party_relationship_partyIdTo_not_in_role", locale);
113                 return ServiceUtil.returnError(errMsg);
114             }
115         } catch (GenericEntityException e) {
116             Debug.logWarning(e, module);
117             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
118             errMsg = UtilProperties.getMessage(resource,"partyrelationshipservices.could_not_create_party_role_read", messageMap, locale);
119             return ServiceUtil.returnError(errMsg);
120         }
121
122         GenericValue partyRelationship = delegator.makeValue("PartyRelationship", UtilMisc.toMap("partyIdFrom", partyIdFrom, "partyIdTo", partyIdTo, "roleTypeIdFrom", roleTypeIdFrom, "roleTypeIdTo", roleTypeIdTo, "fromDate", fromDate));
123         partyRelationship.setNonPKFields(context);
124
125         try {
126             if (delegator.findByPrimaryKey(partyRelationship.getPrimaryKey()) != null) {
127                 errMsg = UtilProperties.getMessage(resource,"partyrelationshipservices.could_not_create_party_role_exists", locale);
128                 return ServiceUtil.returnError(errMsg);
129             }
130         } catch (GenericEntityException e) {
131             Debug.logWarning(e, module);
132             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
133             errMsg = UtilProperties.getMessage(resource,"partyrelationshipservices.could_not_create_party_role_read", messageMap, locale);
134             return ServiceUtil.returnError(errMsg);
135         }
136
137         try {
138             partyRelationship.create();
139         } catch (GenericEntityException e) {
140             Debug.logWarning(e.getMessage(), module);
141             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
142             errMsg = UtilProperties.getMessage(resource,"partyrelationshipservices.could_not_create_party_relationship_write", messageMap, locale);
143             return ServiceUtil.returnError(errMsg);
144         }
145
146         result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
147         return result;
148     }
149
150     /** Updates a PartyRelationship
151      *@param ctx The DispatchContext that this service is operating in
152      *@param context Map containing the input parameters
153      *@return Map with the result of the service, the output parameters
154      */

155     public static Map JavaDoc updatePartyRelationship(DispatchContext ctx, Map JavaDoc context) {
156         Map JavaDoc result = new HashMap JavaDoc();
157         GenericDelegator delegator = ctx.getDelegator();
158         Security security = ctx.getSecurity();
159         GenericValue userLogin = (GenericValue) context.get("userLogin");
160
161         // check permission PARTYMGR_REL_UPDATE or use ServiceUtil.getPartyIdCheckSecurity to check permission
162
if (!security.hasEntityPermission("PARTYMGR", "_REL_UPDATE", userLogin)) {
163             // note: partyId isn't used after this, but it might in the future. for now, it is used only to check the security
164
String JavaDoc partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_UPDATE");
165         }
166         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
167         String JavaDoc errMsg = null;
168
169         if (result.size() > 0)
170             return result;
171
172         String JavaDoc partyIdFrom = (String JavaDoc) context.get("partyIdFrom");
173
174         if (partyIdFrom == null) {
175             partyIdFrom = (String JavaDoc) userLogin.getString("partyId");
176         }
177
178         String JavaDoc partyIdTo = (String JavaDoc) context.get("partyIdTo");
179
180         if (partyIdTo == null) {
181             errMsg = UtilProperties.getMessage(resource,"partyrelationshipservices.cannot_create_party_relationship_partyIdTo_null", locale);
182             return ServiceUtil.returnError(errMsg);
183         }
184
185         String JavaDoc roleTypeIdFrom = (String JavaDoc) context.get("roleTypeIdFrom");
186
187         if (roleTypeIdFrom == null) {
188             roleTypeIdFrom = "_NA_";
189         }
190
191         String JavaDoc roleTypeIdTo = (String JavaDoc) context.get("roleTypeIdTo");
192
193         if (roleTypeIdTo == null) {
194             roleTypeIdTo = "_NA_";
195         }
196
197         GenericValue partyRelationship = null;
198         try {
199             partyRelationship = delegator.findByPrimaryKey("PartyRelationship", UtilMisc.toMap("partyIdFrom", partyIdFrom,
200                             "partyIdTo", partyIdTo, "roleTypeIdFrom", roleTypeIdFrom, "roleTypeIdTo", roleTypeIdTo, "fromDate", context.get("fromDate")));
201         } catch (GenericEntityException e) {
202             Debug.logWarning(e, module);
203             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
204             errMsg = UtilProperties.getMessage(resource,"partyrelationshipservices.could_not_update_party_relation_read", messageMap, locale);
205             return ServiceUtil.returnError(errMsg);
206         }
207
208         if (partyRelationship == null) {
209             errMsg = UtilProperties.getMessage(resource,"partyrelationshipservices.could_not_update_party_relation_not_found", locale);
210             return ServiceUtil.returnError(errMsg);
211         }
212
213         partyRelationship.setNonPKFields(context);
214
215         try {
216             partyRelationship.store();
217         } catch (GenericEntityException e) {
218             Debug.logWarning(e.getMessage(), module);
219             errMsg = UtilProperties.getMessage(resource,"partyrelationshipservices.could_not_update_party_relation_write", locale);
220             return ServiceUtil.returnError(errMsg);
221         }
222
223         result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
224         return result;
225     }
226
227     /** Deletes a PartyRelationship
228      *@param ctx The DispatchContext that this service is operating in
229      *@param context Map containing the input parameters
230      *@return Map with the result of the service, the output parameters
231      */

232     public static Map JavaDoc deletePartyRelationship(DispatchContext ctx, Map JavaDoc context) {
233         Map JavaDoc result = new HashMap JavaDoc();
234         GenericDelegator delegator = ctx.getDelegator();
235         Security security = ctx.getSecurity();
236         GenericValue userLogin = (GenericValue) context.get("userLogin");
237
238         if (!security.hasEntityPermission("PARTYMGR", "_REL_DELETE", userLogin)) {
239             // note: partyId isn't used after this, but it might in the future. for now, it is used only to check the security
240
String JavaDoc partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_DELETE");
241         }
242
243         if (result.size() > 0)
244             return result;
245
246         GenericValue partyRelationship = null;
247
248         try {
249             partyRelationship = delegator.findByPrimaryKey("PartyRelationship", UtilMisc.toMap("partyIdFrom", context.get("partyIdFrom"), "partyIdTo", context.get("partyIdTo"), "roleTypeIdFrom", context.get("roleTypeIdFrom"), "roleTypeIdTo", context.get("roleTypeIdTo"), "fromDate", context.get("fromDate")));
250         } catch (GenericEntityException e) {
251             Debug.logWarning(e, module);
252             return ServiceUtil.returnError("Could not delete party relationship (read failure): " + e.getMessage());
253         }
254
255         if (partyRelationship == null) {
256             return ServiceUtil.returnError("Could not delete party relationship (partyRelationship not found)");
257         }
258
259         try {
260             partyRelationship.remove();
261         } catch (GenericEntityException e) {
262             Debug.logWarning(e.getMessage(), module);
263             return ServiceUtil.returnError("Could delete party role (write failure): " + e.getMessage());
264         }
265
266         result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
267         return result;
268     }
269
270     /** Creates a PartyRelationshipType
271      *@param ctx The DispatchContext that this service is operating in
272      *@param context Map containing the input parameters
273      *@return Map with the result of the service, the output parameters
274      */

275     public static Map JavaDoc createPartyRelationshipType(DispatchContext ctx, Map JavaDoc context) {
276         Map JavaDoc result = new HashMap JavaDoc();
277         GenericDelegator delegator = ctx.getDelegator();
278         Security security = ctx.getSecurity();
279         GenericValue userLogin = (GenericValue) context.get("userLogin");
280
281         String JavaDoc partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_CREATE");
282
283         if (result.size() > 0)
284             return result;
285
286         GenericValue partyRelationshipType = delegator.makeValue("PartyRelationshipType", UtilMisc.toMap("partyRelationshipTypeId", context.get("partyRelationshipTypeId")));
287
288         partyRelationshipType.set("parentTypeId", context.get("parentTypeId"), false);
289         partyRelationshipType.set("hasTable", context.get("hasTable"), false);
290         partyRelationshipType.set("roleTypeIdValidFrom", context.get("roleTypeIdValidFrom"), false);
291         partyRelationshipType.set("roleTypeIdValidTo", context.get("roleTypeIdValidTo"), false);
292         partyRelationshipType.set("description", context.get("description"), false);
293         partyRelationshipType.set("partyRelationshipName", context.get("partyRelationshipName"), false);
294
295         try {
296             if (delegator.findByPrimaryKey(partyRelationshipType.getPrimaryKey()) != null) {
297                 return ServiceUtil.returnError("Could not create party relationship type: already exists");
298             }
299         } catch (GenericEntityException e) {
300             Debug.logWarning(e, module);
301             return ServiceUtil.returnError("Could not create party relationship type (read failure): " + e.getMessage());
302         }
303
304         try {
305             partyRelationshipType.create();
306         } catch (GenericEntityException e) {
307             Debug.logWarning(e.getMessage(), module);
308             return ServiceUtil.returnError("Could not create party relationship type (write failure): " + e.getMessage());
309         }
310
311         result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
312         return result;
313     }
314
315 }
316
Popular Tags