1 24 package org.ofbiz.webapp.pseudotag; 25 26 import java.io.IOException ; 27 import java.text.DateFormat ; 28 import java.text.NumberFormat ; 29 import java.util.Date ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 import javax.servlet.jsp.JspWriter ; 33 import javax.servlet.jsp.PageContext ; 34 35 import org.ofbiz.entity.GenericEntityException; 36 import org.ofbiz.entity.GenericValue; 37 import org.ofbiz.entity.model.ModelEntity; 38 import org.ofbiz.entity.model.ModelField; 39 40 48 public class EntityField { 49 50 PageContext pageContextInternal = null; 51 52 public EntityField(PageContext pageContextInternal) { 53 this.pageContextInternal = pageContextInternal; 54 } 55 56 public void run(String attribute, String field) 57 throws IOException , GenericEntityException { 58 run(attribute, field, null, null, null, null, pageContextInternal); 59 } 60 61 public void run(String attribute, String field, String defaultStr) 62 throws IOException , GenericEntityException { 63 run(attribute, field, null, null, defaultStr, null, pageContextInternal); 64 } 65 66 public void run(String attribute, String field, String prefix, String suffix) 67 throws IOException , GenericEntityException { 68 run(attribute, field, prefix, suffix, null, null, pageContextInternal); 69 } 70 71 72 public void run(String attribute, String field, String prefix, String suffix, 73 String defaultStr, String type) throws IOException , GenericEntityException { 74 run(attribute, field, prefix, suffix, defaultStr, type, pageContextInternal); 75 } 76 77 78 79 public static void run(String attribute, String field, 80 PageContext pageContext) throws IOException , GenericEntityException { 81 run(attribute, field, null, null, null, null, pageContext); 82 } 83 84 public static void run(String attribute, String field, String defaultStr, 85 PageContext pageContext) throws IOException , GenericEntityException { 86 run(attribute, field, null, null, defaultStr, null, pageContext); 87 } 88 89 public static void run(String attribute, String field, String prefix, String suffix, 90 PageContext pageContext) throws IOException , GenericEntityException { 91 run(attribute, field, prefix, suffix, null, null, pageContext); 92 } 93 94 95 public static void run(String attribute, String field, String prefix, String suffix, 96 String defaultStr, String type, PageContext pageContext) throws IOException , GenericEntityException { 97 if (attribute == null || pageContext == null) { 98 throw new IllegalArgumentException ("Required parameter (attribute or pageContext) missing"); 99 } 100 101 if (defaultStr == null) defaultStr = ""; 102 String fieldObjectType = null; 103 Object fieldObject = null; 104 105 108 109 if (type == null) { 111 Object attrObject = pageContext.findAttribute(attribute); 112 113 if (attrObject == null) { 114 fieldObject = defaultStr; 115 fieldObjectType = "comment"; } else { 117 if (attrObject instanceof GenericValue) { 118 GenericValue valueObject = (GenericValue) attrObject; 120 ModelEntity entityModel = valueObject.getModelEntity(); 121 122 fieldObject = valueObject.get(field); 123 124 if (fieldObject != null) { 126 ModelField fieldModel = entityModel.getField(field); 127 128 fieldObjectType = fieldModel.getType(); 129 } else { 130 fieldObject = defaultStr; 132 fieldObjectType = "comment"; } 134 } else if (attrObject instanceof Map ) { 135 Map valueMap = (Map ) attrObject; 136 137 fieldObject = valueMap.get(field); 138 fieldObjectType = "comment"; } else { 140 fieldObject = attrObject; 142 fieldObjectType = "comment"; } 144 } 145 } else { 146 fieldObject = pageContext.findAttribute(attribute); 148 if (fieldObject == null) { 151 fieldObject = defaultStr; 153 } 155 if (type.equalsIgnoreCase("currency")) { 156 if (fieldObject instanceof String ) { 158 String objStr = (String ) fieldObject; 159 160 try { 161 if (objStr.length() > 0) { 162 fieldObject = new Double (objStr); 163 } 164 } catch (NumberFormatException nfe) { 165 throw new IllegalStateException ("String not a number for printing of type currency: " + objStr); 166 } 167 } 168 fieldObjectType = "currency-amount"; 171 } 172 } 173 174 Locale userLocale = null; 176 177 if (false) { 178 userLocale = pageContext.getRequest().getLocale(); 180 } 181 if (userLocale == null) { 182 userLocale = Locale.getDefault(); 183 } 184 185 String fieldString = null; 187 188 if (fieldObject instanceof java.lang.String ) { 189 fieldString = (String ) fieldObject; 190 } else if (fieldObject instanceof java.lang.Double ) { 191 Double doubleValue = (Double ) fieldObject; 192 NumberFormat nf = null; 193 194 if ("currency-amount".equals(fieldObjectType)) { 195 nf = NumberFormat.getCurrencyInstance(userLocale); 197 } else { 198 nf = NumberFormat.getNumberInstance(userLocale); 199 } 200 fieldString = nf.format(doubleValue); 201 } else if (fieldObject instanceof java.lang.Float ) { 202 Float floatValue = (Float ) fieldObject; 203 NumberFormat nf = null; 204 205 if ("currency-amount".equals(fieldObjectType)) { 206 nf = NumberFormat.getCurrencyInstance(userLocale); 208 } else { 209 nf = NumberFormat.getNumberInstance(userLocale); 210 } 211 fieldString = nf.format(floatValue); 212 } else if (fieldObject instanceof java.lang.Long ) { 213 Long longValue = (Long ) fieldObject; 214 NumberFormat nf = NumberFormat.getNumberInstance(userLocale); 215 216 fieldString = nf.format(longValue); 217 } else if (fieldObject instanceof java.lang.Integer ) { 218 Integer intValue = (Integer ) fieldObject; 219 NumberFormat nf = NumberFormat.getNumberInstance(userLocale); 220 221 fieldString = nf.format(intValue); 222 } else if (fieldObject instanceof java.lang.Boolean ) { 223 Boolean booleanValue = (Boolean ) fieldObject; 224 225 if (booleanValue.booleanValue()) { 226 fieldString = "Yes"; 227 } else { 228 fieldString = "No"; 229 } 230 } else if (fieldObject instanceof java.sql.Timestamp ) { 231 Date dateValue = (Date ) fieldObject; 232 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, 233 DateFormat.FULL, userLocale); 234 235 fieldString = df.format(dateValue); 236 } else if (fieldObject instanceof java.sql.Time ) { 237 Date dateValue = (Date ) fieldObject; 238 DateFormat df = DateFormat.getTimeInstance(DateFormat.FULL, userLocale); 239 240 fieldString = df.format(dateValue); 241 } else if (fieldObject instanceof java.sql.Date ) { 242 Date dateValue = (Date ) fieldObject; 243 DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, userLocale); 244 245 fieldString = df.format(dateValue); 246 } else { 247 if (fieldObject != null) { 248 fieldString = fieldObject.toString(); 249 } else { 250 fieldString = ""; 251 } 252 } 253 254 JspWriter out = pageContext.getOut(); 255 256 if (fieldString.length() > 0) { 257 if (prefix != null) 258 out.print(prefix); 259 out.print(fieldString); 260 if (suffix != null) 261 out.print(suffix); 262 } 263 } 264 } 265 | Popular Tags |