KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: PartyServices.java 7119 2006-03-29 19:22:22Z sichen $
3  *
4  * Copyright (c) 2001, 2002, 2003 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  */

25 package org.ofbiz.party.party;
26
27 import java.sql.Timestamp JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Locale JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import javolution.util.FastList;
37 import org.ofbiz.base.util.Debug;
38 import org.ofbiz.base.util.UtilDateTime;
39 import org.ofbiz.base.util.UtilMisc;
40 import org.ofbiz.base.util.UtilProperties;
41 import org.ofbiz.base.util.UtilValidate;
42 import org.ofbiz.entity.GenericDelegator;
43 import org.ofbiz.entity.GenericEntityException;
44 import org.ofbiz.entity.GenericValue;
45 import org.ofbiz.entity.condition.EntityCondition;
46 import org.ofbiz.entity.condition.EntityConditionList;
47 import org.ofbiz.entity.condition.EntityExpr;
48 import org.ofbiz.entity.condition.EntityFieldValue;
49 import org.ofbiz.entity.condition.EntityFunction;
50 import org.ofbiz.entity.condition.EntityOperator;
51 import org.ofbiz.entity.model.DynamicViewEntity;
52 import org.ofbiz.entity.model.ModelKeyMap;
53 import org.ofbiz.entity.util.EntityFindOptions;
54 import org.ofbiz.entity.util.EntityListIterator;
55 import org.ofbiz.entity.util.EntityTypeUtil;
56 import org.ofbiz.entity.util.EntityUtil;
57 import org.ofbiz.security.Security;
58 import org.ofbiz.service.DispatchContext;
59 import org.ofbiz.service.ModelService;
60 import org.ofbiz.service.ServiceUtil;
61 import org.ofbiz.service.LocalDispatcher;
62 import org.ofbiz.service.GenericServiceException;
63
64 /**
65  * Services for Party/Person/Group maintenance
66  *
67  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
68  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
69  * @version $Rev: 7119 $
70  * @since 2.0
71  */

72 public class PartyServices {
73
74     public static final String JavaDoc module = PartyServices.class.getName();
75     public static final String JavaDoc resource = "PartyUiLabels";
76
77     /**
78      * Deletes a Party.
79      * @param ctx The DispatchContext that this service is operating in.
80      * @param context Map containing the input parameters.
81      * @return Map with the result of the service, the output parameters.
82      */

83     public static Map JavaDoc deleteParty(DispatchContext ctx, Map JavaDoc context) {
84
85         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
86
87         /*
88          * pretty serious operation, would delete:
89          * - Party
90          * - PartyRole
91          * - PartyRelationship: from and to
92          * - PartyDataObject
93          * - Person or PartyGroup
94          * - PartyContactMech, but not ContactMech itself
95          * - PartyContactMechPurpose
96          * - Order?
97          *
98          * 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...
99          */

100
101         String JavaDoc errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_delete_party_not_implemented", locale);
102         return ServiceUtil.returnError(errMsg);
103     }
104
105     /**
106      * Creates a Person.
107      * If no partyId is specified a numeric partyId is retrieved from the Party sequence.
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 createPerson(DispatchContext ctx, Map JavaDoc context) {
113         Map JavaDoc result = new HashMap JavaDoc();
114         GenericDelegator delegator = ctx.getDelegator();
115         Timestamp JavaDoc now = UtilDateTime.nowTimestamp();
116         List JavaDoc toBeStored = new LinkedList JavaDoc();
117         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
118         // in most cases userLogin will be null, but get anyway so we can keep track of that info if it is available
119
GenericValue userLogin = (GenericValue) context.get("userLogin");
120
121         String JavaDoc partyId = (String JavaDoc) context.get("partyId");
122
123         // if specified partyId starts with a number, return an error
124
if (partyId != null && partyId.length() > 0 && Character.isDigit(partyId.charAt(0))) {
125             return ServiceUtil.returnError(UtilProperties.getMessage(resource, "party.id_is_digit", locale));
126         }
127
128         // partyId might be empty, so check it and get next seq party id if empty
129
if (partyId == null || partyId.length() == 0) {
130             try {
131                 partyId = delegator.getNextSeqId("Party");
132             } catch (IllegalArgumentException JavaDoc e) {
133                 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "party.id_generation_failure", locale));
134             }
135         }
136
137         // check to see if party object exists, if so make sure it is PERSON type party
138
GenericValue party = null;
139
140         try {
141             party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId));
142         } catch (GenericEntityException e) {
143             Debug.logWarning(e.getMessage(), module);
144         }
145
146         if (party != null) {
147             if (!"PERSON".equals(party.getString("partyTypeId"))) {
148                 return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.create.party_exists_not_person_type", locale));
149             }
150         } else {
151             // create a party if one doesn't already exist with an initial status from the input
152
String JavaDoc statusId = (String JavaDoc) context.get("statusId");
153             Map JavaDoc newPartyMap = UtilMisc.toMap("partyId", partyId, "partyTypeId", "PERSON", "createdDate", now, "lastModifiedDate", now, "statusId", statusId);
154             if (userLogin != null) {
155                 newPartyMap.put("createdByUserLogin", userLogin.get("userLoginId"));
156                 newPartyMap.put("lastModifiedByUserLogin", userLogin.get("userLoginId"));
157             }
158             party = delegator.makeValue("Party", newPartyMap);
159             toBeStored.add(party);
160         }
161
162         GenericValue person = null;
163
164         try {
165             person = delegator.findByPrimaryKey("Person", UtilMisc.toMap("partyId", partyId));
166         } catch (GenericEntityException e) {
167             Debug.logWarning(e.getMessage(), module);
168         }
169
170         if (person != null) {
171             return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.create.person_exists", locale));
172         }
173
174         person = delegator.makeValue("Person", UtilMisc.toMap("partyId", partyId));
175         person.setNonPKFields(context);
176         toBeStored.add(person);
177
178         try {
179             delegator.storeAll(toBeStored);
180         } catch (GenericEntityException e) {
181             Debug.logWarning(e.getMessage(), module);
182             return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.create.db_error", new Object JavaDoc[] { e.getMessage() }, locale));
183         }
184
185         result.put("partyId", partyId);
186         result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
187         return result;
188     }
189
190     /**
191      * Sets a party status.
192      * <b>security check</b>: userLogin must have permission PARTYMGR_STS_UPDATE and the status change must be defined in StatusValidChange.
193      */

194     public static Map JavaDoc setPartyStatus(DispatchContext ctx, Map JavaDoc context) {
195         Map JavaDoc result = new HashMap JavaDoc();
196         GenericDelegator delegator = ctx.getDelegator();
197         Security security = ctx.getSecurity();
198         GenericValue userLogin = (GenericValue) context.get("userLogin");
199         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
200
201         String JavaDoc partyId = (String JavaDoc) context.get("partyId");
202         String JavaDoc statusId = (String JavaDoc) context.get("statusId");
203         Timestamp JavaDoc statusDate = (Timestamp JavaDoc) context.get("statusDate");
204         if (statusDate == null) statusDate = UtilDateTime.nowTimestamp();
205
206         // userLogin must have PARTYMGR_STS_UPDATE. Also, we aren't letting userLogin with same partyId change his own status.
207
if (!security.hasEntityPermission("PARTYMGR", "_STS_UPDATE", userLogin)) {
208             String JavaDoc errorMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.no_permission_to_operation", locale) + ".";
209             Debug.logWarning(errorMsg, module);
210             return ServiceUtil.returnError(errorMsg);
211         }
212         try {
213             GenericValue party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId));
214
215             // check that status is defined as a valid change
216
GenericValue statusValidChange = delegator.findByPrimaryKey("StatusValidChange", UtilMisc.toMap("statusId", party.getString("statusId"), "statusIdTo", statusId));
217             if (statusValidChange == null) {
218                 String JavaDoc errorMsg = "Cannot change party status from " + party.getString("statusId") + " to " + statusId;
219                 Debug.logWarning(errorMsg, module);
220                 return ServiceUtil.returnError(errorMsg);
221             }
222             
223             // record the oldStatusId and change the party status
224
String JavaDoc oldStatusId = party.getString("statusId");
225             party.set("statusId", statusId);
226             party.store();
227
228             // record this status change in PartyStatus table
229
GenericValue partyStatus = delegator.makeValue("PartyStatus", UtilMisc.toMap("partyId", partyId, "statusId", statusId, "statusDate", statusDate));
230             partyStatus.create();
231
232             Map JavaDoc results = ServiceUtil.returnSuccess();
233             results.put("oldStatusId", oldStatusId);
234             return results;
235         } catch (GenericEntityException e) {
236             Debug.logError(e, e.getMessage(), module);
237             return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.update.write_failure", new Object JavaDoc[] { e.getMessage() }, locale));
238         }
239     }
240
241     /**
242      * Updates a Person.
243      * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_GRP_UPDATE permission.
244      * @param ctx The DispatchContext that this service is operating in.
245      * @param context Map containing the input parameters.
246      * @return Map with the result of the service, the output parameters.
247      */

