1 package org.opensync.importexport;
2
3 import org.opensync.tools.*;
4 import java.util.*;
5
6 public class View extends Theme {
7 private Column elements[];
8 private ElementLink elementsLink[];
9
10 private String themeIdParentFile;
11 private String inMainMenu = null;
12 private Hashtable querySelects;
13 private Hashtable elementsIndex;
14 private char rights;
15 private Parent parent;
16 private Vector parents;
17 private DbFile dbfile;
18 private String mode;
19
20 /***
21 * Constructor
22 */
23 public View(Theme t, DbFile db) {
24
25 this.keyWord = t.getKeyWord();
26 this.idParentFile = t.getIdParentFile();
27 themeIdParentFile = this.idParentFile;
28 this.label = t.getLabel();
29 this.idRightField = t.getIdRightField();
30 this.parentFile = t.getParentFile();
31 this.primaryKeys = t.getPrimaryKeys();
32 this.primaryKeyElements = t.getPrimaryKeyElements();
33 this.className = t.getClassName();
34 this.childsTheme = t.getChildsTheme();
35 dbfile = db;
36 elementsIndex = new Hashtable();
37 querySelects = new Hashtable();
38
39 }
40
41 public void initLabels(String lang) {
42 label = DicoReader.getLabelDicoByKey(lang,keyWord);
43 for (int i=0;i<elements.length;i++)
44 this.elements[i].initLabels(lang);
45 }
46
47 public void setElements(Vector e) {
48 elements = new Column[e.size()];
49 e.toArray(elements);
50 e.clear();
51
52 for (int i=0;i<elements.length;i++) {
53 Column el = elements[i];
54 elementsIndex.put(el.getKeyWord(),new Integer(i));
55 }
56 if (Utils.debug) System.out.println("Start buildStatementSelects in sophieTheme...");
57 buildStatementSelects();
58 if (Utils.debug) System.out.println("End buildStatementSelects in sophieTheme...");
59 }
60
61
62
63
64
65 public int getElementsNumber() {
66 return elements.length;
67 }
68 public Column getElement(int i) {
69 return elements[i];
70 }
71 public Column getElementByKey(String k) {
72 for (int i=0;i<elements.length;i++)
73 if (elements[i].equalsByKeyWord(k)) return elements[i];
74 return null;
75 }
76 public Column getElementByField(String k) {
77 for (int i=0;i<elements.length;i++)
78 if (elements[i].equalsByField(k)) return elements[i];
79 return null;
80 }
81 public Column[] getElements() {
82 return elements;
83 }
84
85 public Hashtable getElementsIndex() {
86 return elementsIndex;
87 }
88 public Hashtable getQuerySelects() {
89 return querySelects;
90 }
91
92 public ElementLink[] getLinkChildTheme(String t) {
93 if (childsTheme!=null && childsTheme.containsKey(t)) return (ElementLink[])childsTheme.get(t);
94 else return null;
95 }
96 public boolean isContaintLinkChildTheme(String t) {
97 if (childsTheme!=null && childsTheme.containsKey(t)) return true;
98 else return false;
99 }
100
101 public void setInMainMenu(String b) {
102 inMainMenu = b;
103 }
104
105 public String getInMainMenu() {
106 return inMainMenu;
107 }
108 public String getClassName() {
109 return className;
110 }
111 public void setMode(String b) {
112 if (b==null || b.length()==0) mode = "both";
113 else mode = b;
114 }
115 public String getMode() {
116 return mode;
117 }
118 private void buildStatementSelects() {
119
120
121 createParentTemplate();
122
123 createMultiKeys();
124
125 builQueryTemplate();
126 }
127
128 private void createParentTemplate() {
129
130 int i,j,indexP,iMax,jMax,indexPMax,x,y;
131 boolean found, isFixedCriteria ;
132 Column e;
133 Parent p,px;
134 Parent parent2;
135 Child child2;
136
137 if (Utils.debug) System.out.println("Start createParentTemplate......");
138 parents = new Vector();
139
140 parent = new Parent();
141 parent.setIdParentFile(themeIdParentFile);
142 parent.setLabel("");
143 parents.addElement(parent);
144
145 iMax = elements.length;
146
147
148 for (i=0;i<iMax;i++) {
149
150 e = elements[i];
151
152 String file = e.getFile();
153 String parentFile = e.getParentFile();
154 String keyParent = e.getKeyParent();
155 isFixedCriteria = e.getIsFixedCriteria();
156
157
158
159
160
161 if ( file.equals(themeIdParentFile)==false && isFixedCriteria==false && file.length() > 0 ) {
162
163 found = false;
164
165 if ( parentFile.equals("") == false ) {
166
167
168 if ( searchParentInParentTemplate(parentFile)==false ) {
169
170 parent2 = new Parent();
171 parent2.setIdParentFile(parentFile);
172 parent2.setLabel("");
173 parents.addElement(parent2);
174 }
175
176 if ( searchChildInChildOfParentTemplate(file,parentFile,keyParent)==false ) {
177
178
179 x = parents.size();
180 for(y=0;y<x;y++) {
181 px = (Parent)parents.elementAt(y);
182 if ( parentFile.equals(px.getIdParentFile()) ) {
183 break;
184 }
185 }
186 Parent pa = (Parent)dbfile.getParent(parentFile);
187 jMax = pa.getChildSize();
188 for (j=0;j<jMax;j++) {
189
190 if ( file.equals(pa.getIdChildFile(j))
191 && keyParent.equals(pa.getKeyParentFile(j)) ) {
192
193 Child child = new Child();
194 child.setIdChildFile(pa.getIdChildFile(j));
195 child.setKeyChildFile(pa.getKeyChildFile(j));
196 child.setKeyParentFile(pa.getKeyParentFile(j));
197 px = (Parent)parents.elementAt(y);
198 px.addChild(child);
199 found = true;
200 break;
201 }
202 }
203 }
204
205 if ( parentFile.equals(themeIdParentFile)==false ) {
206 if ( searchChildInChildOfParentTemplate(parentFile,themeIdParentFile,"")==false ) {
207 Parent pa = (Parent)dbfile.getParent(themeIdParentFile);
208 jMax = pa.getChildSize();
209 for (j=0;j<jMax;j++) {
210
211 if ( parentFile.equals(pa.getIdChildFile(j)) ) {
212
213
214 Child child = new Child();
215 child.setIdChildFile(pa.getIdChildFile(j));
216 child.setKeyChildFile(pa.getKeyChildFile(j));
217 child.setKeyParentFile(pa.getKeyParentFile(j));
218
219 parent.addChild(child);
220 break;
221 }
222 }
223 if (j>jMax) {
224
225 file = parentFile;
226 found = false;
227 }
228 }
229 }
230 }
231 else {
232 found = searchChildInChildOfParentTemplate(file,themeIdParentFile,"");
233
234 }
235
236 if (found==false) {
237
238 Parent pa = (Parent)dbfile.getParent(themeIdParentFile);
239 jMax = pa.getChildSize();
240 for (j=0;j<jMax;j++) {
241
242 if ( file.equals(pa.getIdChildFile(j)) ) {
243
244 found = true;
245
246 Child child = new Child();
247 child.setIdChildFile(pa.getIdChildFile(j));
248 child.setKeyChildFile(pa.getKeyChildFile(j));
249 child.setKeyParentFile(pa.getKeyParentFile(j));
250
251 parent.addChild(child);
252 break;
253 }
254 }
255 if ( found==false ) {
256
257 Parent pa1 = (Parent)dbfile.getParent(themeIdParentFile);
258 indexPMax = pa1.getChildSize();
259 for (indexP=0;indexP<indexPMax;indexP++) {
260
261 Parent pa2 = (Parent)dbfile.getParent(pa1.getIdChildFile(indexP));
262 String parentOfchildFilename = pa1.getIdChildFile(indexP);
263
264 jMax = pa2.getChildSize();
265 for (j=0;j<jMax;j++) {
266
267 if ( file.equals(pa2.getIdChildFile(j)) ) {
268
269
270 x = parents.size();
271 found = false;
272 for(y=0;y<x;y++) {
273 px = (Parent)parents.elementAt(y);
274 if ( parentOfchildFilename.equals(px.getIdParentFile()) ) {
275 found = true;
276 break;
277 }
278 }
279 if (found==false) {
280
281 parent2 = new Parent();
282 parent2.setIdParentFile(parentOfchildFilename);
283 parent2.setLabel("");
284 parents.addElement(parent2);
285
286 child2 = new Child();
287 child2.setIdChildFile(pa2.getIdChildFile(j));
288 child2.setKeyChildFile(pa2.getKeyChildFile(j));
289 child2.setKeyParentFile(pa2.getKeyParentFile(j));
290
291 parent2.addChild(child2);
292 }
293 else {
294
295 px = (Parent)parents.elementAt(y);
296 x = px.getChildSize();
297 found = false;
298 for(y=0;y<x;y++) {
299 if ( file.equals(px.getIdChildFile(y)) ) {
300 found = true;
301 break;
302 }
303 }
304 if ( found == false ) {
305
306 child2 = new Child();
307 child2.setIdChildFile(pa2.getIdChildFile(j));
308 child2.setKeyChildFile(pa2.getKeyChildFile(j));
309 child2.setKeyParentFile(pa2.getKeyParentFile(j));
310
311 px.addChild(child2);
312 }
313 }
314
315 parent = (Parent)parents.elementAt(0);
316 x = parent.getChildSize();
317 found = false;
318 for(y=0;y<x;y++) {
319 if ( parentOfchildFilename.equals(parent.getIdChildFile(y)) ) {
320 found = true;
321 break;
322 }
323 }
324 if ( found == false ) {
325
326 Child child = new Child();
327 child.setIdChildFile(pa1.getIdChildFile(indexP));
328 child.setKeyChildFile(pa1.getKeyChildFile(indexP));
329 child.setKeyParentFile(pa1.getKeyParentFile(indexP));
330
331 parent.addChild(child);
332 }
333 }
334 }
335 }
336 }
337 }
338 }
339 }
340
341
342
343
344
345
346
347
348
349
350
351
352 }
353
354 private boolean searchParentInParentTemplate (String filename) {
355 int indexP,indexPMax;
356 boolean foundParent;
357
358
359 indexPMax = parents.size();
360 foundParent = false;
361 for(indexP=0;indexP<indexPMax;indexP++) {
362 Parent pTest = (Parent)parents.elementAt(indexP);
363 if ( filename.equals(pTest.getIdParentFile()) ) {
364 foundParent = true;
365 break;
366 }
367 }
368 return foundParent;
369 }
370
371 private boolean searchChildInChildOfParentTemplate (String childfilename, String parentfilename, String keyparent) {
372 int indexP,indexPMax;
373 int indexC,indexCMax;
374 boolean foundChild;
375
376
377 indexPMax = parents.size();
378 foundChild = false;
379 for(indexP=0;indexP<indexPMax;indexP++) {
380 Parent pTest = (Parent)parents.elementAt(indexP);
381 if ( parentfilename.equals(pTest.getIdParentFile()) ) {
382 Parent cTest = (Parent)parents.elementAt(indexP);
383 indexCMax = cTest.getChildSize();
384 for(indexC=0;indexC<indexCMax;indexC++) {
385 if ( childfilename.equals(cTest.getIdChildFile(indexC)) ) {
386 if ( keyparent.equals("") == false ) {
387 if ( keyparent.equals(cTest.getKeyParentFile(indexC)) ) {
388 foundChild = true;
389 break;
390 }
391 }
392 else {
393 foundChild = true;
394 break;
395 }
396 }
397 }
398 if ( foundChild ) {
399 break;
400 }
401 }
402 }
403 return foundChild;
404 }
405
406 private void createMultiKeys() {
407 int i,j,indexP,iMax,jMax,indexPMax,x,y;
408 boolean found ;
409 Parent p,px;
410 Child child2;
411 if (Utils.debug) System.out.println("Start createMultiKeys......");
412
413 iMax = parents.size();
414 for (i= 0;i<iMax;i++) {
415 px = (Parent)parents.elementAt(i);
416 String parentfilename = px.getIdParentFile();
417 jMax = px.getChildSize();
418 for (j=0;j<jMax;j++) {
419 String file = px.getIdChildFile(j);
420 String keychildfile = px.getKeyChildFile(j);
421 p = (Parent)dbfile.getParent(parentfilename);
422 indexPMax = p.getChildSize();
423 for (indexP=0;indexP<indexPMax;indexP++) {
424 if ( file.equals(p.getIdChildFile(indexP)) ) {
425 if ( keychildfile.equals(p.getKeyChildFile(indexP))==false ) {
426
427 child2 = new Child();
428 child2.setIdChildFile(p.getIdChildFile(indexP));
429 child2.setKeyChildFile(p.getKeyChildFile(indexP));
430 child2.setKeyParentFile(p.getKeyParentFile(indexP));
431
432 px.addChild(child2);
433 }
434 }
435 }
436 }
437 }
438 }
439
440
441
442 private void builQueryTemplate() {
443
444 int i,j,indexP,iMax,jMax,indexPMax;
445 boolean found,isFixedCriteria ;
446 Column e;
447 QuerySelect qs;
448 QuerySelectField f;
449 Parent px;
450
451 Vector tables;
452 String queryText;
453 String criteriaText;
454 String file,field,keyWord,childFile,keychildFile, typeField;
455 tables = new Vector();
456 iMax = elements.length;
457
458
459 for (i=0;i<iMax;i++) {
460
461 e = elements[i];
462 file = e.getFile();
463 field = e.getField();
464 keyWord = e.getKeyWord();
465 typeField = e.getType();
466
467 isFixedCriteria = e.getIsFixedCriteria();
468
469 if (isFixedCriteria==false && file.length() > 0) {
470
471
472 QuerySelect qsTest = (QuerySelect)querySelects.get(file);
473 if (qsTest == null) {
474 qs = new QuerySelect();
475 qs.setFileName(file);
476 querySelects.put(file,qs);
477 tables.addElement(file);
478
479 queryText = "";
480 found = false;
481 }
482 else {
483 qs = qsTest;
484 queryText = qs.getQuerySelect();
485 found = true;
486 }
487
488 if (found==false) {
489 f = new QuerySelectField();
490 f.setKeyWord(e.getKeyWord());
491 f.setTypeField(e.getType());
492 f.setLabel(e.getLabel());
493 f.setFile(e.getFile());
494 f.setField(e.getField());
495 f.setTypeField(e.getType());
496 f.setChildFile("");
497 f.setKeychildFile(e.getKeyParent());
498 f.setIndex(qs.getQuerySelectFieldSize()+1);
499
500 queryText = queryText + e.getField() + ", ";
501 qs.setQuerySelect(queryText);
502 qs.addField(f);
503 }
504 else {
505 if ( searchFieldInQuerySelect(qs, file, field, keyWord) == false ) {
506 f = new QuerySelectField();
507 f.setKeyWord(e.getKeyWord());
508 f.setLabel(e.getLabel());
509 f.setFile(e.getFile());
510 f.setTypeField(e.getType());
511 f.setField(e.getField());
512 f.setChildFile("");
513 f.setKeychildFile(e.getKeyParent());
514 f.setIndex(qs.getQuerySelectFieldSize()+1);
515
516 queryText = queryText + e.getField() + ", ";
517 qs.setQuerySelect(queryText);
518 qs.addField(f);
519 }
520 }
521 }
522 }
523
524
525 iMax = parents.size();
526 for (i= 0;i<iMax;i++) {
527 px = (Parent)parents.elementAt(i);
528 file = px.getIdParentFile();
529 jMax = px.getChildSize();
530 for (j=0;j<jMax;j++) {
531 field = px.getKeyParentFile(j);
532 childFile = px.getIdChildFile(j);
533 keychildFile = px.getKeyChildFile(j);
534
535 QuerySelect qsTest = (QuerySelect)querySelects.get(file);
536 if (qsTest == null) {
537 qs = new QuerySelect();
538 qs.setFileName(file);
539 querySelects.put(file,qs);
540 queryText = "";
541 tables.addElement(file);
542 }
543 else {
544 qs = qsTest;
545 queryText = qs.getQuerySelect();
546 }
547
548 f = new QuerySelectField();
549 f.setKeyWord("|CHILD|"+field);
550 f.setLabel("");
551 f.setFile(file);
552 f.setField(field);
553 f.setChildFile(childFile);
554 f.setKeychildFile(keychildFile);
555 f.setIndex(qs.getQuerySelectFieldSize()+1);
556 f.setTypeField(childFile,keychildFile);
557
558 qs.addField(f);
559
560 queryText = queryText + field + ", ";
561 qs.setQuerySelect(queryText);
562 }
563 }
564
565 if (Utils.debug) {
566 iMax = tables.size();
567 if (Utils.debug) System.out.println("Tables : "+tables);
568 for (i=0;i<iMax;i++) {
569 file = (String)tables.elementAt(i);
570 QuerySelect qsD = (QuerySelect)querySelects.get(file);
571
572 if (Utils.debug) System.out.println("Tables name : "+qsD.getFileName());
573 if (Utils.debug) System.out.println("requete SELECT : "+qsD.getQuerySelect());
574 if (Utils.debug) System.out.println("primaryKeys : "+qsD.getPrimaryKeys());
575 if (Utils.debug) System.out.println("***************************************");
576 jMax = qsD.getQuerySelectFieldSize();
577 for (j=0;j<jMax;j++) {
578 if (Utils.debug) System.out.print("Key " +qsD.getKeyWord(j)+"\t");
579 if (Utils.debug) System.out.print("label "+qsD.getLabel(j)+"\t");
580 if (Utils.debug) System.out.print("file "+qsD.getFile(j)+"\t");
581 if (Utils.debug) System.out.print("Field "+qsD.getField(j)+"\t");
582 if (Utils.debug) System.out.print("Childfile "+qsD.getChildFile(j)+"\t");
583 if (Utils.debug) System.out.print("Keychildfile "+qsD.getKeychildFile(j)+"\t");
584 if (Utils.debug) System.out.print("index "+qsD.getIndex(j)+"\t");
585 if (Utils.debug) System.out.println("Type Field "+qsD.getTypeField(j)+"\t");
586 }
587 if (Utils.debug) System.out.println("***************************************");
588 }
589 }
590 }
591
592 private boolean searchFieldInQuerySelect (QuerySelect qTest, String fl, String fld, String kw ) {
593 int indexP,indexPMax;
594 boolean foundField;
595
596 indexPMax = qTest.getQuerySelectFieldSize();
597 foundField = false;
598 for(indexP=0;indexP<indexPMax;indexP++) {
599 if ( fl.equals(qTest.getFile(indexP))
600 && fld.equals(qTest.getField(indexP))
601 && kw.equals(qTest.getKeyWord(indexP)) ) {
602 foundField = true;
603 break;
604 }
605 }
606 return foundField;
607 }
608
609 public Parent getParent(String idFile) {
610 for (int i=0;i<parents.size();i++) {
611 Parent p = (Parent)parents.elementAt(i);
612 if (p.equals(idFile)) return p;
613 }
614 return null;
615 }
616
617
618
619
620 public Column findElementByKeyWord(String sKeyWord){
621 Column element = null;
622 Column[] columnArray = getElements();
623 for (int i=0;i<columnArray.length;i++) {
624 element = columnArray[i];
625 if (Utils.debug) System.out.println("View::findElementByKeyWord element="+element.getKeyWord());
626 if (element.getKeyWord().equals(sKeyWord)){
627 break;
628 }
629 }
630 return element;
631 }
632
633 }
634