001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.address;
018:
019: import java.util.HashMap;
020: import java.util.List;
021:
022: import org.ofbiz.base.util.Debug;
023: import org.ofbiz.entity.GenericDelegator;
024: import org.ofbiz.entity.GenericEntityException;
025: import org.ofbiz.entity.GenericPK;
026: import org.ofbiz.entity.GenericValue;
027:
028: import com.sourcetap.sfa.util.UserInfo;
029:
030: /**
031: * DOCUMENT ME!
032: *
033: */
034: public class AddressHelper {
035: public static final String module = AddressHelper.class.getName();
036:
037: public static String getMailingAddress(String mailingAddress,
038: String address1, String address2, String address3) {
039: String address = "";
040: boolean hasInfo = false;
041: if ((mailingAddress != null) && (mailingAddress.length() > 0))
042: return mailingAddress;
043:
044: if ((address1 != null) && (address1.length() > 0)) {
045: hasInfo = true;
046: address = address1;
047: }
048:
049: if ((address2 != null) && (address2.length() > 0)) {
050: if (hasInfo)
051: address += "\n";
052: address += address2;
053: hasInfo = true;
054: }
055:
056: if ((address3 != null) && (address3.length() > 0)) {
057: if (hasInfo)
058: address += "\n";
059: address += address3;
060: hasInfo = true;
061: }
062:
063: return address;
064:
065: }
066:
067: /**
068: * DOCUMENT ME!
069: *
070: * @param userInfo
071: * @param delegator
072: * @param entityGV
073: * @param relationTitle
074: *
075: * @return
076: */
077: public static List findRelated(UserInfo userInfo,
078: GenericDelegator delegator, GenericValue entityGV,
079: String relationTitle) {
080: // Find instances of an entity related to the current entity.
081:
082: if (delegator == null) {
083: Debug.logError("[findRelated] Delegator is required.",
084: module);
085:
086: return null;
087: }
088:
089: if (entityGV == null) {
090: Debug.logError(
091: "[findRelated] Entity generic value is required.",
092: module);
093:
094: return null;
095: }
096:
097: String mainEntityName = entityGV.getEntityName();
098: String relatedEntityName = "Address";
099:
100: // Find related Address records. Need special processing because
101: // the relationship cannot be defined in the sfa-config.xml file.
102: GenericPK entityPK = null;
103: String entityKeyString = null;
104:
105: String entityName = entityGV.getEntityName();
106: List relatedGVL = null;
107:
108: try {
109:
110: HashMap findMap = new HashMap();
111: String addressOwnerId = "";
112:
113: if (mainEntityName.equals("Account")) {
114: addressOwnerId = entityGV.getString("accountId");
115: } else if (mainEntityName.equals("Contact")) {
116: addressOwnerId = entityGV.getString("contactId");
117: } else {
118: // Error.
119: Debug.logError(
120: "[findRelated] Entity name must be 'Account' or 'Contact', "
121: + "but is '" + mainEntityName + "'.",
122: module);
123:
124: return null;
125: }
126:
127: findMap.put("addressOwnerId", addressOwnerId);
128: relatedGVL = delegator
129: .findByAnd(relatedEntityName, findMap);
130: } catch (GenericEntityException e) {
131: Debug.logError("[findRelated] Error getting "
132: + "the related " + relatedEntityName + " records: "
133: + e.getLocalizedMessage(), module);
134:
135: return null;
136: }
137:
138: return relatedGVL;
139: }
140: }
|