248     public static Map JavaDoc updatePerson(DispatchContext ctx, Map JavaDoc context) {
249         Map JavaDoc result = new HashMap JavaDoc();
250         GenericDelegator delegator = ctx.getDelegator();
251         Security security = ctx.getSecurity();
252         GenericValue userLogin = (GenericValue) context.get("userLogin");
253         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
254
255         String JavaDoc partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_GRP_UPDATE");
256
257         if (result.size() > 0)
258             return result;
259
260         GenericValue person = null;
261         GenericValue party = null;
262
263         try {
264             person = delegator.findByPrimaryKey("Person", UtilMisc.toMap("partyId", partyId));
265             party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId));
266         } catch (GenericEntityException e) {
267             Debug.logWarning(e, module);
268             return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.update.read_failure", new Object JavaDoc[] { e.getMessage() }, locale));
269         }
270
271         if (person == null || party == null) {
272             return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.update.not_found", locale));
273         }
274
275         person.setNonPKFields(context);
276         party.setNonPKFields(context);
277
278         try {
279             person.store();
280             party.store();
281         } catch (GenericEntityException e) {
282             Debug.logWarning(e.getMessage(), module);
283             return ServiceUtil.returnError(UtilProperties.getMessage(resource, "person.update.write_failure", new Object JavaDoc[] { e.getMessage() }, locale));
284         }
285
286         result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
287         result.put(ModelService.SUCCESS_MESSAGE, UtilProperties.getMessage(resource, "person.update.success", locale));
288         return result;
289     }
290
291     /**
292      * Creates a PartyGroup.
293      * If no partyId is specified a numeric partyId is retrieved from the Party sequence.
294      * @param ctx The DispatchContext that this service is operating in.
295      * @param context Map containing the input parameters.
296      * @return Map with the result of the service, the output parameters.
297      */

298     public static Map JavaDoc createPartyGroup(DispatchContext ctx, Map JavaDoc context) {
299         Map JavaDoc result = new HashMap JavaDoc();
300         GenericDelegator delegator = ctx.getDelegator();
301         GenericValue userLogin = (GenericValue) context.get("userLogin");
302         Timestamp JavaDoc now = UtilDateTime.nowTimestamp();
303
304         String JavaDoc partyId = (String JavaDoc) context.get("partyId");
305         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
306         String JavaDoc errMsg = null;
307
308         // partyId might be empty, so check it and get next seq party id if empty
309
if (partyId == null || partyId.length() == 0) {
310             try {
311                 partyId = delegator.getNextSeqId("Party");
312             } catch (IllegalArgumentException JavaDoc e) {
313                 errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_create_party_group_generation_failure", locale);
314                 return ServiceUtil.returnError(errMsg);
315             }
316         } else {
317             // if specified partyId starts with a number, return an error
318
if (Character.isDigit(partyId.charAt(0))) {
319                 errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_create_party_ID_digit", locale);
320                 return ServiceUtil.returnError(errMsg);
321             }
322         }
323
324         try {
325             // check to see if party object exists, if so make sure it is PARTY_GROUP type party
326
GenericValue party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId));
327             GenericValue partyGroupPartyType = delegator.findByPrimaryKeyCache("PartyType", UtilMisc.toMap("partyTypeId", "PARTY_GROUP"));
328
329             if (partyGroupPartyType == null) {
330                 errMsg = UtilProperties.getMessage(resource,"partyservices.party_type_not_found_in_database_cannot_create_party_group", locale);
331                 return ServiceUtil.returnError(errMsg);
332             }
333
334             if (party != null) {
335                 GenericValue partyType = party.getRelatedOneCache("PartyType");
336
337                 if (!EntityTypeUtil.isType(partyType, partyGroupPartyType)) {
338                     errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_create_party_group_already_exists_not_PARTY_GROUP_type", locale);
339                     return ServiceUtil.returnError(errMsg);
340                 }
341             } else {
342                 // create a party if one doesn't already exist
343
String JavaDoc partyTypeId = "PARTY_GROUP";
344
345                 if (UtilValidate.isNotEmpty(((String JavaDoc) context.get("partyTypeId")))) {
346                     GenericValue desiredPartyType = delegator.findByPrimaryKeyCache("PartyType", UtilMisc.toMap("partyTypeId", context.get("partyTypeId")));
347                     if (desiredPartyType != null && EntityTypeUtil.isType(desiredPartyType, partyGroupPartyType)) {
348                         partyTypeId = desiredPartyType.getString("partyTypeId");
349                     } else {
350                         return ServiceUtil.returnError("The specified partyTypeId [" + context.get("partyTypeId") + "] could not be found or is not a sub-type of PARTY_GROUP");
351                     }
352                 }
353
354                 Map JavaDoc newPartyMap = UtilMisc.toMap("partyId", partyId, "partyTypeId", partyTypeId, "createdDate", now, "lastModifiedDate", now);
355                 if (userLogin != null) {
356                     newPartyMap.put("createdByUserLogin", userLogin.get("userLoginId"));
357                     newPartyMap.put("lastModifiedByUserLogin", userLogin.get("userLoginId"));
358                 }
359                 party = delegator.makeValue("Party", newPartyMap);
360                 party.setNonPKFields(context);
361                 party.create();
362             }
363
364             GenericValue partyGroup = delegator.findByPrimaryKey("PartyGroup", UtilMisc.toMap("partyId", partyId));
365             if (partyGroup != null) {
366                 errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_create_party_group_already_exists", locale);
367                 return ServiceUtil.returnError(errMsg);
368             }
369
370             partyGroup = delegator.makeValue("PartyGroup", UtilMisc.toMap("partyId", partyId));
371             partyGroup.setNonPKFields(context);
372             partyGroup.create();
373         } catch (GenericEntityException e) {
374             Debug.logWarning(e, module);
375             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
376             errMsg = UtilProperties.getMessage(resource,"partyservices.data_source_error_adding_party_group", messageMap, locale);
377             return ServiceUtil.returnError(errMsg);
378         }
379
380         result.put("partyId", partyId);
381         result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
382         return result;
383     }
384
385     /**
386      * Updates a PartyGroup.
387      * @param ctx The DispatchContext that this service is operating in.
388      * @param context Map containing the input parameters.
389      * @return Map with the result of the service, the output parameters.
390      */

