1 package com.instantbank.component.lettertemplate.util;
2
3 import java.util.ArrayList;
4
5 import java.io.Serializable;
6 import com.instantbank.common.utilcomponents.LetterTemplateGlobals;
7 import com.instantbank.lettertemplate.control.util.JSPUtil;
8 import com.instantbank.lettertemplate.control.LetterTemplateEventException;
9
10 import com.instantbank.lettertemplate.editor.web.TemplateTransformer;
11
12
18 public class LetterComponent
19 implements Serializable, LetterViewable {
20
21
24 public static final int HEADER = 1;
25
26
29 public static final int BODY = 2;
30
31
34 public static final int CLOSING = 3;
35
36
39 public static final String IMGTYPE = "I";
40
41
44 public static final String VARTYPE = "V";
45
46
50 public boolean hasChanged = false;
51
52
55 private int type;
56
57
60 private int printType;
61
62
65 private long code;
66
67
70 private String name;
71
72
77 private ArrayList traceImages;
78
79
84 private ArrayList traceVariables;
85
86
93 private String rtfText;
94
95
99 private String stamp;
100
101
102
111 public LetterComponent(int type, int printType) {
112 this.type = type;
113 this.printType = printType;
114 this.code = LetterTemplateGlobals.UNDEF;
115 this.name = LetterTemplateGlobals.STR_UNNAMED;
116 this.stamp = LetterTemplateGlobals.STR_UNDEF;
117 this.hasChanged = false;
118 }
119
120
121
133 public LetterComponent(long code, int type, int printType, String name,
134 String rtfText, ArrayList traceVariables, ArrayList traceImages,
135 String stamp) {
136 this.code = code;
137 this.type = type;
138 this.printType = printType;
139 this.name = name;
140 this.rtfText = rtfText;
141 this.traceVariables = traceVariables;
142 this.traceImages = traceImages;
143 this.stamp = stamp;
144 }
145
146
147
152 public int getType() {
153 return (this.type);
154 }
155
156
157
162 public void setType(int type) {
163 this.type = type;
164 }
165
166
167
172 public int getPrintType() {
173 return (this.printType);
174 }
175
176
177
182 public void setPrintType(int printType) {
183 this.printType = printType;
184 }
185
186
187
192 public long getCode() {
193 return this.code;
194 }
195
196
197
202 public void setCode(long code) {
203 this.code = code;
204 }
205
206
207
212 public String getName() {
213 return (this.name);
214 }
215
216
217
222 public void setName(String name) {
223 this.name = name;
224 }
225
226
227
232 public ArrayList getTraceImages() {
233 return (this.traceImages);
234 }
235
236
237
242 public void setTraceImages(ArrayList traceImages) {
243 this.traceImages = traceImages;
244 }
245
246
247
252 public ArrayList getTraceVariables() {
253 return (this.traceVariables);
254 }
255
256
257
262 public void setTraceVariables(ArrayList traceVariables) {
263 this.traceVariables = traceVariables;
264 }
265
266
267
272 public String getRtfText() {
273 return rtfText;
274 }
275
276
277
282 public void setRtfText(String rtfText) {
283 this.rtfText = rtfText;
284 }
285
286
287
292 public String getStamp() {
293 return stamp;
294 }
295
296
297
302 public void setStamp(String st) {
303 stamp = st;
304 }
305
306
307
313 public String codesToString() {
314 return "<" + code + " , " + name + "," + stamp + ">";
315 }
316
317
318
324 public static String typeToString(int type) {
325 switch (type) {
326 case HEADER:
327 return "header";
328 case BODY:
329 return "body";
330 case CLOSING:
331 return "closing";
332 default:
333 return "unknown";
334 }
335 }
336
337
338
344 public static String typeToPlural(int type) {
345 switch (type) {
346 case HEADER:
347 return "headers";
348 case BODY:
349 return "bodies";
350 case CLOSING:
351 return "closings";
352 default:
353 return "unknown";
354 }
355 }
356
357
358
364 public static String typeToPlural(String type) {
365 String base = type.toLowerCase();
366 if(base.equals("header")) {
367 return "headers";
368 }
369 else if(base.equals("body")) {
370 return "bodies";
371 }
372 else {
373 return "closings";
374 }
375 }
376
377
378
392 public ArrayList[] mergeVarsImages() {
393 int mergeSize = traceImages.size() + traceVariables.size();
394 ArrayList[] merge = new ArrayList[mergeSize];
395 ArrayList slot = null;
396 ArrayList mSlot = null;
397 int v = 0;
398 int i = 0;
399 int m = 0;
400
401 while(m < mergeSize) {
402 if(i >= traceImages.size()) {
403
404 while(v < traceVariables.size()) {
405
406 slot = (ArrayList)traceVariables.get(v);
407 mSlot = new ArrayList(2);
408 mSlot.add(VARTYPE);
409 mSlot.add(slot);
410 merge[m] = mSlot;
411 m++;
412 v++;
413 }
414 }
415 else if(v >= traceVariables.size()) {
416
417 while(i < traceImages.size()) {
418
419 slot = (ArrayList)traceImages.get(i);
420 mSlot = new ArrayList(2);
421 mSlot.add(IMGTYPE);
422 mSlot.add(slot);
423 merge[m] = mSlot;
424 m++;
425 i++;
426 }
427 }
428 else {
429 int posVar;
430 int posImg;
431 ArrayList slotVar;
432 ArrayList slotImg;
433 slotVar = (ArrayList)traceVariables.get(v);
434 posVar = ((Integer)slotVar.get(0)).intValue();
435 slotImg = (ArrayList)traceImages.get(i);
436 posImg = ((Integer)slotImg.get(0)).intValue();
437
438 if(posVar > posImg) {
439
440 mSlot = new ArrayList(2);
441 mSlot.add(VARTYPE);
442 mSlot.add(slotVar);
443 merge[m] = mSlot;
444 m++;
445 v++;
446 }
447 else {
448
449 mSlot = new ArrayList(2);
450 mSlot.add(IMGTYPE);
451 mSlot.add(slotImg);
452 merge[m] = mSlot;
453 m++;
454 i++;
455 }
456 }
457 }
458
459 return merge;
460 }
461
462
463
470 public String mergeToString(ArrayList[] merge) {
471 ArrayList slot;
472 String line;
473 String resp = "VarImg Merge:\n";
474 int pos;
475 long varCode;
476 for(int i = 0; i < merge.length; i++) {
477 slot = merge[i];
478 String type = (String)slot.get(0);
479 slot = (ArrayList)slot.get(1);
480 pos = ((Integer)slot.get(0)).intValue();
481 line = "[ " + type + " pos= " + pos;
482 if(type.equals("V")) {
483 varCode = ((Long)slot.get(1)).longValue();
484 line += " varCode= " + varCode;
485 }
486 line += " ]";
487 resp += line + "\n";
488 }
489 return resp;
490 }
491
492
493
498 public String traceVarsToString() {
499 return traceVarsToString(traceVariables);
500 }
501
502
503
509 public String traceVarsToString(ArrayList trace) {
510 String resp = typeToString(type) + " " + name + " Variables trace: \n";
511 String line;
512 for(int i = 0; i < trace.size(); i++) {
513 ArrayList slot = (ArrayList)trace.get(i);
514 Integer pos = (Integer)slot.get(0);
515 Long varCode = (Long)slot.get(1);
516 line = "[ pos =" + pos + " , varCode= " + varCode + "]";
517 resp += line + "\n";
518 }
519
520 return resp;
521 }
522
523
524
529 public String traceImgToString() {
530 return traceImgToString(traceImages);
531 }
532
533
534
540 public String traceImgToString(ArrayList trace) {
541 String resp = "Images trace: \n";
542 String line;
543 for(int i = 0; i < trace.size(); i++) {
544 ArrayList slot = (ArrayList)trace.get(i);
545 Integer pos = (Integer)slot.get(0);
546 line = "[ pos =" + pos + " ]";
547 resp += line + "\n";
548 }
549 return resp;
550 }
551
552
553
560 public boolean wellFormed(ArrayList trace) {
561 if(trace.size() == 0) {
562 return true;
563 }
564
565 ArrayList slot = (ArrayList)trace.get(0);
566 int pre = ((Integer)slot.get(0)).intValue();
567 for(int i = 1; i < trace.size(); i++) {
568 slot = (ArrayList)trace.get(i);
569 int post = ((Integer)slot.get(0)).intValue();
570 if(post >= pre) {
571 return false;
572 }
573 pre = post;
574 }
575 return true;
576 }
577
578
579
586 public boolean wellFormed(ArrayList[] merge) {
587 if(merge.length == 0) {
588 return true;
589 }
590
591 ArrayList slot = (ArrayList)((ArrayList)merge[0]).get(1);
592 int pre = ((Integer)slot.get(0)).intValue();
593 for(int i = 1; i < merge.length; i++) {
594 slot = (ArrayList)((ArrayList)merge[i]).get(1);
595 int post = ((Integer)slot.get(0)).intValue();
596 if(post >= pre) {
597 return false;
598 }
599 pre = post;
600 }
601 return true;
602 }
603
604
605
611 public ArrayList[] getAllVariables() {
612 ArrayList[] res = new ArrayList[traceVariables.size()];
613
614 int i = 0;
615
616 int j;
617
618 j = traceVariables.size() - 1;
619 while(j >= 0) {
620 res[i++] = (ArrayList)traceVariables.get(j--);
621 }
622 return res;
623 }
624
625
626
632 public ArrayList[] getAllImages() {
633 ArrayList[] res = new ArrayList[traceImages.size()];
634
635 int i = 0;
636
637 int j;
638
639 j = traceImages.size() - 1;
640 while(j >= 0) {
641 res[i++] = (ArrayList)traceImages.get(j--);
642 }
643 return res;
644 }
645
646
647
662 public String extractRtf() {
663 StringBuffer res = new StringBuffer(rtfText);
664
665
666 int startFontTable = 0;
667 int endFontTable = 0;
668 String fontTable = "";
669
670 startFontTable = rtfText.indexOf("\\fonttbl");
671 endFontTable = rtfText.indexOf("}", startFontTable);
672
673 fontTable = rtfText.substring(startFontTable, endFontTable);
674 res.replace(startFontTable, endFontTable,
675 TemplateTransformer.actualFontTable(fontTable));
676
677 return res.toString();
678 }
679
680
681
686 public boolean hasVariables() {
687 return traceVariables != null && traceVariables.size() > 0;
688 }
689
690
691
697 public String toFoTemplate() {
698 String rtf = extractRtf();
699
700
701
702 try {
703 if(hasVariables()) {
704 rtf = TemplateTransformer.substituteVariableTags(rtf, getAllVariables());
705 }
706 }
707 catch(LetterTemplateEventException lex) {
708
709 }
710
711
712 return TemplateTransformer.rtftoFoTemplate(rtf);
713 }
714
715
716
725 public ArrayList[] setOfVariables() {
726 return TemplateTransformer.setOfVariables(traceVariables);
727 }
728 }
729
730