001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.webapp.pseudotag;
019:
020: import java.io.IOException;
021: import java.text.DateFormat;
022: import java.text.NumberFormat;
023: import java.util.Date;
024: import java.util.Locale;
025: import java.util.Map;
026: import javax.servlet.jsp.JspWriter;
027: import javax.servlet.jsp.PageContext;
028:
029: import org.ofbiz.entity.GenericEntityException;
030: import org.ofbiz.entity.GenericValue;
031: import org.ofbiz.entity.model.ModelEntity;
032: import org.ofbiz.entity.model.ModelField;
033:
034: /**
035: * Pseudo-Tag to Print Localized Entity Fields
036: */
037: public class EntityField {
038:
039: PageContext pageContextInternal = null;
040:
041: public EntityField(PageContext pageContextInternal) {
042: this .pageContextInternal = pageContextInternal;
043: }
044:
045: public void run(String attribute, String field) throws IOException,
046: GenericEntityException {
047: run(attribute, field, null, null, null, null,
048: pageContextInternal);
049: }
050:
051: public void run(String attribute, String field, String defaultStr)
052: throws IOException, GenericEntityException {
053: run(attribute, field, null, null, defaultStr, null,
054: pageContextInternal);
055: }
056:
057: public void run(String attribute, String field, String prefix,
058: String suffix) throws IOException, GenericEntityException {
059: run(attribute, field, prefix, suffix, null, null,
060: pageContextInternal);
061: }
062:
063: /** Run the EntityField Pseudo-Tag, all fields except attribute, and field can be null */
064: public void run(String attribute, String field, String prefix,
065: String suffix, String defaultStr, String type)
066: throws IOException, GenericEntityException {
067: run(attribute, field, prefix, suffix, defaultStr, type,
068: pageContextInternal);
069: }
070:
071: /* --- STATIC METHODS --- */
072:
073: public static void run(String attribute, String field,
074: PageContext pageContext) throws IOException,
075: GenericEntityException {
076: run(attribute, field, null, null, null, null, pageContext);
077: }
078:
079: public static void run(String attribute, String field,
080: String defaultStr, PageContext pageContext)
081: throws IOException, GenericEntityException {
082: run(attribute, field, null, null, defaultStr, null, pageContext);
083: }
084:
085: public static void run(String attribute, String field,
086: String prefix, String suffix, PageContext pageContext)
087: throws IOException, GenericEntityException {
088: run(attribute, field, prefix, suffix, null, null, pageContext);
089: }
090:
091: /** Run the EntityField Pseudo-Tag, all fields except attribute, field, and pageContext can be null */
092: public static void run(String attribute, String field,
093: String prefix, String suffix, String defaultStr,
094: String type, PageContext pageContext) throws IOException,
095: GenericEntityException {
096: if (attribute == null || pageContext == null) {
097: throw new IllegalArgumentException(
098: "Required parameter (attribute or pageContext) missing");
099: }
100:
101: if (defaultStr == null)
102: defaultStr = "";
103: String fieldObjectType = null;
104: Object fieldObject = null;
105:
106: /* TYPE and FIELD should not be used together. TYPE defines the type of an object.
107: * When FIELD is defined, type is assumed to be a GenericValue or a Map.
108: */
109:
110: // We should be a ValueObject
111: if (type == null) {
112: Object attrObject = pageContext.findAttribute(attribute);
113:
114: if (attrObject == null) {
115: fieldObject = defaultStr;
116: fieldObjectType = "comment"; // Default for NULL objects.
117: } else {
118: if (attrObject instanceof GenericValue) {
119: // Get the ValueObject from PageContext.
120: GenericValue valueObject = (GenericValue) attrObject;
121: ModelEntity entityModel = valueObject
122: .getModelEntity();
123:
124: fieldObject = valueObject.get(field);
125:
126: // Get the Object Type.
127: if (fieldObject != null) {
128: ModelField fieldModel = entityModel
129: .getField(field);
130:
131: fieldObjectType = fieldModel.getType();
132: } else {
133: // Debug.logWarning("[EntityFieldTag] : Null ValueObject passed.", module);
134: fieldObject = defaultStr;
135: fieldObjectType = "comment"; // Default for NULL objects.
136: }
137: } else if (attrObject instanceof Map) {
138: Map valueMap = (Map) attrObject;
139:
140: fieldObject = valueMap.get(field);
141: fieldObjectType = "comment"; // Default for NULL objects.
142: } else {
143: // handle non-composite types directly
144: fieldObject = attrObject;
145: fieldObjectType = "comment"; // Default for Strings.
146: }
147: }
148: } else {
149: // We should be either a 'currency' or a java type.
150: fieldObject = pageContext.findAttribute(attribute);
151: // javaType = type;
152: // Set a default for NULL objects.
153: if (fieldObject == null) {
154: // Debug.logWarning("[EntityFieldTag] : Null Object passed.", module);
155: fieldObject = defaultStr;
156: // javaType = "java.lang.String";
157: }
158: if (type.equalsIgnoreCase("currency")) {
159: // Convert the String to a Double for standard processing.
160: if (fieldObject instanceof String) {
161: String objStr = (String) fieldObject;
162:
163: try {
164: if (objStr.length() > 0) {
165: fieldObject = new Double(objStr);
166: }
167: } catch (NumberFormatException nfe) {
168: throw new IllegalStateException(
169: "String not a number for printing of type currency: "
170: + objStr);
171: }
172: }
173: // The default type for currency is Double.
174: // javaType = "java.lang.Double";
175: fieldObjectType = "currency-amount";
176: }
177: }
178:
179: // Get the Locale from the Request object.
180: Locale userLocale = null;
181:
182: if (false) {
183: // disable this until we get i18n issues addressed
184: userLocale = pageContext.getRequest().getLocale();
185: }
186: if (userLocale == null) {
187: userLocale = Locale.getDefault();
188: }
189:
190: // Format the Object based on its type.
191: String fieldString = null;
192:
193: if (fieldObject instanceof java.lang.String) {
194: fieldString = (String) fieldObject;
195: } else if (fieldObject instanceof java.lang.Double) {
196: Double doubleValue = (Double) fieldObject;
197: NumberFormat nf = null;
198:
199: if ("currency-amount".equals(fieldObjectType)) {
200: // TODO: convert currency to current Locale
201: nf = NumberFormat.getCurrencyInstance(userLocale);
202: } else {
203: nf = NumberFormat.getNumberInstance(userLocale);
204: }
205: fieldString = nf.format(doubleValue);
206: } else if (fieldObject instanceof java.lang.Float) {
207: Float floatValue = (Float) fieldObject;
208: NumberFormat nf = null;
209:
210: if ("currency-amount".equals(fieldObjectType)) {
211: // TODO: convert currency to current Locale
212: nf = NumberFormat.getCurrencyInstance(userLocale);
213: } else {
214: nf = NumberFormat.getNumberInstance(userLocale);
215: }
216: fieldString = nf.format(floatValue);
217: } else if (fieldObject instanceof java.lang.Long) {
218: Long longValue = (Long) fieldObject;
219: NumberFormat nf = NumberFormat
220: .getNumberInstance(userLocale);
221:
222: fieldString = nf.format(longValue);
223: } else if (fieldObject instanceof java.lang.Integer) {
224: Integer intValue = (Integer) fieldObject;
225: NumberFormat nf = NumberFormat
226: .getNumberInstance(userLocale);
227:
228: fieldString = nf.format(intValue);
229: } else if (fieldObject instanceof java.lang.Boolean) {
230: Boolean booleanValue = (Boolean) fieldObject;
231:
232: if (booleanValue.booleanValue()) {
233: fieldString = "Yes";
234: } else {
235: fieldString = "No";
236: }
237: } else if (fieldObject instanceof java.sql.Timestamp) {
238: Date dateValue = (Date) fieldObject;
239: DateFormat df = DateFormat.getDateTimeInstance(
240: DateFormat.LONG, DateFormat.FULL, userLocale);
241:
242: fieldString = df.format(dateValue);
243: } else if (fieldObject instanceof java.sql.Time) {
244: Date dateValue = (Date) fieldObject;
245: DateFormat df = DateFormat.getTimeInstance(DateFormat.FULL,
246: userLocale);
247:
248: fieldString = df.format(dateValue);
249: } else if (fieldObject instanceof java.sql.Date) {
250: Date dateValue = (Date) fieldObject;
251: DateFormat df = DateFormat.getDateInstance(DateFormat.LONG,
252: userLocale);
253:
254: fieldString = df.format(dateValue);
255: } else {
256: if (fieldObject != null) {
257: fieldString = fieldObject.toString();
258: } else {
259: fieldString = "";
260: }
261: }
262:
263: JspWriter out = pageContext.getOut();
264:
265: if (fieldString.length() > 0) {
266: if (prefix != null)
267: out.print(prefix);
268: out.print(fieldString);
269: if (suffix != null)
270: out.print(suffix);
271: }
272: }
273: }
|