391     public static Map JavaDoc updatePartyGroup(DispatchContext ctx, Map JavaDoc context) {
392         Map JavaDoc result = new HashMap JavaDoc();
393         GenericDelegator delegator = ctx.getDelegator();
394         Security security = ctx.getSecurity();
395         GenericValue userLogin = (GenericValue) context.get("userLogin");
396
397         // get the party Id from context if party has permission to update groups, otherwise use getPartyIdCheckSecurity
398
String JavaDoc partyId = null;
399         if (security.hasEntityPermission("PARTYMGR", "_GRP_UPDATE", userLogin)) {
400             partyId = (String JavaDoc) context.get("partyId");
401         } else {
402             partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_UPDATE");
403         }
404         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
405         String JavaDoc errMsg = null;
406
407         if (result.size() > 0)
408             return result;
409
410         GenericValue partyGroup = null;
411         GenericValue party = null;
412
413         try {
414             partyGroup = delegator.findByPrimaryKey("PartyGroup", UtilMisc.toMap("partyId", partyId));
415             party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId));
416         } catch (GenericEntityException e) {
417             Debug.logWarning(e, module);
418             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
419             errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_update_party_information_read", messageMap, locale);
420             return ServiceUtil.returnError(errMsg);
421         }
422
423         if (partyGroup == null || party == null) {
424             errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_update_party_information_not_found", locale);
425             return ServiceUtil.returnError(errMsg);
426         }
427
428         partyGroup.setNonPKFields(context);
429         party.setNonPKFields(context);
430
431         try {
432             partyGroup.store();
433             party.store();
434         } catch (GenericEntityException e) {
435             Debug.logWarning(e.getMessage(), module);
436             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
437             errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_update_party_information_write", messageMap, locale);
438             return ServiceUtil.returnError(errMsg);
439         }
440
441         result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
442         return result;
443     }
444
445     /**
446      * Create an Affiliate entity.
447      * @param ctx The DispatchContext that this service is operating in.
448      * @param context Map containing the input parameters.
449      * @return Map with the result of the service, the output parameters.
450      */

451     public static Map JavaDoc createAffiliate(DispatchContext ctx, Map JavaDoc context) {
452         Map JavaDoc result = new HashMap JavaDoc();
453         GenericDelegator delegator = ctx.getDelegator();
454         GenericValue userLogin = (GenericValue) context.get("userLogin");
455         Timestamp JavaDoc now = UtilDateTime.nowTimestamp();
456
457         String JavaDoc partyId = (String JavaDoc) context.get("partyId");
458         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
459         String JavaDoc errMsg = null;
460
461         if (partyId == null || partyId.length() == 0) {
462             partyId = userLogin.getString("partyId");
463         }
464
465         // if specified partyId starts with a number, return an error
466
if (Character.isDigit(partyId.charAt(0))) {
467             errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_create_affiliate_digit", locale);
468             return ServiceUtil.returnError(errMsg);
469         }
470
471         // partyId might be empty, so check it and get next seq party id if empty
472
if (partyId == null || partyId.length() == 0) {
473             try {
474                 partyId = delegator.getNextSeqId("Party");
475             } catch (IllegalArgumentException JavaDoc e) {
476                 errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_create_affiliate_generation_failure", locale);
477                 return ServiceUtil.returnError(errMsg);
478             }
479         }
480
481         // check to see if party object exists, if so make sure it is AFFILIATE type party
482
GenericValue party = null;
483
484         try {
485             party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId));
486         } catch (GenericEntityException e) {
487             Debug.logWarning(e.getMessage(), module);
488         }
489
490         if (party == null) {
491             errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_create_affiliate_no_party_entity", locale);
492             return ServiceUtil.returnError(errMsg);
493         }
494
495         GenericValue affiliate = null;
496
497         try {
498             affiliate = delegator.findByPrimaryKey("Affiliate", UtilMisc.toMap("partyId", partyId));
499         } catch (GenericEntityException e) {
500             Debug.logWarning(e.getMessage(), module);
501         }
502
503         if (affiliate != null) {
504             errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_create_affiliate_ID_already_exists", locale);
505             return ServiceUtil.returnError(errMsg);
506         }
507
508         affiliate = delegator.makeValue("Affiliate", UtilMisc.toMap("partyId", partyId));
509         affiliate.setNonPKFields(context);
510         affiliate.set("dateTimeCreated", now, false);
511
512         try {
513             delegator.create(affiliate);
514         } catch (GenericEntityException e) {
515             Debug.logWarning(e.getMessage(), module);
516             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
517             errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_add_affiliate_info_write", messageMap, locale);
518             return ServiceUtil.returnError(errMsg);
519         }
520
521         result.put("partyId", partyId);
522         result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
523         return result;
524     }
525
526     /**
527      * Updates an Affiliate.
528      * <b>security check</b>: userLogin partyId must equal partyId, or must have PARTYMGR_UPDATE permission.
529      * @param ctx The DispatchContext that this service is operating in.
530      * @param context Map containing the input parameters.
531      * @return Map with the result of the service, the output parameters.
532      */

