001: /*
002: * $Id: PartyRelationshipServices.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.sql.Timestamp;
027: import java.util.HashMap;
028: import java.util.Map;
029: import java.util.Locale;
030:
031: import org.ofbiz.base.util.Debug;
032: import org.ofbiz.base.util.UtilDateTime;
033: import org.ofbiz.base.util.UtilMisc;
034: import org.ofbiz.base.util.UtilProperties;
035: import org.ofbiz.entity.GenericDelegator;
036: import org.ofbiz.entity.GenericEntityException;
037: import org.ofbiz.entity.GenericValue;
038: import org.ofbiz.security.Security;
039: import org.ofbiz.service.DispatchContext;
040: import org.ofbiz.service.ModelService;
041: import org.ofbiz.service.ServiceUtil;
042:
043: /**
044: * Services for Party Relationship maintenance
045: *
046: * @author <a href="mailto:cworley@chris-n-april.com">Christopher Worley</a>
047: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
048: * @version $Revision: 1.2 $
049: * @since 2.0
050: */
051: public class PartyRelationshipServices {
052:
053: public static final String module = PartyRelationshipServices.class
054: .getName();
055: public static final String resource = "PartyUiLabels";
056:
057: /** Creates a PartyRelationship
058: *@param ctx The DispatchContext that this service is operating in
059: *@param context Map containing the input parameters
060: *@return Map with the result of the service, the output parameters
061: */
062: public static Map createPartyRelationship(DispatchContext ctx,
063: Map context) {
064: Map result = new HashMap();
065: GenericDelegator delegator = ctx.getDelegator();
066: Security security = ctx.getSecurity();
067: GenericValue userLogin = (GenericValue) context
068: .get("userLogin");
069:
070: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
071: security, context, result, "PARTYMGR", "_CREATE");
072: Locale locale = (Locale) context.get("locale");
073: String errMsg = null;
074:
075: if (result.size() > 0)
076: return result;
077:
078: String partyIdFrom = (String) context.get("partyIdFrom");
079: if (partyIdFrom == null) {
080: partyIdFrom = (String) userLogin.getString("partyId");
081: }
082:
083: String partyIdTo = (String) context.get("partyIdTo");
084: if (partyIdTo == null) {
085: // Map messageMap = UtilMisc.toMap("errProductFeatures", e.getMessage());
086: errMsg = UtilProperties
087: .getMessage(
088: resource,
089: "partyrelationshipservices.cannot_create_party_relationship_partyIdTo_null",
090: locale);
091: return ServiceUtil.returnError(errMsg);
092: }
093:
094: String roleTypeIdFrom = (String) context.get("roleTypeIdFrom");
095: if (roleTypeIdFrom == null) {
096: roleTypeIdFrom = "_NA_";
097: }
098:
099: String roleTypeIdTo = (String) context.get("roleTypeIdTo");
100: if (roleTypeIdTo == null) {
101: roleTypeIdTo = "_NA_";
102: }
103:
104: Timestamp fromDate = (Timestamp) context.get("fromDate");
105: if (fromDate == null) {
106: fromDate = UtilDateTime.nowTimestamp();
107: }
108:
109: try {
110: if (delegator.findByPrimaryKey("PartyRole", UtilMisc.toMap(
111: "partyId", partyIdFrom, "roleTypeId",
112: roleTypeIdFrom)) == null) {
113: errMsg = UtilProperties
114: .getMessage(
115: resource,
116: "partyrelationshipservices.cannot_create_party_relationship_partyIdFrom_not_in_role",
117: locale);
118: return ServiceUtil.returnError(errMsg);
119: }
120:
121: if (delegator.findByPrimaryKey("PartyRole", UtilMisc.toMap(
122: "partyId", partyIdTo, "roleTypeId", roleTypeIdTo)) == null) {
123: errMsg = UtilProperties
124: .getMessage(
125: resource,
126: "partyrelationshipservices.cannot_create_party_relationship_partyIdTo_not_in_role",
127: locale);
128: return ServiceUtil.returnError(errMsg);
129: }
130: } catch (GenericEntityException e) {
131: Debug.logWarning(e, module);
132: Map messageMap = UtilMisc.toMap("errMessage", e
133: .getMessage());
134: errMsg = UtilProperties
135: .getMessage(
136: resource,
137: "partyrelationshipservices.could_not_create_party_role_read",
138: messageMap, locale);
139: return ServiceUtil.returnError(errMsg);
140: }
141:
142: GenericValue partyRelationship = delegator.makeValue(
143: "PartyRelationship", UtilMisc.toMap("partyIdFrom",
144: partyIdFrom, "partyIdTo", partyIdTo,
145: "roleTypeIdFrom", roleTypeIdFrom,
146: "roleTypeIdTo", roleTypeIdTo, "fromDate",
147: fromDate));
148: partyRelationship.setNonPKFields(context);
149:
150: try {
151: if (delegator.findByPrimaryKey(partyRelationship
152: .getPrimaryKey()) != null) {
153: errMsg = UtilProperties
154: .getMessage(
155: resource,
156: "partyrelationshipservices.could_not_create_party_role_exists",
157: locale);
158: return ServiceUtil.returnError(errMsg);
159: }
160: } catch (GenericEntityException e) {
161: Debug.logWarning(e, module);
162: Map messageMap = UtilMisc.toMap("errMessage", e
163: .getMessage());
164: errMsg = UtilProperties
165: .getMessage(
166: resource,
167: "partyrelationshipservices.could_not_create_party_role_read",
168: messageMap, locale);
169: return ServiceUtil.returnError(errMsg);
170: }
171:
172: try {
173: partyRelationship.create();
174: } catch (GenericEntityException e) {
175: Debug.logWarning(e.getMessage(), module);
176: Map messageMap = UtilMisc.toMap("errMessage", e
177: .getMessage());
178: errMsg = UtilProperties
179: .getMessage(
180: resource,
181: "partyrelationshipservices.could_not_create_party_relationship_write",
182: messageMap, locale);
183: return ServiceUtil.returnError(errMsg);
184: }
185:
186: result.put(ModelService.RESPONSE_MESSAGE,
187: ModelService.RESPOND_SUCCESS);
188: return result;
189: }
190:
191: /** Updates a PartyRelationship
192: *@param ctx The DispatchContext that this service is operating in
193: *@param context Map containing the input parameters
194: *@return Map with the result of the service, the output parameters
195: */
196: public static Map updatePartyRelationship(DispatchContext ctx,
197: Map context) {
198: Map result = new HashMap();
199: GenericDelegator delegator = ctx.getDelegator();
200: Security security = ctx.getSecurity();
201: GenericValue userLogin = (GenericValue) context
202: .get("userLogin");
203:
204: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
205: security, context, result, "PARTYMGR", "_UPDATE");
206: Locale locale = (Locale) context.get("locale");
207: String errMsg = null;
208:
209: if (result.size() > 0)
210: return result;
211:
212: String partyIdFrom = (String) context.get("partyIdFrom");
213:
214: if (partyIdFrom == null) {
215: partyIdFrom = (String) userLogin.getString("partyId");
216: }
217:
218: String partyIdTo = (String) context.get("partyIdTo");
219:
220: if (partyIdTo == null) {
221: errMsg = UtilProperties
222: .getMessage(
223: resource,
224: "partyrelationshipservices.cannot_create_party_relationship_partyIdTo_null",
225: locale);
226: return ServiceUtil.returnError(errMsg);
227: }
228:
229: String roleTypeIdFrom = (String) context.get("roleTypeIdFrom");
230:
231: if (roleTypeIdFrom == null) {
232: roleTypeIdFrom = "_NA_";
233: }
234:
235: String roleTypeIdTo = (String) context.get("roleTypeIdTo");
236:
237: if (roleTypeIdTo == null) {
238: roleTypeIdTo = "_NA_";
239: }
240:
241: GenericValue partyRelationship = null;
242: try {
243: partyRelationship = delegator.findByPrimaryKey(
244: "PartyRelationship", UtilMisc.toMap("partyIdFrom",
245: partyIdFrom, "partyIdTo", partyIdTo,
246: "roleTypeIdFrom", roleTypeIdFrom,
247: "roleTypeIdTo", roleTypeIdTo, "fromDate",
248: context.get("fromDate")));
249: } catch (GenericEntityException e) {
250: Debug.logWarning(e, module);
251: Map messageMap = UtilMisc.toMap("errMessage", e
252: .getMessage());
253: errMsg = UtilProperties
254: .getMessage(
255: resource,
256: "partyrelationshipservices.could_not_update_party_relation_read",
257: messageMap, locale);
258: return ServiceUtil.returnError(errMsg);
259: }
260:
261: if (partyRelationship == null) {
262: errMsg = UtilProperties
263: .getMessage(
264: resource,
265: "partyrelationshipservices.could_not_update_party_relation_not_found",
266: locale);
267: return ServiceUtil.returnError(errMsg);
268: }
269:
270: partyRelationship.setNonPKFields(context);
271:
272: try {
273: partyRelationship.store();
274: } catch (GenericEntityException e) {
275: Debug.logWarning(e.getMessage(), module);
276: errMsg = UtilProperties
277: .getMessage(
278: resource,
279: "partyrelationshipservices.could_not_update_party_relation_write",
280: locale);
281: return ServiceUtil.returnError(errMsg);
282: }
283:
284: result.put(ModelService.RESPONSE_MESSAGE,
285: ModelService.RESPOND_SUCCESS);
286: return result;
287: }
288:
289: /** Deletes a PartyRelationship
290: *@param ctx The DispatchContext that this service is operating in
291: *@param context Map containing the input parameters
292: *@return Map with the result of the service, the output parameters
293: */
294: public static Map deletePartyRelationship(DispatchContext ctx,
295: Map context) {
296: Map result = new HashMap();
297: GenericDelegator delegator = ctx.getDelegator();
298: Security security = ctx.getSecurity();
299: GenericValue userLogin = (GenericValue) context
300: .get("userLogin");
301:
302: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
303: security, context, result, "PARTYMGR", "_CREATE");
304:
305: if (result.size() > 0)
306: return result;
307:
308: GenericValue partyRelationship = null;
309:
310: try {
311: partyRelationship = delegator.findByPrimaryKey(
312: "PartyRelationship", UtilMisc.toMap("partyIdFrom",
313: context.get("partyIdFrom"), "partyIdTo",
314: context.get("partyIdTo"), "roleTypeIdFrom",
315: context.get("roleTypeIdFrom"),
316: "roleTypeIdTo",
317: context.get("roleTypeIdTo"), "fromDate",
318: context.get("fromDate")));
319: } catch (GenericEntityException e) {
320: Debug.logWarning(e, module);
321: return ServiceUtil
322: .returnError("Could not delete party relationship (read failure): "
323: + e.getMessage());
324: }
325:
326: if (partyRelationship == null) {
327: return ServiceUtil
328: .returnError("Could not delete party relationship (partyRelationship not found)");
329: }
330:
331: try {
332: partyRelationship.remove();
333: } catch (GenericEntityException e) {
334: Debug.logWarning(e.getMessage(), module);
335: return ServiceUtil
336: .returnError("Could delete party role (write failure): "
337: + e.getMessage());
338: }
339:
340: result.put(ModelService.RESPONSE_MESSAGE,
341: ModelService.RESPOND_SUCCESS);
342: return result;
343: }
344:
345: /** Creates a PartyRelationshipType
346: *@param ctx The DispatchContext that this service is operating in
347: *@param context Map containing the input parameters
348: *@return Map with the result of the service, the output parameters
349: */
350: public static Map createPartyRelationshipType(DispatchContext ctx,
351: Map context) {
352: Map result = new HashMap();
353: GenericDelegator delegator = ctx.getDelegator();
354: Security security = ctx.getSecurity();
355: GenericValue userLogin = (GenericValue) context
356: .get("userLogin");
357:
358: String partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin,
359: security, context, result, "PARTYMGR", "_CREATE");
360:
361: if (result.size() > 0)
362: return result;
363:
364: GenericValue partyRelationshipType = delegator.makeValue(
365: "PartyRelationshipType", UtilMisc.toMap(
366: "partyRelationshipTypeId", context
367: .get("partyRelationshipTypeId")));
368:
369: partyRelationshipType.set("parentTypeId", context
370: .get("parentTypeId"), false);
371: partyRelationshipType.set("hasTable", context.get("hasTable"),
372: false);
373: partyRelationshipType.set("roleTypeIdValidFrom", context
374: .get("roleTypeIdValidFrom"), false);
375: partyRelationshipType.set("roleTypeIdValidTo", context
376: .get("roleTypeIdValidTo"), false);
377: partyRelationshipType.set("description", context
378: .get("description"), false);
379: partyRelationshipType.set("partyRelationshipName", context
380: .get("partyRelationshipName"), false);
381:
382: try {
383: if (delegator.findByPrimaryKey(partyRelationshipType
384: .getPrimaryKey()) != null) {
385: return ServiceUtil
386: .returnError("Could not create party relationship type: already exists");
387: }
388: } catch (GenericEntityException e) {
389: Debug.logWarning(e, module);
390: return ServiceUtil
391: .returnError("Could not create party relationship type (read failure): "
392: + e.getMessage());
393: }
394:
395: try {
396: partyRelationshipType.create();
397: } catch (GenericEntityException e) {
398: Debug.logWarning(e.getMessage(), module);
399: return ServiceUtil
400: .returnError("Could not create party relationship type (write failure): "
401: + e.getMessage());
402: }
403:
404: result.put(ModelService.RESPONSE_MESSAGE,
405: ModelService.RESPOND_SUCCESS);
406: return result;
407: }
408:
409: }
|