KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > common > DateExpressionResolver


1 package com.calipso.reportgenerator.common;
2
3 import java.util.Date JavaDoc;
4 import java.util.Calendar JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.text.DateFormat JavaDoc;
8
9 import com.calipso.common.DateEx;
10
11 /**
12  * Esta clase se encarga de resolver las expresiones que generan las columnas calculadas de fechas
13  */

14 public abstract class DateExpressionResolver {
15
16
17   public static String JavaDoc DAY = "DAY";
18   public static String JavaDoc WEEK = "WEEK";
19   public static String JavaDoc YEAR = "YEAR";
20   public static String JavaDoc QUARTER = "QUARTER";
21   public static String JavaDoc FORTNIGTH = "FORTNIGTH";
22   public static String JavaDoc MONTH = "MONTH";
23   public static String JavaDoc SEMESTRE = "SEMESTRE";
24   private static Map JavaDoc resolvers;
25
26   private DateFormat JavaDoc dateFormatter;
27   private String JavaDoc datePattern;
28   private Calendar JavaDoc cal;
29
30   private static String JavaDoc defaultDatePattern = "dd/MM/yyyy";
31
32   /**
33    * Retorna un HashMap que contiene los mecanismos para resolver cada una de las funciones
34    * @return Map
35    */

36   private static Map JavaDoc getResolvers() {
37     if (resolvers == null) {
38       resolvers = new HashMap JavaDoc();
39       resolvers.put(DAY, new ResolveDay());
40       resolvers.put(WEEK, new ResolveWeek());
41       resolvers.put(SEMESTRE, new ResolveSemestre());
42       resolvers.put(YEAR, new ResolveYear());
43       resolvers.put(MONTH, new ResolveMonth());
44       resolvers.put(FORTNIGTH, new ResolveFortnight());
45       resolvers.put(QUARTER, new ResolveQuarter());
46     }
47     return resolvers;
48   }
49
50   /**
51    * Crea e inicializa un <code>DateExpressionResolver</code>
52    */

53   public DateExpressionResolver() {
54     datePattern = defaultDatePattern;
55   }
56
57   /**
58    * Retorna un objeto Calendar
59    * @return Calendar
60    */

61   public Calendar JavaDoc getCal() {
62     return cal;
63   }
64
65   /**
66    * Asigna la fecha a partir de una dateString y un datePattern
67    * @throws com.calipso.reportgenerator.common.InfoException
68    */

69   public void setDate(String JavaDoc dateString, String JavaDoc datePattern) throws InfoException {
70     /*SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
71     String dateFormat = "yyyyMMdd";
72     int length= (dateString).length();
73     if ( length == 8 ){
74       dateFormat = "yyyyMMdd";
75     }else if ( length == 15 ){
76       dateFormat = "yyyyMMddHHmmssSSS";
77     }
78     simpleDateFormat.applyPattern(dateFormat);
79     Date date = null;*/

80     Date JavaDoc date = (new DateEx(dateString, datePattern)).getDate();
81     //try {
82
Calendar JavaDoc calHoy = Calendar.getInstance();
83     calHoy.setTime(new Date JavaDoc());
84     cal = (Calendar JavaDoc) calHoy.clone();
85     //date = simpleDateFormat.parse(dateString);
86
dateFormatter = DateFormat.getDateInstance(DateFormat.SHORT);
87     dateFormatter.setCalendar(cal);
88     cal.setTime(date);
89     /*} catch (ParseException e) {
90       throw new InfoException(LanguageTraslator.traslate("77"), e);
91     }*/

92   }
93
94   /**
95    * Retorna el objeto DateFormat correspondiente
96    * @return DateFormat
97    */

98   public DateFormat JavaDoc getDateFormatter() {
99     return dateFormatter;
100   }
101
102   /**
103    * Retorna un String que representa el formato de la Fecha
104    * @return String
105    */

106   public String JavaDoc getDatePattern() {
107     return datePattern;
108   }
109
110   /**
111    * Asigna un String que representa el formato de la Fecha al atributo datePattern
112    */

113   public void setDatePattern(String JavaDoc datePattern) {
114     this.datePattern = datePattern;
115   }
116
117
118   /**
119    * Devuelve el valor dependiendo de su clave
120    DAY
121    WEEK
122    YEAR
123    QUARTER
124    FORTNIGTH
125    MONTH
126    SEMESTRE
127    */

128
129   public String JavaDoc getValue(String JavaDoc dateValue) throws InfoException {
130     setDate(dateValue, getDatePattern());
131     return doGetValue();
132   };
133   /**
134    * Método abstracto que se sobrescribe para cada tipo
135    * @return
136    */

137   protected abstract String JavaDoc doGetValue();
138
139   /**
140    * Obtiene el valor de la función dateFunction , utilizando el datePatern por default dd/MM/yyyy
141    * @throws com.calipso.reportgenerator.common.InfoException
142    */

143   public static String JavaDoc Resolve(String JavaDoc dateFunction, String JavaDoc dateValue) throws InfoException {
144     DateExpressionResolver resolver = (DateExpressionResolver) getResolvers().get(dateFunction);
145     return resolver.getValue(dateValue);
146   }
147
148   /**
149    * Obtiene el valor de la función dateFunction , utilizando el datePatern nuevo
150    * @throws com.calipso.reportgenerator.common.InfoException
151    */

152   public static String JavaDoc Resolve(String JavaDoc dateFunction, String JavaDoc dateValue, String JavaDoc datePattern) throws InfoException {
153     DateExpressionResolver resolver = (DateExpressionResolver) getResolvers().get(dateFunction);
154     resolver.setDatePattern(datePattern);
155     return resolver.getValue(dateValue);
156   }
157
158   /**
159    * Obtiene el valor de la función dateFunction , utilizando el datePatern nuevo
160    * @throws com.calipso.reportgenerator.common.InfoException
161    */

162   public static String JavaDoc Resolve(String JavaDoc dateFunction, Date JavaDoc date) throws InfoException {
163     DateExpressionResolver resolver = (DateExpressionResolver) getResolvers().get(dateFunction);
164     resolver.dateFormatter = DateFormat.getDateInstance(DateFormat.SHORT);
165     resolver.cal = Calendar.getInstance();
166     resolver.dateFormatter.setCalendar(resolver.cal);
167     resolver.cal.setTime(date);
168     return resolver.doGetValue();
169   }
170
171 }
172
Popular Tags