001: package com.teamkonzept.db;
002:
003: import java.sql.*;
004: import java.util.Enumeration;
005: import java.util.Vector;
006:
007: import com.teamkonzept.lib.*;
008:
009: public class TKDBVectorInterface extends TKDBInterface {
010:
011: String[] tables;
012:
013: Class[] putQueries;
014: Class[] getQueries;
015: Class deleteQuery;
016:
017: public TKDBVectorInterface(Class newPrimQuery,
018: Class updatePrimQuery, Class getPrimQuery,
019: Class delPrimQuery, String[] tables, Class[] putQueries,
020: Class[] getQueries, Class deleteQuery) {
021: super (newPrimQuery, updatePrimQuery, getPrimQuery, delPrimQuery);
022: this .tables = tables;
023: this .deleteQuery = deleteQuery;
024: this .putQueries = putQueries;
025: this .getQueries = getQueries;
026: }
027:
028: public void delEntry(TKDBVectorData dbData) throws SQLException {
029:
030: try {
031: TKDBManager.beginTransaction();
032:
033: delete(dbData);
034: super .delEntry(dbData);
035:
036: TKDBManager.commitTransaction();
037: } catch (Throwable t) {
038: TKDBManager.safeRollbackTransaction(t);
039: }
040: }
041:
042: private void delete(TKDBVectorData dbData) throws SQLException {
043:
044: if (deleteQuery == null) {
045: return;
046: }
047:
048: TKQuery query = TKDBManager.newQuery(deleteQuery);
049:
050: dbData.insertIntoQuery(query);
051: query.execute();
052: query.close();
053: }
054:
055: public void insertVectorElements(TKDBTableData d, TKQuery query,
056: String table, int i) throws SQLException {
057: d.insertIntoQuery(query);
058: }
059:
060: public void doNewEntryTables(TKDBVectorData dbData)
061: throws SQLException {
062: int lists = tables.length;
063: for (int i = 0; i < lists; i++) {
064:
065: String table = tables[i];
066:
067: Enumeration eCN = dbData.getVector(table).elements();
068: while (eCN.hasMoreElements()) {
069: TKDBTableData d = (TKDBTableData) eCN.nextElement();
070: d.updatePrimary(dbData);
071: }
072: }
073: }
074:
075: public void newEntryTables(TKDBVectorData dbData)
076: throws SQLException {
077: try {
078: TKDBManager.beginTransaction();
079: doNewEntryTables(dbData);
080: TKDBManager.commitTransaction();
081: } catch (Throwable t) {
082: TKDBManager.safeRollbackTransaction(t);
083: }
084: }
085:
086: public void newEntry(TKDBVectorData dbData) throws SQLException {
087: try {
088: TKDBManager.beginTransaction();
089:
090: super .newEntry(dbData);
091: doNewEntryTables(dbData);
092: TKDBManager.commitTransaction();
093: } catch (Throwable t) {
094: TKDBManager.safeRollbackTransaction(t);
095: }
096: }
097:
098: public void doPutEntryTables(TKDBVectorData dbData)
099: throws SQLException {
100: delete(dbData);
101:
102: int lists = tables.length;
103: for (int i = 0; i < lists; i++) {
104:
105: TKQuery query = TKDBManager.newQuery(putQueries[i]);
106: String table = tables[i];
107: int rc = 0;
108: Enumeration eCN = dbData.getVector(table).elements();
109: while (eCN.hasMoreElements()) {
110: TKDBTableData d = (TKDBTableData) eCN.nextElement();
111: insertVectorElements(d, query, table, rc++);
112: query.execute();
113: query.close();
114: }
115: }
116: }
117:
118: public void putEntryTables(TKDBVectorData dbData)
119: throws SQLException {
120: try {
121: TKDBManager.beginTransaction();
122: doPutEntryTables(dbData);
123: TKDBManager.commitTransaction();
124: } catch (Throwable t) {
125: TKDBManager.safeRollbackTransaction(t);
126: }
127: }
128:
129: public void putEntry(TKDBVectorData dbData) throws SQLException {
130: try {
131: TKDBManager.beginTransaction();
132:
133: super .updateEntry(dbData);
134: doPutEntryTables(dbData);
135: TKDBManager.commitTransaction();
136: } catch (Throwable t) {
137: TKDBManager.safeRollbackTransaction(t);
138: }
139: }
140:
141: public void getEntryTables(TKDBVectorData dbData)
142: throws SQLException {
143: int lists = tables.length;
144: for (int i = 0; i < lists; i++) {
145:
146: String table = tables[i];
147: if (dbData.isIgnoreTable(table))
148: continue;
149:
150: TKQuery query = TKDBManager.newQuery(getQueries[i]);
151:
152: dbData.insertIntoQuery(query);
153: query.execute();
154: ResultSet rs = query.fetchResultSet();
155: TKDBTableData prototype = dbData.getProtoType(table);
156: TKVector v = dbData.getVector(table);
157: while (rs.next()) {
158: v.addElement(prototype.newFromResultSet(rs));
159: }
160: query.close();
161: }
162: }
163:
164: public void getEntry(TKDBVectorData dbData) throws SQLException {
165: super.getEntry(dbData);
166: getEntryTables(dbData);
167: }
168: }
|