| 1 16 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 ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 40 41 public class PartyContactHelper { 42 43 public static final String module = PartyContactHelper.class.getName(); 44 45 54 public static List getContactMechsByPurpose(String partyId, String contactMechTypeId, String contactMechPurposeTypeId, 55 boolean getActiveOnly, GenericDelegator delegator) throws GenericEntityException { 56 Map lookupParams = UtilMisc.toMap("partyId", partyId, "contactMechTypeId", contactMechTypeId); 58 if (contactMechPurposeTypeId != null) { 59 lookupParams.put("contactMechPurposeTypeId", contactMechPurposeTypeId); 60 } 61 List potentialContactMechs = delegator.findByAnd("PartyContactWithPurpose", lookupParams); 62 63 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 87 public static Map getTelecomNumberMapByPurpose(String partyId, String contactMechPurposeTypeId, 88 boolean getActiveOnly, GenericDelegator delegator) throws GenericEntityException { 89 90 List 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 Map returnMap = telecomNumber.getAllFields(); 105 106 List 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 127 public static GenericValue getTelecomNumberValueByPurpose(String partyId, String contactMechPurposeTypeId, 128 boolean getActiveOnly, GenericDelegator delegator) throws GenericEntityException { 129 130 List 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 154 public static String getTelecomNumberByPurpose(String partyId, String contactMechPurposeTypeId, 155 boolean getActiveOnly, GenericDelegator delegator) throws GenericEntityException { 156 157 StringBuffer buff = new StringBuffer (); 158 159 Map telecomNumber = getTelecomNumberMapByPurpose(partyId, contactMechPurposeTypeId, getActiveOnly, delegator); 160 161 if (telecomNumber == null) return null; 162 163 if (telecomNumber.get("countryCode") != null) { 164 buff.append((String ) telecomNumber.get("countryCode")).append(" "); 165 } 166 if (telecomNumber.get("areaCode") != null) { 167 buff.append("(").append((String ) telecomNumber.get("areaCode")).append(") "); 168 } 169 if (telecomNumber.get("contactNumber") != null) { 170 buff.append((String ) telecomNumber.get("contactNumber")); 171 } 172 if (telecomNumber.get("extension") != null) { 173 buff.append(" x").append((String ) telecomNumber.get("extension")); 174 } 175 176 return (buff.length() == 0 ? null : buff.toString()); 178 } 179 180 187 public static String getTelecomNumberByPurpose(String partyId, String contactMechPurposeTypeId, GenericDelegator delegator) throws GenericEntityException { 188 return getTelecomNumberByPurpose(partyId, contactMechPurposeTypeId, true, delegator); 189 } 190 191 203 public static String getElectronicAddressByPurpose(String partyId, String contactMechTypeId, String contactMechPurposeTypeId, 204 boolean getActiveOnly, GenericDelegator delegator) throws GenericEntityException { 205 206 List 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 228 public static String getElectronicAddressByPurpose(String partyId, String contactMechTypeId, String contactMechPurposeTypeId, 229 GenericDelegator delegator) throws GenericEntityException { 230 return getElectronicAddressByPurpose(partyId, contactMechTypeId, contactMechPurposeTypeId, true, delegator); 231 } 232 233 243 public static GenericValue getPostalAddressValueByPurpose(String partyId, String contactMechPurposeTypeId, 244 boolean getActiveOnly, GenericDelegator delegator) throws GenericEntityException { 245 List 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 268 public static String getAbbrevPostalAddressByPurpose(String partyId, String contactMechPurposeTypeId, 269 GenericDelegator delegator) throws GenericEntityException { 270 GenericValue postalAddress = getPostalAddressValueByPurpose(partyId, contactMechPurposeTypeId, true, delegator); 271 272 String 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 |