533     public static Map JavaDoc updateAffiliate(DispatchContext ctx, Map JavaDoc context) {
534         Map JavaDoc result = new HashMap JavaDoc();
535         GenericDelegator delegator = ctx.getDelegator();
536         Security security = ctx.getSecurity();
537         GenericValue userLogin = (GenericValue) context.get("userLogin");
538
539         String JavaDoc partyId = ServiceUtil.getPartyIdCheckSecurity(userLogin, security, context, result, "PARTYMGR", "_UPDATE");
540         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
541         String JavaDoc errMsg = null;
542
543         if (result.size() > 0)
544             return result;
545
546         GenericValue affiliate = null;
547
548         try {
549             affiliate = delegator.findByPrimaryKey("Affiliate", UtilMisc.toMap("partyId", partyId));
550         } catch (GenericEntityException e) {
551             Debug.logWarning(e, module);
552             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
553             errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_update_affiliate_information_read", messageMap, locale);
554             return ServiceUtil.returnError(errMsg);
555         }
556
557         if (affiliate == null) {
558             errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_update_affiliate_information_not_found", locale);
559             return ServiceUtil.returnError(errMsg);
560         }
561
562         affiliate.setNonPKFields(context);
563
564         try {
565             affiliate.store();
566         } catch (GenericEntityException e) {
567             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
568             errMsg = UtilProperties.getMessage(resource,"partyservices.could_not_update_affiliate_information_write", messageMap, locale);
569             return ServiceUtil.returnError(errMsg);
570         }
571         return ServiceUtil.returnSuccess();
572     }
573
574     /**
575      * Add a PartyNote.
576      * @param dctx The DispatchContext that this service is operating in.
577      * @param context Map containing the input parameters.
578      * @return Map with the result of the service, the output parameters.
579      */

580     public static Map JavaDoc createPartyNote(DispatchContext dctx, Map JavaDoc context) {
581         Map JavaDoc result = new HashMap JavaDoc();
582         GenericDelegator delegator = dctx.getDelegator();
583         LocalDispatcher dispatcher = dctx.getDispatcher();
584         GenericValue userLogin = (GenericValue) context.get("userLogin");
585         String JavaDoc noteString = (String JavaDoc) context.get("note");
586         String JavaDoc partyId = (String JavaDoc) context.get("partyId");
587         String JavaDoc noteId = (String JavaDoc) context.get("noteId");
588         String JavaDoc errMsg = null;
589         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
590         //Map noteCtx = UtilMisc.toMap("note", noteString, "userLogin", userLogin);
591

592         // if no noteId is specified, then create and associate the note with the userLogin
593
if (noteId == null) {
594             Map JavaDoc noteRes = null;
595             try {
596                 noteRes = dispatcher.runSync("createNote", UtilMisc.toMap("partyId", userLogin.getString("partyId"), "note", noteString, "userLogin", userLogin, "locale", locale));
597             } catch (GenericServiceException e) {
598                 Debug.logError(e, e.getMessage(), module);
599                 return ServiceUtil.returnError("Unable to create Note: " + e.getMessage());
600             }
601
602             if (noteRes.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_ERROR))
603                 return noteRes;
604
605             noteId = (String JavaDoc) noteRes.get("noteId");
606
607             if (noteId == null || noteId.length() == 0) {
608                 errMsg = UtilProperties.getMessage(resource,"partyservices.problem_creating_note_no_noteId_returned", locale);
609                 return ServiceUtil.returnError(errMsg);
610             }
611         }
612         result.put("noteId", noteId);
613
614         // Set the party info
615
try {
616             Map JavaDoc fields = UtilMisc.toMap("partyId", partyId, "noteId", noteId);
617             GenericValue v = delegator.makeValue("PartyNote", fields);
618
619             delegator.create(v);
620         } catch (GenericEntityException ee) {
621             Debug.logError(ee, module);
622             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", ee.getMessage());
623             errMsg = UtilProperties.getMessage(resource,"partyservices.problem_associating_note_with_party", messageMap, locale);
624             result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
625             result.put(ModelService.ERROR_MESSAGE, errMsg);
626         }
627         return result;
628     }
629
630     /**
631      * Get the party object(s) from an e-mail address
632      * @param dctx The DispatchContext that this service is operating in.
633      * @param context Map containing the input parameters.
634      * @return Map with the result of the service, the output parameters.
635      */

636     public static Map JavaDoc getPartyFromEmail(DispatchContext dctx, Map JavaDoc context) {
637         Map JavaDoc result = new HashMap JavaDoc();
638         GenericDelegator delegator = dctx.getDelegator();
639         Collection JavaDoc parties = new LinkedList JavaDoc();
640         String JavaDoc email = (String JavaDoc) context.get("email");
641         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
642         String JavaDoc errMsg = null;
643
644         if (email.length() == 0){
645             errMsg = UtilProperties.getMessage(resource,"partyservices.required_parameter_email_cannot_be_empty", locale);
646             return ServiceUtil.returnError(errMsg);
647         }
648
649         try {
650             List JavaDoc exprs = new LinkedList JavaDoc();
651
652             exprs.add(new EntityExpr(new EntityFunction.UPPER(new EntityFieldValue("infoString")), EntityOperator.LIKE, new EntityFunction.UPPER("%" + email.toUpperCase() + "%")));
653             List JavaDoc c = EntityUtil.filterByDate(delegator.findByAnd("PartyAndContactMech", exprs, UtilMisc.toList("infoString")), true);
654
655             if (Debug.verboseOn()) Debug.logVerbose("List: " + c, module);
656             if (Debug.infoOn()) Debug.logInfo("PartyFromEmail number found: " + c.size(), module);
657             if (c != null) {
658                 Iterator JavaDoc i = c.iterator();
659
660                 while (i.hasNext()) {
661                     GenericValue pacm = (GenericValue) i.next();
662                     GenericValue party = delegator.makeValue("Party", UtilMisc.toMap("partyId", pacm.get("partyId"), "partyTypeId", pacm.get("partyTypeId")));
663
664                     parties.add(UtilMisc.toMap("party", party));
665                 }
666             }
667         } catch (GenericEntityException e) {
668             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
669             errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_get_party_entities_read", messageMap, locale);
670             return ServiceUtil.returnError(errMsg);
671         }
672         if (parties.size() > 0)
673             result.put("parties", parties);
674         return result;
675     }
676
677     /**
678      * Get the party object(s) from a user login ID
679      * @param dctx The DispatchContext that this service is operating in.
680      * @param context Map containing the input parameters.
681      * @return Map with the result of the service, the output parameters.
682      */

