1 package com.calipso.reportgenerator.common; 2 3 import java.util.Date ; 4 import java.util.Calendar ; 5 import java.util.Map ; 6 import java.util.HashMap ; 7 import java.text.DateFormat ; 8 9 import com.calipso.common.DateEx; 10 11 14 public abstract class DateExpressionResolver { 15 16 17 public static String DAY = "DAY"; 18 public static String WEEK = "WEEK"; 19 public static String YEAR = "YEAR"; 20 public static String QUARTER = "QUARTER"; 21 public static String FORTNIGTH = "FORTNIGTH"; 22 public static String MONTH = "MONTH"; 23 public static String SEMESTRE = "SEMESTRE"; 24 private static Map resolvers; 25 26 private DateFormat dateFormatter; 27 private String datePattern; 28 private Calendar cal; 29 30 private static String defaultDatePattern = "dd/MM/yyyy"; 31 32 36 private static Map getResolvers() { 37 if (resolvers == null) { 38 resolvers = new HashMap (); 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 53 public DateExpressionResolver() { 54 datePattern = defaultDatePattern; 55 } 56 57 61 public Calendar getCal() { 62 return cal; 63 } 64 65 69 public void setDate(String dateString, String datePattern) throws InfoException { 70 80 Date date = (new DateEx(dateString, datePattern)).getDate(); 81 Calendar calHoy = Calendar.getInstance(); 83 calHoy.setTime(new Date ()); 84 cal = (Calendar ) calHoy.clone(); 85 dateFormatter = DateFormat.getDateInstance(DateFormat.SHORT); 87 dateFormatter.setCalendar(cal); 88 cal.setTime(date); 89 92 } 93 94 98 public DateFormat getDateFormatter() { 99 return dateFormatter; 100 } 101 102 106 public String getDatePattern() { 107 return datePattern; 108 } 109 110 113 public void setDatePattern(String datePattern) { 114 this.datePattern = datePattern; 115 } 116 117 118 128 129 public String getValue(String dateValue) throws InfoException { 130 setDate(dateValue, getDatePattern()); 131 return doGetValue(); 132 }; 133 137 protected abstract String doGetValue(); 138 139 143 public static String Resolve(String dateFunction, String dateValue) throws InfoException { 144 DateExpressionResolver resolver = (DateExpressionResolver) getResolvers().get(dateFunction); 145 return resolver.getValue(dateValue); 146 } 147 148 152 public static String Resolve(String dateFunction, String dateValue, String datePattern) throws InfoException { 153 DateExpressionResolver resolver = (DateExpressionResolver) getResolvers().get(dateFunction); 154 resolver.setDatePattern(datePattern); 155 return resolver.getValue(dateValue); 156 } 157 158 162 public static String Resolve(String dateFunction, Date 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 |