001: /*
002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.hipergate;
034:
035: import java.sql.SQLException;
036: import java.sql.PreparedStatement;
037: import java.sql.ResultSet;
038: import java.sql.ResultSetMetaData;
039:
040: import com.knowgate.debug.DebugFile;
041: import com.knowgate.jdc.JDCConnection;
042: import com.knowgate.dataobjs.DB;
043: import com.knowgate.dataobjs.DBPersist;
044: import com.knowgate.misc.Gadgets;
045:
046: /**
047: * <p>Table Meta-Attributes</p>
048: * Meta-Attributes are "virtual columns" that may exists for each table on a per
049: * WorkArea basis. This class holds only the meta-attribuyes definitions, not the
050: * actual data contained on them.
051: * @author Sergio Montoro Ten
052: * @version 2.1
053: */
054: public class MetaAttribute extends DBPersist {
055:
056: /**
057: * Create empty Meta-Attribute
058: */
059: public MetaAttribute() {
060: super (DB.k_lu_meta_attrs, "MetaAttribute");
061: }
062:
063: /**
064: * Load Meta-Attribute from database.
065: * @param oConn Database Connection
066: * @param sOwnerGUID GUID of WorkArea to witch the Meta-Attribute belongs.
067: * @param sTableName Name of base table
068: * @param sSectionName Section Name (emulates a column name on base table).
069: * @throws SQLException
070: */
071: public MetaAttribute(JDCConnection oConn, String sOwnerGUID,
072: String sTableName, String sSectionName) throws SQLException {
073: super (DB.k_lu_meta_attrs, "MetaAttribute");
074: PreparedStatement oStmt = oConn
075: .prepareStatement("SELECT * FROM " + DB.k_lu_meta_attrs
076: + " WHERE " + DB.gu_owner + "=? AND "
077: + DB.nm_table + "=? AND " + DB.id_section
078: + "=?", ResultSet.TYPE_FORWARD_ONLY,
079: ResultSet.CONCUR_READ_ONLY);
080: oStmt.setString(1, sOwnerGUID);
081: oStmt.setString(2, sTableName);
082: oStmt.setString(3, sSectionName);
083: ResultSet oRSet = oStmt.executeQuery();
084: boolean bFound = oRSet.next();
085: if (bFound) {
086: ResultSetMetaData oMDat = oRSet.getMetaData();
087: int iColCount = oMDat.getColumnCount();
088: for (int c = 1; c <= iColCount; c++) {
089: put(oMDat.getColumnName(c).toLowerCase(), oRSet
090: .getObject(c));
091: } // next
092: oRSet.close();
093: oStmt.close();
094: } else {
095: oRSet.close();
096: oStmt.close();
097: throw new SQLException(DB.k_lu_meta_attrs
098: + " No data found for " + sTableName + "."
099: + sSectionName + " at WorkArea " + sOwnerGUID,
100: "42S02", 100);
101: }
102: }
103:
104: // ----------------------------------------------------------
105:
106: /**
107: * Delete meta-Attribute definition and data.
108: * @param oConn Database Connection
109: * @throws SQLException
110: * @throws NullPointerException If base table, workarea or attribute is <b>null</b>
111: */
112: public boolean delete(JDCConnection oConn) throws SQLException,
113: NullPointerException {
114: PreparedStatement oStmt;
115: boolean bRetVal;
116:
117: if (DebugFile.trace) {
118: DebugFile
119: .writeln("Begin MetaAttribute.delete([Connection])");
120: }
121:
122: if (isNull(DB.nm_table))
123: throw new NullPointerException(
124: "Base table for meta-attribute not set");
125: if (isNull(DB.gu_owner))
126: throw new NullPointerException(
127: "Owner WorkArea for meta-attribute not set");
128: if (isNull(DB.id_section))
129: throw new NullPointerException(
130: "Section for meta-attribute not set");
131:
132: if (DebugFile.trace) {
133: DebugFile.incIdent();
134: DebugFile
135: .writeln("Connection.prepareStatement(DELETE FROM "
136: + DB.k_lu_meta_attrs + " WHERE "
137: + DB.gu_owner + "='"
138: + getStringNull(DB.gu_owner, "null")
139: + "' AND " + DB.nm_table + "='"
140: + getStringNull(DB.nm_table, "null")
141: + "' AND " + DB.id_section + "='"
142: + getStringNull(DB.id_section, "null")
143: + "'");
144: }
145:
146: oStmt = oConn.prepareStatement("DELETE FROM "
147: + DB.k_lu_meta_attrs + " WHERE " + DB.gu_owner
148: + "=? AND " + DB.nm_table + "=? AND " + DB.id_section
149: + "=?");
150: oStmt.setString(1, getString(DB.gu_owner));
151: oStmt.setString(2, getString(DB.nm_table));
152: oStmt.setString(3, getString(DB.id_section));
153: oStmt.executeUpdate();
154: oStmt.close();
155:
156: bRetVal = super .delete(oConn);
157:
158: if (DebugFile.trace) {
159: DebugFile.decIdent();
160: DebugFile.writeln("End MetaAttribute.delete() : "
161: + String.valueOf(bRetVal));
162: }
163:
164: return bRetVal;
165: } // delete()
166:
167: // ----------------------------------------------------------
168:
169: /**
170: * <p>Store Meta-Attribute Definition</p>
171: * Field pg_attr is automatically assigned to next free value in k_lu_meta_attrs
172: * table for current WorkArea and Base Table.
173: * @param oConn Database Connection
174: * @throws SQLException
175: */
176: public boolean store(JDCConnection oConn) throws SQLException {
177: PreparedStatement oStmt;
178: ResultSet oRSet;
179: Object oMax;
180: Integer iMax;
181: boolean bRetVal;
182:
183: if (DebugFile.trace) {
184: DebugFile.writeln("Begin MetaAttribute.stor([Connection])");
185: DebugFile.incIdent();
186: DebugFile.writeln("Connection.prepareStatement(SELECT MAX("
187: + DB.pg_attr + ")+1 FROM " + DB.k_lu_meta_attrs
188: + " WHERE " + DB.gu_owner + "='"
189: + getStringNull(DB.gu_owner, "") + "' AND "
190: + DB.nm_table + "='"
191: + getStringNull(DB.nm_table, "") + "')");
192: }
193:
194: if (!AllVals.containsKey(DB.pg_attr)) {
195: oStmt = oConn.prepareStatement("SELECT MAX(" + DB.pg_attr
196: + ")+1 FROM " + DB.k_lu_meta_attrs + " WHERE "
197: + DB.gu_owner + "=? AND " + DB.nm_table + "=?");
198: oStmt.setString(1, getString(DB.gu_owner));
199: oStmt.setString(2, getString(DB.nm_table));
200: oRSet = oStmt.executeQuery();
201: if (oRSet.next()) {
202: oMax = oRSet.getObject(1);
203: if (oRSet.wasNull())
204: iMax = new Integer(1);
205: else
206: iMax = new Integer(oMax.toString());
207: } else
208: iMax = new Integer(1);
209: oRSet.close();
210: oStmt.close();
211:
212: if (oConn.getDataBaseProduct() == JDCConnection.DBMS_ORACLE)
213: put(DB.pg_attr, new java.math.BigDecimal(iMax
214: .toString()));
215: else
216: put(DB.pg_attr, new Short(iMax.toString()));
217: }
218:
219: bRetVal = super .store(oConn);
220:
221: if (DebugFile.trace) {
222: DebugFile.decIdent();
223: DebugFile.writeln("End MetaAttribute.store() : "
224: + String.valueOf(bRetVal));
225: }
226:
227: return bRetVal;
228: } // store()
229:
230: // **********************************************************
231: // Public Constants
232:
233: public static final short ClassId = 12;
234: }
|