0001: /**
0002: CounterColumns - insert auto increment values in target counter columns.
0003: Copyright (C) 2002-2003 Together
0004: This library is free software; you can redistribute it and/or
0005: modify it under the terms of the GNU Lesser General Public
0006: License as published by the Free Software Foundation; either
0007: version 2.1 of the License, or (at your option) any later version.
0008: This library is distributed in the hope that it will be useful,
0009: but WITHOUT ANY WARRANTY; without even the implied warranty of
0010: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0011: Lesser General Public License for more details.
0012: You should have received a copy of the GNU Lesser General Public
0013: License along with this library; if not, write to the Free Software
0014: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0015: CounterColumns.java
0016: Date: 11.03.2003.
0017: @version 1.1.0
0018: @author: Milosevic Sinisa sinisa@prozone.co.yu
0019: @author: Radoslav Dutina rale@prozone.co.yu
0020: */package org.webdocwf.util.loader;
0021:
0022: import java.util.Hashtable;
0023: import java.util.Enumeration;
0024: import java.util.Vector;
0025: import org.w3c.dom.Document;
0026: import org.w3c.dom.NodeList;
0027: import org.w3c.dom.Node;
0028: import org.w3c.dom.Element;
0029: import org.w3c.dom.NamedNodeMap;
0030: import java.sql.Connection;
0031: import java.math.BigDecimal;
0032: import java.sql.Statement;
0033: import java.sql.SQLException;
0034: import java.sql.ResultSet;
0035: import org.webdocwf.util.loader.logging.Logger;
0036:
0037: /**
0038: *
0039: * CounterColumns - insert auto increment values in target counter columns.
0040: */
0041: public class CounterColumns {
0042:
0043: private String counterTableName = null;
0044: private String counterNameColumn = null;
0045: private String counterValueColumn = null;
0046:
0047: private Hashtable counterName;
0048: private Hashtable counterStartValue;
0049: private Hashtable counterIncrement;
0050: private Hashtable targetColumnName;
0051: private Hashtable valueMode;
0052: private Hashtable counterStartValueReset;
0053: private Hashtable targetColumnTyp;
0054:
0055: //subCounter columns
0056: private Hashtable subCounterName;
0057: private Hashtable subCounterStartValue;
0058: private Hashtable subCounterIncrement;
0059: private Hashtable subTargetColumnName;
0060: private Hashtable subValueMode;
0061: private Hashtable subCounterKeyColumns;
0062: private Hashtable subCounterKeyValues;
0063: private Hashtable subCounterKeyColumnsTyp;
0064: private Hashtable subTargetColumnTyp;
0065:
0066: private Vector vecCounterName = new Vector();
0067: private Vector vecCounterIncrement = new Vector();
0068: private Vector vecCounterStartValue = new Vector();
0069: private Vector vecTargetColumnName = new Vector();
0070: private Vector vecValueMode = new Vector();
0071: private Vector vecCounterStartValueReset = new Vector();
0072: private Vector vecTargetTableName = new Vector();
0073: private Vector vecTargetTableID = new Vector();
0074:
0075: private Vector vecSubCounterName = new Vector();
0076: private Vector vecSubCounterIncrement = new Vector();
0077: private Vector vecSubCounterStartValue = new Vector();
0078: private Vector vecSubTargetTableName = new Vector();
0079: private Vector vecSubTargetTableID = new Vector();
0080: private Vector vecSubTargetColumnName = new Vector();
0081: private Vector vecSubValueMode = new Vector();
0082: private Vector vecSubKeyColumns = new Vector();
0083: private Vector vecSubKeyColumnsTyp = new Vector();
0084:
0085: private Hashtable currentCounterValue;
0086:
0087: //caching the subcounter value
0088: private Hashtable subCounterCache = new Hashtable();
0089: private Logger logger;
0090:
0091: /**
0092: * Empty constructor of CounterColumns class
0093: */
0094: public CounterColumns() {
0095:
0096: this .counterTableName = null;
0097: this .counterNameColumn = null;
0098: this .counterValueColumn = null;
0099: this .counterName = new Hashtable();
0100: this .counterStartValue = new Hashtable();
0101: this .counterIncrement = new Hashtable();
0102: this .targetColumnName = new Hashtable();
0103: this .targetColumnTyp = new Hashtable();
0104: this .valueMode = new Hashtable();
0105: this .counterStartValueReset = new Hashtable();
0106:
0107: this .subCounterName = new Hashtable();
0108: this .subCounterStartValue = new Hashtable();
0109: this .subCounterIncrement = new Hashtable();
0110: this .subTargetColumnName = new Hashtable();
0111: this .subValueMode = new Hashtable();
0112: this .subCounterKeyColumns = new Hashtable();
0113: this .subCounterKeyValues = new Hashtable();
0114: this .subCounterKeyColumnsTyp = new Hashtable();
0115: this .subTargetColumnTyp = new Hashtable();
0116:
0117: this .vecCounterName = new Vector();
0118: this .vecCounterIncrement = new Vector();
0119: this .vecCounterStartValue = new Vector();
0120: this .vecTargetColumnName = new Vector();
0121: this .vecValueMode = new Vector();
0122: this .vecCounterStartValueReset = new Vector();
0123: this .vecTargetTableName = new Vector();
0124: this .vecTargetTableID = new Vector();
0125: this .currentCounterValue = new Hashtable();
0126:
0127: this .vecSubCounterName = new Vector();
0128: this .vecSubCounterIncrement = new Vector();
0129: this .vecSubCounterStartValue = new Vector();
0130: this .vecSubTargetTableName = new Vector();
0131: this .vecSubTargetTableID = new Vector();
0132: this .vecSubTargetColumnName = new Vector();
0133: this .vecSubValueMode = new Vector();
0134: this .vecSubKeyColumns = new Vector();
0135: this .vecSubKeyColumnsTyp = new Vector();
0136: }
0137:
0138: /**
0139: * This method set logger object
0140: */
0141: public void setLogger(Logger logger) {
0142: this .logger = logger;
0143: }
0144:
0145: /**
0146: * This method read value of counterTableName parameter
0147: * @return string name of counter table
0148: */
0149: public String getCounterTableName() {
0150: return this .counterTableName;
0151: }
0152:
0153: /**
0154: * This method read value of counterValueColumn parameter
0155: * @return String name of column in counter table with current values of each counter
0156: */
0157: public String getCounterValueColumn() {
0158: return this .counterValueColumn;
0159: }
0160:
0161: /**
0162: * This method read value of counterNameColumn parameter
0163: * @return String name of column in counter table with counter names.
0164: */
0165: public String getCounterNameColumn() {
0166: return this .counterNameColumn;
0167: }
0168:
0169: /**
0170: * This method sets the value of counterNameColumn parameter
0171: * @param nameColumn is name of the column
0172: */
0173: public void setCounterNameColumn(String nameColumn) {
0174: this .counterNameColumn = nameColumn;
0175: }
0176:
0177: /**
0178: * This method sets the value of counterValueColumn parameter
0179: * @param valueColumn is value of the column
0180: */
0181: public void setCounterValueColumn(String valueColumn) {
0182: this .counterValueColumn = valueColumn;
0183: }
0184:
0185: /**
0186: * This method sets the value of counterTableName parameter
0187: * @param tableName is table name
0188: */
0189: public void setCounterTableName(String tableName) {
0190: this .counterTableName = tableName;
0191: }
0192:
0193: /**
0194: * This method sets the value of:
0195: * counterTableName, counterNameColumn and counterValueColumn parameters
0196: * @param doc represents Object document
0197: * @param importJob represents current import job
0198: */
0199: public void readConstantColumnAttributes(Document doc, int importJob) {
0200: this .counterTableName = importAttributeValue(doc,
0201: "counterColumns", "counterTableName", importJob);
0202: this .counterNameColumn = importAttributeValue(doc,
0203: "counterColumns", "counterNameColumn", importJob);
0204: this .counterValueColumn = importAttributeValue(doc,
0205: "counterColumns", "counterValueColumn", importJob);
0206: }
0207:
0208: /**
0209: * This method sets the value of next parameters:
0210: * vecCounterName, vecCounterIncrement, vecCounterStartValue, vecTargetColumnName,
0211: * vecValueMode, vecCounterStartValueReset, vecTargetTableName, vecTargetTableID,
0212: * vecSubCounterName, vecSubCounterIncrement, vecSubCounterStartValue,
0213: * vecSubTargetTableName, vecSubTargetTableID, vecSubTargetColumnName,
0214: * vecSubValueMode and vecSubKeyColumns
0215: * @param doc represents Object document
0216: * @param importJob represents current import job
0217: */
0218: public void readConstantColumnsParameters(Document doc,
0219: int importJob) {
0220: this .vecCounterName = importValue(doc, "counterColumn",
0221: "counterName", importJob, null);
0222: this .vecCounterIncrement = importValue(doc, "counterColumn",
0223: "counterIncrement", importJob, "1");
0224: this .vecCounterStartValue = importValue(doc, "counterColumn",
0225: "counterStartValue", importJob, "0");
0226: this .vecTargetColumnName = importValue(doc, "counterColumn",
0227: "targetColumnName", importJob, null);
0228: this .vecValueMode = importValue(doc, "counterColumn",
0229: "valueMode", importJob, "setIfCreated");
0230: this .vecCounterStartValueReset = importValue(doc,
0231: "counterColumn", "counterStartValueReset", importJob,
0232: (new Boolean(false)).toString());
0233: this .vecTargetTableName = importValue(doc, "counterColumn",
0234: "targetTableName", importJob, null);
0235: this .vecTargetTableID = importValue(doc, "counterColumn",
0236: "targetTableID", importJob, null);
0237:
0238: this .vecSubCounterName = importValue(doc, "subCounterColumn",
0239: "counterName", importJob, null);
0240: this .vecSubCounterIncrement = importValue(doc,
0241: "subCounterColumn", "counterIncrement", importJob, "1");
0242: this .vecSubCounterStartValue = importValue(doc,
0243: "subCounterColumn", "counterStartValue", importJob, "0");
0244: this .vecSubTargetTableName = importValue(doc,
0245: "subCounterColumn", "targetTableName", importJob, null);
0246: this .vecSubTargetTableID = importValue(doc, "subCounterColumn",
0247: "targetTableID", importJob, null);
0248: this .vecSubTargetColumnName = importValue(doc,
0249: "subCounterColumn", "targetColumnName", importJob, null);
0250: this .vecSubValueMode = importValue(doc, "subCounterColumn",
0251: "valueMode", importJob, "setIfCreated");
0252:
0253: for (int i = 0; i < this .vecSubCounterName.size(); i++) {
0254: this .vecSubKeyColumns.addElement(importSubCounterKeyValue(
0255: doc, "keyColumnName", importJob, i));
0256: }
0257:
0258: }
0259:
0260: /**
0261: * This method sets the value of Constant Columns parameters
0262: * @param tableName is table name
0263: * @param tableID is table ID
0264: */
0265: public void setConstantColumnsParameters(String tableName,
0266: String tableID) {
0267:
0268: Vector targetCounterName = new Vector();
0269: Vector targetCounterIncrement = new Vector();
0270: Vector targetCounterStartValue = new Vector();
0271: Vector targetTargetColumnName = new Vector();
0272: Vector targetValueMode = new Vector();
0273: Vector targetCounterStartValueReset = new Vector();
0274:
0275: Vector targetSubCounterName = new Vector();
0276: Vector targetSubCounterIncrement = new Vector();
0277: Vector targetSubCounterStartValue = new Vector();
0278: Vector targetSubTargetColumnName = new Vector();
0279: Vector targetSubValueMode = new Vector();
0280: Vector targetSubKeyColumns = new Vector();
0281: Vector subKeyColumnsKey = new Vector();
0282:
0283: for (int i = 0; i < this .vecTargetTableName.size(); i++) {
0284: if (tableName.equalsIgnoreCase(this .vecTargetTableName.get(
0285: i).toString())
0286: && tableID.equalsIgnoreCase(this .vecTargetTableID
0287: .get(i).toString())) {
0288: if (this .vecCounterName.get(i) != null)
0289: targetCounterName.addElement(this .vecCounterName
0290: .get(i).toString());
0291: else
0292: targetCounterName.addElement(null);
0293:
0294: if (this .vecCounterStartValue.get(i) != null)
0295: targetCounterStartValue
0296: .addElement(this .vecCounterStartValue
0297: .get(i).toString());
0298: else
0299: targetCounterStartValue.addElement(null);
0300:
0301: if (this .vecCounterIncrement.get(i) != null)
0302: targetCounterIncrement
0303: .addElement(this .vecCounterIncrement.get(i)
0304: .toString());
0305: else
0306: targetCounterIncrement.addElement(null);
0307:
0308: if (this .vecTargetColumnName.get(i) != null)
0309: targetTargetColumnName
0310: .addElement(this .vecTargetColumnName.get(i)
0311: .toString());
0312: else
0313: targetTargetColumnName.addElement(null);
0314:
0315: if (this .vecValueMode.get(i) != null)
0316: targetValueMode.addElement(this .vecValueMode.get(i)
0317: .toString());
0318: else
0319: targetValueMode.addElement(null);
0320:
0321: if (this .vecCounterStartValueReset.get(i) != null)
0322: targetCounterStartValueReset
0323: .addElement(this .vecCounterStartValueReset
0324: .get(i).toString());
0325: else
0326: targetCounterStartValueReset.addElement(null);
0327:
0328: //sub counter columns
0329: }
0330: }
0331: for (int i = 0; i < this .vecSubTargetTableName.size(); i++) {
0332: if (tableName.equalsIgnoreCase(this .vecSubTargetTableName
0333: .get(i).toString())
0334: && tableID
0335: .equalsIgnoreCase(this .vecSubTargetTableID
0336: .get(i).toString())) {
0337: if (this .vecSubCounterName.get(i) != null) {
0338: targetSubCounterName
0339: .addElement(this .vecSubCounterName.get(i)
0340: .toString());
0341: } else
0342: targetSubCounterName.addElement(null);
0343: if (this .vecSubCounterStartValue.get(i) != null)
0344: targetSubCounterStartValue
0345: .addElement(this .vecSubCounterStartValue
0346: .get(i).toString());
0347: else
0348: targetSubCounterStartValue.addElement(null);
0349: if (this .vecSubCounterIncrement.get(i) != null)
0350: targetSubCounterIncrement
0351: .addElement(this .vecSubCounterIncrement
0352: .get(i).toString());
0353: else
0354: targetSubCounterIncrement.addElement(null);
0355: if (this .vecSubTargetColumnName.get(i) != null)
0356: targetSubTargetColumnName
0357: .addElement(this .vecSubTargetColumnName
0358: .get(i).toString());
0359: else
0360: targetSubTargetColumnName.addElement(null);
0361: if (this .vecSubValueMode.get(i) != null)
0362: targetSubValueMode.addElement(this .vecSubValueMode
0363: .get(i).toString());
0364: else
0365: targetSubValueMode.addElement(null);
0366: if (this .vecSubKeyColumns.get(i) != null)
0367: targetSubKeyColumns
0368: .addElement(this .vecSubKeyColumns.get(i));
0369: else
0370: targetSubKeyColumns.addElement(null);
0371:
0372: }
0373: }
0374: this .counterName.put(tableName + "_" + tableID,
0375: targetCounterName);
0376: this .counterIncrement.put(tableName + "_" + tableID,
0377: targetCounterIncrement);
0378: this .counterStartValue.put(tableName + "_" + tableID,
0379: targetCounterStartValue);
0380: this .targetColumnName.put(tableName + "_" + tableID,
0381: targetTargetColumnName);
0382: this .valueMode.put(tableName + "_" + tableID, targetValueMode);
0383: this .counterStartValueReset.put(tableName + "_" + tableID,
0384: targetCounterStartValueReset);
0385:
0386: // subCounter columns
0387: this .subCounterName.put(tableName + "_" + tableID,
0388: targetSubCounterName);
0389: this .subCounterIncrement.put(tableName + "_" + tableID,
0390: targetSubCounterIncrement);
0391: this .subCounterStartValue.put(tableName + "_" + tableID,
0392: targetSubCounterStartValue);
0393: this .subTargetColumnName.put(tableName + "_" + tableID,
0394: targetSubTargetColumnName);
0395: this .subValueMode.put(tableName + "_" + tableID,
0396: targetSubValueMode);
0397: this .subCounterKeyColumns.put(tableName + "_" + tableID,
0398: targetSubKeyColumns);
0399:
0400: }
0401:
0402: private int getVectorsSize() {
0403: if (this .vecCounterName != null)
0404: return this .vecCounterName.size();
0405: else
0406: return 0;
0407: }
0408:
0409: /**
0410: * This method read value of counterNames parameter
0411: * @param tableName is table name
0412: * @param tableID is table ID
0413: * @return vector
0414: */
0415: public Vector getCounterName(String tableName, String tableID) {
0416: return (Vector) this .counterName.get(tableName + "_" + tableID);
0417: }
0418:
0419: /**
0420: * This method read value of counterIncrement parameter
0421: * @param tableName is table name
0422: * @param tableID is table ID
0423: * @return Vector of increment values for all counter columns in table tableName.
0424: */
0425: public Vector getCounterIncrement(String tableName, String tableID) {
0426: return (Vector) this .counterIncrement.get(tableName + "_"
0427: + tableID);
0428: }
0429:
0430: /**
0431: * This method read value of counterStartValue parameter
0432: * @param tableName is table name
0433: * @param tableID is table ID
0434: * @return Vector of start values for all counter columns in table tableName.
0435: */
0436: public Vector getCounterStartValue(String tableName, String tableID) {
0437: return (Vector) this .counterStartValue.get(tableName + "_"
0438: + tableID);
0439: }
0440:
0441: /**
0442: * This method read value of targetColumnName parameter
0443: * @param tableName is table name
0444: * @param tableID is table ID
0445: * @return Vector of counter column names for all counter columns in table tableName.
0446: */
0447: public Vector getTargetColumnName(String tableName, String tableID) {
0448: return (Vector) this .targetColumnName.get(tableName + "_"
0449: + tableID);
0450:
0451: }
0452:
0453: /**
0454: * This method read value of targetColumnTyp parameter
0455: * @param tableName is table name
0456: * @param tableID is table ID
0457: * @return Vector of counter column types for all counter columns in table tableName.
0458: */
0459: public Vector getTargetColumnTyp(String tableName, String tableID) {
0460: return (Vector) this .targetColumnTyp.get(tableName + "_"
0461: + tableID);
0462:
0463: }
0464:
0465: /**
0466: * This method read value of valueMode parameter
0467: * @param tableName is table name
0468: * @param tableID is table ID
0469: * @return vector
0470: */
0471: public Vector getValueMode(String tableName, String tableID) {
0472: return (Vector) this .valueMode.get(tableName + "_" + tableID);
0473: }
0474:
0475: /**
0476: * This method read value of counterStartValueReset parameter
0477: * @param tableName is table name
0478: * @param tableID is table ID
0479: * @return vector
0480: */
0481: public Vector getCounterStartValueReset(String tableName,
0482: String tableID) {
0483: return (Vector) this .counterStartValueReset.get(tableName + "_"
0484: + tableID);
0485: }
0486:
0487: //subCounter columns
0488: /**
0489: * This method read value of subCounterName parameter
0490: * @param tableName is table name
0491: * @param tableID is table ID
0492: * @return vector
0493: */
0494: public Vector getSubCounterName(String tableName, String tableID) {
0495: return (Vector) this .subCounterName.get(tableName + "_"
0496: + tableID);
0497: }
0498:
0499: /**
0500: * This method read value of getSubCounterIncrement parameter
0501: * @param tableName is table name
0502: * @param tableID is table ID
0503: * @return vector
0504: */
0505: public Vector getSubCounterIncrement(String tableName,
0506: String tableID) {
0507: return (Vector) this .subCounterIncrement.get(tableName + "_"
0508: + tableID);
0509: }
0510:
0511: /**
0512: * This method read value of getSubCounterStartValue parameter
0513: * @param tableName is table name
0514: * @param tableID is table ID
0515: * @return vector
0516: */
0517: public Vector getSubCounterStartValue(String tableName,
0518: String tableID) {
0519: return (Vector) this .subCounterStartValue.get(tableName + "_"
0520: + tableID);
0521: }
0522:
0523: /**
0524: * This method read value of subTargetColumnName parameter
0525: * @param tableName is table name
0526: * @param tableID is table ID
0527: * @return vector
0528: */
0529: public Vector getSubTargetColumnName(String tableName,
0530: String tableID) {
0531: return (Vector) this .subTargetColumnName.get(tableName + "_"
0532: + tableID);
0533:
0534: }
0535:
0536: /**
0537: * This method read value of subTargetColumnTyp parameter
0538: * @param tableName is table name
0539: * @param tableID is table ID
0540: * @return vector
0541: */
0542: public Vector getSubTargetColumnTyp(String tableName, String tableID) {
0543: return (Vector) this .subTargetColumnTyp.get(tableName + "_"
0544: + tableID);
0545:
0546: }
0547:
0548: /**
0549: * This method read value of subValueMode parameter
0550: * @param tableName is table name
0551: * @param tableID is table ID
0552: * @return vector
0553: */
0554: public Vector getSubValueMode(String tableName, String tableID) {
0555: return (Vector) this .subValueMode
0556: .get(tableName + "_" + tableID);
0557: }
0558:
0559: /**
0560: * This method read value of subCounterKeyColumns parameter
0561: * @param tableName is table name
0562: * @param tableID is table ID
0563: * @return vector
0564: */
0565: public Vector getSubCounterKeyColumns(String tableName,
0566: String tableID) {
0567: return (Vector) this .subCounterKeyColumns.get(tableName + "_"
0568: + tableID);
0569: }
0570:
0571: /**
0572: * This method read value of subCounterKeyColumnsTyp parameter
0573: * @param tableName is table name
0574: * @param tableID is table ID
0575: * @return vector
0576: */
0577: public Vector getSubCounterKeyColumnsTyp(String tableName,
0578: String tableID) {
0579: return (Vector) this .subCounterKeyColumnsTyp.get(tableName
0580: + "_" + tableID);
0581: }
0582:
0583: /**
0584: * This method read value of subCounterKeyValues parameter
0585: * @param tableName is table name
0586: * @param tableID is table ID
0587: * @return vector
0588: */
0589: public Vector getSubCounterKeyValues(String tableName,
0590: String tableID) {
0591: return (Vector) this .subCounterKeyValues.get(tableName + "_"
0592: + tableID);
0593: }
0594:
0595: /**
0596: * This method set value of subCounterKeyValues parameter
0597: * @param tableName defines table name
0598: * @param tableID defines table ID
0599: * @param values is hashtable new key values
0600: */
0601: public void setSubCounterKeyValues(String tableName,
0602: String tableID, Hashtable values) {
0603:
0604: Enumeration keys = values.keys();
0605: Vector vecColumns = (Vector) this .subCounterKeyColumns
0606: .get(tableName + "_" + tableID);
0607: Vector vecValues = new Vector(vecColumns.size());
0608:
0609: String column;
0610: for (int i = 0; i < vecColumns.size(); i++) {
0611: Vector subValues = new Vector();
0612: for (int j = 0; j < ((Vector) vecColumns.get(i)).size(); j++) {
0613: column = ((Vector) vecColumns.get(i)).get(j).toString();
0614: subValues.add(values.get(column));
0615: }
0616: vecValues.add(subValues);
0617: }
0618: this .subCounterKeyValues.put(tableName + "_" + tableID,
0619: vecValues);
0620: }
0621:
0622: /**
0623: * This method set values for Counter columns
0624: * @param tableName is table name
0625: * @param tableID is table ID
0626: */
0627: public void setCounterValue(String tableName, String tableID) {
0628: Vector targetCounterValue = new Vector();
0629: Vector oldValues = getCounterValue(tableName, tableID);
0630: Vector counterIncrement = getCounterIncrement(tableName,
0631: tableID);
0632: for (int i = 0; i < counterIncrement.size(); i++) {
0633: BigDecimal newValue = new BigDecimal(oldValues.get(i)
0634: .toString());
0635: newValue = newValue.add(new BigDecimal(counterIncrement
0636: .get(i).toString()));
0637: targetCounterValue.addElement(newValue);
0638: }
0639: this .currentCounterValue.put(tableName + "_" + tableID,
0640: targetCounterValue);
0641: }
0642:
0643: /**
0644: * This method set values for currentCounterValue parameter
0645: * @param tableName is table name
0646: * @param tableID is table ID
0647: * @return Vector of current counter values for table - tableName
0648: */
0649: public Vector getCounterValue(String tableName, String tableID) {
0650: return (Vector) this .currentCounterValue.get(tableName + "_"
0651: + tableID);
0652: }
0653:
0654: /**
0655: * This method set values for currentCounterValue parameter
0656: * @param tableName is table name
0657: * @param tableID is table ID
0658: * @param conn is connection to target database
0659: * @param firstColumn is first column
0660: * @throws SQLException
0661: */
0662: public void setTargetColumnStartValues(String tableName,
0663: String tableID, Connection conn, int firstColumn)
0664: throws SQLException {
0665: Vector values = new Vector();
0666: //TODO ZORAN
0667: Vector valuesFromTable = readCounter(tableName, tableID, conn,
0668: firstColumn);
0669: for (int i = 0; i < this .getCounterName(tableName, tableID)
0670: .size(); i++) {
0671: if (this .getCounterStartValueReset(tableName, tableID).get(
0672: i).toString().equalsIgnoreCase("true")) {
0673: if (this .getCounterStartValue(tableName, tableID)
0674: .get(i) != null) {
0675: BigDecimal newValue = new BigDecimal((this
0676: .getCounterStartValue(tableName, tableID))
0677: .get(i).toString());
0678: values.addElement(newValue);
0679: } else
0680: values.addElement(null);
0681: } else if (this .getCounterStartValueReset(tableName,
0682: tableID).get(i).toString()
0683: .equalsIgnoreCase("false")) {
0684: if (this .getCounterStartValue(tableName, tableID)
0685: .get(i) != null) {
0686: BigDecimal newValue = new BigDecimal(
0687: valuesFromTable.get(i).toString());
0688: values.addElement(newValue);
0689: } else
0690: values.addElement(null);
0691: }
0692: }
0693: this .currentCounterValue.put(tableName + "_" + tableID, values);
0694: }
0695:
0696: /**
0697: * Method importValue reads values from desired XML tag and puts them into Vector.
0698: * @param doc Parsed import XML file.
0699: * @param tagName The name of XML tag.
0700: * @param strAttrName The name of tag attribute which reads input strValue.
0701: * @param iImportJobItem Number of ImportDefinition tag which is processed.
0702: * @param defaultValue The default value of strattrname attribute.
0703: * @return Vector of importing values.
0704: */
0705: private Vector importValue(Document doc, String tagName,
0706: String strAttrName, int iImportJobItem, String defaultValue) {
0707: Vector strValue = new Vector();
0708: NodeList tagBasic = doc
0709: .getElementsByTagName("importDefinition");
0710: if (tagBasic.getLength() != 0) {
0711: Element docFragment = (Element) tagBasic
0712: .item(iImportJobItem);
0713: NodeList tag = docFragment.getElementsByTagName(tagName);
0714: for (int i = 0; i < tag.getLength(); i++) {
0715: String nodeValue = "";
0716: if (strAttrName != null) {
0717: NamedNodeMap attrs = tag.item(i).getAttributes();
0718: Node nodeResult = attrs.getNamedItem(strAttrName);
0719: if (nodeResult != null)
0720: nodeValue = nodeResult.getNodeValue();
0721: else
0722: nodeValue = defaultValue;
0723: strValue.addElement(nodeValue);
0724: } else {
0725: NodeList nodeText = tag.item(i).getChildNodes();
0726: if (nodeText.item(0) != null) {
0727: nodeValue = nodeText.item(0).getNodeValue();
0728: strValue.addElement(nodeValue);
0729: }
0730: }
0731: }
0732: }
0733: return strValue;
0734: }
0735:
0736: /**
0737: * Method importSubCounterKeyValue reads values from strAttrName attribute
0738: * XML tag and puts them into Vector.
0739: * @param doc Parsed import XML file.
0740: * @param strAttrName The name of tag attribute which reads input strValue.
0741: * @param iImportJobItem Number of ImportDefinition tag which is processed.
0742: * @param iSubCounterItem Number of subCounterColumn tag which is processed.
0743: * @return Vector of importing values.
0744: */
0745: private Vector importSubCounterKeyValue(Document doc,
0746: String strAttrName, int iImportJobItem, int iSubCounterItem) {
0747: Vector strValue = null;
0748: NodeList tagBasic = doc
0749: .getElementsByTagName("importDefinition");
0750: if (tagBasic.getLength() != 0) {
0751: Element docFragment = (Element) tagBasic
0752: .item(iImportJobItem);
0753: NodeList tag = docFragment
0754: .getElementsByTagName("subCounterColumn");
0755: if (tag.getLength() != 0) {
0756: Element docCounterColumn = (Element) tag
0757: .item(iSubCounterItem);
0758: NodeList tagSubCounterColumn = docCounterColumn
0759: .getElementsByTagName("subCounterKeyColumn");
0760: strValue = new Vector();
0761: for (int i = 0; i < tagSubCounterColumn.getLength(); i++) {
0762: String nodeValue = "";
0763: if (strAttrName != null) {
0764: NamedNodeMap attrs = tagSubCounterColumn
0765: .item(i).getAttributes();
0766: Node nodeResult = attrs
0767: .getNamedItem(strAttrName);
0768: if (nodeResult != null) {
0769: nodeValue = nodeResult.getNodeValue();
0770: } else
0771: nodeValue = null;
0772: strValue.addElement(nodeValue);
0773:
0774: } else {
0775: NodeList nodeText = tag.item(i).getChildNodes();
0776: if (nodeText.item(0) != null) {
0777: nodeValue = nodeText.item(0).getNodeValue();
0778: strValue.addElement(nodeValue);
0779: }
0780: }
0781: }
0782: }
0783: }
0784: return strValue;
0785: }
0786:
0787: // /**
0788: // * This method check the update operation
0789: // * @param tableName is table name
0790: // * @param tableID is table ID
0791: // * @param conn is connection
0792: // * @return true if update operation OK, false otherwise
0793: // * @throws SQLException
0794: // */
0795: // public boolean updateCounter(String tableName, String tableID,
0796: // Connection conn) throws SQLException {
0797: //
0798: // String strQuery = null;
0799: //
0800: // Vector counterNames = this.getCounterName(tableName, tableID);
0801: // Vector counterValues = this.getCounterValue(tableName, tableID);
0802: // Vector counterIncrements = this.getCounterIncrement(tableName, tableID);
0803: // for (int i = 0; i < counterNames.size(); i++) {
0804: // Statement stmt = conn.createStatement();
0805: // BigDecimal newValue = new BigDecimal(counterValues.get(i).toString());
0806: // strQuery = queryUpdateCounter(counterNames.get(i).toString(), newValue);
0807: // stmt.executeUpdate(strQuery);
0808: // stmt.close();
0809: // }
0810: // return true;
0811: // }
0812:
0813: /**
0814: * This method check the update operation
0815: * @param conn is connection
0816: * @throws SQLException
0817: */
0818: public void updateCounter(Connection conn) throws SQLException {
0819: String strQuery = "";
0820: for (int i = 0; i < this .vecTargetTableName.size(); i++) {
0821: String tableName = this .vecTargetTableName.get(i)
0822: .toString();
0823: String tableID = this .vecTargetTableID.get(i).toString();
0824: Vector counterNames = this .getCounterName(tableName,
0825: tableID);
0826: Vector counterValues = this .getCounterValue(tableName,
0827: tableID);
0828: Vector counterIncrements = this .getCounterIncrement(
0829: tableName, tableID);
0830: for (int j = 0; j < counterNames.size(); j++) {
0831: Statement stmt = conn.createStatement();
0832: BigDecimal newValue = new BigDecimal(counterValues.get(
0833: i).toString());
0834: strQuery = queryUpdateCounter(counterNames.get(i)
0835: .toString(), newValue);
0836: stmt.executeUpdate(strQuery);
0837: stmt.close();
0838: }
0839: }
0840: }
0841:
0842: /**
0843: * This method sets the value of counter parameter
0844: * @param tableName is table name
0845: * @param tableID is table ID
0846: * @param conn is connection
0847: * @return true
0848: * @throws SQLException
0849: */
0850: private boolean insertCounter(String tableName, String tableID,
0851: Connection conn) throws SQLException {
0852:
0853: String strQuery = null;
0854:
0855: Vector counterNames = this .getCounterName(tableName, tableID);
0856: Vector startValues = this .getCounterIncrement(tableName,
0857: tableID);
0858: for (int i = 0; i < counterNames.size(); i++) {
0859: Statement stmt = conn.createStatement();
0860: BigDecimal newValue = new BigDecimal(startValues.get(i)
0861: .toString());
0862: strQuery = queryInsertCounter(counterNames.get(i)
0863: .toString(), newValue);
0864: stmt.execute(strQuery);
0865: stmt.close();
0866: }
0867: return true;
0868: }
0869:
0870: /**
0871: * This method read value of counter parameter
0872: * @param tableName is table name
0873: * @param tableID is table ID
0874: * @param conn is connection
0875: * @param firstColumn is first column
0876: * @return true
0877: * @throws SQLException
0878: */
0879: private Vector readCounter(String tableName, String tableID,
0880: Connection conn, int firstColumn) throws SQLException {
0881:
0882: String strQuery = null;
0883: BigDecimal value = new BigDecimal(0);
0884: Vector startValues = new Vector();
0885: Vector counterNames = this .getCounterName(tableName, tableID);
0886: Vector counterValues = this .getCounterValue(tableName, tableID);
0887: Vector counterIncrements = this .getCounterIncrement(tableName,
0888: tableID);
0889: Vector defaultStartValues = this .getCounterIncrement(tableName,
0890: tableID);
0891:
0892: for (int i = 0; i < counterNames.size(); i++) {
0893: Statement stmt = conn.createStatement();
0894: strQuery = querySelectCounter(counterNames.get(i)
0895: .toString());
0896: this .logger.write("full", "\tQuery '" + strQuery
0897: + "' will be executed");
0898: ResultSet rset = stmt.executeQuery(strQuery);
0899: if (rset.next()) {
0900: if (firstColumn == 0)
0901: value = new BigDecimal(rset.getString(0));
0902: else
0903: value = new BigDecimal(rset.getString(1));
0904: } else {
0905: insertCounter(tableName, tableID, conn);
0906: value = new BigDecimal(defaultStartValues.get(i)
0907: .toString());
0908: }
0909: stmt.close();
0910: rset.close();
0911: startValues.addElement(value);
0912:
0913: }
0914: return startValues;
0915: }
0916:
0917: /**
0918: *
0919: * @param counterColumnName is name of the column
0920: * @return query
0921: */
0922: private String querySelectCounter(String counterColumnName) {
0923: String query = "select ";
0924:
0925: query += this .counterValueColumn + " from "
0926: + this .counterTableName + " where "
0927: + this .counterNameColumn + " = '" + counterColumnName
0928: + "'";
0929: return query;
0930: }
0931:
0932: /**
0933: *
0934: * @param counterColumnName is column name
0935: * @param counterColumnValue is column value
0936: * @return query
0937: */
0938: private String queryInsertCounter(String counterColumnName,
0939: BigDecimal counterColumnValue) {
0940: String query = "INSERT into ";
0941:
0942: query += this .counterTableName + " (" + this .counterNameColumn
0943: + ", " + this .counterValueColumn + ") VALUES('"
0944: + counterColumnName + "', "
0945: + counterColumnValue.intValue() + ")";
0946: return query;
0947: }
0948:
0949: /**
0950: *
0951: * @param counterColumnName is colulmn name
0952: * @param newValue is column value
0953: * @return query
0954: */
0955:
0956: private String queryUpdateCounter(String counterColumnName,
0957: BigDecimal newValue) {
0958: String query = "update ";
0959: query += this .counterTableName + " set "
0960: + this .counterValueColumn + " = " + newValue.toString()
0961: + " where " + this .counterNameColumn + " = '"
0962: + counterColumnName + "'";
0963: return query;
0964: }
0965:
0966: /**
0967: * This method read value of sub counter parameter
0968: * @param tableName is table name
0969: * @param tableID is table ID
0970: * @param conn is connection
0971: * @param firstColumn is first column
0972: * @return vector
0973: * @throws SQLException
0974: */
0975: public Vector readSubCounterValue(String tableName, String tableID,
0976: Connection conn, int firstColumn, String tableMode,
0977: ConfigReader targetConfigReader) throws SQLException,
0978: LoaderException {
0979:
0980: BigDecimal value = new BigDecimal(0);
0981: Vector startValues = new Vector();
0982:
0983: this .logger.write("full",
0984: "\treadSubCounterValue method is started");
0985:
0986: Vector columns = this .getSubCounterKeyColumns(tableName,
0987: tableID);
0988: Vector typs = this .getSubCounterKeyColumnsTyp(tableName,
0989: tableID);
0990: Vector subColumnNames = this .getSubTargetColumnName(tableName,
0991: tableID);
0992: Vector subIncrement = this .getSubCounterIncrement(tableName,
0993: tableID);
0994: Vector subCounterStartValues = this .getSubCounterStartValue(
0995: tableName, tableID);
0996: Vector subCounterKeyColumnValues = this .getSubCounterKeyValues(
0997: tableName, tableID);
0998:
0999: for (int i = 0; i < subColumnNames.size(); i++) {
1000:
1001: String strQuery = "select ";
1002: Statement stmt = conn.createStatement();
1003: strQuery += subColumnNames.get(i).toString() + " from "
1004: + tableName + " where ";
1005: for (int j = 0; j < ((Vector) columns.get(i)).size(); j++) {
1006: if (((Vector) subCounterKeyColumnValues.get(i)).get(j) != null
1007: && !((Vector) subCounterKeyColumnValues.get(i))
1008: .get(j).toString().equalsIgnoreCase("")) {
1009: //ZK change this from CheckType to targetConfigReader
1010: try {
1011: if (!targetConfigReader.isNumber(((Vector) typs
1012: .get(i)).get(j).toString()))
1013: strQuery += ((Vector) columns.get(i))
1014: .get(j).toString()
1015: + " = '"
1016: + ((Vector) subCounterKeyColumnValues
1017: .get(i)).get(j).toString()
1018: + "' and ";
1019: else
1020: strQuery += ((Vector) columns.get(i))
1021: .get(j).toString()
1022: + " = "
1023: + ((Vector) subCounterKeyColumnValues
1024: .get(i)).get(j).toString()
1025: + " and ";
1026: } catch (LoaderException e) {
1027: LoaderException le = new LoaderException(
1028: "Exception:This sql type isn't present in conf file for target database. Yuo must add it into conf file.",
1029: (Throwable) e);
1030: throw le;
1031: }
1032: } else
1033: strQuery += ((Vector) columns.get(i)).get(j)
1034: .toString()
1035: + " is null and ";
1036: }
1037:
1038: if (strQuery.endsWith(" and "))
1039: strQuery = strQuery.substring(0, strQuery.length() - 5);
1040:
1041: if (tableMode.equalsIgnoreCase("cache")) {
1042: //caching the subcounter value
1043: Object obj = subCounterCache.get(strQuery);
1044: if (obj == null) { //query isn't cached before
1045: this .logger.write("full", "\tQuery '" + strQuery
1046: + "' will be executed");
1047: ResultSet rset = stmt.executeQuery(strQuery);
1048: BigDecimal currentValue = new BigDecimal(0);
1049: if (rset.next()) {
1050: if (firstColumn == 0) {
1051: value = new BigDecimal(rset.getString(0));
1052: currentValue = value;
1053: while (rset.next()) {
1054: value = new BigDecimal(rset
1055: .getString(0));
1056: if (currentValue.intValue() < value
1057: .intValue())
1058: currentValue = value;
1059: }
1060: } else { //firstColumnResult==1
1061: value = new BigDecimal(rset.getString(1));
1062: currentValue = value;
1063: while (rset.next()) {
1064: value = new BigDecimal(rset
1065: .getString(1));
1066: if (currentValue.intValue() < value
1067: .intValue())
1068: currentValue = value;
1069: }
1070: }
1071: value = value.add(new BigDecimal(subIncrement
1072: .get(i).toString()));
1073: } else {
1074: value = new BigDecimal(subCounterStartValues
1075: .get(i).toString());
1076: }
1077: subCounterCache.put(strQuery, value);
1078: stmt.close();
1079: } else { //query is cached before
1080: BigDecimal oldValue = (BigDecimal) obj;
1081: value = oldValue.add(new BigDecimal(subIncrement
1082: .get(i).toString()));
1083: subCounterCache.remove(strQuery);
1084: subCounterCache.put(strQuery, value);
1085: }
1086: } else {//tableMode=query
1087: this .logger.write("full", "\tQuery '" + strQuery
1088: + "' will be executed");
1089: ResultSet rset = stmt.executeQuery(strQuery);
1090: BigDecimal currentValue = new BigDecimal(0);
1091: if (rset.next()) {
1092: if (firstColumn == 0) {
1093: value = new BigDecimal(rset.getString(0));
1094: currentValue = value;
1095: while (rset.next()) {
1096: value = new BigDecimal(rset.getString(0));
1097: if (currentValue.intValue() < value
1098: .intValue())
1099: currentValue = value;
1100: }
1101: } else { //firstColumnResult==1
1102: value = new BigDecimal(rset.getString(1));
1103: currentValue = value;
1104: while (rset.next()) {
1105: value = new BigDecimal(rset.getString(1));
1106: if (currentValue.intValue() < value
1107: .intValue())
1108: currentValue = value;
1109: }
1110: }
1111: value = value.add(new BigDecimal(subIncrement
1112: .get(i).toString()));
1113: } else {
1114: value = new BigDecimal(subCounterStartValues.get(i)
1115: .toString());
1116: }
1117: stmt.close();
1118:
1119: }
1120: startValues.addElement(value);
1121: }
1122: this .logger.write("full",
1123: "\treadSubCounterValue method is finished");
1124: return startValues;
1125: }
1126:
1127: /**
1128: * This method reset cach for subcounter
1129: */
1130: public void resetSubCounterCache() {
1131: if (this .subCounterCache.size() > 0)
1132: this .subCounterCache.clear();
1133: }
1134:
1135: /**
1136: * Method importAttributeValue reads value for strAttrName attribute in strTagName tag.
1137: * This method return this value.
1138: * @param doc Parsed import XML file.
1139: * @param strTagName The name of tag where attribute is situated.
1140: * @param strAttrName The name of tag attribute which reads input value.
1141: * @param iImportJobItem Number of ImportDefinition tag which is processed.
1142: * @return String - importing value.
1143: */
1144: private String importAttributeValue(Document doc,
1145: String strTagName, String strAttrName, int iImportJobItem) {
1146: String strValue = "";
1147: NodeList tagBasic = doc
1148: .getElementsByTagName("importDefinition");
1149: if (tagBasic.getLength() != 0) {
1150: Element docFragment = (Element) tagBasic
1151: .item(iImportJobItem);
1152: // NodeList tag = docFragment.getElementsByTagName(tagName);
1153: // for (int i = 0; i < tag.getLength(); i++) {
1154:
1155: tagBasic = docFragment.getElementsByTagName(strTagName);
1156: if (tagBasic.getLength() != 0) {
1157: docFragment = (Element) tagBasic.item(0);
1158: if (docFragment != null)
1159: strValue = docFragment.getAttribute(strAttrName);
1160: }
1161: }
1162: return strValue;
1163: }
1164:
1165: /**
1166: * Method counterColumnTypes is used to put types of constant columns into
1167: * global vector sorted in target tables. If there is an error, Exception
1168: * "SQLException" or "NullPointerException" is thrown.
1169: * @param c Connection to target database.
1170: * @param tableName is table name
1171: * @param tableID is table ID
1172: * @param firstColumn is first column
1173: * @throws SQLException Constructs an SQLException object with a reason.
1174: * @throws NullPointerException Constructs a NullPointerException with the specified detail message.
1175: */
1176: public void counterColumnTypes(String tableName, String tableID,
1177: Connection c, int firstColumn,
1178: boolean columnsSuportedTarget,
1179: ConfigReader configReaderTarget) throws SQLException,
1180: NullPointerException {
1181: int iCnt = 0;
1182: try {
1183: Vector columnNames = this .getSubCounterKeyColumns(
1184: tableName, tableID);
1185: Statement stmtConstant = c.createStatement();
1186: Vector typs = new Vector();
1187: Vector subTyps = new Vector();
1188: String strQuery = "select ";
1189: ResultSet rsetConstant = null;
1190:
1191: if (columnNames.size() != 0) {
1192: for (int i = 0; i < columnNames.size(); i++) {
1193: for (int j = 0; j < ((Vector) columnNames.get(i))
1194: .size(); j++) {
1195: strQuery += ((Vector) columnNames.get(i))
1196: .get(j).toString()
1197: + ", ";
1198: }
1199: strQuery = strQuery.substring(0,
1200: strQuery.length() - 2);
1201: strQuery += " from " + tableName;
1202: //ZK change this. Because of problems with getColumnTypeName()method. Some drivers doesn't support it.
1203: //start
1204: if (columnsSuportedTarget) {
1205:
1206: rsetConstant = c.getMetaData().getColumns(
1207: c.getCatalog(), null, tableName, "%");
1208: String columnName = "";
1209: String columnType = "";
1210: while (rsetConstant.next()) {
1211: columnName = rsetConstant
1212: .getString(3 + firstColumn);
1213: columnType = rsetConstant
1214: .getString(5 + firstColumn);
1215: Vector temp = (Vector) columnNames.get(i);
1216: for (int j = 0; j < temp.size(); j++) {
1217: if (temp.get(j).toString()
1218: .equalsIgnoreCase(columnName)) {
1219: typs.add(columnType);
1220: }
1221: }
1222: }
1223: } else {//TODO ZK ADDED stmtConstant.setMaxRows(1). Place this as parameter in conf file, like maxRowsSuported
1224: if (configReaderTarget.getMaxRowsSupported()) {
1225: stmtConstant.setMaxRows(1);
1226: }
1227: rsetConstant = stmtConstant
1228: .executeQuery(strQuery);
1229:
1230: for (int j = 0; j < ((Vector) columnNames
1231: .get(i)).size(); j++) {
1232:
1233: typs
1234: .add(rsetConstant.getMetaData()
1235: .getColumnTypeName(
1236: j + firstColumn));
1237: }
1238:
1239: }
1240: rsetConstant.close();
1241: subTyps.addElement(typs);
1242: }
1243: }
1244: this .subCounterKeyColumnsTyp.put(tableName + "_" + tableID,
1245: subTyps);
1246: stmtConstant.close();
1247:
1248: } catch (SQLException ex) {
1249: throw ex;
1250: } catch (NullPointerException ex) {
1251: throw ex;
1252: }
1253: }
1254:
1255: /**
1256: * This method reset all variables
1257: */
1258: public void reset() {
1259: this .counterTableName = null;
1260: this .counterNameColumn = null;
1261: this .counterValueColumn = null;
1262: this .counterName = new Hashtable();
1263: this .counterStartValue = new Hashtable();
1264: this .counterIncrement = new Hashtable();
1265: this .targetColumnName = new Hashtable();
1266: this .targetColumnTyp = new Hashtable();
1267: this .valueMode = new Hashtable();
1268: this .counterStartValueReset = new Hashtable();
1269:
1270: this .subCounterName = new Hashtable();
1271: this .subCounterStartValue = new Hashtable();
1272: this .subCounterIncrement = new Hashtable();
1273: this .subTargetColumnName = new Hashtable();
1274: this .subValueMode = new Hashtable();
1275: this .subCounterKeyColumns = new Hashtable();
1276: this .subCounterKeyValues = new Hashtable();
1277: this .subCounterKeyColumnsTyp = new Hashtable();
1278: this .subTargetColumnTyp = new Hashtable();
1279:
1280: this .vecCounterName = new Vector();
1281: this .vecCounterIncrement = new Vector();
1282: this .vecCounterStartValue = new Vector();
1283: this .vecTargetColumnName = new Vector();
1284: this .vecValueMode = new Vector();
1285: this .vecCounterStartValueReset = new Vector();
1286: this .vecTargetTableName = new Vector();
1287: this .vecTargetTableID = new Vector();
1288: this .currentCounterValue = new Hashtable();
1289:
1290: this .vecSubCounterName = new Vector();
1291: this .vecSubCounterIncrement = new Vector();
1292: this .vecSubCounterStartValue = new Vector();
1293: this .vecSubTargetTableName = new Vector();
1294: this .vecSubTargetTableID = new Vector();
1295: this .vecSubTargetColumnName = new Vector();
1296: this .vecSubValueMode = new Vector();
1297: this .vecSubKeyColumns = new Vector();
1298: this .vecSubKeyColumnsTyp = new Vector();
1299: this.subCounterCache.clear();
1300: }
1301: }
|