683     public static Map JavaDoc getPartyFromUserLogin(DispatchContext dctx, Map JavaDoc context) {
684         Debug.logWarning("Running the getPartyFromUserLogin Service...", module);
685         Map JavaDoc result = new HashMap JavaDoc();
686         GenericDelegator delegator = dctx.getDelegator();
687         Collection JavaDoc parties = new LinkedList JavaDoc();
688         String JavaDoc userLoginId = (String JavaDoc) context.get("userLoginId");
689         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
690
691         if (userLoginId.length() == 0)
692             return ServiceUtil.returnError("Required parameter 'userLoginId' cannot be empty.");
693
694         try {
695             List JavaDoc exprs = new LinkedList JavaDoc();
696
697             exprs.add(new EntityExpr(new EntityFunction.UPPER(new EntityFieldValue("userLoginId")), EntityOperator.LIKE, new EntityFunction.UPPER("%" + userLoginId.toUpperCase() + "%")));
698             Collection JavaDoc ulc = delegator.findByAnd("PartyAndUserLogin", exprs, UtilMisc.toList("userloginId"));
699
700             if (Debug.verboseOn()) Debug.logVerbose("Collection: " + ulc, module);
701             if (Debug.infoOn()) Debug.logInfo("PartyFromUserLogin number found: " + ulc.size(), module);
702             if (ulc != null) {
703                 Iterator JavaDoc i = ulc.iterator();
704
705                 while (i.hasNext()) {
706                     GenericValue ul = (GenericValue) i.next();
707                     GenericValue party = delegator.makeValue("Party", UtilMisc.toMap("partyId", ul.get("partyId"), "partyTypeId", ul.get("partyTypeId")));
708
709                     parties.add(UtilMisc.toMap("party", party));
710                 }
711             }
712         } catch (GenericEntityException e) {
713             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
714             String JavaDoc errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_get_party_entities_read", messageMap, locale);
715             return ServiceUtil.returnError(errMsg);
716         }
717         if (parties.size() > 0) {
718             result.put("parties", parties);
719         }
720         return result;
721     }
722
723     /**
724      * Get the party object(s) from person information
725      * @param dctx The DispatchContext that this service is operating in.
726      * @param context Map containing the input parameters.
727      * @return Map with the result of the service, the output parameters.
728      */

729     public static Map JavaDoc getPartyFromPerson(DispatchContext dctx, Map JavaDoc context) {
730         Map JavaDoc result = new HashMap JavaDoc();
731         GenericDelegator delegator = dctx.getDelegator();
732         Collection JavaDoc parties = new LinkedList JavaDoc();
733         String JavaDoc firstName = (String JavaDoc) context.get("firstName");
734         String JavaDoc lastName = (String JavaDoc) context.get("lastName");
735         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
736
737         if (firstName == null) {
738             firstName = "";
739         }
740         if (lastName == null) {
741             lastName = "";
742         }
743         if (firstName.length() == 0 && lastName.length() == 0){
744             String JavaDoc errMsg = UtilProperties.getMessage(resource,"partyservices.both_names_cannot_be_empty", locale);
745             return ServiceUtil.returnError(errMsg);
746         }
747
748         try {
749             List JavaDoc exprs = new LinkedList JavaDoc();
750
751             exprs.add(new EntityExpr(new EntityFunction.UPPER(new EntityFieldValue("firstName")), EntityOperator.LIKE, new EntityFunction.UPPER("%" + firstName.toUpperCase() + "%")));
752             exprs.add(new EntityExpr(new EntityFunction.UPPER(new EntityFieldValue("lastName")), EntityOperator.LIKE, new EntityFunction.UPPER("%" + lastName.toUpperCase() + "%")));
753             Collection JavaDoc pc = delegator.findByAnd("Person", exprs, UtilMisc.toList("lastName", "firstName", "partyId"));
754
755             if (Debug.infoOn()) Debug.logInfo("PartyFromPerson number found: " + pc.size(), module);
756             if (pc != null) {
757                 Iterator JavaDoc i = pc.iterator();
758
759                 while (i.hasNext()) {
760                     GenericValue person = (GenericValue) i.next();
761                     GenericValue party = delegator.makeValue("Party", UtilMisc.toMap("partyId", person.get("partyId"), "partyTypeId", "PERSON"));
762
763                     parties.add(UtilMisc.toMap("person", person, "party", party));
764                 }
765             }
766         } catch (GenericEntityException e) {
767             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
768             String JavaDoc errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_get_party_entities_read", messageMap, locale);
769             return ServiceUtil.returnError(errMsg);
770         }
771         if (parties.size() > 0) {
772             result.put("parties", parties);
773         }
774         return result;
775     }
776
777     /**
778      * Get the party object(s) from party group name.
779      * @param dctx The DispatchContext that this service is operating in.
780      * @param context Map containing the input parameters.
781      * @return Map with the result of the service, the output parameters.
782      */

