001: /*
002: * Copyright 2004-2007 Gary Bentley
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may
005: * not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package org.josql.functions;
016:
017: import java.util.Date;
018:
019: import java.text.SimpleDateFormat;
020: import java.text.DecimalFormat;
021:
022: import com.gentlyweb.utils.TimeDuration;
023: import com.gentlyweb.utils.Timing;
024: import com.gentlyweb.utils.Getter;
025:
026: import org.josql.Query;
027: import org.josql.QueryExecutionException;
028:
029: public class FormattingFunctions extends AbstractFunctionHandler {
030:
031: public static final String HANDLER_ID = "_internal_formatting";
032:
033: public static String DEFAULT_DATE_FORMAT_SPEC = "dd/MMM/yyyy";
034: public static String DEFAULT_DATE_TIME_FORMAT_SPEC = FormattingFunctions.DEFAULT_DATE_FORMAT_SPEC
035: + ", hh:mm:ss";
036: public static String DEFAULT_DECIMAL_FORMAT_SPEC = "###,###,###.##";
037:
038: private SimpleDateFormat defSDF = new SimpleDateFormat(
039: FormattingFunctions.DEFAULT_DATE_FORMAT_SPEC);
040: private SimpleDateFormat defSDTF = new SimpleDateFormat(
041: FormattingFunctions.DEFAULT_DATE_TIME_FORMAT_SPEC);
042:
043: public String formatTimeDuration(Object o)
044: throws QueryExecutionException {
045:
046: if (o instanceof Number) {
047:
048: return TimeDuration.getInstance(((Number) o).longValue())
049: .format();
050:
051: }
052:
053: if (o instanceof Date) {
054:
055: return TimeDuration.getInstance((Date) o).format();
056:
057: }
058:
059: if (o instanceof TimeDuration) {
060:
061: return TimeDuration.getInstance((TimeDuration) o).format();
062:
063: }
064:
065: if (o instanceof Timing) {
066:
067: return TimeDuration.getInstance((Timing) o).format();
068:
069: }
070:
071: throw new QueryExecutionException("Type: "
072: + o.getClass().getName() + " not supported.");
073:
074: }
075:
076: public void setDefaultDateFormatSpec(String spec) {
077:
078: this .defSDF = new SimpleDateFormat(spec);
079:
080: }
081:
082: public String formatDate(Object o) throws QueryExecutionException {
083:
084: if (o == null) {
085:
086: throw new QueryExecutionException(
087: "Cannot format a null date.");
088:
089: }
090:
091: Date d = null;
092:
093: if (o instanceof Date) {
094:
095: d = (Date) o;
096:
097: }
098:
099: if (o instanceof Number) {
100:
101: d = new Date(((Number) o).longValue());
102:
103: }
104:
105: // If this is a string try and parse the string first, basically convert
106: // from one format to another.
107: if (o instanceof String) {
108:
109: d = ((ConversionFunctions) this .q
110: .getFunctionHandler(ConversionFunctions.HANDLER_ID))
111: .toDate((String) o);
112:
113: }
114:
115: if (d == null) {
116:
117: throw new QueryExecutionException("Type: "
118: + o.getClass().getName() + " not supported.");
119:
120: }
121:
122: return this .defSDF.format(d);
123:
124: }
125:
126: public String formatDateTime(Object o)
127: throws QueryExecutionException {
128:
129: if (o == null) {
130:
131: throw new QueryExecutionException(
132: "Cannot format a null date.");
133:
134: }
135:
136: Date d = null;
137:
138: if (o instanceof Date) {
139:
140: d = (Date) o;
141:
142: }
143:
144: if (o instanceof Number) {
145:
146: d = new Date(((Number) o).longValue());
147:
148: }
149:
150: // If this is a string try and parse the string first, basically convert
151: // from one format to another.
152: if (o instanceof String) {
153:
154: d = ((ConversionFunctions) this .q
155: .getFunctionHandler(ConversionFunctions.HANDLER_ID))
156: .toDate((String) o);
157:
158: }
159:
160: if (d == null) {
161:
162: throw new QueryExecutionException("Type: "
163: + o.getClass().getName() + " not supported.");
164:
165: }
166:
167: return this .defSDTF.format(d);
168:
169: }
170:
171: public String formatDate(Query q, Object o, Getter g, String spec,
172: String saveValueName) throws QueryExecutionException {
173:
174: if (g != null) {
175:
176: try {
177:
178: o = g.getValue(o);
179:
180: } catch (Exception e) {
181:
182: throw new QueryExecutionException(
183: "Unable to get value from accessor: " + g, e);
184:
185: }
186:
187: }
188:
189: if (o == null) {
190:
191: return null + "";
192:
193: }
194:
195: Date d = null;
196:
197: if (o instanceof Date) {
198:
199: d = (Date) o;
200:
201: }
202:
203: if (o instanceof Long) {
204:
205: d = new Date(((Long) o).longValue());
206:
207: }
208:
209: Object so = null;
210:
211: if (saveValueName != null) {
212:
213: so = q.getSaveValue(saveValueName);
214:
215: }
216:
217: SimpleDateFormat df = null;
218:
219: if (so != null) {
220:
221: df = (SimpleDateFormat) so;
222:
223: } else {
224:
225: if (spec == null) {
226:
227: spec = FormattingFunctions.DEFAULT_DATE_FORMAT_SPEC;
228:
229: }
230:
231: df = new SimpleDateFormat(spec);
232:
233: }
234:
235: return df.format(d);
236:
237: }
238:
239: public String formatNumber(Object n) throws QueryExecutionException {
240:
241: return this .formatNumber(this .q, n, null, null);
242:
243: }
244:
245: public String formatNumber(Query q, Object o, String spec,
246: String saveValueName) throws QueryExecutionException {
247:
248: if (!(o instanceof Number)) {
249:
250: if (o == null) {
251:
252: return "NaN (null)";
253:
254: }
255:
256: return "NaN (" + o.getClass().getName() + ")";
257:
258: }
259:
260: if (o == null) {
261:
262: return "0";
263:
264: }
265:
266: Object so = null;
267:
268: if (saveValueName != null) {
269:
270: so = q.getSaveValue(saveValueName);
271:
272: }
273:
274: Number n = (Number) o;
275:
276: DecimalFormat df = null;
277:
278: if (so != null) {
279:
280: if (!(so instanceof DecimalFormat)) {
281:
282: throw new QueryExecutionException(
283: "Expected save value: \"" + saveValueName
284: + "\" object to be of type: "
285: + DecimalFormat.class.getName()
286: + ", is: " + so.getClass().getName());
287:
288: }
289:
290: df = (DecimalFormat) so;
291:
292: } else {
293:
294: if (spec == null) {
295:
296: spec = FormattingFunctions.DEFAULT_DECIMAL_FORMAT_SPEC;
297:
298: }
299:
300: df = new DecimalFormat(spec);
301:
302: }
303:
304: return df.format(n.doubleValue());
305:
306: }
307:
308: public String formatNumber(Query q, Object o, Getter g,
309: String spec, String saveValueName)
310: throws QueryExecutionException {
311:
312: try {
313:
314: o = g.getValue(o);
315:
316: } catch (Exception e) {
317:
318: throw new QueryExecutionException(
319: "Unable to get value from accessor: " + g, e);
320:
321: }
322:
323: return this.formatNumber(q, o, spec, saveValueName);
324:
325: }
326:
327: }
|