1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.juddi.datastore.jdbc;
17
18 import java.sql.Connection;
19 import java.sql.PreparedStatement;
20 import java.sql.ResultSet;
21 import java.util.Vector;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.juddi.datatype.KeyedReference;
26 import org.apache.juddi.registry.RegistryEngine;
27 import org.apache.juddi.util.Config;
28
29 /***
30 * @author Steve Viens (sviens@apache.org)
31 */
32 class TModelCategoryTable
33 {
34
35 private static Log log = LogFactory.getLog(TModelCategoryTable.class);
36
37 static String insertSQL = null;
38 static String selectSQL = null;
39 static String deleteSQL = null;
40 static String tablePrefix = "";
41
42 static
43 {
44 tablePrefix = Config.getStringProperty(
45 RegistryEngine.PROPNAME_TABLE_PREFIX,RegistryEngine.DEFAULT_TABLE_PREFIX);
46
47 StringBuffer sql = null;
48
49
50 sql = new StringBuffer(150);
51 sql.append("INSERT INTO ").append(tablePrefix).append("TMODEL_CATEGORY (");
52 sql.append("TMODEL_KEY,");
53 sql.append("CATEGORY_ID,");
54 sql.append("TMODEL_KEY_REF,");
55 sql.append("KEY_NAME,");
56 sql.append("KEY_VALUE) ");
57 sql.append("VALUES (?,?,?,?,?)");
58 insertSQL = sql.toString();
59
60
61 sql = new StringBuffer(200);
62 sql.append("SELECT ");
63 sql.append("TMODEL_KEY_REF,");
64 sql.append("KEY_NAME,");
65 sql.append("KEY_VALUE, ");
66 sql.append("CATEGORY_ID ");
67 sql.append("FROM ").append(tablePrefix).append("TMODEL_CATEGORY ");
68 sql.append("WHERE TMODEL_KEY=? ");
69 sql.append("ORDER BY CATEGORY_ID");
70 selectSQL = sql.toString();
71
72
73 sql = new StringBuffer(100);
74 sql.append("DELETE FROM ").append(tablePrefix).append("TMODEL_CATEGORY ");
75 sql.append("WHERE TMODEL_KEY=?");
76 deleteSQL = sql.toString();
77 }
78
79 /***
80 * Insert new row into the TMODEL_CATEGORY table.
81 *
82 * @param tModelKey String to the parent TModelEntity object.
83 * @param keyRefs A Vector of KeyedReference instances to insert.
84 * @param connection JDBC connection
85 * @throws java.sql.SQLException
86 */
87 public static void insert(
88 String tModelKey,
89 Vector keyRefs,
90 Connection connection)
91 throws java.sql.SQLException
92 {
93 PreparedStatement statement = null;
94
95 try
96 {
97 statement = connection.prepareStatement(insertSQL);
98 statement.setString(1, tModelKey.toString());
99
100 int listSize = keyRefs.size();
101 for (int categoryID = 0; categoryID < listSize; categoryID++)
102 {
103 KeyedReference keyRef = (KeyedReference) keyRefs.elementAt(categoryID);
104
105
106 String tModelKeyValue = null;
107 if (keyRef.getTModelKey() != null)
108 tModelKeyValue = keyRef.getTModelKey().toString();
109
110
111 statement.setInt(2, categoryID);
112 statement.setString(3, tModelKeyValue);
113 statement.setString(4, keyRef.getKeyName());
114 statement.setString(5, keyRef.getKeyValue());
115
116 if (log.isDebugEnabled()) {
117 log.debug(
118 "insert into " + tablePrefix + "TMODEL_CATEGORY table:\n\n\t"
119 + insertSQL
120 + "\n\t BUSINESS_KEY="
121 + tModelKey.toString()
122 + "\n\t CATEGORY_ID="
123 + categoryID
124 + "\n\t TMODEL_KEY_REF="
125 + tModelKeyValue
126 + "\n\t KEY_NAME="
127 + keyRef.getKeyName()
128 + "\n\t KEY_VALUE="
129 + keyRef.getKeyValue()
130 + "\n");
131 }
132
133
134 statement.executeUpdate();
135 }
136 }
137 finally
138 {
139 try
140 {
141 statement.close();
142 }
143 catch (Exception e)
144 {
145 }
146 }
147 }
148
149 /***
150 * Select all rows from the TMODEL_CATEGORY table for a given TModelKey.
151 *
152 * @param tModelKey String
153 * @param connection JDBC connection
154 * @throws java.sql.SQLException
155 */
156 public static Vector select(String tModelKey, Connection connection)
157 throws java.sql.SQLException
158 {
159 Vector keyRefList = new Vector();
160 PreparedStatement statement = null;
161 ResultSet resultSet = null;
162
163 try
164 {
165
166 statement = connection.prepareStatement(selectSQL);
167 statement.setString(1, tModelKey.toString());
168
169 if (log.isDebugEnabled()) {
170 log.debug(
171 "select from " + tablePrefix + "TMODEL_CATEGORY table:\n\n\t"
172 + selectSQL
173 + "\n\t TMODEL_KEY="
174 + tModelKey.toString()
175 + "\n");
176 }
177
178
179 resultSet = statement.executeQuery();
180 while (resultSet.next())
181 {
182 KeyedReference keyRef = new KeyedReference();
183 keyRef.setTModelKey(resultSet.getString(1));
184 keyRef.setKeyName(resultSet.getString(2));
185 keyRef.setKeyValue(resultSet.getString(3));
186 keyRefList.add(keyRef);
187 }
188
189 return keyRefList;
190 }
191 finally
192 {
193 try
194 {
195 resultSet.close();
196 statement.close();
197 }
198 catch (Exception e)
199 {
200 }
201 }
202 }
203
204 /***
205 * Delete multiple rows from the TMODEL_CATEGORY table that are assigned to the
206 * TModelKey specified.
207 *
208 * @param tModelKey String
209 * @param connection JDBC connection
210 * @throws java.sql.SQLException
211 */
212 public static void delete(String tModelKey, Connection connection)
213 throws java.sql.SQLException
214 {
215 PreparedStatement statement = null;
216
217 try
218 {
219
220 statement = connection.prepareStatement(deleteSQL);
221 statement.setString(1, tModelKey.toString());
222
223 if (log.isDebugEnabled()) {
224 log.debug(
225 "delete from " + tablePrefix + "TMODEL_CATEGORY table:\n\n\t"
226 + deleteSQL
227 + "\n\t TMODEL_KEY="
228 + tModelKey.toString()
229 + "\n");
230 }
231
232
233 statement.executeUpdate();
234 }
235 finally
236 {
237 try
238 {
239 statement.close();
240 }
241 catch (Exception e)
242 {
243 }
244 }
245 }
246 }