KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > pseudotag > EntityField


1 /*
2  * $Id: EntityField.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2003 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.webapp.pseudotag;
25
26 import java.io.IOException JavaDoc;
27 import java.text.DateFormat JavaDoc;
28 import java.text.NumberFormat JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32 import javax.servlet.jsp.JspWriter JavaDoc;
33 import javax.servlet.jsp.PageContext JavaDoc;
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 /**
41  * Pseudo-Tag to Print Localized Entity Fields
42  *
43  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
44  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
45  * @version $Rev: 5462 $
46  * @since 2.0
47  */

48 public class EntityField {
49
50     PageContext JavaDoc pageContextInternal = null;
51
52     public EntityField(PageContext JavaDoc pageContextInternal) {
53         this.pageContextInternal = pageContextInternal;
54     }
55
56     public void run(String JavaDoc attribute, String JavaDoc field)
57         throws IOException JavaDoc, GenericEntityException {
58         run(attribute, field, null, null, null, null, pageContextInternal);
59     }
60
61     public void run(String JavaDoc attribute, String JavaDoc field, String JavaDoc defaultStr)
62         throws IOException JavaDoc, GenericEntityException {
63         run(attribute, field, null, null, defaultStr, null, pageContextInternal);
64     }
65
66     public void run(String JavaDoc attribute, String JavaDoc field, String JavaDoc prefix, String JavaDoc suffix)
67         throws IOException JavaDoc, GenericEntityException {
68         run(attribute, field, prefix, suffix, null, null, pageContextInternal);
69     }
70
71     /** Run the EntityField Pseudo-Tag, all fields except attribute, and field can be null */
72     public void run(String JavaDoc attribute, String JavaDoc field, String JavaDoc prefix, String JavaDoc suffix,
73         String JavaDoc defaultStr, String JavaDoc type) throws IOException JavaDoc, GenericEntityException {
74         run(attribute, field, prefix, suffix, defaultStr, type, pageContextInternal);
75     }
76
77     /* --- STATIC METHODS --- */
78
79     public static void run(String JavaDoc attribute, String JavaDoc field,
80         PageContext JavaDoc pageContext) throws IOException JavaDoc, GenericEntityException {
81         run(attribute, field, null, null, null, null, pageContext);
82     }
83
84     public static void run(String JavaDoc attribute, String JavaDoc field, String JavaDoc defaultStr,
85         PageContext JavaDoc pageContext) throws IOException JavaDoc, GenericEntityException {
86         run(attribute, field, null, null, defaultStr, null, pageContext);
87     }
88
89     public static void run(String JavaDoc attribute, String JavaDoc field, String JavaDoc prefix, String JavaDoc suffix,
90         PageContext JavaDoc pageContext) throws IOException JavaDoc, GenericEntityException {
91         run(attribute, field, prefix, suffix, null, null, pageContext);
92     }
93
94     /** Run the EntityField Pseudo-Tag, all fields except attribute, field, and pageContext can be null */
95     public static void run(String JavaDoc attribute, String JavaDoc field, String JavaDoc prefix, String JavaDoc suffix,
96         String JavaDoc defaultStr, String JavaDoc type, PageContext JavaDoc pageContext) throws IOException JavaDoc, GenericEntityException {
97         if (attribute == null || pageContext == null) {
98             throw new IllegalArgumentException JavaDoc("Required parameter (attribute or pageContext) missing");
99         }
100
101         if (defaultStr == null) defaultStr = "";
102         String JavaDoc fieldObjectType = null;
103         Object JavaDoc fieldObject = null;
104
105         /* TYPE and FIELD should not be used together. TYPE defines the type of an object.
106          * When FIELD is defined, type is assumed to be a GenericValue or a Map.
107          */

108
109         // We should be a ValueObject
110
if (type == null) {
111             Object JavaDoc attrObject = pageContext.findAttribute(attribute);
112
113             if (attrObject == null) {
114                 fieldObject = defaultStr;
115                 fieldObjectType = "comment"; // Default for NULL objects.
116
} else {
117                 if (attrObject instanceof GenericValue) {
118                     // Get the ValueObject from PageContext.
119
GenericValue valueObject = (GenericValue) attrObject;
120                     ModelEntity entityModel = valueObject.getModelEntity();
121
122                     fieldObject = valueObject.get(field);
123
124                     // Get the Object Type.
125
if (fieldObject != null) {
126                         ModelField fieldModel = entityModel.getField(field);
127
128                         fieldObjectType = fieldModel.getType();
129                     } else {
130                         // Debug.logWarning("[EntityFieldTag] : Null ValueObject passed.", module);
131
fieldObject = defaultStr;
132                         fieldObjectType = "comment"; // Default for NULL objects.
133
}
134                 } else if (attrObject instanceof Map JavaDoc) {
135                     Map JavaDoc valueMap = (Map JavaDoc) attrObject;
136
137                     fieldObject = valueMap.get(field);
138                     fieldObjectType = "comment"; // Default for NULL objects.
139
} else {
140                     // handle non-composite types directly
141
fieldObject = attrObject;
142                     fieldObjectType = "comment"; // Default for Strings.
143
}
144             }
145         } else {
146             // We should be either a 'currency' or a java type.
147
fieldObject = pageContext.findAttribute(attribute);
148             // javaType = type;
149
// Set a default for NULL objects.
150
if (fieldObject == null) {
151                 // Debug.logWarning("[EntityFieldTag] : Null Object passed.", module);
152
fieldObject = defaultStr;
153                 // javaType = "java.lang.String";
154
}
155             if (type.equalsIgnoreCase("currency")) {
156                 // Convert the String to a Double for standard processing.
157
if (fieldObject instanceof String JavaDoc) {
158                     String JavaDoc objStr = (String JavaDoc) fieldObject;
159
160                     try {
161                         if (objStr.length() > 0) {
162                             fieldObject = new Double JavaDoc(objStr);
163                         }
164                     } catch (NumberFormatException JavaDoc nfe) {
165                         throw new IllegalStateException JavaDoc("String not a number for printing of type currency: " + objStr);
166                     }
167                 }
168                 // The default type for currency is Double.
169
// javaType = "java.lang.Double";
170
fieldObjectType = "currency-amount";
171             }
172         }
173
174         // Get the Locale from the Request object.
175
Locale JavaDoc userLocale = null;
176
177         if (false) {
178             // disable this until we get i18n issues addressed
179
userLocale = pageContext.getRequest().getLocale();
180         }
181         if (userLocale == null) {
182             userLocale = Locale.getDefault();
183         }
184
185         // Format the Object based on its type.
186
String JavaDoc fieldString = null;
187
188         if (fieldObject instanceof java.lang.String JavaDoc) {
189             fieldString = (String JavaDoc) fieldObject;
190         } else if (fieldObject instanceof java.lang.Double JavaDoc) {
191             Double JavaDoc doubleValue = (Double JavaDoc) fieldObject;
192             NumberFormat JavaDoc nf = null;
193
194             if ("currency-amount".equals(fieldObjectType)) {
195                 // TODO: convert currency to current Locale
196
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 JavaDoc) {
202             Float JavaDoc floatValue = (Float JavaDoc) fieldObject;
203             NumberFormat JavaDoc nf = null;
204
205             if ("currency-amount".equals(fieldObjectType)) {
206                 // TODO: convert currency to current Locale
207
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 JavaDoc) {
213             Long JavaDoc longValue = (Long JavaDoc) fieldObject;
214             NumberFormat JavaDoc nf = NumberFormat.getNumberInstance(userLocale);
215
216             fieldString = nf.format(longValue);
217         } else if (fieldObject instanceof java.lang.Integer JavaDoc) {
218             Integer JavaDoc intValue = (Integer JavaDoc) fieldObject;
219             NumberFormat JavaDoc nf = NumberFormat.getNumberInstance(userLocale);
220
221             fieldString = nf.format(intValue);
222         } else if (fieldObject instanceof java.lang.Boolean JavaDoc) {
223             Boolean JavaDoc booleanValue = (Boolean JavaDoc) fieldObject;
224
225             if (booleanValue.booleanValue()) {
226                 fieldString = "Yes";
227             } else {
228                 fieldString = "No";
229             }
230         } else if (fieldObject instanceof java.sql.Timestamp JavaDoc) {
231             Date JavaDoc dateValue = (Date JavaDoc) fieldObject;
232             DateFormat JavaDoc df = DateFormat.getDateTimeInstance(DateFormat.LONG,
233                     DateFormat.FULL, userLocale);
234
235             fieldString = df.format(dateValue);
236         } else if (fieldObject instanceof java.sql.Time JavaDoc) {
237             Date JavaDoc dateValue = (Date JavaDoc) fieldObject;
238             DateFormat JavaDoc df = DateFormat.getTimeInstance(DateFormat.FULL, userLocale);
239
240             fieldString = df.format(dateValue);
241         } else if (fieldObject instanceof java.sql.Date JavaDoc) {
242             Date JavaDoc dateValue = (Date JavaDoc) fieldObject;
243             DateFormat JavaDoc 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 JavaDoc 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