783     public static Map JavaDoc getPartyFromPartyGroup(DispatchContext dctx, Map JavaDoc context) {
784         Map JavaDoc result = new HashMap JavaDoc();
785         GenericDelegator delegator = dctx.getDelegator();
786         Collection JavaDoc parties = new LinkedList JavaDoc();
787         String JavaDoc groupName = (String JavaDoc) context.get("groupName");
788         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
789
790         if (groupName.length() == 0) {
791             return ServiceUtil.returnError("Required parameter 'groupName' cannot be empty.");
792         }
793
794         try {
795             List JavaDoc exprs = new LinkedList JavaDoc();
796
797             exprs.add(new EntityExpr(new EntityFunction.UPPER(new EntityFieldValue("groupName")), EntityOperator.LIKE, new EntityFunction.UPPER("%" + groupName.toUpperCase() + "%")));
798             Collection JavaDoc pc = delegator.findByAnd("PartyGroup", exprs, UtilMisc.toList("groupName", "partyId"));
799
800             if (Debug.infoOn()) Debug.logInfo("PartyFromGroup number found: " + pc.size(), module);
801             if (pc != null) {
802                 Iterator JavaDoc i = pc.iterator();
803
804                 while (i.hasNext()) {
805                     GenericValue group = (GenericValue) i.next();
806                     GenericValue party = delegator.makeValue("Party", UtilMisc.toMap("partyId", group.get("partyId"), "partyTypeId", "PARTY_GROUP"));
807
808                     parties.add(UtilMisc.toMap("partyGroup", group, "party", party));
809                 }
810             }
811         } catch (GenericEntityException e) {
812             Map JavaDoc messageMap = UtilMisc.toMap("errMessage", e.getMessage());
813             String JavaDoc errMsg = UtilProperties.getMessage(resource,"partyservices.cannot_get_party_entities_read", messageMap, locale);
814             return ServiceUtil.returnError(errMsg);
815         }
816         if (parties.size() > 0) {
817             result.put("parties", parties);
818         }
819         return result;
820     }
821
822     public static Map JavaDoc getPerson(DispatchContext dctx, Map JavaDoc context) {
823         Map JavaDoc result = new HashMap JavaDoc();
824         GenericDelegator delegator = dctx.getDelegator();
825         String JavaDoc partyId = (String JavaDoc) context.get("partyId");
826         GenericValue person = null;
827
828         try {
829             person = delegator.findByPrimaryKeyCache("Person", UtilMisc.toMap("partyId", partyId));
830         } catch (GenericEntityException e) {
831             return ServiceUtil.returnError("Cannot get person entity (read failure): " + e.getMessage());
832         }
833         if (person != null) {
834             result.put("lookupPerson", person);
835         }
836         return result;
837     }
838
839     public static Map JavaDoc createRoleType(DispatchContext dctx, Map JavaDoc context) {
840         Map JavaDoc result = new HashMap JavaDoc();
841         GenericDelegator delegator = dctx.getDelegator();
842         GenericValue roleType = null;
843
844         try {
845             roleType = delegator.makeValue("RoleType", null);
846             roleType.setPKFields(context);
847             roleType.setNonPKFields(context);
848             roleType = delegator.create(roleType);
849         } catch (GenericEntityException e) {
850             Debug.logError(e, module);
851             return ServiceUtil.returnError("Cannot create role type entity (write failure): " + e.getMessage());
852         }
853         if (roleType != null) {
854             result.put("roleType", roleType);
855         }
856         return result;
857     }
858
859     public static Map JavaDoc createPartyDataSource(DispatchContext ctx, Map JavaDoc context) {
860         GenericDelegator delegator = ctx.getDelegator();
861         Security security = ctx.getSecurity();
862         GenericValue userLogin = (GenericValue) context.get("userLogin");
863         Locale JavaDoc locale = (Locale JavaDoc) context.get("locale");
864
865         // input data
866
String JavaDoc partyId = (String JavaDoc) context.get("partyId");
867         String JavaDoc dataSourceId = (String JavaDoc) context.get("dataSourceId");
868         Timestamp JavaDoc fromDate = (Timestamp JavaDoc) context.get("fromDate");
869         if (fromDate == null) fromDate = UtilDateTime.nowTimestamp();
870
871         // userLogin must have PARTYMGR_SRC_CREATE permission
872
if (!security.hasEntityPermission("PARTYMGR", "_SRC_CREATE", userLogin)) {
873             String JavaDoc errorMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.no_permission_to_operation", locale) + ".";
874             return ServiceUtil.returnError(errorMsg);
875         }
876         try {
877             // validate the existance of party and dataSource
878
GenericValue party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId));
879             GenericValue dataSource = delegator.findByPrimaryKey("DataSource", UtilMisc.toMap("dataSourceId", dataSourceId));
880             if (party == null || dataSource == null) {
881                 List JavaDoc errorList = UtilMisc.toList("Cannot create PartyDataSource");
882                 if (party == null) errorList.add("party with ID [" + partyId + "] was not found ");
883                 if (dataSource == null) errorList.add("data source with ID [" + dataSourceId + "] was not found ");
884                 return ServiceUtil.returnError(errorList);
885             }
886
887             // create the PartyDataSource
888
GenericValue partyDataSource = delegator.makeValue("PartyDataSource", UtilMisc.toMap("partyId", partyId, "dataSourceId", dataSourceId, "fromDate", fromDate));
889             partyDataSource.create();
890
891         } catch (GenericEntityException e) {
892             Debug.logError(e, e.getMessage(), module);
893             return ServiceUtil.returnError(e.getMessage());
894         }
895         return ServiceUtil.returnSuccess();
896     }
897
898     public static Map JavaDoc findParty(DispatchContext dctx, Map JavaDoc context) {
899         Map JavaDoc result = ServiceUtil.returnSuccess();
900         GenericDelegator delegator = dctx.getDelegator();
901
902         String JavaDoc extInfo = (String JavaDoc) context.get("extInfo");
903
904         // get the role types
905
try {
906             List JavaDoc roleTypes = delegator.findAll("RoleType", UtilMisc.toList("description"));
907             result.put("roleTypes", roleTypes);
908         } catch (GenericEntityException e) {
909             String JavaDoc errMsg = "Error looking up RoleTypes: " + e.toString();
910             Debug.logError(e, errMsg, module);
911             return ServiceUtil.returnError(errMsg);
912         }
913
914         // current role type
915
String JavaDoc roleTypeId;
916         try {
917             roleTypeId = (String JavaDoc) context.get("roleTypeId");
918             if (roleTypeId != null && roleTypeId.length() > 0) {
919                 GenericValue currentRole = delegator.findByPrimaryKeyCache("RoleType", UtilMisc.toMap("roleTypeId", roleTypeId));
920                 result.put("currentRole", currentRole);
921             }
922         } catch (GenericEntityException e) {
923             String JavaDoc errMsg = "Error looking up current RoleType: " + e.toString();
924             Debug.logError(e, errMsg, module);
925             return ServiceUtil.returnError(errMsg);
926         }
927
928         // current state
929
String JavaDoc stateProvinceGeoId;
930         try {
931             stateProvinceGeoId = (String JavaDoc) context.get("stateProvinceGeoId");
932             if (stateProvinceGeoId != null && stateProvinceGeoId.length() > 0) {
933                 GenericValue currentStateGeo = delegator.findByPrimaryKeyCache("Geo", UtilMisc.toMap("geoId", stateProvinceGeoId));
934                 result.put("currentStateGeo", currentStateGeo);
935             }
936         } catch (GenericEntityException e) {
937             String JavaDoc errMsg = "Error looking up current stateProvinceGeo: " + e.toString();
938             Debug.logError(e, errMsg, module);
939             return ServiceUtil.returnError(errMsg);
940         }
941
942         // set the page parameters
943
int viewIndex = 1;
944         try {
945             viewIndex = Integer.parseInt((String JavaDoc) context.get("VIEW_INDEX"));
946         } catch (Exception JavaDoc e) {
947             viewIndex = 1;
948         }
949         result.put("viewIndex", new Integer JavaDoc(viewIndex));
950
951         int viewSize = 20;
952         try {
953             viewSize = Integer.parseInt((String JavaDoc) context.get("VIEW_SIZE"));
954         } catch (Exception JavaDoc e) {
955             viewSize = 20;
956         }
957         result.put("viewSize", new Integer JavaDoc(viewSize));
958
959         // get the lookup flag
960
String JavaDoc lookupFlag = (String JavaDoc) context.get("lookupFlag");
961
962         // blank param list
963
String JavaDoc paramList = "";
964
965         List JavaDoc partyList = null;
966         int partyListSize = 0;
967         int lowIndex = 0;
968         int highIndex = 0;
969
970         if ("Y".equals(lookupFlag)) {
971             String JavaDoc showAll = (context.get("showAll") != null ? (String JavaDoc) context.get("showAll") : "N");
972             paramList = paramList + "&lookupFlag=" + lookupFlag + "&showAll=" + showAll + "&extInfo=" + extInfo;
973
974             // create the dynamic view entity
975
DynamicViewEntity dynamicView = new DynamicViewEntity();
976
977             // default view settings
978
dynamicView.addMemberEntity("PT", "Party");
979             dynamicView.addAlias("PT", "partyId");
980             dynamicView.addAlias("PT", "partyTypeId");
981             dynamicView.addRelation("one-nofk", "", "PartyType", ModelKeyMap.makeKeyMapList("partyTypeId"));
982             dynamicView.addRelation("many", "", "UserLogin", ModelKeyMap.makeKeyMapList("partyId"));
983
984             // define the main condition & expression list
985
List JavaDoc andExprs = FastList.newInstance();
986             EntityCondition mainCond = null;
987
988             List JavaDoc orderBy = FastList.newInstance();
989             List JavaDoc fieldsToSelect = FastList.newInstance();
990             // fields we need to select; will be used to set distinct
991
fieldsToSelect.add("partyId");
992             fieldsToSelect.add("partyTypeId");
993
994             // get the params
995
String JavaDoc partyId = (String JavaDoc) context.get("partyId");
996             String JavaDoc userLoginId = (String JavaDoc) context.get("userLoginId");
997             String JavaDoc firstName = (String JavaDoc) context.get("firstName");
998             String JavaDoc lastName = (String JavaDoc) context.get("lastName");
999             String JavaDoc groupName = (String JavaDoc) context.get("groupName");
1000
1001            if (!"Y".equals(showAll)) {
1002                // check for a partyId
1003
if (partyId != null && partyId.length() > 0) {
1004                    paramList = paramList + "&partyId=" + partyId;
1005                    andExprs.add(new EntityExpr("partyId", true, EntityOperator.LIKE, "%"+partyId+"%", true));
1006                }
1007
1008                // ----
1009
// UserLogin Fields
1010
// ----
1011

1012                // filter on user login
1013
if (userLoginId != null && userLoginId.length() > 0) {
1014                    paramList = paramList + "&userLoginId=" + userLoginId;
1015
1016                    // modify the dynamic view
1017
dynamicView.addMemberEntity("UL", "UserLogin");
1018                    dynamicView.addAlias("UL", "userLoginId");
1019                    dynamicView.addViewLink("PT", "UL", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
1020
1021                    // add the expr
1022
andExprs.add(new EntityExpr("userLoginId", true, EntityOperator.LIKE, "%"+userLoginId+"%", true));
1023
1024                    fieldsToSelect.add("userLoginId");
1025                }
1026
1027                // ----
1028
// PartyGroup Fields
1029
// ----
1030

1031                // filter on groupName
1032
if (groupName != null && groupName.length() > 0) {
1033                    paramList = paramList + "&groupName=" + groupName;
1034
1035                    // modify the dynamic view
1036
dynamicView.addMemberEntity("PG", "PartyGroup");
1037                    dynamicView.addAlias("PG", "groupName");
1038                    dynamicView.addViewLink("PT", "PG", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
1039
1040                    // add the expr
1041
andExprs.add(new EntityExpr("groupName", true, EntityOperator.LIKE, "%"+groupName+"%", true));
1042                }
1043
1044                // ----
1045
// Person Fields
1046
// ----
1047

1048                // modify the dynamic view
1049
if ((firstName != null && firstName.length() > 0) || (lastName != null && lastName.length() > 0)) {
1050                    dynamicView.addMemberEntity("PE", "Person");
1051                    dynamicView.addAlias("PE", "firstName");
1052                    dynamicView.addAlias("PE", "lastName");
1053                    dynamicView.addViewLink("PT", "PE", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
1054
1055                    fieldsToSelect.add("firstName");
1056                    fieldsToSelect.add("lastName");
1057                    orderBy.add("lastName");
1058                    orderBy.add("firstName");
1059                }
1060
1061                // filter on firstName
1062
if (firstName != null && firstName.length() > 0) {
1063                    paramList = paramList + "&firstName=" + firstName;
1064                    andExprs.add(new EntityExpr("firstName", true, EntityOperator.LIKE, "%"+firstName+"%", true));
1065                }
1066
1067                // filter on lastName
1068
if (lastName != null && lastName.length() > 0) {
1069                    paramList = paramList + "&lastName=" + lastName;
1070                    andExprs.add(new EntityExpr("lastName", true, EntityOperator.LIKE, "%"+lastName+"%", true));
1071                }
1072
1073                // ----
1074
// RoleType Fields
1075
// ----
1076

1077                // filter on role member
1078
if (roleTypeId != null && !"ANY".equals(roleTypeId)) {
1079                    paramList = paramList + "&roleTypeId=" + roleTypeId;
1080
1081                    // add role to view
1082
dynamicView.addMemberEntity("PR", "PartyRole");
1083                    dynamicView.addAlias("PR", "roleTypeId");
1084                    dynamicView.addViewLink("PT", "PR", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
1085
1086                    // add the expr
1087
andExprs.add(new EntityExpr("roleTypeId", EntityOperator.EQUALS, roleTypeId));
1088
1089                    fieldsToSelect.add("roleTypeId");
1090                }
1091
1092                // ----
1093
// PostalAddress fields
1094
// ----
1095
if ("P".equals(extInfo)) {
1096                    // add address to dynamic view
1097
dynamicView.addMemberEntity("PC", "PartyContactMech");
1098                    dynamicView.addMemberEntity("PA", "PostalAddress");
1099                    dynamicView.addAlias("PC", "contactMechId");
1100                    dynamicView.addAlias("PA", "address1");
1101                    dynamicView.addAlias("PA", "address2");
1102                    dynamicView.addAlias("PA", "city");
1103                    dynamicView.addAlias("PA", "stateProvinceGeoId");
1104                    dynamicView.addAlias("PA", "countryGeoId");
1105                    dynamicView.addAlias("PA", "postalCode");
1106                    dynamicView.addViewLink("PT", "PC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
1107                    dynamicView.addViewLink("PC", "PA", Boolean.FALSE, ModelKeyMap.makeKeyMapList("contactMechId"));
1108
1109                    // filter on address1
1110
String JavaDoc address1 = (String JavaDoc) context.get("address1");
1111                    if (address1 != null && address1.length() > 0) {
1112                        paramList = paramList + "&address1=" + address1;
1113                        andExprs.add(new EntityExpr("address1", true, EntityOperator.LIKE, "%" + address1 + "%", true));
1114                    }
1115
1116                    // filter on address2
1117
String JavaDoc address2 = (String JavaDoc) context.get("address2");
1118                    if (address2 != null && address2.length() > 0) {
1119                        paramList = paramList + "&address2=" + address2;
1120                        andExprs.add(new EntityExpr("address2", true, EntityOperator.LIKE, "%" + address2 + "%", true));
1121                    }
1122
1123                    // filter on city
1124
String JavaDoc city = (String JavaDoc) context.get("city");
1125                    if (city != null && city.length() > 0) {
1126                        paramList = paramList + "&city=" + city;
1127                        andExprs.add(new EntityExpr("city", true, EntityOperator.EQUALS, city, true));
1128                    }
1129
1130                    // filter on state geo
1131
if (stateProvinceGeoId != null && !"ANY".equals(stateProvinceGeoId)) {
1132                        paramList = paramList + "&stateProvinceGeoId=" + stateProvinceGeoId;
1133                        andExprs.add(new EntityExpr("stateProvinceGeoId", EntityOperator.EQUALS, stateProvinceGeoId));
1134                    }
1135
1136                    // filter on postal code
1137
String JavaDoc postalCode = (String JavaDoc) context.get("postalCode");
1138                    if (postalCode != null && postalCode.length() > 0) {
1139                        paramList = paramList + "&postalCode=" + postalCode;
1140                        andExprs.add(new EntityExpr("postalCode", true, EntityOperator.LIKE, "%" + postalCode + "%", true));
1141                    }
1142
1143                    fieldsToSelect.add("postalCode");
1144                }
1145
1146                // ----
1147
// Generic CM Fields
1148
// ----
1149
if ("O".equals(extInfo)) {
1150                    // add info to dynamic view
1151
dynamicView.addMemberEntity("PC", "PartyContactMech");
1152                    dynamicView.addMemberEntity("CM", "ContactMech");
1153                    dynamicView.addAlias("PC", "contactMechId");
1154                    dynamicView.addAlias("CM", "infoString");
1155                    dynamicView.addViewLink("PT", "PC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
1156                    dynamicView.addViewLink("PC", "CM", Boolean.FALSE, ModelKeyMap.makeKeyMapList("contactMechId"));
1157
1158                    // filter on infoString
1159
String JavaDoc infoString = (String JavaDoc) context.get("infoString");
1160                    if (infoString != null && infoString.length() > 0) {
1161                        paramList = paramList + "&infoString=" + infoString;
1162                        andExprs.add(new EntityExpr("infoString", true, EntityOperator.LIKE, "%"+infoString+"%", true));
1163                    }
1164
1165                    fieldsToSelect.add("infoString");
1166                }
1167
1168                // ----
1169
// TelecomNumber Fields
1170
// ----
1171
if ("T".equals(extInfo)) {
1172                    // add telecom to dynamic view
1173
dynamicView.addMemberEntity("PC", "PartyContactMech");
1174                    dynamicView.addMemberEntity("TM", "TelecomNumber");
1175                    dynamicView.addAlias("PC", "contactMechId");
1176                    dynamicView.addAlias("TM", "countryCode");
1177                    dynamicView.addAlias("TM", "areaCode");
1178                    dynamicView.addAlias("TM", "contactNumber");
1179                    dynamicView.addViewLink("PT", "PC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
1180                    dynamicView.addViewLink("PC", "TM", Boolean.FALSE, ModelKeyMap.makeKeyMapList("contactMechId"));
1181
1182                    // filter on countryCode
1183
String JavaDoc countryCode = (String JavaDoc) context.get("countryCode");
1184                    if (countryCode != null && countryCode.length() > 0) {
1185                        paramList = paramList + "&countryCode=" + countryCode;
1186                        andExprs.add(new EntityExpr("countryCode", true, EntityOperator.EQUALS, countryCode, true));
1187                    }
1188
1189                    // filter on areaCode
1190
String JavaDoc areaCode = (String JavaDoc) context.get("areaCode");
1191                    if (areaCode != null && areaCode.length() > 0) {
1192                        paramList = paramList + "&areaCode=" + areaCode;
1193                        andExprs.add(new EntityExpr("areaCode", true, EntityOperator.EQUALS, areaCode, true));
1194                    }
1195
1196                    // filter on contact number
1197
String JavaDoc contactNumber = (String JavaDoc) context.get("contactNumber");
1198                    if (contactNumber != null && contactNumber.length() > 0) {
1199                        paramList = paramList + "&contactNumber=" + contactNumber;
1200                        andExprs.add(new EntityExpr("contactNumber", true, EntityOperator.EQUALS, contactNumber, true));
1201                    }
1202
1203                    fieldsToSelect.add("contactNumber");
1204                    fieldsToSelect.add("areaCode");
1205                }
1206
1207                // ---- End of Dynamic View Creation
1208

1209                // build the main condition
1210
if (andExprs.size() > 0) mainCond = new EntityConditionList(andExprs, EntityOperator.AND);
1211            }
1212
1213            Debug.logInfo("In findParty mainCond=" + mainCond, module);
1214
1215            // do the lookup
1216
if (mainCond != null || "Y".equals(showAll)) {
1217                try {
1218                    // set distinct on so we only get one row per order
1219
EntityFindOptions findOpts = new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, true);
1220                    // using list iterator
1221
EntityListIterator pli = delegator.findListIteratorByCondition(dynamicView, mainCond, null, fieldsToSelect, orderBy, findOpts);
1222
1223                    // get the indexes for the partial list
1224
lowIndex = (((viewIndex - 1) * viewSize) + 1);
1225                    highIndex = viewIndex * viewSize;
1226
1227                    // get the partial list for this page
1228
partyList = pli.getPartialList(lowIndex, viewSize);
1229
1230                    // attempt to get the full size
1231
pli.last();
1232                    partyListSize = pli.currentIndex();
1233                    if (highIndex > partyListSize) {
1234                        highIndex = partyListSize;
1235                    }
1236
1237                    // close the list iterator
1238
pli.close();
1239                } catch (GenericEntityException e) {
1240                    String JavaDoc errMsg = "Failure in party find operation, rolling back transaction: " + e.toString();
1241                    Debug.logError(e, errMsg, module);
1242                    return ServiceUtil.returnError(errMsg);
1243                }
1244            } else {
1245                partyListSize = 0;
1246            }
1247        }
1248
1249        if (partyList == null) partyList = FastList.newInstance();
1250        result.put("partyList", partyList);
1251        result.put("partyListSize", new Integer JavaDoc(partyListSize));
1252        result.put("paramList", paramList);
1253        result.put("highIndex", new Integer JavaDoc(highIndex));
1254        result.put("lowIndex", new Integer JavaDoc(lowIndex));
1255
1256        return result;
1257    }
1258}
1259
Popular Tags