KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipergate > MetaAttribute


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.hipergate;
34
35 import java.sql.SQLException JavaDoc;
36 import java.sql.PreparedStatement JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.ResultSetMetaData JavaDoc;
39
40 import com.knowgate.debug.DebugFile;
41 import com.knowgate.jdc.JDCConnection;
42 import com.knowgate.dataobjs.DB;
43 import com.knowgate.dataobjs.DBPersist;
44 import com.knowgate.misc.Gadgets;
45
46 /**
47  * <p>Table Meta-Attributes</p>
48  * Meta-Attributes are "virtual columns" that may exists for each table on a per
49  * WorkArea basis. This class holds only the meta-attribuyes definitions, not the
50  * actual data contained on them.
51  * @author Sergio Montoro Ten
52  * @version 2.1
53  */

54 public class MetaAttribute extends DBPersist {
55
56   /**
57    * Create empty Meta-Attribute
58    */

59   public MetaAttribute() {
60    super(DB.k_lu_meta_attrs, "MetaAttribute");
61   }
62
63   /**
64    * Load Meta-Attribute from database.
65    * @param oConn Database Connection
66    * @param sOwnerGUID GUID of WorkArea to witch the Meta-Attribute belongs.
67    * @param sTableName Name of base table
68    * @param sSectionName Section Name (emulates a column name on base table).
69    * @throws SQLException
70    */

71   public MetaAttribute(JDCConnection oConn, String JavaDoc sOwnerGUID, String JavaDoc sTableName, String JavaDoc sSectionName) throws SQLException JavaDoc {
72     super(DB.k_lu_meta_attrs, "MetaAttribute");
73     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT * FROM "+DB.k_lu_meta_attrs+" WHERE "+DB.gu_owner+"=? AND "+DB.nm_table+"=? AND "+DB.id_section+"=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
74     oStmt.setString(1, sOwnerGUID);
75     oStmt.setString(2, sTableName);
76     oStmt.setString(3, sSectionName);
77     ResultSet JavaDoc oRSet = oStmt.executeQuery();
78     boolean bFound = oRSet.next();
79     if (bFound) {
80       ResultSetMetaData JavaDoc oMDat = oRSet.getMetaData();
81       int iColCount = oMDat.getColumnCount();
82       for (int c=1; c<=iColCount; c++) {
83         put(oMDat.getColumnName(c).toLowerCase(), oRSet.getObject(c));
84       } // next
85
oRSet.close();
86       oStmt.close();
87     }
88     else {
89       oRSet.close();
90       oStmt.close();
91       throw new SQLException JavaDoc(DB.k_lu_meta_attrs + " No data found for "+sTableName+"."+sSectionName+" at WorkArea "+sOwnerGUID,"42S02",100);
92     }
93   }
94
95   // ----------------------------------------------------------
96

97   /**
98    * Delete meta-Attribute definition and data.
99    * @param oConn Database Connection
100    * @throws SQLException
101    * @throws NullPointerException If base table, workarea or attribute is <b>null</b>
102    */

103   public boolean delete(JDCConnection oConn)
104     throws SQLException JavaDoc,NullPointerException JavaDoc {
105     PreparedStatement JavaDoc oStmt;
106     boolean bRetVal;
107
108     if (DebugFile.trace) {
109       DebugFile.writeln("Begin MetaAttribute.delete([Connection])");
110     }
111
112     if (isNull(DB.nm_table))
113       throw new NullPointerException JavaDoc("Base table for meta-attribute not set");
114     if (isNull(DB.gu_owner))
115       throw new NullPointerException JavaDoc("Owner WorkArea for meta-attribute not set");
116     if (isNull(DB.id_section))
117       throw new NullPointerException JavaDoc("Section for meta-attribute not set");
118
119     if (DebugFile.trace) {
120       DebugFile.incIdent();
121       DebugFile.writeln("Connection.prepareStatement(DELETE FROM " + DB.k_lu_meta_attrs + " WHERE " + DB.gu_owner + "='"+getStringNull(DB.gu_owner,"null")+"' AND " + DB.nm_table + "='" + getStringNull(DB.nm_table,"null")+"' AND " + DB.id_section + "='" + getStringNull(DB.id_section,"null") + "'");
122     }
123
124     oStmt = oConn.prepareStatement("DELETE FROM " + DB.k_lu_meta_attrs + " WHERE " + DB.gu_owner + "=? AND " + DB.nm_table + "=? AND " + DB.id_section + "=?");
125     oStmt.setString(1, getString(DB.gu_owner));
126     oStmt.setString(2, getString(DB.nm_table));
127     oStmt.setString(3, getString(DB.id_section));
128     oStmt.executeUpdate();
129     oStmt.close();
130
131     bRetVal = super.delete(oConn);
132
133     if (DebugFile.trace) {
134       DebugFile.decIdent();
135       DebugFile.writeln("End MetaAttribute.delete() : " + String.valueOf(bRetVal));
136     }
137
138     return bRetVal;
139   } // delete()
140

141   // ----------------------------------------------------------
142

143   /**
144    * <p>Store Meta-Attribute Definition</p>
145    * Field pg_attr is automatically assigned to next free value in k_lu_meta_attrs
146    * table for current WorkArea and Base Table.
147    * @param oConn Database Connection
148    * @throws SQLException
149    */

150   public boolean store(JDCConnection oConn) throws SQLException JavaDoc {
151     PreparedStatement JavaDoc oStmt;
152     ResultSet JavaDoc oRSet;
153     Object JavaDoc oMax;
154     Integer JavaDoc iMax;
155     boolean bRetVal;
156
157     if (DebugFile.trace) {
158       DebugFile.writeln("Begin MetaAttribute.stor([Connection])");
159       DebugFile.incIdent();
160       DebugFile.writeln("Connection.prepareStatement(SELECT MAX(" + DB.pg_attr + ")+1 FROM " + DB.k_lu_meta_attrs + " WHERE " + DB.gu_owner + "='" + getStringNull(DB.gu_owner, "") + "' AND " + DB.nm_table + "='" + getStringNull(DB.nm_table,"") + "')");
161     }
162
163     if (!AllVals.containsKey(DB.pg_attr)) {
164       oStmt = oConn.prepareStatement("SELECT MAX(" + DB.pg_attr + ")+1 FROM " + DB.k_lu_meta_attrs + " WHERE " + DB.gu_owner + "=? AND " + DB.nm_table + "=?");
165       oStmt.setString(1, getString(DB.gu_owner));
166       oStmt.setString(2, getString(DB.nm_table));
167       oRSet = oStmt.executeQuery();
168       if (oRSet.next()) {
169         oMax = oRSet.getObject(1);
170         if (oRSet.wasNull())
171           iMax = new Integer JavaDoc(1);
172         else
173           iMax = new Integer JavaDoc(oMax.toString());
174       }
175       else
176         iMax = new Integer JavaDoc(1);
177       oRSet.close();
178       oStmt.close();
179
180       if (oConn.getDataBaseProduct()==JDCConnection.DBMS_ORACLE)
181         put (DB.pg_attr, new java.math.BigDecimal JavaDoc(iMax.toString()));
182       else
183         put (DB.pg_attr, new Short JavaDoc(iMax.toString()));
184     }
185
186     bRetVal = super.store(oConn);
187
188     if (DebugFile.trace) {
189       DebugFile.decIdent();
190       DebugFile.writeln("End MetaAttribute.store() : " + String.valueOf(bRetVal));
191     }
192
193     return bRetVal;
194   } // store()
195

196   // **********************************************************
197
// Public Constants
198

199   public static final short ClassId = 12;
200 }
201
Popular Tags