KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > party > contact > ContactMechWorker


1 /*
2  * $Id: ContactMechWorker.java 6379 2005-12-19 17:31:44Z jaz $
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.contact;
25
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.servlet.ServletRequest JavaDoc;
34 import javax.servlet.jsp.PageContext JavaDoc;
35
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.base.util.UtilMisc;
38 import org.ofbiz.base.util.UtilProperties;
39 import org.ofbiz.base.util.UtilValidate;
40 import org.ofbiz.entity.GenericDelegator;
41 import org.ofbiz.entity.GenericEntityException;
42 import org.ofbiz.entity.GenericValue;
43 import org.ofbiz.entity.util.EntityUtil;
44
45 /**
46  * Worker methods for Contact Mechanisms
47  *
48  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
49  * @version $Rev: 6379 $
50  * @since 2.0
51  */

52 public class ContactMechWorker {
53     
54     public static final String JavaDoc module = ContactMechWorker.class.getName();
55     
56     public static void getPartyContactMechValueMaps(PageContext JavaDoc pageContext, String JavaDoc partyId, boolean showOld, String JavaDoc partyContactMechValueMapsAttr) {
57         GenericDelegator delegator = (GenericDelegator) pageContext.getRequest().getAttribute("delegator");
58         List JavaDoc partyContactMechValueMaps = getPartyContactMechValueMaps(delegator, partyId, showOld);
59         if (partyContactMechValueMaps.size() > 0) {
60             pageContext.setAttribute(partyContactMechValueMapsAttr, partyContactMechValueMaps);
61         }
62     }
63     
64     public static List JavaDoc getPartyContactMechValueMaps(GenericDelegator delegator, String JavaDoc partyId, boolean showOld) {
65        return getPartyContactMechValueMaps(delegator, partyId, showOld, null);
66     }
67     
68     public static List JavaDoc getPartyContactMechValueMaps(GenericDelegator delegator, String JavaDoc partyId, boolean showOld, String JavaDoc contactMechTypeId) {
69         List JavaDoc partyContactMechValueMaps = new LinkedList JavaDoc();
70
71         Iterator JavaDoc allPartyContactMechs = null;
72
73         try {
74             List JavaDoc tempCol = delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", partyId));
75             if(contactMechTypeId != null) {
76                 List JavaDoc tempColTemp = new LinkedList JavaDoc();
77                 for(Iterator JavaDoc iterator = tempCol.iterator(); iterator.hasNext();) {
78                     GenericValue partyContactMech = (GenericValue) iterator.next();
79                     GenericValue contactMech = delegator.getRelatedOne("ContactMech", partyContactMech);
80                     if(contactMech != null && contactMechTypeId.equals(contactMech.getString("contactMechTypeId"))) {
81                         tempColTemp.add(partyContactMech);
82                     }
83                         
84                 }
85                 tempCol = tempColTemp;
86             }
87             if (!showOld) tempCol = EntityUtil.filterByDate(tempCol, true);
88             allPartyContactMechs = UtilMisc.toIterator(tempCol);
89         } catch (GenericEntityException e) {
90             Debug.logWarning(e, module);
91         }
92
93         while (allPartyContactMechs != null && allPartyContactMechs.hasNext()) {
94             GenericValue partyContactMech = (GenericValue) allPartyContactMechs.next();
95             GenericValue contactMech = null;
96
97             try {
98                 contactMech = partyContactMech.getRelatedOne("ContactMech");
99             } catch (GenericEntityException e) {
100                 Debug.logWarning(e, module);
101             }
102             if (contactMech != null) {
103                 Map JavaDoc partyContactMechValueMap = new HashMap JavaDoc();
104
105                 partyContactMechValueMaps.add(partyContactMechValueMap);
106                 partyContactMechValueMap.put("contactMech", contactMech);
107                 partyContactMechValueMap.put("partyContactMech", partyContactMech);
108
109                 try {
110                     partyContactMechValueMap.put("contactMechType", contactMech.getRelatedOneCache("ContactMechType"));
111                 } catch (GenericEntityException e) {
112                     Debug.logWarning(e, module);
113                 }
114
115                 try {
116                     List JavaDoc partyContactMechPurposes = partyContactMech.getRelated("PartyContactMechPurpose");
117
118                     if (!showOld) partyContactMechPurposes = EntityUtil.filterByDate(partyContactMechPurposes, true);
119                     partyContactMechValueMap.put("partyContactMechPurposes", partyContactMechPurposes);
120                 } catch (GenericEntityException e) {
121                     Debug.logWarning(e, module);
122                 }
123
124                 try {
125                     if ("POSTAL_ADDRESS".equals(contactMech.getString("contactMechTypeId"))) {
126                         partyContactMechValueMap.put("postalAddress", contactMech.getRelatedOne("PostalAddress"));
127                     } else if ("TELECOM_NUMBER".equals(contactMech.getString("contactMechTypeId"))) {
128                         partyContactMechValueMap.put("telecomNumber", contactMech.getRelatedOne("TelecomNumber"));
129                     }
130                 } catch (GenericEntityException e) {
131                     Debug.logWarning(e, module);
132                 }
133             }
134         }
135
136         return partyContactMechValueMaps;
137     }
138     
139     public static List JavaDoc getFacilityContactMechValueMaps(GenericDelegator delegator, String JavaDoc facilityId, boolean showOld, String JavaDoc contactMechTypeId) {
140         List JavaDoc facilityContactMechValueMaps = new LinkedList JavaDoc();
141
142         Iterator JavaDoc allFacilityContactMechs = null;
143
144         try {
145             List JavaDoc tempCol = delegator.findByAnd("FacilityContactMech", UtilMisc.toMap("facilityId", facilityId));
146             if(contactMechTypeId != null) {
147                 List JavaDoc tempColTemp = new LinkedList JavaDoc();
148                 for(Iterator JavaDoc iterator = tempCol.iterator(); iterator.hasNext();) {
149                     GenericValue partyContactMech = (GenericValue) iterator.next();
150                     GenericValue contactMech = delegator.getRelatedOne("ContactMech", partyContactMech);
151                     if(contactMech != null && contactMechTypeId.equals(contactMech.getString("contactMechTypeId"))) {
152                         tempColTemp.add(partyContactMech);
153                     }
154                         
155                 }
156                 tempCol = tempColTemp;
157             }
158             if (!showOld) tempCol = EntityUtil.filterByDate(tempCol, true);
159             allFacilityContactMechs = UtilMisc.toIterator(tempCol);
160         } catch (GenericEntityException e) {
161             Debug.logWarning(e, module);
162         }
163
164         while (allFacilityContactMechs != null && allFacilityContactMechs.hasNext()) {
165             GenericValue facilityContactMech = (GenericValue) allFacilityContactMechs.next();
166             GenericValue contactMech = null;
167
168             try {
169                 contactMech = facilityContactMech.getRelatedOne("ContactMech");
170             } catch (GenericEntityException e) {
171                 Debug.logWarning(e, module);
172             }
173             if (contactMech != null) {
174                 Map JavaDoc facilityContactMechValueMap = new HashMap JavaDoc();
175
176                 facilityContactMechValueMaps.add(facilityContactMechValueMap);
177                 facilityContactMechValueMap.put("contactMech", contactMech);
178                 facilityContactMechValueMap.put("facilityContactMech", facilityContactMech);
179
180                 try {
181                     facilityContactMechValueMap.put("contactMechType", contactMech.getRelatedOneCache("ContactMechType"));
182                 } catch (GenericEntityException e) {
183                     Debug.logWarning(e, module);
184                 }
185
186                 try {
187                     List JavaDoc facilityContactMechPurposes = facilityContactMech.getRelated("FacilityContactMechPurpose");
188
189                     if (!showOld) facilityContactMechPurposes = EntityUtil.filterByDate(facilityContactMechPurposes, true);
190                     facilityContactMechValueMap.put("facilityContactMechPurposes", facilityContactMechPurposes);
191                 } catch (GenericEntityException e) {
192                     Debug.logWarning(e, module);
193                 }
194
195                 try {
196                     if ("POSTAL_ADDRESS".equals(contactMech.getString("contactMechTypeId"))) {
197                         facilityContactMechValueMap.put("postalAddress", contactMech.getRelatedOne("PostalAddress"));
198                     } else if ("TELECOM_NUMBER".equals(contactMech.getString("contactMechTypeId"))) {
199                         facilityContactMechValueMap.put("telecomNumber", contactMech.getRelatedOne("TelecomNumber"));
200                     }
201                 } catch (GenericEntityException e) {
202                     Debug.logWarning(e, module);
203                 }
204             }
205         }
206
207         return facilityContactMechValueMaps;
208     }
209     
210
211     public static void getOrderContactMechValueMaps(PageContext JavaDoc pageContext, String JavaDoc orderId, String JavaDoc orderContactMechValueMapsAttr) {
212         GenericDelegator delegator = (GenericDelegator) pageContext.getRequest().getAttribute("delegator");
213         List JavaDoc maps = getOrderContactMechValueMaps(delegator, orderId);
214         if (maps != null && maps.size() > 0) {
215             pageContext.setAttribute(orderContactMechValueMapsAttr, maps);
216         }
217     }
218     public static List JavaDoc getOrderContactMechValueMaps(GenericDelegator delegator, String JavaDoc orderId) {
219         List JavaDoc orderContactMechValueMaps = new LinkedList JavaDoc();
220
221         Iterator JavaDoc allOrderContactMechs = null;
222
223         try {
224             Collection JavaDoc tempCol = delegator.findByAnd("OrderContactMech", UtilMisc.toMap("orderId", orderId), UtilMisc.toList("contactMechPurposeTypeId"));
225
226             allOrderContactMechs = UtilMisc.toIterator(tempCol);
227         } catch (GenericEntityException e) {
228             Debug.logWarning(e, module);
229         }
230
231         while (allOrderContactMechs != null && allOrderContactMechs.hasNext()) {
232             GenericValue orderContactMech = (GenericValue) allOrderContactMechs.next();
233             GenericValue contactMech = null;
234
235             try {
236                 contactMech = orderContactMech.getRelatedOne("ContactMech");
237             } catch (GenericEntityException e) {
238                 Debug.logWarning(e, module);
239             }
240             if (contactMech != null) {
241                 Map JavaDoc orderContactMechValueMap = new HashMap JavaDoc();
242
243                 orderContactMechValueMaps.add(orderContactMechValueMap);
244                 orderContactMechValueMap.put("contactMech", contactMech);
245                 orderContactMechValueMap.put("orderContactMech", orderContactMech);
246
247                 try {
248                     orderContactMechValueMap.put("contactMechType", contactMech.getRelatedOneCache("ContactMechType"));
249                 } catch (GenericEntityException e) {
250                     Debug.logWarning(e, module);
251                 }
252
253                 try {
254                     GenericValue contactMechPurposeType = orderContactMech.getRelatedOne("ContactMechPurposeType");
255
256                     orderContactMechValueMap.put("contactMechPurposeType", contactMechPurposeType);
257                 } catch (GenericEntityException e) {
258                     Debug.logWarning(e, module);
259                 }
260
261                 try {
262                     if ("POSTAL_ADDRESS".equals(contactMech.getString("contactMechTypeId"))) {
263                         orderContactMechValueMap.put("postalAddress", contactMech.getRelatedOne("PostalAddress"));
264                     } else if ("TELECOM_NUMBER".equals(contactMech.getString("contactMechTypeId"))) {
265                         orderContactMechValueMap.put("telecomNumber", contactMech.getRelatedOne("TelecomNumber"));
266                     }
267                 } catch (GenericEntityException e) {
268                     Debug.logWarning(e, module);
269                 }
270             }
271         }
272
273         return orderContactMechValueMaps;
274     }
275
276     public static Collection JavaDoc getWorkEffortContactMechValueMaps(GenericDelegator delegator, String JavaDoc workEffortId) {
277         Collection JavaDoc workEffortContactMechValueMaps = new LinkedList JavaDoc();
278
279         Iterator JavaDoc allWorkEffortContactMechs = null;
280
281         try {
282             Collection JavaDoc tempCol = delegator.findByAnd("WorkEffortContactMech", UtilMisc.toMap("workEffortId", workEffortId));
283             allWorkEffortContactMechs = UtilMisc.toIterator(tempCol);
284         } catch (GenericEntityException e) {
285             Debug.logWarning(e, module);
286         }
287
288         while (allWorkEffortContactMechs != null && allWorkEffortContactMechs.hasNext()) {
289             GenericValue workEffortContactMech = (GenericValue) allWorkEffortContactMechs.next();
290             GenericValue contactMech = null;
291
292             try {
293                 contactMech = workEffortContactMech.getRelatedOne("ContactMech");
294             } catch (GenericEntityException e) {
295                 Debug.logWarning(e, module);
296             }
297             if (contactMech != null) {
298                 Map JavaDoc workEffortContactMechValueMap = new HashMap JavaDoc();
299
300                 workEffortContactMechValueMaps.add(workEffortContactMechValueMap);
301                 workEffortContactMechValueMap.put("contactMech", contactMech);
302                 workEffortContactMechValueMap.put("workEffortContactMech", workEffortContactMech);
303
304                 try {
305                     workEffortContactMechValueMap.put("contactMechType", contactMech.getRelatedOneCache("ContactMechType"));
306                 } catch (GenericEntityException e) {
307                     Debug.logWarning(e, module);
308                 }
309
310                 try {
311                     if ("POSTAL_ADDRESS".equals(contactMech.getString("contactMechTypeId"))) {
312                         workEffortContactMechValueMap.put("postalAddress", contactMech.getRelatedOne("PostalAddress"));
313                     } else if ("TELECOM_NUMBER".equals(contactMech.getString("contactMechTypeId"))) {
314                         workEffortContactMechValueMap.put("telecomNumber", contactMech.getRelatedOne("TelecomNumber"));
315                     }
316                 } catch (GenericEntityException e) {
317                     Debug.logWarning(e, module);
318                 }
319             }
320         }
321
322         return workEffortContactMechValueMaps.size() > 0 ? workEffortContactMechValueMaps : null;
323     }
324     
325     /** TO BE REMOVED (DEJ 20030221): This method was for use in a JSP and when they are removed this can be removed as well rather than being maintained, should be removed when eCommerce and party mgr and possible other places are converted to FTL */
326     public static void getContactMechAndRelated(PageContext JavaDoc pageContext, String JavaDoc partyId, String JavaDoc contactMechAttr, String JavaDoc contactMechIdAttr,
327         String JavaDoc partyContactMechAttr, String JavaDoc partyContactMechPurposesAttr, String JavaDoc contactMechTypeIdAttr, String JavaDoc contactMechTypeAttr, String JavaDoc purposeTypesAttr,
328         String JavaDoc postalAddressAttr, String JavaDoc telecomNumberAttr, String JavaDoc requestNameAttr, String JavaDoc donePageAttr, String JavaDoc tryEntityAttr, String JavaDoc contactMechTypesAttr) {
329
330         ServletRequest JavaDoc request = pageContext.getRequest();
331         GenericDelegator delegator = (GenericDelegator) pageContext.getRequest().getAttribute("delegator");
332
333         boolean tryEntity = true;
334
335         if (request.getAttribute("_ERROR_MESSAGE_") != null) tryEntity = false;
336         if ("true".equals(request.getParameter("tryEntity"))) tryEntity = true;
337
338         String JavaDoc donePage = request.getParameter("DONE_PAGE");
339
340         if (donePage == null) donePage = (String JavaDoc) request.getAttribute("DONE_PAGE");
341         if (donePage == null || donePage.length() <= 0) donePage = "viewprofile";
342         pageContext.setAttribute(donePageAttr, donePage);
343
344         String JavaDoc contactMechTypeId = request.getParameter("preContactMechTypeId");
345
346         if (contactMechTypeId == null) contactMechTypeId = (String JavaDoc) request.getAttribute("preContactMechTypeId");
347         if (contactMechTypeId != null)
348             tryEntity = false;
349
350         String JavaDoc contactMechId = request.getParameter("contactMechId");
351
352         if (request.getAttribute("contactMechId") != null)
353             contactMechId = (String JavaDoc) request.getAttribute("contactMechId");
354
355         GenericValue contactMech = null;
356
357         if (contactMechId != null) {
358             pageContext.setAttribute(contactMechIdAttr, contactMechId);
359
360             // try to find a PartyContactMech with a valid date range
361
List JavaDoc partyContactMechs = null;
362
363             try {
364                 partyContactMechs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId)), true);
365             } catch (GenericEntityException e) {
366                 Debug.logWarning(e, module);
367             }
368
369             GenericValue partyContactMech = EntityUtil.getFirst(partyContactMechs);
370
371             if (partyContactMech != null) {
372                 pageContext.setAttribute(partyContactMechAttr, partyContactMech);
373
374                 Collection JavaDoc partyContactMechPurposes = null;
375
376                 try {
377                     partyContactMechPurposes = EntityUtil.filterByDate(partyContactMech.getRelated("PartyContactMechPurpose"), true);
378                 } catch (GenericEntityException e) {
379                     Debug.logWarning(e, module);
380                 }
381                 if (partyContactMechPurposes != null && partyContactMechPurposes.size() > 0)
382                     pageContext.setAttribute(partyContactMechPurposesAttr, partyContactMechPurposes);
383             }
384
385             try {
386                 contactMech = delegator.findByPrimaryKey("ContactMech", UtilMisc.toMap("contactMechId", contactMechId));
387             } catch (GenericEntityException e) {
388                 Debug.logWarning(e, module);
389             }
390
391             if (contactMech != null) {
392                 pageContext.setAttribute(contactMechAttr, contactMech);
393                 contactMechTypeId = contactMech.getString("contactMechTypeId");
394             }
395         }
396
397         if (contactMechTypeId != null) {
398             pageContext.setAttribute(contactMechTypeIdAttr, contactMechTypeId);
399
400             try {
401                 GenericValue contactMechType = delegator.findByPrimaryKey("ContactMechType", UtilMisc.toMap("contactMechTypeId", contactMechTypeId));
402
403                 if (contactMechType != null)
404                     pageContext.setAttribute(contactMechTypeAttr, contactMechType);
405             } catch (GenericEntityException e) {
406                 Debug.logWarning(e, module);
407             }
408
409             Collection JavaDoc purposeTypes = new LinkedList JavaDoc();
410             Iterator JavaDoc typePurposes = null;
411
412             try {
413                 typePurposes = UtilMisc.toIterator(delegator.findByAnd("ContactMechTypePurpose", UtilMisc.toMap("contactMechTypeId", contactMechTypeId)));
414             } catch (GenericEntityException e) {
415                 Debug.logWarning(e, module);
416             }
417             while (typePurposes != null && typePurposes.hasNext()) {
418                 GenericValue contactMechTypePurpose = (GenericValue) typePurposes.next();
419                 GenericValue contactMechPurposeType = null;
420
421                 try {
422                     contactMechPurposeType = contactMechTypePurpose.getRelatedOne("ContactMechPurposeType");
423                 } catch (GenericEntityException e) {
424                     Debug.logWarning(e, module);
425                 }
426                 if (contactMechPurposeType != null) {
427                     purposeTypes.add(contactMechPurposeType);
428                 }
429             }
430             if (purposeTypes.size() > 0)
431                 pageContext.setAttribute(purposeTypesAttr, purposeTypes);
432         }
433
434         String JavaDoc requestName;
435
436         if (contactMech == null) {
437             // create
438
if ("POSTAL_ADDRESS".equals(contactMechTypeId)) {
439                 if (request.getParameter("contactMechPurposeTypeId") != null || request.getAttribute("contactMechPurposeTypeId") != null) {
440                     requestName = "createPostalAddressAndPurpose";
441                 } else {
442                     requestName = "createPostalAddress";
443                 }
444             } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) {
445                 requestName = "createTelecomNumber";
446             } else if ("EMAIL_ADDRESS".equals(contactMechTypeId)) {
447                 requestName = "createEmailAddress";
448             } else {
449                 requestName = "createContactMech";
450             }
451         } else {
452             // update
453
if ("POSTAL_ADDRESS".equals(contactMechTypeId)) {
454                 requestName = "updatePostalAddress";
455             } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) {
456                 requestName = "updateTelecomNumber";
457             } else if ("EMAIL_ADDRESS".equals(contactMechTypeId)) {
458                 requestName = "updateEmailAddress";
459             } else {
460                 requestName = "updateContactMech";
461             }
462         }
463         pageContext.setAttribute(requestNameAttr, requestName);
464
465         if ("POSTAL_ADDRESS".equals(contactMechTypeId)) {
466             GenericValue postalAddress = null;
467
468             try {
469                 if (contactMech != null) postalAddress = contactMech.getRelatedOne("PostalAddress");
470             } catch (GenericEntityException e) {
471                 Debug.logWarning(e, module);
472             }
473             if (postalAddress != null) pageContext.setAttribute(postalAddressAttr, postalAddress);
474         } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) {
475             GenericValue telecomNumber = null;
476
477             try {
478                 if (contactMech != null) telecomNumber = contactMech.getRelatedOne("TelecomNumber");
479             } catch (GenericEntityException e) {
480                 Debug.logWarning(e, module);
481             }
482             if (telecomNumber != null) pageContext.setAttribute(telecomNumberAttr, telecomNumber);
483         }
484
485         if ("true".equals(request.getParameter("useValues"))) tryEntity = true;
486         pageContext.setAttribute(tryEntityAttr, new Boolean JavaDoc(tryEntity));
487
488         try {
489             Collection JavaDoc contactMechTypes = delegator.findAllCache("ContactMechType", null);
490
491             if (contactMechTypes != null) {
492                 pageContext.setAttribute(contactMechTypesAttr, contactMechTypes);
493             }
494         } catch (GenericEntityException e) {
495             Debug.logWarning(e, module);
496         }
497     }
498
499     public static void getContactMechAndRelated(ServletRequest JavaDoc request, String JavaDoc partyId, Map JavaDoc target) {
500         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
501
502         boolean tryEntity = true;
503         if (request.getAttribute("_ERROR_MESSAGE_") != null) tryEntity = false;
504         if ("true".equals(request.getParameter("tryEntity"))) tryEntity = true;
505
506         String JavaDoc donePage = request.getParameter("DONE_PAGE");
507         if (donePage == null) donePage = (String JavaDoc) request.getAttribute("DONE_PAGE");
508         if (donePage == null || donePage.length() <= 0) donePage = "viewprofile";
509         target.put("donePage", donePage);
510
511         String JavaDoc contactMechTypeId = request.getParameter("preContactMechTypeId");
512
513         if (contactMechTypeId == null) contactMechTypeId = (String JavaDoc) request.getAttribute("preContactMechTypeId");
514         if (contactMechTypeId != null)
515             tryEntity = false;
516
517         String JavaDoc contactMechId = request.getParameter("contactMechId");
518
519         if (request.getAttribute("contactMechId") != null)
520             contactMechId = (String JavaDoc) request.getAttribute("contactMechId");
521
522         GenericValue contactMech = null;
523
524         if (contactMechId != null) {
525             target.put("contactMechId", contactMechId);
526
527             // try to find a PartyContactMech with a valid date range
528
List JavaDoc partyContactMechs = null;
529
530             try {
531                 partyContactMechs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", partyId, "contactMechId", contactMechId)), true);
532             } catch (GenericEntityException e) {
533                 Debug.logWarning(e, module);
534             }
535
536             GenericValue partyContactMech = EntityUtil.getFirst(partyContactMechs);
537
538             if (partyContactMech != null) {
539                 target.put("partyContactMech", partyContactMech);
540
541                 Collection JavaDoc partyContactMechPurposes = null;
542
543                 try {
544                     partyContactMechPurposes = EntityUtil.filterByDate(partyContactMech.getRelated("PartyContactMechPurpose"), true);
545                 } catch (GenericEntityException e) {
546                     Debug.logWarning(e, module);
547                 }
548                 if (partyContactMechPurposes != null && partyContactMechPurposes.size() > 0)
549                     target.put("partyContactMechPurposes", partyContactMechPurposes);
550             }
551
552             try {
553                 contactMech = delegator.findByPrimaryKey("ContactMech", UtilMisc.toMap("contactMechId", contactMechId));
554             } catch (GenericEntityException e) {
555                 Debug.logWarning(e, module);
556             }
557
558             if (contactMech != null) {
559                 target.put("contactMech", contactMech);
560                 contactMechTypeId = contactMech.getString("contactMechTypeId");
561             }
562         }
563
564         if (contactMechTypeId != null) {
565             target.put("contactMechTypeId", contactMechTypeId);
566
567             try {
568                 GenericValue contactMechType = delegator.findByPrimaryKey("ContactMechType", UtilMisc.toMap("contactMechTypeId", contactMechTypeId));
569
570                 if (contactMechType != null)
571                     target.put("contactMechType", contactMechType);
572             } catch (GenericEntityException e) {
573                 Debug.logWarning(e, module);
574             }
575
576             Collection JavaDoc purposeTypes = new LinkedList JavaDoc();
577             Iterator JavaDoc typePurposes = null;
578
579             try {
580                 typePurposes = UtilMisc.toIterator(delegator.findByAnd("ContactMechTypePurpose", UtilMisc.toMap("contactMechTypeId", contactMechTypeId)));
581             } catch (GenericEntityException e) {
582                 Debug.logWarning(e, module);
583             }
584             while (typePurposes != null && typePurposes.hasNext()) {
585                 GenericValue contactMechTypePurpose = (GenericValue) typePurposes.next();
586                 GenericValue contactMechPurposeType = null;
587
588                 try {
589                     contactMechPurposeType = contactMechTypePurpose.getRelatedOne("ContactMechPurposeType");
590                 } catch (GenericEntityException e) {
591                     Debug.logWarning(e, module);
592                 }
593                 if (contactMechPurposeType != null) {
594                     purposeTypes.add(contactMechPurposeType);
595                 }
596             }
597             if (purposeTypes.size() > 0)
598                 target.put("purposeTypes", purposeTypes);
599         }
600
601         String JavaDoc requestName;
602
603         if (contactMech == null) {
604             // create
605
if ("POSTAL_ADDRESS".equals(contactMechTypeId)) {
606                 if (request.getParameter("contactMechPurposeTypeId") != null || request.getAttribute("contactMechPurposeTypeId") != null) {
607                     requestName = "createPostalAddressAndPurpose";
608                 } else {
609                     requestName = "createPostalAddress";
610                 }
611             } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) {
612                 requestName = "createTelecomNumber";
613             } else if ("EMAIL_ADDRESS".equals(contactMechTypeId)) {
614                 requestName = "createEmailAddress";
615             } else {
616                 requestName = "createContactMech";
617             }
618         } else {
619             // update
620
if ("POSTAL_ADDRESS".equals(contactMechTypeId)) {
621                 requestName = "updatePostalAddress";
622             } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) {
623                 requestName = "updateTelecomNumber";
624             } else if ("EMAIL_ADDRESS".equals(contactMechTypeId)) {
625                 requestName = "updateEmailAddress";
626             } else {
627                 requestName = "updateContactMech";
628             }
629         }
630         target.put("requestName", requestName);
631
632         if ("POSTAL_ADDRESS".equals(contactMechTypeId)) {
633             GenericValue postalAddress = null;
634
635             try {
636                 if (contactMech != null) postalAddress = contactMech.getRelatedOne("PostalAddress");
637             } catch (GenericEntityException e) {
638                 Debug.logWarning(e, module);
639             }
640             if (postalAddress != null) target.put("postalAddress", postalAddress);
641         } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) {
642             GenericValue telecomNumber = null;
643
644             try {
645                 if (contactMech != null) telecomNumber = contactMech.getRelatedOne("TelecomNumber");
646             } catch (GenericEntityException e) {
647                 Debug.logWarning(e, module);
648             }
649             if (telecomNumber != null) target.put("telecomNumber", telecomNumber);
650         }
651
652         if ("true".equals(request.getParameter("useValues"))) tryEntity = true;
653         target.put("tryEntity", new Boolean JavaDoc(tryEntity));
654
655         try {
656             Collection JavaDoc contactMechTypes = delegator.findAllCache("ContactMechType", null);
657
658             if (contactMechTypes != null) {
659                 target.put("contactMechTypes", contactMechTypes);
660             }
661         } catch (GenericEntityException e) {
662             Debug.logWarning(e, module);
663         }
664     }
665     
666     public static void getFacilityContactMechAndRelated(ServletRequest JavaDoc request, String JavaDoc facilityId, Map JavaDoc target) {
667         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
668
669         boolean tryEntity = true;
670         if (request.getAttribute("_ERROR_MESSAGE") != null) tryEntity = false;
671         if ("true".equals(request.getParameter("tryEntity"))) tryEntity = true;
672
673         String JavaDoc donePage = request.getParameter("DONE_PAGE");
674         if (donePage == null) donePage = (String JavaDoc) request.getAttribute("DONE_PAGE");
675         if (donePage == null || donePage.length() <= 0) donePage = "viewprofile";
676         target.put("donePage", donePage);
677
678         String JavaDoc contactMechTypeId = request.getParameter("preContactMechTypeId");
679
680         if (contactMechTypeId == null) contactMechTypeId = (String JavaDoc) request.getAttribute("preContactMechTypeId");
681         if (contactMechTypeId != null)
682             tryEntity = false;
683
684         String JavaDoc contactMechId = request.getParameter("contactMechId");
685
686         if (request.getAttribute("contactMechId") != null)
687             contactMechId = (String JavaDoc) request.getAttribute("contactMechId");
688
689         GenericValue contactMech = null;
690
691         if (contactMechId != null) {
692             target.put("contactMechId", contactMechId);
693
694             // try to find a PartyContactMech with a valid date range
695
List JavaDoc facilityContactMechs = null;
696
697             try {
698                 facilityContactMechs = EntityUtil.filterByDate(delegator.findByAnd("FacilityContactMech", UtilMisc.toMap("facilityId", facilityId, "contactMechId", contactMechId)), true);
699             } catch (GenericEntityException e) {
700                 Debug.logWarning(e, module);
701             }
702
703             GenericValue facilityContactMech = EntityUtil.getFirst(facilityContactMechs);
704
705             if (facilityContactMech != null) {
706                 target.put("facilityContactMech", facilityContactMech);
707
708                 Collection JavaDoc facilityContactMechPurposes = null;
709
710                 try {
711                     facilityContactMechPurposes = EntityUtil.filterByDate(facilityContactMech.getRelated("FacilityContactMechPurpose"), true);
712                 } catch (GenericEntityException e) {
713                     Debug.logWarning(e, module);
714                 }
715                 if (facilityContactMechPurposes != null && facilityContactMechPurposes.size() > 0)
716                     target.put("facilityContactMechPurposes", facilityContactMechPurposes);
717             }
718
719             try {
720                 contactMech = delegator.findByPrimaryKey("ContactMech", UtilMisc.toMap("contactMechId", contactMechId));
721             } catch (GenericEntityException e) {
722                 Debug.logWarning(e, module);
723             }
724
725             if (contactMech != null) {
726                 target.put("contactMech", contactMech);
727                 contactMechTypeId = contactMech.getString("contactMechTypeId");
728             }
729         }
730
731         if (contactMechTypeId != null) {
732             target.put("contactMechTypeId", contactMechTypeId);
733
734             try {
735                 GenericValue contactMechType = delegator.findByPrimaryKey("ContactMechType", UtilMisc.toMap("contactMechTypeId", contactMechTypeId));
736
737                 if (contactMechType != null)
738                     target.put("contactMechType", contactMechType);
739             } catch (GenericEntityException e) {
740                 Debug.logWarning(e, module);
741             }
742
743             Collection JavaDoc purposeTypes = new LinkedList JavaDoc();
744             Iterator JavaDoc typePurposes = null;
745
746             try {
747                 typePurposes = UtilMisc.toIterator(delegator.findByAnd("ContactMechTypePurpose", UtilMisc.toMap("contactMechTypeId", contactMechTypeId)));
748             } catch (GenericEntityException e) {
749                 Debug.logWarning(e, module);
750             }
751             while (typePurposes != null && typePurposes.hasNext()) {
752                 GenericValue contactMechTypePurpose = (GenericValue) typePurposes.next();
753                 GenericValue contactMechPurposeType = null;
754
755                 try {
756                     contactMechPurposeType = contactMechTypePurpose.getRelatedOne("ContactMechPurposeType");
757                 } catch (GenericEntityException e) {
758                     Debug.logWarning(e, module);
759                 }
760                 if (contactMechPurposeType != null) {
761                     purposeTypes.add(contactMechPurposeType);
762                 }
763             }
764             if (purposeTypes.size() > 0)
765                 target.put("purposeTypes", purposeTypes);
766         }
767
768         String JavaDoc requestName;
769
770         if (contactMech == null) {
771             // create
772
if ("POSTAL_ADDRESS".equals(contactMechTypeId)) {
773                 if (request.getParameter("contactMechPurposeTypeId") != null || request.getAttribute("contactMechPurposeTypeId") != null) {
774                     requestName = "createPostalAddressAndPurpose";
775                 } else {
776                     requestName = "createPostalAddress";
777                 }
778             } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) {
779                 requestName = "createTelecomNumber";
780             } else if ("EMAIL_ADDRESS".equals(contactMechTypeId)) {
781                 requestName = "createEmailAddress";
782             } else {
783                 requestName = "createContactMech";
784             }
785         } else {
786             // update
787
if ("POSTAL_ADDRESS".equals(contactMechTypeId)) {
788                 requestName = "updatePostalAddress";
789             } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) {
790                 requestName = "updateTelecomNumber";
791             } else if ("EMAIL_ADDRESS".equals(contactMechTypeId)) {
792                 requestName = "updateEmailAddress";
793             } else {
794                 requestName = "updateContactMech";
795             }
796         }
797         target.put("requestName", requestName);
798
799         if ("POSTAL_ADDRESS".equals(contactMechTypeId)) {
800             GenericValue postalAddress = null;
801
802             try {
803                 if (contactMech != null) postalAddress = contactMech.getRelatedOne("PostalAddress");
804             } catch (GenericEntityException e) {
805                 Debug.logWarning(e, module);
806             }
807             if (postalAddress != null) target.put("postalAddress", postalAddress);
808         } else if ("TELECOM_NUMBER".equals(contactMechTypeId)) {
809             GenericValue telecomNumber = null;
810
811             try {
812                 if (contactMech != null) telecomNumber = contactMech.getRelatedOne("TelecomNumber");
813             } catch (GenericEntityException e) {
814                 Debug.logWarning(e, module);
815             }
816             if (telecomNumber != null) target.put("telecomNumber", telecomNumber);
817         }
818
819         if ("true".equals(request.getParameter("useValues"))) tryEntity = true;
820         target.put("tryEntity", new Boolean JavaDoc(tryEntity));
821
822         try {
823             Collection JavaDoc contactMechTypes = delegator.findAllCache("ContactMechType", null);
824
825             if (contactMechTypes != null) {
826                 target.put("contactMechTypes", contactMechTypes);
827             }
828         } catch (GenericEntityException e) {
829             Debug.logWarning(e, module);
830         }
831     }
832
833     /** TO BE REMOVED (DEJ 20030221): This method was for use in a JSP and when they are removed this can be removed as well rather than being maintained, should be removed when eCommerce and party mgr and possible other places are converted to FTL */
834     public static void getPartyPostalAddresses(PageContext JavaDoc pageContext, String JavaDoc partyId, String JavaDoc curContactMechId, String JavaDoc postalAddressInfosAttr) {
835         ServletRequest JavaDoc request = pageContext.getRequest();
836         List JavaDoc postalAddressInfos = getPartyPostalAddresses(request, partyId, curContactMechId);
837         if (postalAddressInfos.size() > 0) {
838             pageContext.setAttribute(postalAddressInfosAttr, postalAddressInfos);
839         }
840     }
841     
842     public static List JavaDoc getPartyPostalAddresses(ServletRequest JavaDoc request, String JavaDoc partyId, String JavaDoc curContactMechId) {
843         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
844         List JavaDoc postalAddressInfos = new LinkedList JavaDoc();
845
846         Iterator JavaDoc allPartyContactMechs = null;
847
848         try {
849             allPartyContactMechs = UtilMisc.toIterator(EntityUtil.filterByDate(delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", partyId)), true));
850         } catch (GenericEntityException e) {
851             Debug.logWarning(e, module);
852         }
853         while (allPartyContactMechs != null && allPartyContactMechs.hasNext()) {
854             GenericValue partyContactMech = (GenericValue) allPartyContactMechs.next();
855             GenericValue contactMech = null;
856
857             try {
858                 contactMech = partyContactMech.getRelatedOne("ContactMech");
859             } catch (GenericEntityException e) {
860                 Debug.logWarning(e, module);
861             }
862             if (contactMech != null && "POSTAL_ADDRESS".equals(contactMech.getString("contactMechTypeId")) && !contactMech.getString("contactMechId").equals(curContactMechId)) {
863                 Map JavaDoc postalAddressInfo = new HashMap JavaDoc();
864
865                 postalAddressInfos.add(postalAddressInfo);
866                 postalAddressInfo.put("contactMech", contactMech);
867                 postalAddressInfo.put("partyContactMech", partyContactMech);
868
869                 try {
870                     GenericValue postalAddress = contactMech.getRelatedOne("PostalAddress");
871                     postalAddressInfo.put("postalAddress", postalAddress);
872                 } catch (GenericEntityException e) {
873                     Debug.logWarning(e, module);
874                 }
875
876                 try {
877                     List JavaDoc partyContactMechPurposes = EntityUtil.filterByDate(partyContactMech.getRelated("PartyContactMechPurpose"), true);
878                     postalAddressInfo.put("partyContactMechPurposes", partyContactMechPurposes);
879                 } catch (GenericEntityException e) {
880                     Debug.logWarning(e, module);
881                 }
882             }
883         }
884
885         return postalAddressInfos;
886     }
887
888     /** TO BE REMOVED (DEJ 20030221): This method was for use in a JSP and when they are removed this can be removed as well rather than being maintained, should be removed when eCommerce and party mgr and possible other places are converted to FTL */
889     public static void getCurrentPostalAddress(PageContext JavaDoc pageContext, String JavaDoc partyId, String JavaDoc curContactMechId,
890             String JavaDoc curPartyContactMechAttr, String JavaDoc curContactMechAttr, String JavaDoc curPostalAddressAttr, String JavaDoc curPartyContactMechPurposesAttr) {
891         ServletRequest JavaDoc request = pageContext.getRequest();
892         Map JavaDoc results = getCurrentPostalAddress(request, partyId, curContactMechId);
893         if (results.get("curPartyContactMech") != null) pageContext.setAttribute(curPartyContactMechAttr, results.get("curPartyContactMech"));
894         if (results.get("curContactMech") != null) pageContext.setAttribute(curContactMechAttr, results.get("curContactMech"));
895         if (results.get("curPostalAddress") != null) pageContext.setAttribute(curPostalAddressAttr, results.get("curPostalAddress"));
896         if (results.get("curPartyContactMechPurposes") != null) pageContext.setAttribute(curPartyContactMechPurposesAttr, results.get("curPartyContactMechPurposes"));
897     }
898     public static Map JavaDoc getCurrentPostalAddress(ServletRequest JavaDoc request, String JavaDoc partyId, String JavaDoc curContactMechId) {
899         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
900         Map JavaDoc results = new HashMap JavaDoc();
901         
902         if (curContactMechId != null) {
903             List JavaDoc partyContactMechs = null;
904
905             try {
906                 partyContactMechs = EntityUtil.filterByDate(delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", partyId, "contactMechId", curContactMechId)), true);
907             } catch (GenericEntityException e) {
908                 Debug.logWarning(e, module);
909             }
910             GenericValue curPartyContactMech = EntityUtil.getFirst(partyContactMechs);
911             results.put("curPartyContactMech", curPartyContactMech);
912
913             GenericValue curContactMech = null;
914             if (curPartyContactMech != null) {
915                 try {
916                     curContactMech = curPartyContactMech.getRelatedOne("ContactMech");
917                 } catch (GenericEntityException e) {
918                     Debug.logWarning(e, module);
919                 }
920
921                 Collection JavaDoc curPartyContactMechPurposes = null;
922                 try {
923                     curPartyContactMechPurposes = EntityUtil.filterByDate(curPartyContactMech.getRelated("PartyContactMechPurpose"), true);
924                 } catch (GenericEntityException e) {
925                     Debug.logWarning(e, module);
926                 }
927                 results.put("curPartyContactMechPurposes", curPartyContactMechPurposes);
928             }
929             results.put("curContactMech", curContactMech);
930
931             GenericValue curPostalAddress = null;
932             if (curContactMech != null) {
933                 try {
934                     curPostalAddress = curContactMech.getRelatedOne("PostalAddress");
935                 } catch (GenericEntityException e) {
936                     Debug.logWarning(e, module);
937                 }
938             }
939
940             results.put("curPostalAddress", curPostalAddress);
941         }
942         return results;
943     }
944
945     public static boolean isUspsAddress(GenericValue postalAddress) {
946         if (postalAddress == null) {
947             // null postal address is not a USPS address
948
return false;
949         }
950         if (!"PostalAddress".equals(postalAddress.getEntityName())) {
951             // not a postal address not a USPS address
952
return false;
953         }
954
955         // get and clean the address strings
956
String JavaDoc addr1 = postalAddress.getString("address1");
957         String JavaDoc addr2 = postalAddress.getString("address2");
958
959         // get the matching string from general.properties
960
String JavaDoc matcher = UtilProperties.getPropertyValue("general.properties", "usps.address.match");
961         if (UtilValidate.isNotEmpty(matcher)) {
962             if (addr1 != null && addr1.toLowerCase().matches(matcher)) {
963                 return true;
964             }
965             if (addr2 != null && addr2.toLowerCase().matches(matcher)) {
966                 return true;
967             }
968         }
969
970         return false;
971     }
972
973     public static boolean isCompanyAddress(GenericValue postalAddress, String JavaDoc companyPartyId) {
974         if (postalAddress == null) {
975             // null postal address is not an internal address
976
return false;
977         }
978         if (!"PostalAddress".equals(postalAddress.getEntityName())) {
979             // not a postal address not an internal address
980
return false;
981         }
982         if (companyPartyId == null) {
983             // no partyId not an internal address
984
return false;
985         }
986
987         String JavaDoc state = postalAddress.getString("stateProvinceGeoId");
988         String JavaDoc addr1 = postalAddress.getString("address1");
989         String JavaDoc addr2 = postalAddress.getString("address2");
990         if (state != null) {
991             state = state.replaceAll("\\W", "").toLowerCase();
992         } else {
993             state = "";
994         }
995         if (addr1 != null) {
996             addr1 = addr1.replaceAll("\\W", "").toLowerCase();
997         } else {
998             addr1 = "";
999         }
1000        if (addr2 != null) {
1001            addr2 = addr2.replaceAll("\\W", "").toLowerCase();
1002        } else {
1003            addr2 = "";
1004        }
1005
1006        // get all company addresses
1007
GenericDelegator delegator = postalAddress.getDelegator();
1008        List JavaDoc postalAddresses = new LinkedList JavaDoc();
1009        try {
1010            List JavaDoc partyContactMechs = delegator.findByAnd("PartyContactMech", UtilMisc.toMap("partyId", companyPartyId));
1011            partyContactMechs = EntityUtil.filterByDate(partyContactMechs);
1012            if (partyContactMechs != null) {
1013                Iterator JavaDoc pci = partyContactMechs.iterator();
1014                while (pci.hasNext()) {
1015                    GenericValue pcm = (GenericValue) pci.next();
1016                    GenericValue addr = pcm.getRelatedOne("PostalAddress");
1017                    if (addr != null) {
1018                        postalAddresses.add(addr);
1019                    }
1020                }
1021            }
1022        } catch (GenericEntityException e) {
1023            Debug.logError(e, "Unable to get party postal addresses", module);
1024        }
1025
1026        if (postalAddresses != null) {
1027            Iterator JavaDoc pai = postalAddresses.iterator();
1028            while (pai.hasNext()) {
1029                GenericValue addr = (GenericValue) pai.next();
1030                String JavaDoc thisAddr1 = addr.getString("address1");
1031                String JavaDoc thisAddr2 = addr.getString("address2");
1032                String JavaDoc thisState = addr.getString("stateProvinceGeoId");
1033                if (thisState != null) {
1034                    thisState = thisState.replaceAll("\\W", "").toLowerCase();
1035                } else {
1036                    thisState = "";
1037                }
1038                if (thisAddr1 != null) {
1039                    thisAddr1 = thisAddr1.replaceAll("\\W", "").toLowerCase();
1040                } else {
1041                    thisAddr1 = "";
1042                }
1043                if (thisAddr2 != null) {
1044                    thisAddr2 = thisAddr2.replaceAll("\\W", "").toLowerCase();
1045                } else {
1046                    thisAddr2 = "";
1047                }
1048                if (thisAddr1.equals(addr1) && thisAddr2.equals(addr2) && thisState.equals(state)) {
1049                    return true;
1050                }
1051            }
1052        }
1053
1054        return false;
1055    }
1056
1057    public static String JavaDoc getContactMechAttribute(GenericDelegator delegator, String JavaDoc contactMechId, String JavaDoc attrName) {
1058        GenericValue attr = null;
1059        try {
1060            attr = delegator.findByPrimaryKey("ContactMechAttribute", UtilMisc.toMap("contactMechId", contactMechId, "attrName", attrName));
1061        } catch (GenericEntityException e) {
1062            Debug.logError(e, module);
1063        }
1064        if (attr == null) {
1065            return null;
1066        } else {
1067            return attr.getString("attrValue");
1068        }
1069    }
1070}
1071
Popular Tags