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.sql.Timestamp;
22 import java.util.Vector;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.juddi.datatype.business.BusinessEntity;
27 import org.apache.juddi.registry.RegistryEngine;
28 import org.apache.juddi.util.Config;
29
30 /***
31 * @author Steve Viens (sviens@apache.org)
32 */
33 class BusinessEntityTable
34 {
35
36 private static Log log = LogFactory.getLog(BusinessEntityTable.class);
37
38 static String insertSQL = null;
39 static String deleteSQL = null;
40 static String selectSQL = null;
41 static String selectByPublisherSQL = null;
42 static String verifyOwnershipSQL = null;
43 static String selectPublisherSQL = null;
44 static String tablePrefix = "";
45
46 static
47 {
48 tablePrefix = Config.getStringProperty(
49 RegistryEngine.PROPNAME_TABLE_PREFIX,RegistryEngine.DEFAULT_TABLE_PREFIX);
50
51 StringBuffer sql = null;
52
53
54 sql = new StringBuffer(150);
55 sql.append("INSERT INTO ").append(tablePrefix).append("BUSINESS_ENTITY (");
56 sql.append("BUSINESS_KEY,");
57 sql.append("AUTHORIZED_NAME,");
58 sql.append("PUBLISHER_ID,");
59 sql.append("OPERATOR,");
60 sql.append("LAST_UPDATE) ");
61 sql.append("VALUES (?,?,?,?,?)");
62 insertSQL = sql.toString();
63
64
65 sql = new StringBuffer(100);
66 sql.append("DELETE FROM ").append(tablePrefix).append("BUSINESS_ENTITY ");
67 sql.append("WHERE BUSINESS_KEY=?");
68 deleteSQL = sql.toString();
69
70
71 sql = new StringBuffer(200);
72 sql.append("SELECT ");
73 sql.append("AUTHORIZED_NAME,");
74 sql.append("OPERATOR ");
75 sql.append("FROM ").append(tablePrefix).append("BUSINESS_ENTITY ");
76 sql.append("WHERE BUSINESS_KEY=?");
77 selectSQL = sql.toString();
78
79
80 sql = new StringBuffer(200);
81 sql.append("SELECT ");
82 sql.append("BUSINESS_KEY ");
83 sql.append("FROM ").append(tablePrefix).append("BUSINESS_ENTITY ");
84 sql.append("WHERE PUBLISHER_ID=?");
85 selectByPublisherSQL = sql.toString();
86
87
88 sql = new StringBuffer(200);
89 sql.append("SELECT ");
90 sql.append("* ");
91 sql.append("FROM ").append(tablePrefix).append("BUSINESS_ENTITY ");
92 sql.append("WHERE BUSINESS_KEY=? ");
93 sql.append("AND PUBLISHER_ID=?");
94 verifyOwnershipSQL = sql.toString();
95
96
97 sql = new StringBuffer(200);
98 sql.append("SELECT ");
99 sql.append("PUBLISHER_ID ");
100 sql.append("FROM ").append(tablePrefix).append("BUSINESS_ENTITY ");
101 sql.append("WHERE BUSINESS_KEY=?");
102 selectPublisherSQL = sql.toString();
103 }
104
105 /***
106 * Insert new row into the BUSINESS_ENTITY table.
107 *
108 * @param business object holding values to be inserted
109 * @param publisherID
110 * @param connection connection
111 * @throws java.sql.SQLException
112 */
113 public static void insert(BusinessEntity business,String publisherID,Connection connection)
114 throws java.sql.SQLException
115 {
116 PreparedStatement statement = null;
117 Timestamp timeStamp = new Timestamp(System.currentTimeMillis());
118
119 try
120 {
121 statement = connection.prepareStatement(insertSQL);
122 statement.setString(1,business.getBusinessKey());
123 statement.setString(2,business.getAuthorizedName());
124 statement.setString(3,publisherID);
125 statement.setString(4,business.getOperator());
126 statement.setTimestamp(5,timeStamp);
127
128 if (log.isDebugEnabled()) {
129 log.debug("insert into " + tablePrefix + "BUSINESS_ENTITY table:\n\n\t" + insertSQL +
130 "\n\t BUSINESS_KEY=" + business.getBusinessKey() +
131 "\n\t AUTHORIZED_NAME=" + business.getAuthorizedName() +
132 "\n\t PUBLISHER_ID=" + publisherID +
133 "\n\t OPERATOR=" + business.getOperator() +
134 "\n\t LAST_UPDATE=" + timeStamp.getTime() + "\n");
135 }
136
137
138 statement.executeUpdate();
139 }
140 finally
141 {
142 try {
143 statement.close();
144 }
145 catch (Exception e)
146 {
147 log.warn("An Exception was encountered while attempting to close " +
148 "the Insert BusinessEntity PreparedStatement: "+e.getMessage(),e);
149 }
150 }
151 }
152
153 /***
154 * Delete row from the BUSINESS_ENTITY table.
155 *
156 * @param businessKey key value
157 * @param connection JDBC Connection
158 * @throws java.sql.SQLException
159 */
160 public static void delete(String businessKey,Connection connection)
161 throws java.sql.SQLException
162 {
163 PreparedStatement statement = null;
164
165 try
166 {
167
168 statement = connection.prepareStatement(deleteSQL);
169 statement.setString(1,businessKey.toString());
170
171 if (log.isDebugEnabled()) {
172 log.debug("delete from " + tablePrefix + "BUSINESS_ENTITY table:\n\n\t" + deleteSQL +
173 "\n\t BUSINESS_KEY=" + businessKey.toString() + "\n");
174 }
175
176
177 statement.executeUpdate();
178 }
179 finally
180 {
181 try {
182 statement.close();
183 }
184 catch (Exception e)
185 {
186 log.warn("An Exception was encountered while attempting to close " +
187 "the Delete BusinessEntity PreparedStatement: "+e.getMessage(),e);
188 }
189 }
190 }
191
192 /***
193 * Select one row from the BUSINESS_ENTITY table.
194 *
195 * @param businessKey key value
196 * @param connection JDBC Connection
197 * @throws java.sql.SQLException
198 */
199 public static BusinessEntity select(String businessKey,Connection connection)
200 throws java.sql.SQLException
201 {
202 BusinessEntity business = null;
203 PreparedStatement statement = null;
204 ResultSet resultSet = null;
205
206 try
207 {
208 statement = connection.prepareStatement(selectSQL);
209 statement.setString(1,businessKey.toString());
210
211 if (log.isDebugEnabled()) {
212 log.debug("select from " + tablePrefix + "BUSINESS_ENTITY table:\n\n\t" + selectSQL +
213 "\n\t BUSINESS_KEY=" + businessKey.toString() + "\n");
214 }
215
216 resultSet = statement.executeQuery();
217 if (resultSet.next())
218 {
219 business = new BusinessEntity();
220 business.setBusinessKey(businessKey);
221 business.setAuthorizedName(resultSet.getString(1));
222 business.setOperator(resultSet.getString(2));
223 }
224
225 return business;
226 }
227 finally
228 {
229 try {
230 resultSet.close();
231 statement.close();
232 }
233 catch (Exception e)
234 {
235 log.warn("An Exception was encountered while attempting to close " +
236 "the Select BusinessEntity ResultSet and PreparedStatement: "+e.getMessage(),e);
237 }
238 }
239 }
240
241 /***
242 * Select all BusinessKeys from the business_entities table for a given
243 * 'publisherID' value.
244 *
245 * @param publisherID The user id of the BusinessEntity owner.
246 * @param connection JDBC connection
247 * @return Vector A Vector of BusinessKeys
248 * @throws java.sql.SQLException
249 */
250 public static Vector selectByPublisherID(String publisherID,Connection connection)
251 throws java.sql.SQLException
252 {
253 Vector keyList = new Vector();
254 PreparedStatement statement = null;
255 ResultSet resultSet = null;
256
257 try
258 {
259
260 statement = connection.prepareStatement(selectByPublisherSQL);
261 statement.setString(1,publisherID);
262
263 if (log.isDebugEnabled()) {
264 log.debug("select from " + tablePrefix + "BUSINESS_ENTITY table:\n\n\t" + selectByPublisherSQL +
265 "\n\t PUBLISHER_ID=" + publisherID + "\n");
266 }
267
268
269 resultSet = statement.executeQuery();
270 while (resultSet.next())
271 keyList.add(resultSet.getString(1));
272
273 return keyList;
274 }
275 finally
276 {
277 try {
278 resultSet.close();
279 statement.close();
280 }
281 catch (Exception e)
282 {
283 log.warn("An Exception was encountered while attempting to close " +
284 "the Select BusinessEntity ResultSet and PreparedStatement: "+e.getMessage(),e);
285 }
286 }
287 }
288
289 /***
290 * Verify that 'publisherID' has the authority to update or delete
291 * BusinessEntity identified by the businessKey parameter
292 *
293 * @param businessKey
294 * @param publisherID
295 * @param connection
296 * @throws java.sql.SQLException
297 */
298 public static boolean verifyOwnership(String businessKey,String publisherID,Connection connection)
299 throws java.sql.SQLException
300 {
301 if ((businessKey == null) || (publisherID == null))
302 return false;
303
304 boolean authorized = false;
305 PreparedStatement statement = null;
306 ResultSet resultSet = null;
307
308 try
309 {
310 statement = connection.prepareStatement(verifyOwnershipSQL);
311 statement.setString(1,businessKey);
312 statement.setString(2,publisherID);
313
314 if (log.isDebugEnabled()) {
315 log.debug("checking ownership of " + tablePrefix + "BUSINESS_ENTITY:\n\n\t" + verifyOwnershipSQL +
316 "\n\t BUSINESS_KEY=" + businessKey +
317 "\n\t PUBLISHER_ID=" + publisherID + "\n");
318 }
319
320 resultSet = statement.executeQuery();
321 if (resultSet.next())
322 authorized = true;
323
324 return authorized;
325 }
326 finally
327 {
328 try {
329 resultSet.close();
330 statement.close();
331 }
332 catch (Exception e) {
333 }
334 }
335
336 /***
337 * Return the 'publisherID' for the BusinessEntity identified by the
338 * businessKey parameter. Retusn null if the business entity key does
339 * not represent a valid BuisnessEntity or if no publisherID is specified
340 * for that particular BusinessEntity.
341 *
342 * @param businessKey
343 * @param connection
344 * @return publisherID or null if no publisherID is available.
345 * @throws java.sql.SQLException
346 */
347 public static String selectPublisherID(String businessKey,Connection connection)
348 throws java.sql.SQLException
349 {
350 if (businessKey == null)
351 return null;
352
353 String publisherID = null;
354 PreparedStatement statement = null;
355 ResultSet resultSet = null;
356
357 try
358 {
359 statement = connection.prepareStatement(selectPublisherSQL);
360 statement.setString(1,businessKey);
361
362 if (log.isDebugEnabled()) {
363 log.debug("fetching publishers ID for BUSINESS_ENTITY:\n\n\t" + selectPublisherSQL +
364 "\n\t BUSINESS_KEY=" + businessKey + "\n");
365 }
366
367 resultSet = statement.executeQuery();
368 if (resultSet.next())
369 publisherID = resultSet.getString(1);
370
371 return publisherID;
372 }
373 finally
374 {
375 try {
376 resultSet.close();
377 statement.close();
378 }
379 catch (Exception e) {
380 }
381 }
382 }