KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > party > party > PartyHelper


1 /*
2  * $Id: PartyHelper.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.party.party;
25
26 import org.ofbiz.base.util.Debug;
27 import org.ofbiz.base.util.UtilFormatOut;
28 import org.ofbiz.base.util.UtilMisc;
29 import org.ofbiz.entity.GenericDelegator;
30 import org.ofbiz.entity.GenericEntityException;
31 import org.ofbiz.entity.GenericValue;
32 import org.ofbiz.entity.model.ModelEntity;
33
34 /**
35  * PartyHelper
36  *
37  * @author <a HREF="mailto:epabst@bigfoot.com">Eric Pabst</a>
38  * @version $Rev: 5462 $
39  * @since 2.0
40  */

41 public class PartyHelper {
42     
43     public static final String JavaDoc module = PartyHelper.class.getName();
44     
45     public static String JavaDoc getPartyName(GenericValue partyObject) {
46         return getPartyName(partyObject, false);
47     }
48
49     public static String JavaDoc getPartyName(GenericDelegator delegator, String JavaDoc partyId, boolean lastNameFirst) {
50         GenericValue partyObject = null;
51         try {
52             partyObject = delegator.findByPrimaryKey("PartyNameView", UtilMisc.toMap("partyId", partyId));
53         } catch (GenericEntityException e) {
54             Debug.logError(e, "Error finding PartyNameView in getPartyName", module);
55         }
56         if (partyObject == null) {
57             return partyId;
58         } else {
59             return formatPartyNameObject(partyObject, lastNameFirst);
60         }
61     }
62
63     public static String JavaDoc getPartyName(GenericValue partyObject, boolean lastNameFirst) {
64         if (partyObject == null) {
65             return "";
66         }
67         if ("PartyGroup".equals(partyObject.getEntityName()) || "Person".equals(partyObject.getEntityName())) {
68             return formatPartyNameObject(partyObject, lastNameFirst);
69         } else {
70             String JavaDoc partyId = null;
71             try {
72                 partyId = partyObject.getString("partyId");
73             } catch (IllegalArgumentException JavaDoc e) {
74                 Debug.logError(e, "Party object does not contain a party ID", module);
75             }
76
77             if (partyId == null) {
78                 Debug.logWarning("No party ID found; cannot get name based on entity: " + partyObject.getEntityName(), module);
79                 return "";
80             } else {
81                 return getPartyName(partyObject.getDelegator(), partyId, lastNameFirst);
82             }
83         }
84     }
85     
86     public static String JavaDoc formatPartyNameObject(GenericValue partyValue, boolean lastNameFirst) {
87         if (partyValue == null) {
88             return "";
89         }
90         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
91         ModelEntity modelEntity = partyValue.getModelEntity();
92         if (modelEntity.isField("firstName") && modelEntity.isField("middleName") && modelEntity.isField("lastName")) {
93             if (lastNameFirst) {
94                 if (UtilFormatOut.checkNull(partyValue.getString("lastName")) != null) {
95                     result.append(UtilFormatOut.checkNull(partyValue.getString("lastName")));
96                     if (partyValue.getString("firstName") != null) {
97                         result.append(", ");
98                     }
99                 }
100                 result.append(UtilFormatOut.checkNull(partyValue.getString("firstName")));
101             } else {
102                 result.append(UtilFormatOut.ifNotEmpty(partyValue.getString("firstName"), "", " "));
103                 result.append(UtilFormatOut.ifNotEmpty(partyValue.getString("middleName"), "", " "));
104                 result.append(UtilFormatOut.checkNull(partyValue.getString("lastName")));
105             }
106         }
107         if (modelEntity.isField("groupName") && partyValue.get("groupName") != null) {
108             result.append(partyValue.getString("groupName"));
109         }
110         return result.toString();
111     }
112 }
113
Popular Tags