KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensourcestrategies > crmsfa > party > PartyContactHelper


1 /*
2  * Copyright (c) 2006 - 2007 Open Source Strategies, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the Honest Public License.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * Honest Public License for more details.
11  *
12  * You should have received a copy of the Honest Public License
13  * along with this program; if not, write to Funambol,
14  * 643 Bair Island Road, Suite 305 - Redwood City, CA 94063, USA
15  */

16 /* Copyright (c) 2005-2006 Open Source Strategies, Inc. */
17
18 package com.opensourcestrategies.crmsfa.party;
19
20 import org.ofbiz.base.util.Debug;
21 import org.ofbiz.base.util.UtilMisc;
22 import org.ofbiz.base.util.UtilDateTime;
23 import org.ofbiz.entity.GenericDelegator;
24 import org.ofbiz.entity.GenericEntityException;
25 import org.ofbiz.entity.GenericValue;
26 import org.ofbiz.entity.util.EntityUtil;
27
28 import java.sql.Timestamp JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * This class is a series of convenience methods to help extract particular types of contact information, so it's
34  * easier to get the Primary Phone Number, Email Address, Postal Address of a party without having to work with the
35  * highly normalized OFBIZ contact data model. Mostly to help out with form widgets
36  *
37  * @author sichen@opensourcestrategies.com
38  *
39  */

