1 package org.opensync.tools;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.FileOutputStream;
8 import java.io.FileWriter;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.InputStreamReader;
12 import java.text.ParseException;
13 import java.text.SimpleDateFormat;
14 import java.util.ArrayList;
15 import java.util.Calendar;
16 import java.util.Date;
17 import java.util.GregorianCalendar;
18 import java.util.HashMap;
19 import java.util.Hashtable;
20 import java.util.Iterator;
21 import java.util.StringTokenizer;
22 import java.util.Vector;
23
24 import org.xml.sax.SAXException;
25
26
27
28 public final class Utils {
29 private static long millisec;
30 private static Hashtable files;
31 public static String sqllogfile="SQLlog.txt";
32 private static String path ;
33
34
35 public static boolean debug=false ;
36 public static boolean m_bSqlTimings=false ;
37
38
39 public static boolean log =false ;
40
41 public static void setDebug (boolean s) {
42 debug = s;
43 }
44
45
46 public static void setLangue (String s) {
47 langue = s.toUpperCase().trim();
48 }
49 public static String langue;
50
51 public static void setPath (String s) {
52 path = s;
53 }
54
55 public static String getPath () {
56 return path;
57 }
58
59 public static void setLogFile (boolean s) {
60 log = s;
61 }
62
63 public static void SqlLogToFile (String s) {
64 LogToFile (sqllogfile,s,false);
65 }
66
67 public static void LogToFile (String logfile,String s) {
68 LogToFile (logfile,s,false);
69 }
70
71 public static void LogToFile (String logfile,String s,boolean processComputeTime) {
72
73 if (log) {
74
75 FileOutputStream out;
76 Calendar date = Calendar.getInstance();
77 try {
78
79 if (files==null) files = new Hashtable();
80 if ((out=(FileOutputStream)files.get(path + logfile))==null) {
81 out = new FileOutputStream(path + logfile,true);
82 files.put(path + logfile,out);
83 }
84 if (processComputeTime) {
85 long i = System.currentTimeMillis();
86 out.write((date.getTime().toString()+" - "+date.get(Calendar.MILLISECOND)+"\t"+s+"\t"+(i-millisec)+"\r\n").getBytes());
87 millisec = i;
88 }
89 else out.write( (date.getTime().toString()+" - "+date.get(Calendar.MILLISECOND)+"\t"+s+"\r\n").getBytes() );
90 out.flush();
91
92 }
93 catch (FileNotFoundException e) {
94 System.out.println("path + logFile"+path+logfile);
95 System.out.println("Log file could not be accessed");
96 }
97 catch (IOException e) {
98 System.out.println("IOexception for Logfile");
99 }
100 }
101 }
102
103 public static String checkSpecialCharXml(String s) {
104
105 s = checkSpecialChar(s, '\000', "");
106 s = checkSpecialChar(s, '\001', "");
107 s = checkSpecialChar(s, '\002', "");
108 s = checkSpecialChar(s, '\003', "");
109 s = checkSpecialChar(s, '\004', "");
110 s = checkSpecialChar(s, '\005', "");
111 s = checkSpecialChar(s, '\006', "");
112 s = checkSpecialChar(s, '\007', "");
113 s = checkSpecialChar(s, '\010', "");
114 s = checkSpecialChar(s, '\011', "");
115 s = checkSpecialChar(s, '\012', "");
116 s = checkSpecialChar(s, '\013', "");
117 s = checkSpecialChar(s, '\014', "");
118 s = checkSpecialChar(s, '\015', "");
119 s = checkSpecialChar(s, '\016', "");
120 s = checkSpecialChar(s, '\017', "");
121 s = checkSpecialChar(s, '\020', "");
122 s = checkSpecialChar(s, '\021', "");
123 s = checkSpecialChar(s, '\022', "");
124 s = checkSpecialChar(s, '\023', "");
125 s = checkSpecialChar(s, '\024', "");
126 s = checkSpecialChar(s, '\025', "");
127 s = checkSpecialChar(s, '\026', "");
128 s = checkSpecialChar(s, '\027', "");
129 s = checkSpecialChar(s, '\030', "");
130 s = checkSpecialChar(s, '\031', "");
131 s = checkSpecialChar(s, '\032', "");
132 s = checkSpecialChar(s, '\033', "");
133 s = checkSpecialChar(s, '\034', "");
134 s = checkSpecialChar(s, '\035', "");
135 s = checkSpecialChar(s, '\036', "");
136 s = checkSpecialChar(s, '\037', "");
137
138 s = checkSpecialChar(s, '&', "&");
139 s = checkSpecialChar(s, '"', """);
140 s = checkSpecialChar(s, '<', "<");
141 s = checkSpecialChar(s, '>', ">");
142
143 s = checkSpecialChar(s, '\u00C0', "A");
144 s = checkSpecialChar(s, '\u00C1', "A");
145 s = checkSpecialChar(s, '\u00C2', "A");
146 s = checkSpecialChar(s, '\u00C3', "A");
147 s = checkSpecialChar(s, '\u00C5', "A");
148 s = checkSpecialChar(s, '\u00C6', "A");
149 s = checkSpecialChar(s, '\u00C7', "C");
150 s = checkSpecialChar(s, '\u00C8', "E");
151 s = checkSpecialChar(s, '\u00C9', "E");
152 s = checkSpecialChar(s, '\u00CA', "E");
153 s = checkSpecialChar(s, '\u00CB', "E");
154 s = checkSpecialChar(s, '\u00CC', "I");
155 s = checkSpecialChar(s, '\u00CD', "I");
156 s = checkSpecialChar(s, '\u00CE', "I");
157 s = checkSpecialChar(s, '\u00CF', "I");
158 s = checkSpecialChar(s, '\u00D0', "D");
159 s = checkSpecialChar(s, '\u00D1', "N");
160 s = checkSpecialChar(s, '\u00D2', "O");
161 s = checkSpecialChar(s, '\u00D3', "O");
162 s = checkSpecialChar(s, '\u00D4', "0");
163 s = checkSpecialChar(s, '\u00D5', "0");
164 s = checkSpecialChar(s, '\u00D6', "0");
165 s = checkSpecialChar(s, '\u00D8', "0");
166 s = checkSpecialChar(s, '\u00D9', "U");
167 s = checkSpecialChar(s, '\u00DA', "N");
168 s = checkSpecialChar(s, '\u00DB', "N");
169 s = checkSpecialChar(s, '\u00DC', "N");
170 s = checkSpecialChar(s, '\u00DD', "Y");
171 s = checkSpecialChar(s, '\u00DE', "I");
172 s = checkSpecialChar(s, '\u00DF', "B");
173 s = checkSpecialChar(s, '\u00E0', "A");
174 s = checkSpecialChar(s, '\u00E1', "A");
175 s = checkSpecialChar(s, '\u00E2', "A");
176 s = checkSpecialChar(s, '\u00E3', "A");
177 s = checkSpecialChar(s, '\u00E4', "A");
178 s = checkSpecialChar(s, '\u00E5', "A");
179 s = checkSpecialChar(s, '\u00E6', "A");
180 s = checkSpecialChar(s, '\u00E7', "C");
181 s = checkSpecialChar(s, '\u00E8', "E");
182 s = checkSpecialChar(s, '\u00E9', "E");
183 s = checkSpecialChar(s, '\u00EA', "E");
184 s = checkSpecialChar(s, '\u00EB', "E");
185 s = checkSpecialChar(s, '\u00EC', "I");
186 s = checkSpecialChar(s, '\u00ED', "I");
187 s = checkSpecialChar(s, '\u00EE', "I");
188 s = checkSpecialChar(s, '\u00EF', "I");
189 s = checkSpecialChar(s, '\u00F0', "D");
190 s = checkSpecialChar(s, '\u00F1', "N");
191 s = checkSpecialChar(s, '\u00F2', "O");
192 s = checkSpecialChar(s, '\u00F3', "O");
193 s = checkSpecialChar(s, '\u00F4', "O");
194 s = checkSpecialChar(s, '\u00F5', "O");
195 s = checkSpecialChar(s, '\u00F6', "O");
196 s = checkSpecialChar(s, '\u00F8', "O");
197 s = checkSpecialChar(s, '\u00F9', "U");
198 s = checkSpecialChar(s, '\u00FA', "U");
199 s = checkSpecialChar(s, '\u00FB', "U");
200 s = checkSpecialChar(s, '\u00FC', "U");
201 s = checkSpecialChar(s, '\u00FD', "Y");
202 s = checkSpecialChar(s, '\u00FE', "I");
203 s = checkSpecialChar(s, '\u00FF', "Y");
204
205 return s;
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 public static String replaceWordInString(String content, String word, String value) {
222 String str = new String(content);
223 String replacedStr = "";
224 int start = str.indexOf(word);
225 while (start != -1) {
226 replacedStr += (str.substring(0, start) + value);
227 str = str.substring(start + word.length());
228 start = str.indexOf(word);
229 }
230 replacedStr += str;
231 return replacedStr;
232 }
233
234 public static String checkSpecialChar(String in, char c, String replace ) {
235
236 if ( in==null || in.equals("") ) return "";
237
238 StringBuffer out;
239 String s1;
240 int i,j;
241 boolean found;
242
243 i = -1;
244 j = 0;
245 found = false;
246 out = new StringBuffer();
247 while ((i=in.indexOf(c,i+1)) != -1) {
248 found = true;
249 s1 = in.substring(j,i);
250 out.append(s1);
251 out.append(replace);
252 j = i+1;
253 }
254 if (found==true) {
255 s1 = in.substring(j);
256 out.append(s1);
257 in = out.toString();
258 }
259
260 return in;
261 }
262
263
264 public static String checkLengthAreaText(String in, String length ) {
265
266 if ( in==null || in.equals("") ) return "";
267
268 int i = (int)Integer.parseInt(length);
269
270 return in.substring(0,Math.min(i-1,in.length()));
271 }
272
273 public static void processXmlDateTimeSqlAsDateTimeUser(String lang, String what, StringBuffer _xmlStringOut,Element _e, Vector _dateUser, String _dataValue, String separator) throws SAXException,FileNotFoundException,IOException {
274 int i,iMax;
275 String tagName;
276
277 if (what.equals("D")) {
278 i=0;
279 iMax=3;
280 tagName="<DATE ";
281 }
282 else {
283 i=3;
284 iMax=5;
285 tagName="<TIME ";
286 }
287
288 for (int x=i; x<iMax ; x++) {
289
290 String vl = "";
291
292 if ( what.equals("D") && _dataValue!=null && _dataValue.length() > 0 ) vl = processDateSqlAsDateUser(_dataValue,(String)_dateUser.elementAt(x), _e.getFormatDateSql(), _e.getSeparatorDateSql());
293
294 if (vl.trim().equals("")==false && vl.trim().equals("DATE")) vl="AAAA";
295 if (vl.trim().equals("")==false && vl.trim().equals("SY")) vl="MM";
296 if (vl.trim().equals("")==false && vl.trim().equals("ST")) vl="DD";
297
298 if ( what.equals("H") && _dataValue!=null && _dataValue.length() > 0 ) vl = processTimeSqlAsTimeUser(_dataValue,(String)_dateUser.elementAt(x), _e.getFormatTimeSql(), _e.getSeparatorTimeSql());
299 if (vl.trim().equals("")==false && vl.trim().equals("HE")) vl="HH";
300 if (vl.trim().equals("")==false && vl.trim().equals("UR")) vl="MN";
301
302 String c = "IdKey = \""+_e.getKeyWord()+"_"+(String)_dateUser.elementAt(x)+"\" ";
303 String la = "Label = \""+DicoReader.getLabelDicoByKey(lang,(String)_dateUser.elementAt(x))+"\" ";
304 String s = "Separator = \""+separator+"\" ";
305 Integer len = new Integer(_dateUser.elementAt(x).toString().length());
306 String lg = "Length = \""+len.toString()+"\" InitValue=\""+vl+"\" ";
307
308 _xmlStringOut.append(tagName);
309 _xmlStringOut.append(c);
310 _xmlStringOut.append(la);
311 _xmlStringOut.append(lg);
312 if (x!=i) _xmlStringOut.append(s);
313 _xmlStringOut.append("/>\n");
314 }
315 }
316
317 public static String processDateSqlAsDateUser (String value, String what, String sql, String sep ) {
318
319 int pos = sql.indexOf(what);
320
321
322
323
324 int index = value.indexOf("|*|");
325 if (index >= 0)
326 value = value.substring(0,index);
327
328 if (value.length()<=8) sep="";
329 else sep="*";
330 if (sep==null || sep.length()==0) {
331 if (pos == 5) pos = 4;
332 if (pos == 8) pos = 6;
333 }
334 if (value.length()< 8 || value.length()>10 ) return "";
335 return value.substring(pos,pos+what.length());
336 }
337
338 public static String processTimeSqlAsTimeUser (String value, String what, String sql, String sep ) {
339
340
341
342 int pos = sql.indexOf(what);
343
344 int index = value.indexOf("|*|");
345 if (index >= 0)
346 value = value.substring(0,index);
347
348 if (sep==null || sep.length()==0) {
349 if (pos == 3 && value.indexOf(":")==-1) pos = 2;
350 }
351
352
353 if (value!=null && value.length()>0) return value.substring(pos,pos+what.length());
354 else return "";
355 }
356
357 public static String transformTimeSqlAsTimeUser(String _data, Vector _dateUser, String _separatorTimeUser, String _timeSql, String _separatorTimeSql) {
358 StringBuffer stringTimeUser = new StringBuffer(5);
359 for (int x=3; x<5 ; x++) {
360 stringTimeUser.append( processTimeSqlAsTimeUser(_data,(String)_dateUser.elementAt(x), _timeSql, _separatorTimeSql) );
361 stringTimeUser.append(_separatorTimeUser);
362 }
363 stringTimeUser.delete(stringTimeUser.length()-1,stringTimeUser.length());
364 _data = stringTimeUser.toString();
365 return _data;
366 }
367
368 public static String transformDateSqlAsDateUser(String _data, Vector _dateUser, String _separatorDateUser, String _dateSql, String _separatorDateSql) {
369 StringBuffer stringDateUser = new StringBuffer(10);
370 for (int x=0; x<3 ; x++) {
371 stringDateUser.append( processDateSqlAsDateUser(_data,(String)_dateUser.elementAt(x), _dateSql, _separatorDateSql) );
372 stringDateUser.append(_separatorDateUser);
373 }
374 stringDateUser.delete(stringDateUser.length()-1,stringDateUser.length());
375 _data = stringDateUser.toString();
376 return _data;
377 }
378
379 public static String transformTimeUserAsTimeSql(String _data, String _timeSql, String _separatorTimeSql) {
380
381 int i,j;
382 String temp,formatSql;
383
384 StringBuffer stringTimeSql = new StringBuffer();
385 j=0;
386 i=0;
387
388
389 while ((i=_timeSql.indexOf("|",i+1)) != -1) {
390 temp = _timeSql.substring(j,i);
391 stringTimeSql.append(temp);
392 stringTimeSql.append(_separatorTimeSql);
393 j = i+1;
394 }
395 temp = _timeSql.substring(j);
396 stringTimeSql.append(temp);
397 formatSql = stringTimeSql.toString();
398
399 if (_data!=null && _data.length()>0) {
400 if (_data.indexOf(":")!=-1) {
401
402 temp = _data.substring(0,2);
403 i = formatSql.indexOf("HH",0);
404 if (i>-1) stringTimeSql.replace(i,i+2,temp);
405
406 temp = _data.substring(3,5);
407 i = formatSql.indexOf("MN",0);
408 if (i>-1) stringTimeSql.replace(i,i+2,temp);
409 _data = stringTimeSql.toString();
410 }
411 else {
412
413 temp = _data.substring(0,2);
414 i = formatSql.indexOf("HH",0);
415 if (i>-1) stringTimeSql.replace(i,i+2,temp);
416
417 temp = _data.substring(2,4);
418 i = formatSql.indexOf("MN",0);
419 if (i>-1) stringTimeSql.replace(i,i+2,temp);
420 _data = stringTimeSql.toString();
421 }
422 }
423 if (Utils.debug) System.out.println(" Time SQL sans | : "+_data);
424 return _data;
425 }
426
427 public static String transformDateUserAsDateSql(String _data, String _dateSql, String _separatorDateSql) {
428
429 int i,j;
430 String temp,formatSql;
431
432 if (Utils.debug) System.out.println("User data="+_data+ " SQL date="+_dateSql+" seperator="+_separatorDateSql);
433 StringBuffer stringDateSql = new StringBuffer();
434 j=0;
435 i=0;
436
437
438 while ((i=_dateSql.indexOf("|",i+1)) != -1) {
439 temp = _dateSql.substring(j,i);
440 stringDateSql.append(temp);
441 stringDateSql.append(_separatorDateSql);
442 j = i+1;
443 }
444 temp = _dateSql.substring(j);
445 stringDateSql.append(temp);
446 formatSql = stringDateSql.toString();
447
448 if (_data!=null && _data.length()>0) {
449 if (_data.indexOf("-")!=-1) {
450
451 temp = _data.substring(0,4);
452 i = formatSql.indexOf("AAAA",0);
453 if (i>-1) stringDateSql.replace(i,i+4,temp);
454
455 temp = _data.substring(5,7);
456 i = formatSql.indexOf("MM",0);
457 if (i>-1) stringDateSql.replace(i,i+2,temp);
458
459 temp = _data.substring(8,10);
460 i = formatSql.indexOf("DD",0);
461 if (i>-1) stringDateSql.replace(i,i+2,temp);
462 _data = stringDateSql.toString();
463 }
464 else {
465
466 temp = _data.substring(0,4);
467 i = formatSql.indexOf("AAAA",0);
468 if (i>-1) stringDateSql.replace(i,i+4,temp);
469
470 temp = _data.substring(4,6);
471 i = formatSql.indexOf("MM",0);
472 if (i>-1) stringDateSql.replace(i,i+2,temp);
473
474 temp = _data.substring(6,8);
475 i = formatSql.indexOf("DD",0);
476 if (i>-1) stringDateSql.replace(i,i+2,temp);
477 _data = stringDateSql.toString();
478 }
479 }
480 if (Utils.debug) System.out.println(" Date SQL sans | : "+_data);
481 return _data;
482 }
483
484 public static String checkLabelSelected(Element ee, String s, String lang) {
485 int i,imax,j,jmax,k,kmax;
486
487 GroupFixedCriteria grs[];
488 InputFixedCriteria inps[];
489 GroupFixedCriteria gr;
490 InputFixedCriteria in;
491
492 grs = ee.getGroups();
493 imax = grs.length;
494 for (i=0;i<imax;i++) {
495 gr = grs[i];
496 inps = gr.getInputs();
497 jmax = inps.length;
498 for (j=0;j<jmax;j++) {
499 in = gr.getInputByIndex(j);
500 if ( s.length()>0 && s.equals(in.getTextCriteria()) ) {
501 s = DicoReader.getLabelDicoByKey(lang,in.getKeyWord());
502 break;
503 }
504 }
505 }
506 return s;
507 }
508
509 public static String getDateNow () {
510 String y,m,d;
511 int a,b,c;
512
513 Calendar datenow = Calendar.getInstance();
514
515 a = datenow.get(Calendar.YEAR);
516 b = datenow.get(Calendar.MONTH)+1;
517 c = datenow.get(Calendar.DAY_OF_MONTH);
518
519 if (Utils.debug) System.out.println("AAAA-MM-DD : "+a+"-"+b+"-"+c);
520 y = new Integer(a).toString();
521 m = new Integer(b).toString();
522 d = new Integer(c).toString();
523 if (m.length()==1) m = "0"+m;
524 if (d.length()==1) d = "0"+d;
525
526 if (Utils.debug) System.out.println("AAAA-MM-DD : "+(y+"-"+m+"-"+d));
527 return (y+"-"+m+"-"+d);
528 }
529
530 public static String getTimeNow () {
531 String h,m;
532 int a,b;
533
534 Calendar timenow = Calendar.getInstance();
535
536 a = timenow.get(Calendar.HOUR_OF_DAY);
537 b = timenow.get(Calendar.MINUTE);
538 if (Utils.debug) System.out.println("HH-MM : "+a+"-"+b);
539
540 h = new Integer(a).toString();
541 m = new Integer(b).toString();
542
543 if (h.length()==1) h = "0"+h;
544 if (m.length()==1) m = "0"+m;
545
546 if (Utils.debug) System.out.println("HH:MM : "+(h+":"+m));
547 return (h+":"+m);
548 }
549
550 public static String aleatoire() {
551 long aleatoire;
552 String s;
553 aleatoire = System.currentTimeMillis();
554 s = new Long(aleatoire).toString();
555 return s;
556 }
557
558 public static String dateToEntierToDate (String dataDate, boolean separator, int nbJours, boolean plusOuMoins)
559 String y,m,d;
560 int a,b,c;
561
562 SimpleDateFormat formatDate;
563 formatDate = separator ? new SimpleDateFormat("yyyy-MM-dd") : new SimpleDateFormat("yyyyMMdd");
564
565 Calendar calendar = Calendar.getInstance();
566 try {
567 calendar.setTime(formatDate.parse(dataDate));
568 }
569 catch (ParseException p) {
570 return "";
571 }
572
573 if (plusOuMoins) calendar.add(Calendar.DATE,nbJours);
574 else calendar.add(Calendar.DATE,(nbJours*-1));
575
576 a = calendar.get(Calendar.YEAR);
577 b = calendar.get(Calendar.MONTH)+1;
578 c = calendar.get(Calendar.DAY_OF_MONTH);
579
580 if (Utils.debug) System.out.println("AAAA-MM-DD : "+a+"-"+b+"-"+c);
581 y = (String)Integer.toString(a);
582 m = (String)Integer.toString(b);
583 d = (String)Integer.toString(c);
584 if (m.length()==1) m = "0"+m;
585 if (d.length()==1) d = "0"+d;
586
587
588
589 return (y+"-"+m+"-"+d);
590 }
591 /***
592 * @param textString
593 */
594 public static String checkSpecialCharCtrl(String textString) {
595
596 if ( textString==null || textString.trim().length()==0 ) return "";
597
598 StringBuffer out;
599 char s1;
600 int i,j;
601
602
603 j = textString.trim().length();
604 out = new StringBuffer();
605 for (i=0;i<j;i++) {
606 s1 = textString.charAt(i);
607 Character s2 = new Character(s1);
608
609 if ( s2.isISOControl(s1) ) {
610
611 out.append(".");
612 }
613 else out.append(s1);
614
615 }
616
617 return out.toString();
618 }
619
620 /***
621 *
622 */
623 public static String[] tokenize(String sManyTokens,String sDelimiter){
624 StringTokenizer st = new StringTokenizer(sManyTokens,sDelimiter);
625 int nCount = st.countTokens();
626 if (Utils.debug) System.out.println("Utilities::tokenize - nCount="+nCount);
627 String sTokens[] = new String[nCount];
628 for (int i = 0; i < nCount; i++){
629 sTokens[i] = st.nextToken();
630 if (Utils.debug) System.out.println("Utilities::tokenize - token="+sTokens[i]);
631 }
632 return sTokens;
633 }
634
635 /***
636 *
637 */
638 public static int[] tokenizeInts(String sManyTokens,String sDelimiter){
639 StringTokenizer st = new StringTokenizer(sManyTokens,sDelimiter);
640 int nCount = st.countTokens();
641 if (Utils.debug) System.out.println("Utilities::tokenize - nCount="+nCount);
642 int nTokenArray[] = new int[nCount];
643 for (int i = 0; i < nCount; i++){
644 nTokenArray[i] = Integer.parseInt(st.nextToken());
645 if (Utils.debug) System.out.println("Utilities::tokenize - nTokenArray["+i+"]="+nTokenArray[i]);
646 }
647 return nTokenArray;
648 }
649
650
651
652
653 public static boolean translateBoolean(String sBoolean){
654 if (sBoolean == null){
655 return false;
656 }
657 if (sBoolean.equalsIgnoreCase("Y")
658 || sBoolean.equalsIgnoreCase("Yes")
659 || sBoolean.equals("1")
660 || sBoolean.equalsIgnoreCase("True")){
661 return true;
662 }else{
663 return false;
664 }
665 }
666
667 /***
668 *
669 */
670 public static String buildString(String[] sData,String sDelimiter)
671 {
672 if (sData == null){
673 return null;
674 }
675 String sReturn = "";
676 if(sData.length==1){
677 sReturn = sData[0];
678 return sReturn;
679 }
680 for(int i=0;i<sData.length;i++){
681 if(i==sData.length-1){
682 sReturn += sData[i];
683 }else{
684 sReturn += sData[i]+sDelimiter;
685 }
686 }
687 return sReturn;
688 }
689
690 /***
691 *
692 */
693 public static String buildString(Vector vData,String sDelimiter,String sQuote)
694 {
695 if (vData == null
696 || vData.isEmpty()){
697 return "";
698 }
699 String sReturn = "";
700 if(vData.size()==1){
701 sReturn = sQuote+(String)vData.get(0)+sQuote;
702 return sReturn;
703 }
704 for(int i=0;i<vData.size();i++){
705 if(i==vData.size()-1){
706 sReturn += sQuote+(String)vData.get(i)+sQuote;
707 }else{
708 sReturn += sQuote+(String)vData.get(i)+sQuote+sDelimiter;
709 }
710 }
711 return sReturn;
712 }
713
714 /***
715 *
716 */
717 public static String buildString(ArrayList al, String sDelimeter, String sSuffixPrefix)
718 throws Exception{
719 String sMsg = "";
720 for (int i=0;i<al.size();i++){
721 String sString=(String)(al.get(i));
722 if (sMsg != ""){
723 sMsg += sDelimeter;
724 }
725 sMsg += sSuffixPrefix + sString + sSuffixPrefix;
726 }
727 return sMsg;
728 }
729
730 /***
731 *
732 */
733 public static String buildStringUsingInteger(ArrayList al, String sDelimeter, String sSuffixPrefix)
734 throws Exception{
735 String sMsg = "";
736 for (int i=0;i<al.size();i++){
737 Integer nInteger=(Integer)(al.get(i));
738 if (sMsg != ""){
739 sMsg += sDelimeter;
740 }
741 sMsg += sSuffixPrefix + nInteger.toString() + sSuffixPrefix;
742 }
743 return sMsg;
744 }
745
746 /***
747 * Write a string to a file
748 * @param sData Data to be writen to file
749 * @param sFile Fully qualified file name
750 * @return String The file contents.
751 */
752 public static void writeFile(String sData,String sFile)
753 throws FileNotFoundException,IOException{
754 FileWriter fileWriter = null;
755 try{
756 fileWriter = new FileWriter(sFile);
757 fileWriter.write(sData);
758 }finally{
759 if(fileWriter != null){
760 fileWriter.close();
761 }
762 }
763 }
764
765 /***
766 * Get the contents of the file.
767 * @param String Fully qualified file name
768 * @return String The file contents.
769 */
770 public static String readFile(String sFile)
771 throws FileNotFoundException,IOException{
772 String sKeyContents="";
773 FileInputStream fileInputStream = null;
774 try{
775 if (sFile != null){
776 File file = new File(sFile);
777 if(file.exists()){
778 fileInputStream = new FileInputStream(file);
779 }else{
780 fileInputStream = new FileInputStream(sFile);
781 }
782 byte bKeyContents[] = new byte[(int)file.length()];
783 System.out.println("Utils : sFile :"+sFile);
784 System.out.println("Utils : filelength() :"+file.length());
785 fileInputStream.read(bKeyContents);
786 sKeyContents = new String(bKeyContents);
787 }
788 }finally{
789 if(fileInputStream != null){
790 fileInputStream.close();
791 }
792 }
793 return sKeyContents;
794 }
795
796 /***
797 * Get the contents of the file in the classpath
798 * @param String class name of the file whose contents to be read.
799 * @return String text contents of the file
800 */
801 public static String readFileByClassPath(String configTemplateFile)
802 throws Exception{
803 InputStream inputStream = Class.forName("org.opensync.med.service.eservices.util.ESHelper").getResourceAsStream(configTemplateFile);
804 InputStreamReader isReader=new InputStreamReader(inputStream);
805 BufferedReader bReader=new BufferedReader(isReader);
806 String newLine="";
807 StringBuffer sb=new StringBuffer();
808 while((newLine=bReader.readLine())!=null)
809 {
810 sb.append(newLine);
811 sb.append("\n");
812 }
813 bReader.close();
814 inputStream.close();
815 return sb.toString();
816 }
817
818
819
820
821
822
823
824
825 public static int daysElapsed(String sBeginDate, String sEndDate)
826 throws IllegalArgumentException,Exception {
827 if (Utils.debug) System.out.println("Utils::daysElapsed sBeginDate="+sBeginDate+" sEndDate="+sEndDate);
828 if (sBeginDate == null
829 || sBeginDate.equals("")){
830 throw new Exception("Utils::daysElapsed - Start date is empty.");
831 }
832 String sEndDay = "";
833 String sEndMonth = "";
834 String sEndYear = "";
835 try {
836 sEndDay = sEndDate.substring(8);
837 sEndMonth = sEndDate.substring(5,7);
838 sEndYear = sEndDate.substring(0,4);
839 }catch(NumberFormatException nfe) {
840 throw new Exception("The End Date("+sEndDate+") doesn't contain hyphens, please check the data");
841
842 }
843 if (Utils.debug) System.out.println("Utils::daysElapsed sEndDay="+sEndDay+" sEndMonth="+sEndMonth+" sEndYear="+sEndYear);
844
845
846
847
848
849
850
851
852
853
854
855
856 int nDaysElapsed = 0;
857 try {
858 nDaysElapsed =
859
860 (int)(getCalendar(Integer.parseInt(sEndDay),
861 Integer.parseInt(sEndMonth),
862 Integer.parseInt(sEndYear)).getTime().getTime() /
863 (24 * 60 * 60 * 1000)) -
864 (int)(getCalendar(Integer.parseInt(sBeginDate.substring(8)),
865 Integer.parseInt(sBeginDate.substring(5,7)),
866 Integer.parseInt(sBeginDate.substring(0,4))).getTime().getTime() /
867 (24 * 60 * 60 * 1000));
868 }catch(NumberFormatException nfe) {
869 throw new Exception("The Begin Date("+sBeginDate+") doesn't contain hyphens, please check the data");
870
871 }
872 return nDaysElapsed;
873 }
874
875
876
877
878 public static int daysElapsed(int day1, int month1, int year1,
879 int day2, int month2, int year2)
880 throws IllegalArgumentException {
881 return(Math.abs(
882 (int)(getCalendar(day1, month1, year1).getTime().getTime() /
883 (24 * 60 * 60 * 1000)) -
884 (int)(getCalendar(day2, month2, year2).getTime().getTime() /
885 (24 * 60 * 60 * 1000))));
886 }
887
888
889
890
891 public static GregorianCalendar getCalendar(int d, int m, int y)
892 throws IllegalArgumentException {
893
894 GregorianCalendar calendar = new GregorianCalendar();
895 calendar.clear();
896
897 calendar.setLenient(false);
898 calendar.set(y, m - 1, d);
899 return(calendar);
900 }
901
902
903
904
905 public static String dateNow() {
906
907 Calendar calendar = Calendar.getInstance();
908 SimpleDateFormat dateFormat =
909 new SimpleDateFormat("ddMMyyyyHHmmssSSS");
910 Date date = calendar.getTime();
911 return dateFormat.format(calendar.getTime());
912 }
913
914
915
916
917 public static ArrayList convertStringArrayToArrayList(String [][] sStringArray)
918 {
919 ArrayList alResults = new ArrayList();
920 ArrayList alRow;
921 int nMax1 = 0;
922 if (sStringArray!=null) nMax1 = sStringArray.length;
923 else nMax1 = -1;
924 for (int i=0;i<nMax1;i++) {
925 alRow = new ArrayList();
926 int nMax2 = 0;
927 if (sStringArray[i]!=null) nMax2 = sStringArray[i].length;
928 else nMax2 = -1;
929 for (int j=0;j<nMax2;j++) {
930
931 alRow.add(sStringArray[i][j]);
932 }
933 alResults.add(alRow);
934 }
935 return alResults;
936 }
937
938
939
940
941 public static String[][] ConvertArrayListToStringArray(ArrayList alData)
942 {
943 if (alData == null
944 || alData.size() == 0
945 || ((ArrayList)alData.get(0)) == null
946 || ((ArrayList)alData.get(0)).size() == 0){
947 return new String[0][0];
948 }
949 int i=-1;
950 int j=-1;
951 ArrayList alDataInner;
952 String sData = "";
953 String[][] sResultArray = new String[alData.size()][((ArrayList)alData.get(0)).size()];
954 Iterator iterData = alData.iterator();
955 while(iterData.hasNext()){
956 i++;
957 j=-1;
958 alDataInner = (ArrayList)iterData.next();
959 Iterator iterDataInner = alDataInner.iterator();
960 while(iterDataInner.hasNext()){
961 j++;
962 sData = (String)iterDataInner.next();
963 sResultArray[i][j] = sData;
964 }
965 }
966 return sResultArray;
967 }
968
969
970
971
972
973
974 public static HashMap stringToHashMap(String sInput,String sMainToken,String sKeyValueToken)
975 {
976 HashMap hmReturn = new HashMap();
977 if (sInput == null || sInput.equals("")){
978 if (Utils.debug) System.out.println("DashboardView::stringToHashMap returning null");
979 return hmReturn;
980 }
981 String sItem = "";
982 String[] sHashMapItemArray = null;
983 String[] sHashMapArray = tokenize(sInput,sMainToken);
984 for (int i=0;i<sHashMapArray.length;i++){
985 sItem = sHashMapArray[i];
986 sHashMapItemArray = tokenize(sItem,sKeyValueToken);
987 hmReturn.put(sHashMapItemArray[0],sHashMapItemArray[1]);
988 }
989 return hmReturn;
990 }
991
992 /***
993 * Modify the message by carrying out replacement
994 * @param inString the String to be modified.
995 * @param tobeReplaced the subString to be replaced.
996 * @param replacement the replacement string value.
997 * @return modified string
998 */
999 public static String replace(String inString,String tobeReplaced,String replacement)
1000 {
1001 StringBuffer buffer = new StringBuffer(inString);
1002 int start = 0;
1003 int end=0;
1004 int idx;
1005
1006 /***
1007 * loop to replace all the words from the message.
1008 * it gets the start index and the last index of
1009 * sub-string and carries out the replacemnet
1010 */
1011 while(true){
1012
1013 idx = buffer.substring(end,buffer.length()).indexOf(tobeReplaced);
1014
1015
1016 if(idx == -1) {
1017 break;
1018 }
1019 start = end + idx;
1020 end = start + tobeReplaced.length();
1021 buffer.replace(start, end, replacement);
1022
1023
1024 end =start + replacement.length();
1025
1026 if(end >= inString.length()) {
1027 break;
1028 }
1029 }
1030
1031 return new String(buffer);
1032 }
1033
1034
1035
1036
1037 public static int fetchIntegerFromHashMap(HashMap hm,
1038 String sKey,
1039 int nDefault){
1040 int nValue = nDefault;
1041 String sValue = (String)hm.get(sKey);
1042 if (sValue != null && !sValue.equals("")){
1043 nValue = Integer.parseInt(sValue);
1044 }
1045 if (Utils.debug) System.out.println("MathProcessor::fetchIntegerFromHashMap - sValue="+sValue);
1046 return nValue;
1047 }
1048
1049
1050
1051
1052 public static String fetchStringFromHashMap(HashMap hm,
1053 String sKey,
1054 String sDefault){
1055 String sValue = (String)hm.get(sKey);
1056 if (Utils.debug) System.out.println("MathProcessor::fetchStringFromHashMap - sValue="+sValue);
1057 if (sValue == null || sValue.equals("")){
1058 sValue = sDefault;
1059 }
1060 return sValue;
1061 }
1062
1063
1064 public static String fetchDateCommonFormat(String dateValue,String format,String sep) {
1065
1066 if(sep.equals("")) {
1067 sep = "-";
1068 int firstValue = format.indexOf("|");
1069 String yearParam = dateValue.substring(0,firstValue);
1070 String monthParam = dateValue.substring(firstValue,format.indexOf("|",firstValue+1)-1);
1071 int length = yearParam.length()+monthParam.length();
1072 String dateParam = dateValue.substring(length,dateValue.length());
1073
1074 dateValue = yearParam+sep+monthParam+sep+dateParam;
1075
1076
1077
1078 }
1079
1080 return dateValue;
1081
1082
1083 }
1084
1085
1086
1087
1088
1089 public static int indexOfStringFromVector(Vector v,
1090 String sKey,
1091 String sPattern){
1092
1093 int vSize = v.size();
1094 for(int i=0;i<vSize;i++)
1095 {
1096 String strCur = (String)v.elementAt(i);
1097 if(strCur.equals(sKey) || (strCur.indexOf(sKey+sPattern)!=-1) )
1098 return i;
1099 }
1100 return -1;
1101 }
1102
1103 }