1 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 41 public class PartyHelper { 42 43 public static final String module = PartyHelper.class.getName(); 44 45 public static String getPartyName(GenericValue partyObject) { 46 return getPartyName(partyObject, false); 47 } 48 49 public static String getPartyName(GenericDelegator delegator, String 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 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 partyId = null; 71 try { 72 partyId = partyObject.getString("partyId"); 73 } catch (IllegalArgumentException 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 formatPartyNameObject(GenericValue partyValue, boolean lastNameFirst) { 87 if (partyValue == null) { 88 return ""; 89 } 90 StringBuffer result = new StringBuffer (); 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 |