40
41 public class PartyContactHelper {
42     
43     public static final String JavaDoc module = PartyContactHelper.class.getName();
44     
45     /**
46      * This is the base method and returns a List of PartyContactWithPurpose for the chosen parameters. getActiveOnly
47      * @param partyId
48      * @param contactMechTypeId
49      * @param contactMechPurposeTypeId will be used if not null
50      * @param getActiveOnly get only active ones (filter out expired contacts and expired contact purposes)
51      * @param delegator
52      * @return
53      */

54     public static List JavaDoc getContactMechsByPurpose(String JavaDoc partyId, String JavaDoc contactMechTypeId, String JavaDoc contactMechPurposeTypeId,
55             boolean getActiveOnly, GenericDelegator delegator) throws GenericEntityException {
56         // first find the records
57
Map JavaDoc lookupParams = UtilMisc.toMap("partyId", partyId, "contactMechTypeId", contactMechTypeId);
58         if (contactMechPurposeTypeId != null) {
59             lookupParams.put("contactMechPurposeTypeId", contactMechPurposeTypeId);
60         }
61         List JavaDoc potentialContactMechs = delegator.findByAnd("PartyContactWithPurpose", lookupParams);
62         
63         // now do filter by dates if we need to
64
if (getActiveOnly) {
65             potentialContactMechs = EntityUtil.filterByDate(potentialContactMechs, UtilDateTime.nowTimestamp(), "contactFromDate", "contactThruDate", true);
66             potentialContactMechs = EntityUtil.filterByDate(potentialContactMechs, UtilDateTime.nowTimestamp(), "purposeFromDate", "purposeThruDate", true);
67         }
68                 
69         return potentialContactMechs;
70     }
71
72     /**
73      * Helper method to get the first telecom number of a party as a GenericValue for a given
74      * contactMechPurposeTypeId. (ie, PHONE_WORK, PRIMARY_PHONE, etc.) If there are many, the
75      * first in the List is returned. If there are none, then a null is returned.
76      *
77      * The difference between this and getTelecomNumberValueByPurpose() is that this returns a Map that
78      * includes the extension of the phone number. This is due to the datamodel; Extensions are stored in
79      * PartyContactMech. Use this method instead of getTelecomNumberValueByPurpose().
80      *
81      * @param partyId
82      * @param contactMechPurposeTypeId purpose of phone number
83      * @param getActiveOnly
84      * @return Map of TelecomNumber fields + "extension"
85
86      */

87     public static Map JavaDoc getTelecomNumberMapByPurpose(String JavaDoc partyId, String JavaDoc contactMechPurposeTypeId,
88             boolean getActiveOnly, GenericDelegator delegator) throws GenericEntityException {
89
90         List JavaDoc possibleTelecomNumbers = getContactMechsByPurpose(partyId, "TELECOM_NUMBER", contactMechPurposeTypeId, getActiveOnly, delegator);
91         if ((possibleTelecomNumbers == null) || (possibleTelecomNumbers.size() == 0)) {
92             Debug.logInfo("No suitable phone number found for [" + partyId + "] with purpose ["
93                     + contactMechPurposeTypeId + "] and getActiveOnly = [" + getActiveOnly + "]", module);
94             return null;
95         }
96         GenericValue contactMech = (GenericValue) possibleTelecomNumbers.get(0);
97         GenericValue telecomNumber = contactMech.getRelatedOne("TelecomNumber");
98         if (telecomNumber == null) {
99             Debug.logInfo("No telecom number was related to contact mech [" + contactMech + "]", module);
100             return null;
101         }
102
103         // build the return map
104
Map JavaDoc returnMap = telecomNumber.getAllFields();
105
106         // get the extension, which is in the PartyContactMech entity
107
List JavaDoc possiblePartyContactMechs = contactMech.getRelatedByAnd("PartyContactMech", UtilMisc.toMap("partyId", partyId));
108         if (possiblePartyContactMechs.size() > 0) {
109             GenericValue partyContactMech = (GenericValue) possiblePartyContactMechs.get(0);
110             returnMap.put("extension", partyContactMech.get("extension"));
111         }
112
113         return returnMap;
114     }
115
116     /**
117      * Helper method to get the first telecom number of a party as a GenericValue for a given contactMechPurposeTypeId.
118      * (ie, PHONE_WORK, PRIMARY_PHONE, etc.) If there are many, the first in the List is returned.
119      * If there are none, then a null is returned. This is deprecated, use getTelecomNumberMapByPurpose() instead.
120      *
121      * @param partyId
122      * @param contactMechPurposeTypeId purpose of phone number
123      * @param getActiveOnly
124      * @return GenericValue TelecomNumber
125      * @deprecated
126      */

127     public static GenericValue getTelecomNumberValueByPurpose(String JavaDoc partyId, String JavaDoc contactMechPurposeTypeId,
128             boolean getActiveOnly, GenericDelegator delegator) throws GenericEntityException {
129
130         List JavaDoc possibleTelecomNumbers = getContactMechsByPurpose(partyId, "TELECOM_NUMBER", contactMechPurposeTypeId, getActiveOnly, delegator);
131         if ((possibleTelecomNumbers == null) || (possibleTelecomNumbers.size() == 0)) {
132             Debug.logInfo("No suitable phone number found for [" + partyId + "] with purpose ["
133                     + contactMechPurposeTypeId + "] and getActiveOnly = [" + getActiveOnly + "]", module);
134             return null;
135         }
136         GenericValue contactMech = (GenericValue) possibleTelecomNumbers.get(0);
137         GenericValue telecomNumber = contactMech.getRelatedOne("TelecomNumber");
138         if (telecomNumber == null) {
139             Debug.logInfo("No telecom number was related to contact mech [" + contactMech + "]", module);
140         }
141         return telecomNumber;
142     }
143
144     /**
145      * A helper method to return the telecom number of a party as a String for a given contactMechPurposeTypeId
146      * (ie, PHONE_WORK, PRIMARY_PHONE, etc.) If there are many, the first in the List is returned.
147      * (If you need to work with the whole List, use getContactMechByPurpose.
148      *
149      * @param partyId
150      * @param contactMechPurposeTypeId purpose of phone number
151      * @param getActiveOnly
152      * @return String phone number in 1 (123) 4567890 format or null if there is no phone number
153      */

154     public static String JavaDoc getTelecomNumberByPurpose(String JavaDoc partyId, String JavaDoc contactMechPurposeTypeId,
155             boolean getActiveOnly, GenericDelegator delegator) throws GenericEntityException {
156         
157         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
158         
159         Map JavaDoc telecomNumber = getTelecomNumberMapByPurpose(partyId, contactMechPurposeTypeId, getActiveOnly, delegator);
160
161         if (telecomNumber == null) return null;
162
163         if (telecomNumber.get("countryCode") != null) {
164             buff.append((String JavaDoc) telecomNumber.get("countryCode")).append(" ");
165         }
166         if (telecomNumber.get("areaCode") != null) {
167             buff.append("(").append((String JavaDoc) telecomNumber.get("areaCode")).append(") ");
168         }
169         if (telecomNumber.get("contactNumber") != null) {
170             buff.append((String JavaDoc) telecomNumber.get("contactNumber"));
171         }
172         if (telecomNumber.get("extension") != null) {
173             buff.append(" x").append((String JavaDoc) telecomNumber.get("extension"));
174         }
175
176         // done to standardize all API methods to return null when empty
177
return (buff.length() == 0 ? null : buff.toString());
178     }
179     
180     /**
181      * Same as above but only returns currently active phone numbers
182      *
183      * @param partyId
184      * @param contactMechPurposeTypeId
185      * @return
186      */

187     public static String JavaDoc getTelecomNumberByPurpose(String JavaDoc partyId, String JavaDoc contactMechPurposeTypeId, GenericDelegator delegator) throws GenericEntityException {
188         return getTelecomNumberByPurpose(partyId, contactMechPurposeTypeId, true, delegator);
189     }
190     
191     /**
192      * The point of this method is to get the ContactMech.infoString which is the value for any ContactMech of the type of the electronic address.
193      * For example, fetching the contact mech type WEB_ADDRESS with purpose PRIMARY_WEB_URL might result in "http://example.domain"
194      * Returns the infoString from the first one of specified type/purpose
195      * @param partyId
196      * @param contactMechTypeId
197      * @param contactMechPurposeTypeId
198      * @param getActiveOnly
199      * @param delegator
200      * @return
201      * @throws GenericEntityException
202      */

203     public static String JavaDoc getElectronicAddressByPurpose(String JavaDoc partyId, String JavaDoc contactMechTypeId, String JavaDoc contactMechPurposeTypeId,
204             boolean getActiveOnly, GenericDelegator delegator) throws GenericEntityException {
205         
206         List JavaDoc possibleAddresses = getContactMechsByPurpose(partyId, contactMechTypeId, contactMechPurposeTypeId, getActiveOnly, delegator);
207         if ((possibleAddresses != null) && (possibleAddresses.size() > 0)) {
208             GenericValue contactMech = (GenericValue) possibleAddresses.get(0);
209             if (contactMech != null) {
210                 return contactMech.getString("infoString");
211             } else {
212                 Debug.logInfo("No [" + contactMechTypeId + "] related to partyId [" + partyId + "] with purpose [" + contactMechPurposeTypeId + "] and getActiveOnly = [" + getActiveOnly + "]", module);
213             }
214         }
215         
216         return null;
217     }
218     
219     /**
220      * Same as above but only returns active electronic addresses
221      * @param partyId
222      * @param contactMechTypeId
223      * @param contactMechPurposeTypeId
224      * @param delegator
225      * @return
226      * @throws GenericEntityException
227      */

228     public static String JavaDoc getElectronicAddressByPurpose(String JavaDoc partyId, String JavaDoc contactMechTypeId, String JavaDoc contactMechPurposeTypeId,
229             GenericDelegator delegator) throws GenericEntityException {
230         return getElectronicAddressByPurpose(partyId, contactMechTypeId, contactMechPurposeTypeId, true, delegator);
231     }
232     
233     /**
234      * This method returns a GenericValue rather than a String because a PostalAddress is fairly complicated, and the user may want to
235      * format it himself in a FTL page
236      * @param partyId
237      * @param contactMechPurposeTypeId
238      * @param getActiveOnly
239      * @param delegator
240      * @return First PostalAddress of the specified contactMechPurposeTypeId
241      * @throws GenericEntityException
242      */

243     public static GenericValue getPostalAddressValueByPurpose(String JavaDoc partyId, String JavaDoc contactMechPurposeTypeId,
244             boolean getActiveOnly, GenericDelegator delegator) throws GenericEntityException {
245         List JavaDoc possibleAddresses = getContactMechsByPurpose(partyId, "POSTAL_ADDRESS", contactMechPurposeTypeId, getActiveOnly, delegator);
246         
247         if ((possibleAddresses != null) && (possibleAddresses.size() > 0)) {
248             GenericValue contactMech = ((GenericValue) possibleAddresses.get(0)).getRelatedOne("ContactMech");
249             if (contactMech != null) {
250                 return contactMech.getRelatedOne("PostalAddress");
251             } else {
252                 Debug.logInfo("No Postal Address related to partyId [" + partyId + "] with purpose [" + contactMechPurposeTypeId + "] and getActiveOnly = [" + getActiveOnly + "]", module);
253             }
254         }
255         return null;
256     }
257     
258     
259     /**
260      * This is a commonly used shorthand display for a postal address. Only supports currently active addresses.
261      *
262      * @param partyId
263      * @param contactMechPurposeTypeId
264      * @param delegator
265      * @return Abbreviated string for postal address of the contactMechPurposeTypeId. Currently just City, ST. null if no postal address.
266      * @throws GenericEntityException
267      */

268     public static String JavaDoc getAbbrevPostalAddressByPurpose(String JavaDoc partyId, String JavaDoc contactMechPurposeTypeId,
269             GenericDelegator delegator) throws GenericEntityException {
270         GenericValue postalAddress = getPostalAddressValueByPurpose(partyId, contactMechPurposeTypeId, true, delegator);
271         
272         String JavaDoc abbrevPostalAddress = "";
273         if (postalAddress != null) {
274             if (postalAddress.getString("city") != null) {
275                 abbrevPostalAddress += postalAddress.getString("city") + ", ";
276             }
277             if (postalAddress.getString("stateProvinceGeoId") != null) {
278                 abbrevPostalAddress += postalAddress.getString("stateProvinceGeoId");
279             }
280         } else {
281             Debug.logInfo("No Postal Address related to partyId [" + partyId + "] with purpose [" + contactMechPurposeTypeId + "]", module);
282         }
283         
284         if (abbrevPostalAddress.equals("")) {
285             return null;
286         } else {
287             return abbrevPostalAddress;
288         }
289     }
290 }
291
Popular Tags