1 package org.opensync.importexport;
2
3 import java.io.File;
4 import java.io.RandomAccessFile;
5 import java.sql.SQLException;
6 import java.sql.SQLWarning;
7 import java.util.HashMap;
8 import java.util.Hashtable;
9 import java.util.Vector;
10
11 import org.opensync.tools.ConnectionPool;
12 import org.opensync.tools.Constants;
13 import org.opensync.tools.DicoReader;
14 import org.opensync.tools.MacroProcessor;
15 import org.opensync.tools.Parent;
16 import org.opensync.tools.QuerySelectBean;
17 import org.opensync.tools.QuerySelectIndex;
18 import org.opensync.tools.Utils;
19
20
21 public class ExportOfView extends Object {
22
23 private String idParentFile;
24 private StringBuffer conditionWhere, conditionOrder;
25
26 private StringBuffer xmlStringOut;
27 private StringBuffer element = new StringBuffer();
28
29 private Vector keysList, keysSearch;
30 private Hashtable criteria;
31 private View theme;
32 private HashMap m_hmRuntimeData = null;
33
34
35 private Vector resultsIndex;
36 private QuerySelectBean selectBean = null;
37 private QuerySelectIndex querySelectIndex = null;
38 private String results[][]=null;
39
40
41 /***
42 * Constructor
43 */
44 public ExportOfView(ConnectionPool pool,
45 View _view,
46 HashMap hmRuntimeData) throws Exception,SQLException,SQLWarning {
47
48
49 theme = _view;
50 m_hmRuntimeData = hmRuntimeData;
51 if (m_hmRuntimeData == null){
52 m_hmRuntimeData = new HashMap();
53 }
54 idParentFile = theme.getIdParentFile();
55
56 criteria = new Hashtable();
57
58
59
60 keysList = buildKeysStructureTheme(0);
61
62 keysSearch = buildKeysStructureTheme(1);
63
64
65 buildConditionWhere();
66
67
68 buildOrderBy();
69
70
71 launchQuery(pool);
72
73
74
75 }
76
77 /***
78 * Constructor
79 */
80 public ExportOfView(ConnectionPool pool,
81 View _view,
82 HashMap hmRuntimeData,
83 int startline,
84 int numline) throws Exception,SQLException,SQLWarning {
85
86
87 theme = _view;
88 m_hmRuntimeData = hmRuntimeData;
89 if (m_hmRuntimeData == null){
90 m_hmRuntimeData = new HashMap();
91 }
92 idParentFile = theme.getIdParentFile();
93
94 criteria = new Hashtable();
95
96
97
98 keysList = buildKeysStructureTheme(0);
99
100 keysSearch = buildKeysStructureTheme(1);
101
102
103 buildConditionWhere();
104
105
106 buildOrderBy();
107
108
109 launchQuery(pool, startline, numline);
110
111
112
113
114 }
115
116
117 public int getNbResults() {
118 if (results!=null) return results.length;
119 else return 0;
120 }
121
122 public int getNbResultsIndex() {
123 if (resultsIndex!=null) return resultsIndex.size();
124 else return 0;
125 }
126
127
128 private Vector buildKeysStructureTheme (int which) {
129 Vector vector;
130 Column v[];
131 int j,indexP,iMax,jMax,indexPMax;
132 boolean found ;
133 String keyW,keyWT,s1,visible;
134
135 j= 0;
136 vector = new Vector();
137
138 v = theme.getElements();
139 if (Utils.debug) System.out.println("nb Columns : " + v.length);
140 jMax = v.length;
141 for (j=0;j<jMax;j++) {
142 Column e = v[j];
143 if (Utils.debug) System.out.println(" element of : " + e.getKeyWord()+ " VisibleListe "+e.getVisibleList()+" VisibleSearch "+e.getVisibleSearch());
144 visible = "FALSE";
145 switch (which) {
146 case 0 :
147 visible = e.getVisibleList();
148 break;
149 case 1 :
150 visible = e.getVisibleSearch();
151 this.buildStatementCriteria(visible, e);
152 break;
153 }
154 if ( visible!=null && visible.equals("TRUE") ) {
155 keyW = e.getKeyWord();
156 if ( vector.indexOf(keyW) == -1 ) vector.addElement(keyW);
157 }
158 }
159 return vector;
160 }
161
162 void buildStatementCriteria(String visible, Column e) {
163 int i,j,indexP,iMax,jMax,indexPMax;
164
165
166 String file, parentFile, typeField, field;
167 StringBuffer criteriaText;
168
169 criteriaText = new StringBuffer("");
170
171 if ( visible!=null && visible.length()>0 && visible.equals("TRUE")
172 file = e.getFile();
173 parentFile = e.getParentFile();
174 typeField = e.getType();
175 field = e.getField();
176 Parent it = theme.getParent(idParentFile);
177
178
179 if (file.equals(idParentFile)) {
180 criteriaText.append(field);
181 criteriaText.append(" <OPERATOR> <VALUE> ");
182
183
184 if (Utils.debug) System.out.println(" Critère sur ParentFile: " + criteriaText);
185 }
186
187
188 if (file.equals(idParentFile)==false && parentFile.length()==0) {
189 iMax = it.getChildSize();
190 for (i=0;i<iMax;i++) {
191 if( file.equals(it.getIdChildFile(i)) ) break;
192 }
193 if (i<iMax) {
194 criteriaText.append(it.getKeyParentFile(i));
195 criteriaText.append(" IN (SELECT ");
196 criteriaText.append(it.getKeyChildFile(i));
197 criteriaText.append(" FROM ");
198 criteriaText.append(it.getIdChildFile(i));
199 criteriaText.append(" WHERE ");
200 criteriaText.append(field);
201 criteriaText.append(" <OPERATOR> <VALUE> ) ");
202
203
204 }
205 if (Utils.debug) System.out.println(" Critère sur ChildFile: " + criteriaText);
206 }
207
208
209 if (file.equals(idParentFile)==false && parentFile.length()>0) {
210 iMax = it.getChildSize();
211 for (i=0;i<iMax;i++) {
212 if( parentFile.equals(it.getIdChildFile(i)) ) break;
213 }
214 if (i<iMax) {
215 criteriaText.append(it.getKeyParentFile(i));
216 criteriaText.append(" IN (SELECT ");
217 criteriaText.append(it.getKeyChildFile(i));
218 criteriaText.append(" FROM ");
219 criteriaText.append(it.getIdChildFile(i));
220 criteriaText.append(" WHERE ");
221 Parent itbis = theme.getParent(parentFile);
222 iMax = itbis.getChildSize();
223 for (i=0;i<iMax;i++) {
224 if( file.equals(itbis.getIdChildFile(i)) ) break;
225 }
226 if (i<iMax) {
227 criteriaText.append(itbis.getKeyParentFile(i));
228 criteriaText.append(" IN (SELECT ");
229 criteriaText.append(itbis.getKeyChildFile(i));
230 criteriaText.append(" FROM ");
231 criteriaText.append(itbis.getIdChildFile(i));
232 criteriaText.append(" WHERE ");
233 criteriaText.append(field);
234 criteriaText.append(" <OPERATOR> <VALUE> ) )");
235
236
237
238 }
239
240 }
241 if (Utils.debug) System.out.println(" Critère ChildFile of Childfile : " + criteriaText);
242 }
243
244
245 if ( criteriaText.length()>0 ) criteria.put(e.getKeyWord(),criteriaText);
246 }
247 }
248
249 void buildConditionWhere()
250 throws Exception{
251 if (Utils.debug) System.out.println("ExportOfView::buildConditionWhere - begin");
252 int i,iMax,j;
253 String valueCondition="",operateur="";
254 String conditionString,sRuntimeData;
255 StringBuffer conditionStringBuffer;
256 Column e;
257 String keyW,typeF;
258
259 conditionWhere = new StringBuffer("");
260 Hashtable initValues = new Hashtable();
261
262 iMax = keysSearch.size();
263
264 for (i=0;i<iMax;i++) {
265 keyW = (String)keysSearch.elementAt(i);
266 e = theme.getElementByKey(keyW);
267 initValues.put(keyW,e.getSearchValue()+Constants.DELIM_WHERE_CLAUSE_MAIN+e.getOperateur());
268 if (Utils.debug) System.out.println("ExportOfView::buildConditionWhere - specialCriteria keyW="+keyW+" e.getSearchValue()="+e.getSearchValue()+" e.getOperateur()="+e.getOperateur());
269 }
270
271
272 String sPrefix,sSuffix;
273 iMax = keysSearch.size();
274 for (i=0;i<iMax;i++) {
275 keyW = (String)keysSearch.elementAt(i);
276 e = theme.getElementByKey(keyW);
277 typeF = e.getType();
278 valueCondition = (String)initValues.get(keyW);
279 if (e.getPrefix() == null
280 || e.getPrefix().equals("")){
281 sPrefix = " AND ";
282 }else{
283 sPrefix = e.getPrefix();
284 }
285 if (e.getSuffix() == null
286 || e.getSuffix().equals("")){
287 sSuffix = "";
288 }else{
289 sSuffix = e.getSuffix();
290 }
291 if (Utils.debug) System.out.println("ExportOfView::buildConditionWhere - valueCondition1="+valueCondition);
292
293
294
295 String[] sValueConditions = Utils.tokenize(valueCondition,Constants.DELIM_WHERE_CLAUSE_MAIN);
296 if (sValueConditions == null
297 || sValueConditions.length == 0){
298 break;
299 }
300 String[] sSearchValues = Utils.tokenize(sValueConditions[0],Constants.DELIM_WHERE_CLAUSE_ITEM);
301 String[] sOperators = Utils.tokenize(sValueConditions[1],Constants.DELIM_WHERE_CLAUSE_ITEM);
302 for (int l=0;l<sOperators.length;l++){
303 valueCondition = sSearchValues[l] + Constants.DELIM_WHERE_CLAUSE_ITEM + sOperators[l];
304 if (Utils.debug) System.out.println("ExportOfView::buildConditionWhere - valueCondition2="+valueCondition);
305 j = valueCondition.indexOf(Constants.DELIM_WHERE_CLAUSE_ITEM);
306 if (j != -1 ) {
307 operateur = valueCondition.substring(j+Constants.DELIM_WHERE_CLAUSE_ITEM.length());
308 valueCondition = valueCondition.substring(0,j);
309 if (typeF.equals("C") || typeF.equals("T")){
310 valueCondition = Utils.checkSpecialChar(valueCondition , '\'' , "''" );
311 }
312 }
313 if (Utils.debug) System.out.println("ExportOfView::buildConditionWhere - valueCondition3="+valueCondition);
314 if (valueCondition != null
315 && valueCondition.startsWith("$")){
316 sRuntimeData = (String)m_hmRuntimeData.get(valueCondition);
317 if (sRuntimeData != null
318 && !sRuntimeData.equals("")){
319 valueCondition = sRuntimeData;
320 }else{
321 sRuntimeData = MacroProcessor.buildMacro(valueCondition,e,null);
322 if (sRuntimeData != null
323 && !sRuntimeData.equals("")){
324 valueCondition = sRuntimeData;
325 }else{
326
327 if (Utils.debug) System.out.println("ExportOfView::buildConditionWhere - skipping where clause item because "+valueCondition+" is null or empty.");
328 continue;
329 }
330 }
331 }
332 if (Utils.debug) System.out.println("ExportOfView::buildConditionWhere - valueCondition4="+valueCondition);
333 conditionStringBuffer = new StringBuffer("");
334 conditionStringBuffer.append(criteria.get(keyW));
335 conditionString = conditionStringBuffer.toString();
336 j = conditionString.indexOf("<OPERATOR>");
337 if (j > -1) conditionStringBuffer.replace(j,j+11,operateur);
338 conditionString = conditionStringBuffer.toString();
339 j = conditionString.indexOf("<VALUE>");
340 if (j > -1) {
341 if (valueCondition.equals(Constants.EMPTY_ELEMENT)){
342 valueCondition = "";
343 }
344 if (operateur.toUpperCase().indexOf("IN") > -1){
345 conditionStringBuffer.replace(j,j+7,"("+valueCondition.trim().toUpperCase()+")");
346 }else if (valueCondition.trim().length()>0){
347 if (typeF.equals("C") || typeF.equals("T") || typeF.equals("D")){
348 conditionStringBuffer.replace(j,j+7,"'"+valueCondition.trim().toUpperCase()+"'");
349 }else{
350 conditionStringBuffer.replace(j,j+7,valueCondition.trim().toUpperCase());
351 }
352 }
353 else conditionStringBuffer.replace(j,j+7," ");
354 }
355
356 conditionWhere.append(sPrefix);
357 conditionWhere.append(conditionStringBuffer.toString());
358 conditionWhere.append(sSuffix);
359 if (Utils.debug) System.out.println("ExportOfView::buildConditionWhere - conditionWhere="+conditionWhere);
360 }
361 }
362
363 if (Utils.debug) System.out.println("Condition : "+ conditionWhere.toString());
364
365 }
366
367 void buildOrderBy() {
368 conditionOrder = new StringBuffer("");
369 Column e;
370 String typeF;
371 int iMax = keysSearch.size();
372 if (Utils.debug) System.out.println("ExportOfView::buildOrderBy - iMax="+iMax);
373 for (int i=0;i<iMax;i++) {
374 String keyW = (String)keysSearch.elementAt(i);
375 if (Utils.debug) System.out.println("ExportOfView::buildOrderBy - keyW="+keyW);
376 e = theme.getElementByKey(keyW);
377 if (Utils.debug) System.out.println("ExportOfView::buildOrderBy - e.getOrderBy()="+e.getOrderBy());
378 if (e.getOrderBy() != null && !e.getOrderBy().equals(""))
379 {
380 if (conditionOrder.toString().equals(""))
381 {
382 conditionOrder.append(" ORDER BY ");
383 }
384 conditionOrder.append(e.getField());
385 conditionOrder.append(" ");
386 conditionOrder.append(e.getOrderBy());
387 }
388 }
389 if (Utils.debug) System.out.println("ExportOfView::buildOrderBy = orderBy="+conditionOrder.toString());
390 }
391
392 public void launchQuery(ConnectionPool pool)throws SQLException,SQLWarning{
393
394 resultsIndex = new Vector();
395 querySelectIndex = new QuerySelectIndex(pool,
396 theme.getPrimaryKeyElements(),
397 idParentFile,
398 conditionWhere.toString(),
399 getConditionOrder(),
400 resultsIndex);
401 if (Utils.debug) System.out.println("ExportOfView::launchQuery - End querySelectIndex of theme and Start QueryselectBean : "+idParentFile+" Results Index size === " + resultsIndex.size() + " key list size"+keysList.size());
402
403 if (resultsIndex.size()>0 && keysList.size()>0) {
404
405 results = new String[resultsIndex.size()][theme.getElementsNumber()];
406 if (Utils.debug) System.out.println("ExportOfView::launchQuery - theme.getElementsIndex()="+ theme.getElementsIndex()+
407 " rows="+resultsIndex.size()+
408 " columns="+theme.getElementsNumber());
409 selectBean = new QuerySelectBean(pool,
410 theme.getQuerySelects(),
411 theme.getElementsIndex(),
412 idParentFile,
413 conditionWhere.toString(),
414 getConditionOrder(),
415 results,
416 keysList,
417 keysList);
418 selectBean.start();
419 if (Utils.debug) System.out.println("getData End **********************************************************");
420 }
421 }
422
423 public void launchQuery(ConnectionPool pool, int startline, int numline)throws SQLException,SQLWarning{
424
425 resultsIndex = new Vector();
426 querySelectIndex = new QuerySelectIndex(pool,
427 theme.getPrimaryKeyElements(),
428 idParentFile,
429 conditionWhere.toString(),
430 getConditionOrder(),
431 resultsIndex);
432 if (Utils.debug) System.out.println("ExportOfView::launchQuery - End querySelectIndex of theme and Start QueryselectBean : "+idParentFile+" Results Index size === " + resultsIndex.size() + " key list size"+keysList.size());
433
434 if (resultsIndex.size()>0 && keysList.size()>0) {
435
436
437
438 if ( (resultsIndex.size() - startline) < 0)
439 results = new String[0][theme.getElementsNumber()];
440 else if ( (resultsIndex.size() - startline) > numline)
441 results = new String[numline][theme.getElementsNumber()];
442 else
443 results = new String[resultsIndex.size() - startline][theme.getElementsNumber()];
444
445 if (Utils.debug) System.out.println(" No of Lines : " + results.length);
446
447 numline = results.length;
448 if (numline == 0)
449 resultsIndex.clear();
450
451 String cWhere = querySelectIndex.getConditionWhere(startline, numline, false);
452
453
454
455 querySelectIndex.setPositionStartLine(startline);
456
457
458 if (Utils.debug) System.out.println("ExportOfView::launchQuery - theme.getElementsIndex()="+ theme.getElementsIndex()+
459 " rows="+resultsIndex.size()+
460 " columns="+theme.getElementsNumber());
461 selectBean = new QuerySelectBean(pool,
462 theme.getQuerySelects(),
463 theme.getElementsIndex(),
464 idParentFile,
465
466 cWhere,
467 getConditionOrder(),
468 results,
469 keysList,
470 keysList);
471 selectBean.start();
472 if (Utils.debug) System.out.println("getData End **********************************************************");
473 }
474 }
475
476
477 public String getDataListXml () throws java.io.IOException {
478
479 Vector v,k;
480 View t;
481 int iMax,indexPMax,i,indexP,indCol;
482 String keyW,data;
483
484 t = theme;
485 k = keysList;
486 Hashtable elementsIndex = t.getElementsIndex();
487 System.out.println("getDataListXml Start *********************************************************with resultsIndex size :"+resultsIndex.size());
488
489 if (selectBean == null || resultsIndex.size()==0) return "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><DATA></DATA>";
490
491 if (Utils.debug) System.out.println("waiting for results.");
492 try {
493 while (selectBean.isAlive()) {
494 if (Utils.debug) System.out.print(".");
495 synchronized(this) {wait(100);}
496 }
497 }
498 catch (InterruptedException e) {
499 if (Utils.debug) System.out.println("cannot wait...");
500 }
501
502 xmlStringOut = new StringBuffer();
503 element = new StringBuffer();
504
505 xmlStringOut.append("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
506 xmlStringOut.append("<DATA>\n");
507
508 if (results!=null) iMax = results.length;
509 else iMax = -1;
510 indexPMax = k.size();
511 System.out.println("number of records is: "+iMax);
512 for (i=0;i<iMax;i++) {
513 xmlStringOut.append("<ROW>\n");
514 element.delete(0,element.capacity());
515 for (indexP=0;indexP<indexPMax;indexP++) {
516 keyW = (String)k.elementAt(indexP);
517
518 int length = Integer.parseInt(t.getElementByKey(keyW).getLength());
519
520 if ( elementsIndex.containsKey(keyW) ) {
521 indCol = ((Integer)elementsIndex.get(keyW)).intValue();
522
523 data = results[i][indCol];
524 if (data == null || data.length() == 0) data = "";
525 }
526 else data = "";
527
528 int length_before_checkingSpecialCharXml = data.length();
529 String data_before_checkingSpecialCharXml = data;
530
531 data=Utils.checkSpecialCharXml(data);
532
533 element.append("<"+keyW+">");
534
535 if (length_before_checkingSpecialCharXml > length) {
536 data_before_checkingSpecialCharXml = data_before_checkingSpecialCharXml.substring(0, length);
537 data=Utils.checkSpecialCharXml(data_before_checkingSpecialCharXml);
538
539 element.append(data.substring(0, data.length()) );
540 } else
541
542 element.append(data);
543 element.append("</"+keyW+">\n");
544 }
545 xmlStringOut.append(element);
546 xmlStringOut.append("</ROW>\n");
547 }
548 xmlStringOut.append("</DATA>\n");
549
550
551 return xmlStringOut.toString();
552 }
553
554 public String getDataListXmlGenericTags (String lg) throws java.io.IOException {
555
556 Vector v,k;
557 View t;
558 int iMax,indexPMax,i,indexP,indCol;
559 String keyW,data;
560
561 t = theme;
562 k = keysList;
563 Hashtable elementsIndex = t.getElementsIndex();
564 System.out.println("getDataListXml Start *********************************************************with resultsIndex size :"+resultsIndex.size());
565
566 if (selectBean == null || resultsIndex.size()==0) return "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><DATA></DATA>";
567
568 if (Utils.debug) System.out.println("waiting for results.");
569 try {
570 while (selectBean.isAlive()) {
571 if (Utils.debug) System.out.print(".");
572 synchronized(this) {wait(100);}
573 }
574 }
575 catch (InterruptedException e) {
576 if (Utils.debug) System.out.println("cannot wait...");
577 }
578
579 xmlStringOut = new StringBuffer();
580 element = new StringBuffer();
581 indexPMax = k.size();
582
583 xmlStringOut.append("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
584 xmlStringOut.append("<OUTPUT>\n");
585 xmlStringOut.append("<HEADERS>\n");
586 for (indexP=0;indexP<indexPMax;indexP++) {
587 keyW = (String)k.elementAt(indexP);
588 xmlStringOut.append("<HEADER Label=\""+DicoReader.getLabelDicoByKey(lg,keyW)+"\"/>");
589 }
590 xmlStringOut.append("</HEADERS>\n");
591 xmlStringOut.append("<DATA>\n");
592
593 if (results!=null) iMax = results.length;
594 else iMax = -1;
595 System.out.println("number of records is: "+iMax);
596 for (i=0;i<iMax;i++) {
597 xmlStringOut.append("<ROW>\n");
598 element.delete(0,element.capacity());
599 for (indexP=0;indexP<indexPMax;indexP++) {
600 keyW = (String)k.elementAt(indexP);
601
602 int length = Integer.parseInt(t.getElementByKey(keyW).getLength());
603
604 if ( elementsIndex.containsKey(keyW) ) {
605 indCol = ((Integer)elementsIndex.get(keyW)).intValue();
606
607 data = results[i][indCol];
608 if (data == null || data.length() == 0) data = "";
609 }
610 else data = "";
611
612 data=Utils.checkSpecialCharXml(data);
613
614 element.append("<COLUMN Key=\""+keyW+"\" Value=\"");
615 if (data.length() > length)
616 element.append(data.substring(0, length) );
617 else
618 element.append(data);
619 element.append("\"/>\n");
620 }
621 xmlStringOut.append(element);
622 xmlStringOut.append("</ROW>\n");
623 }
624 xmlStringOut.append("</DATA>\n");
625 xmlStringOut.append("</OUTPUT>\n");
626
627
628 return xmlStringOut.toString();
629 }
630
631 private void writeIntoFile(String xml, String fileName) throws java.io.IOException {
632
633 File dstFile;
634
635 //*****************//
636
637 //*****************//
638
639 dstFile = new File(fileName);
640 dstFile.createNewFile();
641
642
643
644 RandomAccessFile file=new RandomAccessFile(dstFile,"rw");
645 file.seek(file.length());
646 file.writeBytes(xml);
647 //*****************************//
648
649 //*****************************//
650 file.close();
651 }
652
653 public String[][] getResults()
654 {
655 try {
656 if (Utils.debug) System.out.println("ExportOfView::getResults - waiting for results");
657 if (selectBean != null){
658 while (selectBean.isAlive()) {
659 if (Utils.debug) System.out.print(".");
660 synchronized(this){
661 wait(100);
662 }
663
664 }
665 }
666 }
667 catch (InterruptedException e) {
668 if (Utils.debug) System.out.println("cannot wait...");
669 }
670 return results;
671 }
672
673 public String getConditionOrder() {
674 if ( conditionOrder == null ) return ("");
675 return this.conditionOrder.toString();
676 }
677
678
679
680 }
681