1 package org.webdocwf.util.loader;
2
3 /***
4 CounterColumns - insert auto increment values in target counter columns.
5
6 Copyright (C) 2002-2003 Together
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 CounterColumns.java
23 Date: 11.03.2003.
24 @version 1.1.0
25 @author: Milosevic Sinisa sinisa@prozone.co.yu
26 */
27
28 import java.util.Hashtable;
29 import java.util.Enumeration;
30 import java.util.Vector;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.NodeList;
33 import org.w3c.dom.Node;
34 import org.w3c.dom.Element;
35 import org.w3c.dom.NamedNodeMap;
36 import java.sql.Connection;
37 import java.math.BigDecimal;
38 import java.sql.Statement;
39 import java.sql.SQLException;
40 import java.sql.ResultSet;
41
42 /***
43 CounterColumns - insert auto increment values in target counter columns.
44 */
45
46 public class CounterColumns {
47
48 private String counterTableName=null;
49 private String counterNameColumn=null;
50 private String counterValueColumn=null;
51
52 private Hashtable counterName;
53 private Hashtable counterStartValue;
54 private Hashtable counterIncrement;
55 private Hashtable targetColumnName;
56 private Hashtable valueMode;
57 private Hashtable counterStartValueReset;
58 private Hashtable targetColumnTyp;
59
60 //subCounter columns
61 private Hashtable subCounterName;
62 private Hashtable subCounterStartValue;
63 private Hashtable subCounterIncrement;
64 private Hashtable subTargetColumnName;
65 private Hashtable subValueMode;
66 private Hashtable subCounterKeyColumns;
67 private Hashtable subCounterKeyValues;
68 private Hashtable subCounterKeyColumnsTyp;
69 private Hashtable subTargetColumnTyp;
70
71 private Vector vecCounterName=new Vector();
72 private Vector vecCounterIncrement=new Vector();
73 private Vector vecCounterStartValue=new Vector();
74 private Vector vecTargetColumnName=new Vector();
75 private Vector vecValueMode=new Vector();
76 private Vector vecCounterStartValueReset=new Vector();
77 private Vector vecTargetTableName=new Vector();
78 private Vector vecTargetTableID=new Vector();
79
80 private Vector vecSubCounterName=new Vector();
81 private Vector vecSubCounterIncrement=new Vector();
82 private Vector vecSubCounterStartValue=new Vector();
83 private Vector vecSubTargetTableName=new Vector();
84 private Vector vecSubTargetTableID=new Vector();
85 private Vector vecSubTargetColumnName=new Vector();
86 private Vector vecSubValueMode=new Vector();
87 private Vector vecSubKeyColumns=new Vector();
88 private Vector vecSubKeyColumnsTyp=new Vector();
89
90 private Hashtable currentCounterValue;
91
92 /***
93 *
94 */
95 public CounterColumns() {
96
97 this.counterTableName=null;
98 this.counterNameColumn=null;
99 this.counterValueColumn=null;
100 this.counterName=new Hashtable();
101 this.counterStartValue=new Hashtable();
102 this.counterIncrement=new Hashtable();
103 this.targetColumnName=new Hashtable();
104 this.targetColumnTyp=new Hashtable();
105 this.valueMode=new Hashtable();
106 this.counterStartValueReset=new Hashtable();
107
108 this.subCounterName=new Hashtable();
109 this.subCounterStartValue=new Hashtable();
110 this.subCounterIncrement=new Hashtable();
111 this.subTargetColumnName=new Hashtable();
112 this.subValueMode=new Hashtable();
113 this.subCounterKeyColumns=new Hashtable();
114 this.subCounterKeyValues=new Hashtable();
115 this.subCounterKeyColumnsTyp=new Hashtable();
116 this.subTargetColumnTyp=new Hashtable();
117
118 this.vecCounterName=new Vector();
119 this.vecCounterIncrement=new Vector();
120 this.vecCounterStartValue=new Vector();
121 this.vecTargetColumnName=new Vector();
122 this.vecValueMode=new Vector();
123 this.vecCounterStartValueReset=new Vector();
124 this.vecTargetTableName=new Vector();
125 this.vecTargetTableID=new Vector();
126 this.currentCounterValue=new Hashtable();
127
128 this.vecSubCounterName=new Vector();
129 this.vecSubCounterIncrement=new Vector();
130 this.vecSubCounterStartValue=new Vector();
131 this.vecSubTargetTableName=new Vector();
132 this.vecSubTargetTableID=new Vector();
133 this.vecSubTargetColumnName=new Vector();
134 this.vecSubValueMode=new Vector();
135 this.vecSubKeyColumns=new Vector();
136 this.vecSubKeyColumnsTyp=new Vector();
137 }
138
139 /***
140 *
141 * @return String name of counter table
142 */
143 public String getCounterTableName() {
144 return this.counterTableName;
145 }
146
147 /***
148 *
149 * @return String name of column in counter table with current values of each counter
150 */
151 public String getCounterValueColumn() {
152 return this.counterValueColumn;
153 }
154
155 /***
156 *
157 * @return String name of column in counter table with counter names.
158 */
159 public String getCounterNameColumn() {
160 return this.counterNameColumn;
161 }
162
163 /***
164 *
165 * @param nameColumn
166 */
167 public void setCounterNameColumn(String nameColumn) {
168 this.counterNameColumn=nameColumn;
169 }
170
171 /***
172 *
173 * @param valueColumn
174 */
175 public void setCounterValueColumn(String valueColumn) {
176 this.counterValueColumn=valueColumn;
177 }
178
179 /***
180 *
181 * @param tableName
182 */
183 public void setCounterTableName(String tableName) {
184 this.counterTableName=tableName;
185 }
186
187 /***
188 *
189 * @param doc
190 * @param importJob
191 */
192 public void readConstantColumnAttributes(Document doc, int importJob) {
193 this.counterTableName=importAttributeValue(doc,"counterColumns","counterTableName",importJob);
194 this.counterNameColumn=importAttributeValue(doc,"counterColumns","counterNameColumn",importJob);
195 this.counterValueColumn=importAttributeValue(doc,"counterColumns","counterValueColumn",importJob);
196 }
197
198 /***
199 *
200 * @param doc
201 * @param importJob
202 */
203 public void readConstantColumnsParameters(Document doc, int importJob) {
204 this.vecCounterName=importValue(doc,"counterColumn","counterName",importJob,null);
205 this.vecCounterIncrement=importValue(doc,"counterColumn","counterIncrement",importJob,"1");
206 this.vecCounterStartValue=importValue(doc,"counterColumn","counterStartValue",importJob,"0");
207 this.vecTargetColumnName=importValue(doc,"counterColumn","targetColumnName",importJob,null);
208 this.vecValueMode=importValue(doc,"counterColumn","valueMode",importJob,"setIfCreated");
209 this.vecCounterStartValueReset=importValue(doc,"counterColumn","counterStartValueReset",importJob,(new Boolean(false)).toString());
210 this.vecTargetTableName=importValue(doc,"counterColumn","targetTableName",importJob,null);
211 this.vecTargetTableID=importValue(doc,"counterColumn","targetTableID",importJob,null);
212
213 this.vecSubCounterName=importValue(doc,"subCounterColumn","counterName",importJob,null);
214 this.vecSubCounterIncrement=importValue(doc,"subCounterColumn","counterIncrement",importJob,"1");
215 this.vecSubCounterStartValue=importValue(doc,"subCounterColumn","counterStartValue",importJob,"0");
216 this.vecSubTargetTableName=importValue(doc,"subCounterColumn","targetTableName",importJob,null);
217 this.vecSubTargetTableID=importValue(doc,"subCounterColumn","targetTableID",importJob,null);
218 this.vecSubTargetColumnName=importValue(doc,"subCounterColumn","targetColumnName",importJob,null);
219 this.vecSubValueMode=importValue(doc,"subCounterColumn","valueMode",importJob,"setIfCreated");
220
221 for(int i=0; i<this.vecSubCounterName.size(); i++){
222 this.vecSubKeyColumns.addElement(importSubCounterKeyValue(doc,"keyColumnName",importJob,i));
223 }
224
225
226 }
227
228 /***
229 *
230 * @param tableName
231 * @param tableID
232 */
233 public void setConstantColumnsParameters(String tableName, String tableID) {
234
235
236 Vector targetCounterName=new Vector();
237 Vector targetCounterIncrement=new Vector();
238 Vector targetCounterStartValue=new Vector();
239 Vector targetTargetColumnName=new Vector();
240 Vector targetValueMode=new Vector();
241 Vector targetCounterStartValueReset=new Vector();
242
243 Vector targetSubCounterName=new Vector();
244 Vector targetSubCounterIncrement=new Vector();
245 Vector targetSubCounterStartValue=new Vector();
246 Vector targetSubTargetColumnName=new Vector();
247 Vector targetSubValueMode=new Vector();
248 Vector targetSubKeyColumns=new Vector();
249 Vector subKeyColumnsKey = new Vector();
250
251 for(int i=0; i<this.vecTargetTableName.size(); i++) {
252 if(tableName.equalsIgnoreCase(this.vecTargetTableName.get(i).toString())&&
253 tableID.equalsIgnoreCase(this.vecTargetTableID.get(i).toString())){
254 if(this.vecCounterName.get(i)!=null)
255 targetCounterName.addElement(this.vecCounterName.get(i).toString());
256 else
257 targetCounterName.addElement(null);
258
259 if(this.vecCounterStartValue.get(i)!=null)
260 targetCounterStartValue.addElement(this.vecCounterStartValue.get(i).toString());
261 else
262 targetCounterStartValue.addElement(null);
263
264 if(this.vecCounterIncrement.get(i)!=null)
265 targetCounterIncrement.addElement(this.vecCounterIncrement.get(i).toString());
266 else
267 targetCounterIncrement.addElement(null);
268
269 if(this.vecTargetColumnName.get(i)!=null)
270 targetTargetColumnName.addElement(this.vecTargetColumnName.get(i).toString());
271 else
272 targetTargetColumnName.addElement(null);
273
274 if(this.vecValueMode.get(i)!=null)
275 targetValueMode.addElement(this.vecValueMode.get(i).toString());
276 else
277 targetValueMode.addElement(null);
278
279 if(this.vecCounterStartValueReset.get(i)!=null)
280 targetCounterStartValueReset.addElement(this.vecCounterStartValueReset.get(i).toString());
281 else
282 targetCounterStartValueReset.addElement(null);
283
284 //sub counter columns
285 }
286 }
287 for(int i=0; i<this.vecSubTargetTableName.size(); i++) {
288 if(tableName.equalsIgnoreCase(this.vecSubTargetTableName.get(i).toString())&&
289 tableID.equalsIgnoreCase(this.vecSubTargetTableID.get(i).toString())){
290 if(this.vecSubCounterName.get(i)!=null) {
291 targetSubCounterName.addElement(this.vecSubCounterName.get(i).toString());
292 }
293 else
294 targetSubCounterName.addElement(null);
295 if(this.vecSubCounterStartValue.get(i)!=null)
296 targetSubCounterStartValue.addElement(this.vecSubCounterStartValue.get(i).toString());
297 else
298 targetSubCounterStartValue.addElement(null);
299 if(this.vecSubCounterIncrement.get(i)!=null)
300 targetSubCounterIncrement.addElement(this.vecSubCounterIncrement.get(i).toString());
301 else
302 targetSubCounterIncrement.addElement(null);
303 if(this.vecSubTargetColumnName.get(i)!=null)
304 targetSubTargetColumnName.addElement(this.vecSubTargetColumnName.get(i).toString());
305 else
306 targetSubTargetColumnName.addElement(null);
307 if(this.vecSubValueMode.get(i)!=null)
308 targetSubValueMode.addElement(this.vecSubValueMode.get(i).toString());
309 else
310 targetSubValueMode.addElement(null);
311 if(this.vecSubKeyColumns.get(i)!=null)
312 targetSubKeyColumns.addElement(this.vecSubKeyColumns.get(i));
313 else
314 targetSubKeyColumns.addElement(null);
315
316 }
317 }
318 this.counterName.put(tableName+"_"+tableID,targetCounterName);
319 this.counterIncrement.put(tableName+"_"+tableID,targetCounterIncrement);
320 this.counterStartValue.put(tableName+"_"+tableID,targetCounterStartValue);
321 this.targetColumnName.put(tableName+"_"+tableID,targetTargetColumnName);
322 this.valueMode.put(tableName+"_"+tableID,targetValueMode);
323 this.counterStartValueReset.put(tableName+"_"+tableID,targetCounterStartValueReset);
324
325 // subCounter columns
326 this.subCounterName.put(tableName+"_"+tableID,targetSubCounterName);
327 this.subCounterIncrement.put(tableName+"_"+tableID,targetSubCounterIncrement);
328 this.subCounterStartValue.put(tableName+"_"+tableID,targetSubCounterStartValue);
329 this.subTargetColumnName.put(tableName+"_"+tableID,targetSubTargetColumnName);
330 this.subValueMode.put(tableName+"_"+tableID,targetSubValueMode);
331 this.subCounterKeyColumns.put(tableName+"_"+tableID,targetSubKeyColumns);
332
333 }
334
335 /***
336 *
337 * @return
338 */
339 private int getVectorsSize(){
340 if(this.vecCounterName!=null)
341 return this.vecCounterName.size();
342 else
343 return 0;
344 }
345
346 /***
347 *
348 * @param tableName
349 * @param tableID
350 * @return
351 */
352 public Vector getCounterName(String tableName, String tableID) {
353 return (Vector)this.counterName.get(tableName+"_"+tableID);
354 }
355
356 /***
357 *
358 * @param tableName
359 * @param tableID
360 * @return Vector of increment values for all counter columns in table tableName.
361 */
362 public Vector getCounterIncrement(String tableName, String tableID) {
363 return (Vector)this.counterIncrement.get(tableName+"_"+tableID);
364 }
365
366 /***
367 *
368 * @param tableName
369 * @param tableID
370 * @return Vector of start values for all counter columns in table tableName.
371 */
372 public Vector getCounterStartValue(String tableName, String tableID) {
373 return (Vector)this.counterStartValue.get(tableName+"_"+tableID);
374 }
375
376 /***
377 *
378 * @param tableName
379 * @param tableID
380 * @return Vector of counter column names for all counter columns in table tableName.
381 */
382 public Vector getTargetColumnName(String tableName, String tableID) {
383 return (Vector)this.targetColumnName.get(tableName+"_"+tableID);
384
385 }
386
387
388 /***
389 *
390 * @param tableName
391 * @param tableID
392 * @return Vector of counter column types for all counter columns in table tableName.
393 */
394 public Vector getTargetColumnTyp(String tableName, String tableID) {
395 return (Vector)this.targetColumnTyp.get(tableName+"_"+tableID);
396
397 }
398
399 /***
400 *
401 * @param tableName
402 * @param tableID
403 * @return
404 */
405 public Vector getValueMode(String tableName, String tableID) {
406 return (Vector)this.valueMode.get(tableName+"_"+tableID);
407 }
408
409 /***
410 *
411 * @param tableName
412 * @param tableID
413 * @return
414 */
415 public Vector getCounterStartValueReset(String tableName, String tableID) {
416 return (Vector)this.counterStartValueReset.get(tableName+"_"+tableID);
417 }
418
419 //subCounter columns
420 /***
421 *
422 * @param tableName
423 * @param tableID
424 * @return
425 */
426 public Vector getSubCounterName(String tableName, String tableID) {
427 return (Vector)this.subCounterName.get(tableName+"_"+tableID);
428 }
429
430 /***
431 *
432 * @param tableName
433 * @param tableID
434 * @return
435 */
436 public Vector getSubCounterIncrement(String tableName, String tableID) {
437 return (Vector)this.subCounterIncrement.get(tableName+"_"+tableID);
438 }
439
440 /***
441 *
442 * @param tableName
443 * @param tableID
444 * @return
445 */
446 public Vector getSubCounterStartValue(String tableName, String tableID) {
447 return (Vector)this.subCounterStartValue.get(tableName+"_"+tableID);
448 }
449
450 /***
451 *
452 * @param tableName
453 * @param tableID
454 * @return
455 */
456 public Vector getSubTargetColumnName(String tableName, String tableID) {
457 return (Vector)this.subTargetColumnName.get(tableName+"_"+tableID);
458
459 }
460
461
462 /***
463 *
464 * @param tableName
465 * @param tableID
466 * @return
467 */
468 public Vector getSubTargetColumnTyp(String tableName, String tableID) {
469 return (Vector)this.subTargetColumnTyp.get(tableName+"_"+tableID);
470
471 }
472
473 /***
474 *
475 * @param tableName
476 * @param tableID
477 * @return
478 */
479 public Vector getSubValueMode(String tableName, String tableID) {
480 return (Vector)this.subValueMode.get(tableName+"_"+tableID);
481 }
482
483 /***
484 *
485 * @param tableName
486 * @param tableID
487 * @return
488 */
489 public Vector getSubCounterKeyColumns(String tableName, String tableID) {
490 return (Vector)this.subCounterKeyColumns.get(tableName+"_"+tableID);
491 }
492
493
494 /***
495 *
496 * @param tableName
497 * @param tableID
498 * @return
499 */
500 public Vector getSubCounterKeyColumnsTyp(String tableName, String tableID) {
501 return (Vector)this.subCounterKeyColumnsTyp.get(tableName+"_"+tableID);
502 }
503
504 /***
505 *
506 * @param tableName
507 * @param tableID
508 * @return
509 */
510 public Vector getSubCounterKeyValues(String tableName, String tableID) {
511 return (Vector)this.subCounterKeyValues.get(tableName+"_"+tableID);
512 }
513
514 /*
515 * Set values for subCounter key columns
516 * @param Hashtable new key values.
517 */
518 public void setSubCounterKeyValues(String tableName, String tableID, Hashtable values) {
519
520
521 Enumeration keys = values.keys();
522 Vector vecColumns=(Vector)this.subCounterKeyColumns.get(tableName+"_"+tableID);
523 Vector vecValues=new Vector(vecColumns.size());
524
525 String column;
526 for(int i=0; i<vecColumns.size(); i++) {
527 Vector subValues = new Vector();
528 for(int j=0; j<((Vector)vecColumns.get(i)).size(); j++) {
529 column = ((Vector)vecColumns.get(i)).get(j).toString();
530 subValues.add(values.get(column));
531 }
532 vecValues.add(subValues);
533 }
534 this.subCounterKeyValues.put(tableName+"_"+tableID,vecValues);
535 }
536
537 /***
538 *
539 * @param tableName
540 * @param tableID
541 */
542 public void setCounterValue(String tableName, String tableID) {
543 Vector targetCounterValue=new Vector();
544 Vector oldValues=getCounterValue(tableName,tableID);
545 Vector counterIncrement = getCounterIncrement(tableName,tableID);
546 for(int i=0;i<counterIncrement.size();i++) {
547 BigDecimal newValue = new BigDecimal(oldValues.get(i).toString());
548 newValue=newValue.add(new BigDecimal(counterIncrement.get(i).toString()));
549 targetCounterValue.addElement(newValue);
550 }
551 this.currentCounterValue.put(tableName+"_"+tableID,targetCounterValue);
552 }
553
554 /***
555 *
556 * @param tableName
557 * @param tableID
558 * @return Vector of current counter values for table - tableName
559 */
560 public Vector getCounterValue(String tableName, String tableID) {
561 return (Vector)this.currentCounterValue.get(tableName+"_"+tableID);
562 }
563
564 /***
565 *
566 * @param tableName
567 * @param tableID
568 * @param conn
569 * @param firstColumn
570 * @throws SQLException
571 */
572 public void setTargetColumnStartValues(String tableName, String tableID, Connection conn, int firstColumn)
573 throws SQLException{
574 Vector values=new Vector();
575 Vector valuesFromTable = readCounter(tableName,tableID,conn,firstColumn);
576 for(int i=0; i<this.getCounterName(tableName,tableID).size();i++) {
577 if(this.getCounterStartValueReset(tableName,tableID).get(i).toString().equalsIgnoreCase("true")) {
578 if(this.getCounterStartValue(tableName, tableID).get(i)!=null) {
579 BigDecimal newValue = new BigDecimal((this.getCounterStartValue(tableName, tableID)).get(i).toString());
580 values.addElement(newValue);
581 }
582 else
583 values.addElement(null);
584 }
585 else if(this.getCounterStartValueReset(tableName,tableID).get(i).toString().equalsIgnoreCase("false")) {
586 if(this.getCounterStartValue(tableName, tableID).get(i)!=null) {
587 BigDecimal newValue = new BigDecimal(valuesFromTable.get(i).toString());
588 values.addElement(newValue);
589 }
590 else
591 values.addElement(null);
592 }
593 }
594 this.currentCounterValue.put(tableName+"_"+tableID,values);
595 }
596
597
598 /***
599 * Method importValue reads values from desired XML tag and puts them into Vector.
600 * @param doc Parsed import XML file.
601 * @param tagName The name of XML tag.
602 * @param strAttrName The name of tag attribute which reads input strValue.
603 * @param iImportJobItem Number of ImportDefinition tag which is processed.
604 * @param defaultValue The default value of strattrname attribute.
605 * @return Vector of importing values.
606 */
607 private Vector importValue (Document doc, String tagName, String strAttrName,
608 int iImportJobItem, String defaultValue) {
609 Vector strValue = new Vector();
610 NodeList tagBasic = doc.getElementsByTagName("importDefinition");
611 if (tagBasic.getLength() != 0) {
612 Element docFragment = (Element)tagBasic.item(iImportJobItem);
613 NodeList tag = docFragment.getElementsByTagName(tagName);
614 for (int i = 0; i < tag.getLength(); i++) {
615 String nodeValue = "";
616 if (strAttrName != null) {
617 NamedNodeMap attrs = tag.item(i).getAttributes();
618 Node nodeResult = attrs.getNamedItem(strAttrName);
619 if (nodeResult != null)
620 nodeValue = nodeResult.getNodeValue();
621 else
622 nodeValue = defaultValue;
623 strValue.addElement(nodeValue);
624 }
625 else {
626 NodeList nodeText = tag.item(i).getChildNodes();
627 if (nodeText.item(0) != null) {
628 nodeValue = nodeText.item(0).getNodeValue();
629 strValue.addElement(nodeValue);
630 }
631 }
632 }
633 }
634 return strValue;
635 }
636
637
638 /***
639 * Method importSubCounterKeyValue reads values from strAttrName attribute
640 * XML tag and puts them into Vector.
641 * @param doc Parsed import XML file.
642 * @param strAttrName The name of tag attribute which reads input strValue.
643 * @param iImportJobItem Number of ImportDefinition tag which is processed.
644 * @param iSubCounterItem Number of subCounterColumn tag which is processed.
645 * @return Vector of importing values.
646 */
647 private Vector importSubCounterKeyValue (Document doc, String strAttrName,
648 int iImportJobItem, int iSubCounterItem) {
649 Vector strValue = null;
650 NodeList tagBasic = doc.getElementsByTagName("importDefinition");
651 if (tagBasic.getLength() != 0) {
652 Element docFragment = (Element)tagBasic.item(iImportJobItem);
653 NodeList tag = docFragment.getElementsByTagName("subCounterColumn");
654 if (tag.getLength() != 0) {
655 Element docCounterColumn = (Element)tag.item(iSubCounterItem);
656 NodeList tagSubCounterColumn = docCounterColumn.getElementsByTagName("subCounterKeyColumn");
657 strValue=new Vector();
658 for (int i = 0; i < tagSubCounterColumn.getLength(); i++) {
659 String nodeValue = "";
660 if (strAttrName != null) {
661 NamedNodeMap attrs = tagSubCounterColumn.item(i).getAttributes();
662 Node nodeResult = attrs.getNamedItem(strAttrName);
663 if (nodeResult != null) {
664 nodeValue = nodeResult.getNodeValue();
665 }
666 else
667 nodeValue = null;
668 strValue.addElement(nodeValue);
669
670 }
671 else {
672 NodeList nodeText = tag.item(i).getChildNodes();
673 if (nodeText.item(0) != null) {
674 nodeValue = nodeText.item(0).getNodeValue();
675 strValue.addElement(nodeValue);
676 }
677 }
678 }
679 }
680 }
681 return strValue;
682 }
683
684
685 /***
686 *
687 * @param tableName
688 * @param tableID
689 * @param conn
690 * @return true if update operation OK, false otherwise
691 * @throws SQLException
692 */
693 public boolean updateCounter(String tableName, String tableID, Connection conn) throws SQLException{
694
695 String strQuery =null;
696
697 Vector counterNames = this.getCounterName(tableName, tableID);
698 Vector counterValues = this.getCounterValue(tableName, tableID);
699 Vector counterIncrements = this.getCounterIncrement(tableName, tableID);
700 for(int i=0; i<counterNames.size(); i++) {
701 Statement stmt = conn.createStatement();
702 BigDecimal newValue=new BigDecimal(counterValues.get(i).toString());
703 // newValue=newValue.add(new BigDecimal((counterIncrements.get(i)).toString()));
704 strQuery=queryUpdateCounter(counterNames.get(i).toString(),newValue);
705 stmt.executeUpdate(strQuery);
706 stmt.close();
707 }
708 return true;
709 }
710
711 /***
712 *
713 * @param tableName
714 * @param tableID
715 * @param conn
716 * @return
717 * @throws SQLException
718 */
719 private boolean insertCounter(String tableName, String tableID, Connection conn) throws SQLException{
720
721 String strQuery =null;
722
723 Vector counterNames = this.getCounterName(tableName, tableID);
724 Vector startValues = this.getCounterIncrement(tableName,tableID);
725 for(int i=0; i<counterNames.size(); i++) {
726 Statement stmt = conn.createStatement();
727 BigDecimal newValue = new BigDecimal(startValues.get(i).toString());
728 strQuery=queryInsertCounter(counterNames.get(i).toString(),newValue);
729 stmt.execute(strQuery);
730 stmt.close();
731 }
732 return true;
733 }
734
735 /***
736 *
737 * @param tableName
738 * @param tableID
739 * @param conn
740 * @param firstColumn
741 * @return
742 * @throws SQLException
743 */
744 private Vector readCounter(String tableName, String tableID, Connection conn, int firstColumn) throws SQLException{
745
746 String strQuery =null;
747 BigDecimal value=new BigDecimal(0);
748 Vector startValues=new Vector();
749 Vector counterNames = this.getCounterName(tableName, tableID);
750 Vector counterValues = this.getCounterValue(tableName, tableID);
751 Vector counterIncrements = this.getCounterIncrement(tableName, tableID);
752 Vector defaultStartValues = this.getCounterIncrement(tableName,tableID);
753
754 for(int i=0; i<counterNames.size(); i++) {
755 Statement stmt = conn.createStatement();
756 strQuery=querySelectCounter(counterNames.get(i).toString());
757 ResultSet rset=stmt.executeQuery(strQuery);
758 if(rset.next()) {
759 if(firstColumn==0)
760 value=new BigDecimal(rset.getString(0));
761 else
762 value=new BigDecimal(rset.getString(1));
763 }
764 else {
765 insertCounter(tableName,tableID,conn);
766 value=new BigDecimal(defaultStartValues.get(i).toString());
767 }
768 stmt.close();
769 startValues.addElement(value);
770
771 }
772 return startValues;
773 }
774
775 /***
776 *
777 * @param counterColumnName
778 * @return
779 */
780 private String querySelectCounter(String counterColumnName) {
781 String query = "select ";
782
783 query+=this.counterValueColumn+" from "+this.counterTableName+" where "+
784 this.counterNameColumn+" = '"+counterColumnName+"'";
785 return query;
786 }
787
788 /***
789 *
790 * @param counterColumnName
791 * @param counterColumnValue
792 * @return
793 */
794 private String queryInsertCounter(String counterColumnName, BigDecimal counterColumnValue) {
795 String query = "INSERT into ";
796
797 query+=this.counterTableName+" ("+this.counterNameColumn+", "+ this.counterValueColumn+") VALUES('"+
798 counterColumnName+"', "+counterColumnValue.intValue()+")";
799 return query;
800 }
801
802 /***
803 *
804 * @param counterColumnName
805 * @param newValue
806 * @return
807 */
808
809 private String queryUpdateCounter(String counterColumnName, BigDecimal newValue) {
810 String query = "update ";
811 query+=this.counterTableName+" set "+
812 this.counterValueColumn+" = "+newValue.toString()+" where "+
813 this.counterNameColumn+" = '"+counterColumnName+"'";
814 return query;
815 }
816
817 /***
818 *
819 * @param tableName
820 * @param tableID
821 * @param conn
822 * @param firstColumn
823 * @return
824 * @throws SQLException
825 */
826 public Vector readSubCounterValue(String tableName, String tableID, Connection conn, int firstColumn ) throws SQLException{
827
828 BigDecimal value=new BigDecimal(0);
829 Vector startValues=new Vector();
830
831 Vector columns = this.getSubCounterKeyColumns(tableName, tableID);
832 Vector typs = this.getSubCounterKeyColumnsTyp(tableName, tableID);
833 Vector subColumnNames = this.getSubTargetColumnName(tableName, tableID);
834 Vector subIncrement=this.getSubCounterIncrement(tableName, tableID);
835 Vector subCounterStartValues = this.getSubCounterStartValue(tableName, tableID);
836 Vector subCounterKeyColumnValues = this.getSubCounterKeyValues(tableName, tableID);
837
838 for(int i=0; i<subColumnNames.size(); i++) {
839
840 String strQuery ="select ";
841 Statement stmt = conn.createStatement();
842 strQuery+=subColumnNames.get(i).toString()+" from "+tableName+" where ";
843 for(int j=0; j<((Vector)columns.get(i)).size(); j++) {
844 if(((Vector)subCounterKeyColumnValues.get(i)).get(j)!=null) {
845 if(!isNumber(((Vector)typs.get(i)).get(j).toString()))
846 strQuery+= ((Vector)columns.get(i)).get(j).toString()+" = '"+((Vector)subCounterKeyColumnValues.get(i)).get(j).toString()+"' and ";
847 else
848 strQuery+= ((Vector)columns.get(i)).get(j).toString()+" = "+((Vector)subCounterKeyColumnValues.get(i)).get(j).toString()+" and ";
849 }
850 else
851 strQuery+= ((Vector)columns.get(i)).get(j).toString()+" is null and ";
852 }
853 if(strQuery.endsWith(" and "))
854 strQuery=strQuery.substring(0,strQuery.length()-5);
855 ResultSet rset=stmt.executeQuery(strQuery);
856 BigDecimal currentValue = new BigDecimal(0);
857 if(rset.next()) {
858 if(firstColumn==0) {
859 value=new BigDecimal(rset.getString(0));
860 currentValue=value;
861 while(rset.next()) {
862 value=new BigDecimal(rset.getString(0));
863 if(currentValue.intValue() < value.intValue())
864 currentValue=value;
865 }
866 }
867 else {
868 value=new BigDecimal(rset.getString(1));
869 currentValue=value;
870 while(rset.next()) {
871 value=new BigDecimal(rset.getString(1));
872 if(currentValue.intValue() < value.intValue())
873 currentValue=value;
874 }
875 }
876 value=value.add(new BigDecimal(subIncrement.get(i).toString()));
877 }
878 else {
879 value=new BigDecimal(subCounterStartValues.get(i).toString());
880 }
881 stmt.close();
882 startValues.addElement(value);
883 }
884 return startValues;
885 }
886
887
888
889
890
891 /***
892 * Method importAttributeValue reads value for strAttrName attribute in strTagName tag.
893 * This method return this value.
894 * @param doc Parsed import XML file.
895 * @param strTagName The name of tag where attribute is situated.
896 * @param strAttrName The name of tag attribute which reads input value.
897 * @param iImportJobItem Number of ImportDefinition tag which is processed.
898 * @return String - importing value.
899 */
900 private String importAttributeValue (Document doc, String strTagName, String strAttrName,
901 int iImportJobItem) {
902 String strValue = "";
903 NodeList tagBasic = doc.getElementsByTagName("importDefinition");
904 if (tagBasic.getLength() != 0) {
905 Element docFragment = (Element)tagBasic.item(iImportJobItem);
906 // NodeList tag = docFragment.getElementsByTagName(tagName);
907 // for (int i = 0; i < tag.getLength(); i++) {
908
909 tagBasic = docFragment.getElementsByTagName(strTagName);
910 if(tagBasic.getLength()!=0) {
911 docFragment = (Element)tagBasic.item(0);
912 if (docFragment != null)
913 strValue = docFragment.getAttribute(strAttrName);
914 }
915 }
916 return strValue;
917 }
918
919
920 /***
921 * Method counterColumnTypes is used to put types of constant columns into
922 * global vector sorted in target tables.If there is an error, Exception
923 * "SQLException" or "NullPointerException" is thrown.
924 * @param c Connection to target database.
925 * @throws SQLException Constructs an SQLException object with a reason.
926 * @throws NullPointerException Constructs a NullPointerException with the specified detail message.
927 */
928 public void counterColumnTypes (String tableName, String tableID, Connection c, int firstColumn) throws SQLException, NullPointerException {
929 int iCnt = 0;
930 try {
931
932 Vector columnNames = this.getSubCounterKeyColumns(tableName,tableID);
933 Statement stmtConstant = c.createStatement();
934 Vector typs = new Vector();
935 Vector subTyps = new Vector();
936 String strQuery = "select ";
937
938 if(columnNames.size()!=0) {
939 for (int i = 0; i < columnNames.size(); i++) {
940 for(int j=0; j<((Vector)columnNames.get(i)).size(); j++) {
941 strQuery += ((Vector)columnNames.get(i)).get(j).toString() +
942 ", ";
943 }
944 strQuery = strQuery.substring(0, strQuery.length() - 2);
945 strQuery += " from " + tableName;
946
947 ResultSet rsetConstant = stmtConstant.executeQuery(strQuery);
948 for(int j=0; j<((Vector)columnNames.get(i)).size(); j++) {
949 if (firstColumn == 1)
950 typs.add(rsetConstant.getMetaData().getColumnTypeName(j + 1));
951 else
952 typs.add(rsetConstant.getMetaData().getColumnTypeName(j));
953 }
954 rsetConstant.close();
955 subTyps.addElement(typs);
956 }
957 }
958 this.subCounterKeyColumnsTyp.put(tableName+"_"+tableID,subTyps);
959 stmtConstant.close();
960
961
962
963
964 } catch (SQLException ex) {
965 throw ex;
966 } catch (NullPointerException ex) {
967 throw ex;
968 }
969 }
970
971 /***
972 * Method isNumber is used for checking column type.
973 * @param s String that represents column type.
974 * @return True if it is numeric and false if it is not.
975 */
976 private boolean isNumber (String s) {
977 boolean type = false;
978 if (s.equalsIgnoreCase("decimal") || s.equalsIgnoreCase("int") || s.equalsIgnoreCase("numeric")
979 || s.equalsIgnoreCase("real")|| s.equalsIgnoreCase("bigdecimal")|| s.equalsIgnoreCase("short")
980 || s.equalsIgnoreCase("long")|| s.equalsIgnoreCase("float")|| s.equalsIgnoreCase("double"))
981 type = true;
982 return type;
983 }
984
985
986 /***
987 *
988 */
989 public void reset() {
990 this.counterTableName=null;
991 this.counterNameColumn=null;
992 this.counterValueColumn=null;
993 this.counterName=new Hashtable();
994 this.counterStartValue=new Hashtable();
995 this.counterIncrement=new Hashtable();
996 this.targetColumnName=new Hashtable();
997 this.targetColumnTyp=new Hashtable();
998 this.valueMode=new Hashtable();
999 this.counterStartValueReset=new Hashtable();
1000
1001 this.subCounterName=new Hashtable();
1002 this.subCounterStartValue=new Hashtable();
1003 this.subCounterIncrement=new Hashtable();
1004 this.subTargetColumnName=new Hashtable();
1005 this.subValueMode=new Hashtable();
1006 this.subCounterKeyColumns=new Hashtable();
1007 this.subCounterKeyValues=new Hashtable();
1008 this.subCounterKeyColumnsTyp=new Hashtable();
1009 this.subTargetColumnTyp=new Hashtable();
1010
1011 this.vecCounterName=new Vector();
1012 this.vecCounterIncrement=new Vector();
1013 this.vecCounterStartValue=new Vector();
1014 this.vecTargetColumnName=new Vector();
1015 this.vecValueMode=new Vector();
1016 this.vecCounterStartValueReset=new Vector();
1017 this.vecTargetTableName=new Vector();
1018 this.vecTargetTableID=new Vector();
1019 this.currentCounterValue=new Hashtable();
1020
1021 this.vecSubCounterName=new Vector();
1022 this.vecSubCounterIncrement=new Vector();
1023 this.vecSubCounterStartValue=new Vector();
1024 this.vecSubTargetTableName=new Vector();
1025 this.vecSubTargetTableID=new Vector();
1026 this.vecSubTargetColumnName=new Vector();
1027 this.vecSubValueMode=new Vector();
1028 this.vecSubKeyColumns=new Vector();
1029 this.vecSubKeyColumnsTyp=new Vector();
1030 }
1031 }
1032
1033
This page automatically generated by Maven