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
32
33 public MacroProcessor(){
34 }
35
36
37
38
39
40
41
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
52
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
66
67 }
68 return null;
69 }
70
71
72
73
74
75
76
77
78
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
93
94
95
96
97
98
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
141
142
143
144
145
146
147
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
162
163
164
165
166
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
182
183
184
185
186
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);
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
202
203
204
205
206
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
223
224
225
226
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
236
237
238
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
257
258
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
268
269
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
312 }
313 }
314 if (Utils.debug) System.out.println("MacroProcessor::buildRuntimeData - hmRuntimeData="+hmRuntimeData);
315 return hmRuntimeData;
316 }
317
318 }