001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.party.contact;
019:
020: import java.util.Collection;
021: import java.util.Collections;
022: import java.util.List;
023:
024: import org.ofbiz.base.util.Debug;
025: import org.ofbiz.base.util.UtilMisc;
026: import org.ofbiz.entity.GenericEntityException;
027: import org.ofbiz.entity.GenericValue;
028: import org.ofbiz.entity.util.EntityUtil;
029:
030: /**
031: * Accessors for Contact Mechanisms
032: */
033: public class ContactHelper {
034:
035: public static final String module = ContactHelper.class.getName();
036:
037: public static Collection getContactMech(GenericValue party,
038: boolean includeOld) {
039: return getContactMech(party, null, null, includeOld);
040: }
041:
042: public static Collection getContactMechByType(GenericValue party,
043: String contactMechTypeId, boolean includeOld) {
044: return getContactMech(party, null, contactMechTypeId,
045: includeOld);
046: }
047:
048: public static Collection getContactMechByPurpose(
049: GenericValue party, String contactMechPurposeTypeId,
050: boolean includeOld) {
051: return getContactMech(party, contactMechPurposeTypeId, null,
052: includeOld);
053: }
054:
055: public static Collection getContactMech(GenericValue party,
056: String contactMechPurposeTypeId, String contactMechTypeId,
057: boolean includeOld) {
058: if (party == null)
059: return null;
060: try {
061: List partyContactMechList;
062:
063: if (contactMechPurposeTypeId == null) {
064: partyContactMechList = party
065: .getRelated("PartyContactMech");
066: } else {
067: List list;
068:
069: list = party.getRelatedByAnd("PartyContactMechPurpose",
070: UtilMisc.toMap("contactMechPurposeTypeId",
071: contactMechPurposeTypeId));
072: if (!includeOld) {
073: list = EntityUtil.filterByDate(list, true);
074: }
075: partyContactMechList = EntityUtil.getRelated(
076: "PartyContactMech", list);
077: }
078: if (!includeOld) {
079: partyContactMechList = EntityUtil.filterByDate(
080: partyContactMechList, true);
081: }
082: partyContactMechList = EntityUtil.orderBy(
083: partyContactMechList, UtilMisc
084: .toList("fromDate DESC"));
085: if (contactMechTypeId == null) {
086: return EntityUtil.getRelated("ContactMech",
087: partyContactMechList);
088: } else {
089: return EntityUtil.getRelatedByAnd("ContactMech",
090: UtilMisc.toMap("contactMechTypeId",
091: contactMechTypeId),
092: partyContactMechList);
093: }
094: } catch (GenericEntityException gee) {
095: Debug.logWarning(gee, module);
096: return Collections.EMPTY_LIST;
097: }
098: }
099:
100: public static String formatCreditCard(GenericValue creditCardInfo) {
101: StringBuffer result = new StringBuffer(16);
102:
103: result.append(creditCardInfo.getString("cardType"));
104: String cardNumber = creditCardInfo.getString("cardNumber");
105:
106: if (cardNumber != null && cardNumber.length() > 4) {
107: result.append(' ').append(
108: cardNumber.substring(cardNumber.length() - 4));
109: }
110: result.append(' ').append(
111: creditCardInfo.getString("expireDate"));
112: return result.toString();
113: }
114:
115: }
|