001: /*
002: * $Id: ContactHelper.java,v 1.2 2003/11/04 18:46:30 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.party.contact;
026:
027: import java.util.Collection;
028: import java.util.Collections;
029: import java.util.List;
030:
031: import org.ofbiz.base.util.Debug;
032: import org.ofbiz.base.util.UtilMisc;
033: import org.ofbiz.entity.GenericEntityException;
034: import org.ofbiz.entity.GenericValue;
035: import org.ofbiz.entity.util.EntityUtil;
036:
037: /**
038: * Accessors for Contact Mechanisms
039: *
040: * @author <a href="mailto:epabst@bigfoot.com">Eric Pabst</a>
041: * @version $Revision: 1.2 $
042: * @since 2.0
043: */
044: public class ContactHelper {
045:
046: public static final String module = ContactHelper.class.getName();
047:
048: public static Collection getContactMech(GenericValue party,
049: boolean includeOld) {
050: return getContactMech(party, null, null, includeOld);
051: }
052:
053: public static Collection getContactMechByType(GenericValue party,
054: String contactMechTypeId, boolean includeOld) {
055: return getContactMech(party, null, contactMechTypeId,
056: includeOld);
057: }
058:
059: public static Collection getContactMechByPurpose(
060: GenericValue party, String contactMechPurposeTypeId,
061: boolean includeOld) {
062: return getContactMech(party, contactMechPurposeTypeId, null,
063: includeOld);
064: }
065:
066: public static Collection getContactMech(GenericValue party,
067: String contactMechPurposeTypeId, String contactMechTypeId,
068: boolean includeOld) {
069: if (party == null)
070: return null;
071: try {
072: List partyContactMechList;
073:
074: if (contactMechPurposeTypeId == null) {
075: partyContactMechList = party
076: .getRelated("PartyContactMech");
077: } else {
078: List list;
079:
080: list = party.getRelatedByAnd("PartyContactMechPurpose",
081: UtilMisc.toMap("contactMechPurposeTypeId",
082: contactMechPurposeTypeId));
083: if (!includeOld) {
084: list = EntityUtil.filterByDate(list, true);
085: }
086: partyContactMechList = EntityUtil.getRelated(
087: "PartyContactMech", list);
088: }
089: if (!includeOld) {
090: partyContactMechList = EntityUtil.filterByDate(
091: partyContactMechList, true);
092: }
093: partyContactMechList = EntityUtil.orderBy(
094: partyContactMechList, UtilMisc
095: .toList("fromDate DESC"));
096: if (contactMechTypeId == null) {
097: return EntityUtil.getRelated("ContactMech",
098: partyContactMechList);
099: } else {
100: return EntityUtil.getRelatedByAnd("ContactMech",
101: UtilMisc.toMap("contactMechTypeId",
102: contactMechTypeId),
103: partyContactMechList);
104: }
105: } catch (GenericEntityException gee) {
106: Debug.logWarning(gee, module);
107: return Collections.EMPTY_LIST;
108: }
109: }
110:
111: public static String formatCreditCard(GenericValue creditCardInfo) {
112: StringBuffer result = new StringBuffer(16);
113:
114: result.append(creditCardInfo.getString("cardType"));
115: String cardNumber = creditCardInfo.getString("cardNumber");
116:
117: if (cardNumber != null && cardNumber.length() > 4) {
118: result.append(' ').append(
119: cardNumber.substring(cardNumber.length() - 4));
120: }
121: result.append(' ').append(
122: creditCardInfo.getString("expireDate"));
123: return result.toString();
124: }
125:
126: }
|