001: /*
002: * $Id: PartyHelper.java,v 1.2 2004/01/22 15:36:12 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: package org.ofbiz.party.party;
025:
026: import org.ofbiz.base.util.Debug;
027: import org.ofbiz.base.util.UtilFormatOut;
028: import org.ofbiz.base.util.UtilMisc;
029: import org.ofbiz.base.util.UtilValidate;
030: import org.ofbiz.entity.GenericDelegator;
031: import org.ofbiz.entity.GenericEntityException;
032: import org.ofbiz.entity.GenericValue;
033:
034: /**
035: * PartyHelper
036: *
037: * @author <a href="mailto:epabst@bigfoot.com">Eric Pabst</a>
038: * @version $Revision: 1.2 $
039: * @since 2.0
040: */
041: public class PartyHelper {
042:
043: public static final String module = PartyHelper.class.getName();
044:
045: public static String formatPartyId(String partyId,
046: GenericDelegator delegator) {
047: if (UtilValidate.isEmpty(partyId))
048: return "(none)";
049: GenericValue person = null;
050:
051: try {
052: person = delegator.findByPrimaryKey("Person", UtilMisc
053: .toMap("partyId", partyId));
054: } catch (GenericEntityException gee) {
055: Debug.logWarning(gee, module);
056: }
057: if (person != null) {
058: return getPersonName(person);
059: } else {
060: return partyId;
061: }
062: }
063:
064: public static String getPersonName(GenericValue person) {
065: StringBuffer result = new StringBuffer(20);
066:
067: if (person != null) {
068: result.append(UtilFormatOut.ifNotEmpty(person
069: .getString("firstName"), "", " "));
070: result.append(UtilFormatOut.ifNotEmpty(person
071: .getString("middleName"), "", " "));
072: result.append(UtilFormatOut.checkNull(person
073: .getString("lastName")));
074: }
075: return result.toString().trim();
076: }
077:
078: public static String getPartyName(GenericValue partyObject) {
079: return getPartyName(partyObject, false);
080: }
081:
082: public static String getPartyName(GenericValue partyObject,
083: boolean lastNameFirst) {
084: StringBuffer result = new StringBuffer(20);
085:
086: GenericValue workingObject = null;
087: if ("UserLogin".equals(partyObject.getEntityName())) {
088: try {
089: workingObject = partyObject.getDelegator()
090: .findByPrimaryKey(
091: "Person",
092: UtilMisc.toMap("partyId", partyObject
093: .getString("partyId")));
094:
095: if (workingObject == null) {
096: workingObject = partyObject
097: .getDelegator()
098: .findByPrimaryKey(
099: "PartyGroup",
100: UtilMisc
101: .toMap(
102: "partyId",
103: partyObject
104: .getString("partyId")));
105: }
106: } catch (GenericEntityException e) {
107: Debug.logWarning(e, module);
108: }
109: } else {
110: workingObject = partyObject;
111: }
112:
113: if (workingObject != null) {
114: if ("Person".equals(workingObject.getEntityName())) {
115: if (lastNameFirst) {
116: if (UtilFormatOut.checkNull(workingObject
117: .getString("lastName")) != null) {
118: result.append(UtilFormatOut
119: .checkNull(workingObject
120: .getString("lastName")));
121: if (UtilFormatOut.checkNull(workingObject
122: .getString("firstName")) != null) {
123: result.append(", ");
124: }
125: }
126: if (UtilFormatOut.checkNull(workingObject
127: .getString("firstName")) != null) {
128: result.append(UtilFormatOut
129: .checkNull(workingObject
130: .getString("firstName")));
131: }
132: } else {
133: result.append(UtilFormatOut.ifNotEmpty(
134: workingObject.getString("firstName"), "",
135: " "));
136: result.append(UtilFormatOut.ifNotEmpty(
137: workingObject.getString("middleName"), "",
138: " "));
139: result.append(UtilFormatOut.checkNull(workingObject
140: .getString("lastName")));
141: }
142: } else if ("PartyGroup".equals(workingObject
143: .getEntityName())) {
144: result.append(workingObject.getString("groupName"));
145: }
146: }
147:
148: return result.toString().trim();
149: }
150: }
|