KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > ftl > OfbizAmountTransform


1 /*
2  * $Id: OfbizAmountTransform.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 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  */

25 package org.ofbiz.webapp.ftl;
26
27 import java.io.IOException JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Map JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32
33 import freemarker.core.Environment;
34 import freemarker.ext.beans.BeanModel;
35 import freemarker.ext.beans.NumberModel;
36 import freemarker.template.SimpleNumber;
37 import freemarker.template.SimpleScalar;
38 import freemarker.template.TemplateModelException;
39 import freemarker.template.TemplateScalarModel;
40 import freemarker.template.TemplateTransformModel;
41
42 import org.ofbiz.base.util.Debug;
43 import org.ofbiz.base.util.UtilFormatOut;
44 import org.ofbiz.base.util.UtilHttp;
45
46 /**
47  * OfbizAmountTransform - Freemarker Transform for content links
48  *
49  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
50  * @author <a HREF="mailto:tiz@sastau.it">Jacopo Cappellato</a>
51  * @version $Rev: 5462 $
52  */

53 public class OfbizAmountTransform implements TemplateTransformModel {
54
55     public static final String JavaDoc module = OfbizAmountTransform.class.getName();
56     public static final String JavaDoc SPELLED_OUT_FORMAT = "spelled-out";
57     
58     private static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key) {
59         String JavaDoc result = "";
60         Object JavaDoc o = args.get(key);
61         if (o != null) {
62             if (Debug.verboseOn()) Debug.logVerbose("Arg Object : " + o.getClass().getName(), module);
63             if (o instanceof TemplateScalarModel) {
64                 TemplateScalarModel s = (TemplateScalarModel) o;
65                 try {
66                     result = s.getAsString();
67                 } catch (TemplateModelException e) {
68                     Debug.logError(e, "Template Exception", module);
69                 }
70             } else {
71               result = o.toString();
72             }
73         }
74         return result;
75     }
76
77     private static Double JavaDoc getAmount(Map JavaDoc args, String JavaDoc key) {
78         if (args.containsKey(key)) {
79             Object JavaDoc o = args.get(key);
80             if (Debug.verboseOn()) Debug.logVerbose("Amount Object : " + o.getClass().getName(), module);
81
82             // handle nulls better
83
if (o == null) {
84                 o = new Double JavaDoc(0.00);
85             }
86
87             if (o instanceof NumberModel) {
88                 NumberModel s = (NumberModel) o;
89                 return new Double JavaDoc( s.getAsNumber().doubleValue() );
90             }
91             if (o instanceof SimpleNumber) {
92                 SimpleNumber s = (SimpleNumber) o;
93                 return new Double JavaDoc( s.getAsNumber().doubleValue() );
94             }
95             if (o instanceof SimpleScalar) {
96                 SimpleScalar s = (SimpleScalar) o;
97                 return new Double JavaDoc( s.getAsString() );
98             }
99             return new Double JavaDoc( o.toString() );
100         }
101         return new Double JavaDoc(0.00);
102     }
103     
104     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args) {
105         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
106
107         final Double JavaDoc amount = OfbizAmountTransform.getAmount(args, "amount");
108         final String JavaDoc locale = OfbizAmountTransform.getArg(args, "locale");
109         final String JavaDoc format = OfbizAmountTransform.getArg(args, "format");
110
111         return new Writer JavaDoc(out) {
112             public void write(char cbuf[], int off, int len) {
113                 buf.append(cbuf, off, len);
114             }
115
116             public void flush() throws IOException JavaDoc {
117                 out.flush();
118             }
119
120             public void close() throws IOException JavaDoc {
121                 try {
122                     if (Debug.verboseOn()) Debug.logVerbose("parms: " + amount + " " + format + " " + locale, module);
123                     Locale JavaDoc localeObj = null;
124                     if (locale.length() < 1) {
125                         // Load the locale from the session
126
Environment env = Environment.getCurrentEnvironment();
127                         BeanModel req = (BeanModel) env.getVariable("request");
128                         if (req != null) {
129                             HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) req.getWrappedObject();
130                             localeObj = UtilHttp.getLocale(request);
131                         } else {
132                             localeObj = env.getLocale();
133                         }
134                     } else {
135                         localeObj = new Locale JavaDoc(locale);
136                     }
137                     if (format.equals(OfbizAmountTransform.SPELLED_OUT_FORMAT)) {
138                         out.write(UtilFormatOut.formatSpelledOutAmount(amount.doubleValue(), localeObj));
139                     } else {
140                         out.write(UtilFormatOut.formatAmount(amount.doubleValue(), localeObj));
141                     }
142                 } catch (TemplateModelException e) {
143                     throw new IOException JavaDoc(e.getMessage());
144                 }
145             }
146         };
147     }
148 }
149
Popular Tags