View Javadoc

1   package org.opensync.tools;
2   
3   import java.io.IOException;
4   import java.text.SimpleDateFormat;
5   import java.util.ArrayList;
6   import java.util.Calendar;
7   import java.util.Date;
8   import java.util.GregorianCalendar;
9   import java.util.HashMap;
10  
11  import javax.xml.parsers.ParserConfigurationException;
12  
13  import org.opensync.importexport.View;
14  import org.xml.sax.SAXException;
15  
16  /***
17   * Takes a macro and returns the result.  Currently used to build the where
18   * clause for the dashboard.  For example, when sMacro = "$Today",
19   * then todays date is returned.  When macro = $MONTH_BEGIN-2, then returns
20   * first day of month for two months ago.
21   * Format of dates are dependent on the format specified in the theme element.
22   * @version 1.0
23   * @author Keith Stumpf
24   */
25  
26  public class MacroProcessor {
27  
28    static int m_nRandomNumber=0;
29  
30   /*
31    * Default constructor
32    */
33    public MacroProcessor(){
34    }
35  
36   /*
37    * Controller method.  Pass in any macro and this method delagate to proper method.
38    * @param       sMacro            Macro name, i.e.: $Today or $MONTH_BEGIN
39    * @param       element           A theme element
40    * @param       alRow             ArrayList of a row of data from result set
41    * @return      String            Depending on the macro.  See class description.
42    */
43    public static String buildMacro(String sMacro,
44                                    Element element,
45                                    ArrayList alRow)
46        throws Exception{
47      if (sMacro.startsWith(Constants.MACRO_MONTH_BEGIN)){
48        return macroMonthBegin(sMacro,element);
49      }else if (sMacro.startsWith(Constants.MACRO_MONTH_END)){
50        return macroMonthEnd(sMacro,element);
51      //}else if (sMacro.startsWith(Constants.MACRO_TODAY)){
52      //  return macroToday(sMacro,element);
53      }else if (sMacro.startsWith(Constants.MACRO_TODAY)){
54        String sResult = macroToday(sMacro,element);
55        if (Utils.debug) System.out.println("MacroProcessor::buildMacro - result="+sResult);
56        return sResult;
57      }else if (sMacro.startsWith(Constants.MACRO_YEAR_BEGIN)){
58        return macroYearBegin(sMacro,element);
59      }else if (sMacro.startsWith(Constants.MACRO_RANDOM_NUMBER)){
60        return macroRandomNumber(sMacro,element);
61      }else if (sMacro.startsWith(Constants.MACRO_YEAR_END)){
62        return macroYearEnd(sMacro,element);
63      }else if (sMacro.startsWith(Constants.MACRO_RESULT_DATA)){
64        return macroResultData(sMacro,element,alRow);
65      //}else if (sMacro.startsWith(Constants.MACRO_NOW)){
66      //  return macroNow(sMacro,element);
67      }
68      return null;
69    }
70  
71   /*
72    * Builds the month begin date.  Macro format is $MONTH_BEGIN, followed by
73    * +, -, or nothing.  For example MONTH_BEGIN+2, $MONTH_BEGIN-10, or $MONTH_BEGIN.
74    * $MONTH_BEGIN-2 returns first day of month for two months ago.
75    * Format of date is dependent on the format specified in the theme element.
76    * @param       sMacro            Macro name, i.e.: $MONTH_BEGIN-10
77    * @param       element           A theme element
78    * @return      String            First day of month for month requested.
79    */
80    public static String macroMonthBegin(String sMacro,Element element){
81      if (Utils.debug) System.out.println("MacroProcessor::macroMonthBegin - sMacro="+sMacro);
82      int nRollAmount = determineRollAmount(sMacro);
83      GregorianCalendar cal = new GregorianCalendar();
84      cal.add(Calendar.MONTH,nRollAmount);
85      cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH));
86      String sDateString = simpleDate(cal.getTime());
87      String sReturn = Utils.transformDateUserAsDateSql(sDateString,element.getFormatDateSql(),element.getSeparatorDateSql());
88      return sReturn;
89    }
90  
91   /*
92    * Builds the month end date.  Macro format is $MONTH_END, followed by
93    * +, -, or nothing.  For example MONTH_END+2, $MONTH_END-10, or $MONTH_END
94    * $MONTH_END-2 returns last day of month for two months ago.
95    * Format of date is dependent on the format specified in the theme element.
96    * @param       sMacro            Macro name, i.e.: $MONTH_END-10
97    * @param       element           A theme element
98    * @return      String            First day of month for month requested.
99    */
100   public static String macroMonthEnd(String sMacro,Element element){
101     if (Utils.debug) System.out.println("MacroProcessor::macroMonthEnd - sMacro="+sMacro);
102     int nRollAmount = determineRollAmount(sMacro);
103     GregorianCalendar cal = new GregorianCalendar();
104     cal.add(Calendar.MONTH,nRollAmount);
105     cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
106     String sDateString = simpleDate(cal.getTime());
107     String sReturn = Utils.transformDateUserAsDateSql(sDateString,element.getFormatDateSql(),element.getSeparatorDateSql());
108     return sReturn;
109   }
110 
111  /*
112   * Builds todays date.  Macro format is $TODAY.
113   * Format of date is dependent on the format specified in the theme element.
114   * @param       sMacro            Macro name, i.e.: $MONTH_END-10
115   * @param       element           A theme element
116   * @return      String            First day of month for month requested.
117   */
118   //public static String macroToday(String sMacro,Element element){
119   //  if (Utils.debug) System.out.println("MacroProcessor::macroMonthEnd - sMacro="+sMacro);
120   //  GregorianCalendar cal = new GregorianCalendar();
121   //  String sDateString = simpleDate(cal.getTime());
122   //  String sReturn = Utils.transformDateUserAsDateSql(sDateString,element.getFormatDateSql(),element.getSeparatorDateSql());
123   //  return sReturn;
124   //}
125 
126  /*
127   * Builds todays date including the time.  Macro format is $NOW.
128   * Format of date and time is dependent on the format specified in the theme element.
129   * @param       sMacro            Macro name, i.e.: $MONTH_END-10
130   * @param       element           A theme element
131   * @return      String            First day of month for month requested.
132   */
133   public static String macroNow(String sMacro,Element element){
134     if (Utils.debug) System.out.println("MacroProcessor::macroNow - sMacro="+sMacro);
135     String sReturn = "2002-06-01";
136     return sReturn;
137   }
138 
139  /*
140   * Builds a date with + / - the specified number of days from today.
141   * Macro format is $TODAY, followed by
142   * +, -, or nothing.  For example DAY+2, $TODAY-10, or $TODAY.
143   * $MONTH_BEGIN-2 returns first day of month for two months ago.
144   * Format of date is dependent on the format specified in the theme element.
145   * @param       sMacro            Macro name, i.e.: $TODAY-10
146   * @param       element           A theme element
147   * @return      String            Date + / - number of days from today
148   */
149   public static String macroToday(String sMacro,Element element){
150     if (Utils.debug) System.out.println("MacroProcessor::macroToday - sMacro="+sMacro);
151     GregorianCalendar cal = new GregorianCalendar();
152     int nRollAmount = determineRollAmount(sMacro);
153     cal.add(Calendar.DAY_OF_MONTH,nRollAmount);
154     String sDateString = simpleDate(cal.getTime());
155     String sReturn = Utils.transformDateUserAsDateSql(sDateString,element.getFormatDateSql(),element.getSeparatorDateSql());
156     if (Utils.debug) System.out.println("MacroProcessor::macroToday - result="+sReturn);
157     return sReturn;
158   }
159 
160  /*
161   * Builds a date with todays year and month=10 and day=01
162   * Macro format is $YEAR_BEGIN.
163   * Format of date is dependent on the format specified in the theme element.
164   * @param       sMacro            Macro name, i.e.: $YEAR_BEGIN
165   * @param       element           A theme element
166   * @return      String            First day of year for this year.
167   */
168   public static String macroYearBegin(String sMacro,Element element){
169     if (Utils.debug) System.out.println("MacroProcessor::macroYearBegin - sMacro="+sMacro);
170     GregorianCalendar cal = new GregorianCalendar();
171     int nRollAmount = determineRollAmount(sMacro);
172     cal.add(Calendar.YEAR,nRollAmount);
173     cal.set(Calendar.MONTH,0);
174     cal.set(Calendar.DAY_OF_MONTH,1);
175     String sDateString = simpleDate(cal.getTime());
176     String sReturn = Utils.transformDateUserAsDateSql(sDateString,element.getFormatDateSql(),element.getSeparatorDateSql());
177     return sReturn;
178   }
179 
180  /*
181   * Builds a date with todays year and month=10 and day=01
182   * Macro format is $YEAR_BEGIN.
183   * Format of date is dependent on the format specified in the theme element.
184   * @param       sMacro            Macro name, i.e.: $YEAR_BEGIN
185   * @param       element           A theme element
186   * @return      String            First day of year for this year.
187   */
188   public static String macroYearEnd(String sMacro,Element element){
189     if (Utils.debug) System.out.println("MacroProcessor::macroYearEnd - sMacro="+sMacro);
190     GregorianCalendar cal = new GregorianCalendar();
191     int nRollAmount = determineRollAmount(sMacro);
192     cal.add(Calendar.YEAR,nRollAmount);
193     cal.set(Calendar.MONTH,11); // zero based, so december.
194     cal.set(Calendar.DAY_OF_MONTH,31);
195     String sDateString = simpleDate(cal.getTime());
196     String sReturn = Utils.transformDateUserAsDateSql(sDateString,element.getFormatDateSql(),element.getSeparatorDateSql());
197     return sReturn;
198   }
199 
200  /*
201   * Builds a date with todays year and month=10 and day=01
202   * Macro format is $YEAR_BEGIN.
203   * Format of date is dependent on the format specified in the theme element.
204   * @param       sMacro            Macro name, i.e.: $YEAR_BEGIN
205   * @param       element           A theme element
206   * @return      String            First day of year for this year.
207   */
208   public static String macroResultData(String sMacro,Element element,ArrayList alRow)
209       throws Exception{
210     if (Utils.debug) System.out.println("MacroProcessor::macroResultData - sMacro="+sMacro);
211     String[] sTokensArray = Utils.tokenize(sMacro,",");
212     if (sTokensArray.length != 2){
213       throw new Exception("MacroProcessor::macroResultData - macro "+Constants.MACRO_RESULT_DATA+" needs to have 2 parms.");
214     }
215     if (Utils.debug) System.out.println("MacroProcessor::macroResultData - sTokensArray[1]="+sTokensArray[1]);
216     int nColumn = Integer.parseInt(sTokensArray[1]);
217     String sReturn = (String)alRow.get(nColumn);
218     return sReturn;
219   }
220 
221  /*
222   * Builds a random number
223   * Macro format is $RANDOM_NUMBER.
224   * @param       sMacro            Macro name, i.e.: $RANDOM_NUMBER
225   * @param       element           A theme element
226   * @return      String            First day of year for this year.
227   */
228   public static String macroRandomNumber(String sMacro,Element element){
229     if (Utils.debug) System.out.println("MacroProcessor::macroRandomNumber - sMacro="+sMacro);
230     String sReturn = Utils.aleatoire() + m_nRandomNumber++;
231     return sReturn;
232   }
233 
234  /*
235   * Looks in a macro for either + or -, and returns the integer value.
236   * For example if macro is $TODAY-10, returns -10 as integer
237   * @param       sMacro            Macro name, i.e.: $TODAY-10
238   * @return      String            First day of month for month requested.
239   */
240   public static int determineRollAmount(String sMacro){
241     int nRollAmount = 0;
242     String[] sTokensArray = Utils.tokenize(sMacro,"-");
243     if (sTokensArray.length > 1){
244       nRollAmount = Integer.parseInt(sTokensArray[1]) * -1;
245     }else{
246       sTokensArray = Utils.tokenize(sMacro,"+");
247       if (sTokensArray.length > 1){
248         nRollAmount = Integer.parseInt(sTokensArray[1]);
249       }
250     }
251     if (Utils.debug) System.out.println("MacroProcessor::determineRollAmount - nRollAmount="+nRollAmount);
252     return nRollAmount;
253   }
254 
255  /*
256   * Formats a Date
257   * @param       date              Date
258   * @return      String            SimpleDate
259   */
260   public static String simpleDate(Date date){
261     SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMdd");
262     String sDateString = formatter.format(date);
263     return sDateString;
264   }
265 
266  /*
267   * Builds a HashMap of where clause data from sql piece of user dashboard xml.
268   * @param       hmRuntimeData       HashMap of where clause data - if it exist append more, else create new one.
269   * @return      HashMap             HashMap of where clause data
270   */
271   public static HashMap buildRuntimeData(View view,HashMap hmRuntimeDataIn,ArrayList alConditionalData)
272       throws Exception,IOException,SAXException,ParserConfigurationException{
273     if (Utils.debug) System.out.println("MacroProcessor::buildRuntimeData - begin");
274     if (Utils.debug) System.out.println("MacroProcessor::buildRuntimeData - hmRuntimeDataIn="+hmRuntimeDataIn);
275     HashMap hmRuntimeData = null;
276     if (hmRuntimeDataIn == null){
277       hmRuntimeData = new HashMap();
278     }else{
279       hmRuntimeData = (HashMap)hmRuntimeDataIn.clone();
280     }
281     String sThemeKeyword = "";
282     String sWhereKeyword = "";
283     String sMacro = "";
284     String sOperator = "";
285     String sRuntimeData = "";
286     ConditionalData conditionalData = null;
287     for (int i=0;i<alConditionalData.size();i++){
288       conditionalData = (ConditionalData)alConditionalData.get(i);
289       sThemeKeyword = conditionalData.getThemeKeyword();
290       sWhereKeyword = conditionalData.getWhereKeyword();
291       sMacro = conditionalData.getMacro();
292       sOperator = conditionalData.getOperator();
293       if (Utils.debug) System.out.println("MacroProcessor::buildRuntimeData - processing condition,"+
294                                           " sThemeKeyword="+sThemeKeyword+
295                                           " sWhereKeyword="+sWhereKeyword+
296                                           " sMacro="+sMacro+
297                                           " sOperator="+sOperator);
298       sRuntimeData = MacroProcessor.buildMacro(sMacro,view.findElementByKeyWord(sThemeKeyword),null);
299       if (sRuntimeData != null){
300         if (sOperator != null &&
301            !sOperator.equals("")){
302           sRuntimeData += sOperator;
303         }
304         if (Utils.debug) System.out.println("MacroProcessor::buildRuntimeData - adding to hashmap key="+sWhereKeyword+
305                                                                                           " value="+sRuntimeData);
306         hmRuntimeData.put(sWhereKeyword,sRuntimeData);
307       }else{
308         if (Utils.debug) System.out.println("MacroProcessor::buildRuntimeData - adding to hashmap key="+sWhereKeyword+
309                                                                                           " value="+sMacro);
310         hmRuntimeData.put(sWhereKeyword,sMacro);
311         //throw new Exception("MacroProcessor::buildRuntimeData - macro not found for "+sWhereClauseItemArray[1]);
312       }
313     }
314     if (Utils.debug) System.out.println("MacroProcessor::buildRuntimeData - hmRuntimeData="+hmRuntimeData);
315     return hmRuntimeData;
316   }
317 
318 }