KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > datastore > jdbc > BusinessEntityTable


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.juddi.datastore.jdbc;
17
18 import java.sql.Connection JavaDoc;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.Timestamp JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.juddi.datatype.business.BusinessEntity;
27
28 /**
29  * @author Steve Viens (sviens@apache.org)
30  */

31 class BusinessEntityTable
32 {
33   // private reference to the jUDDI logger
34
private static Log log = LogFactory.getLog(BusinessEntityTable.class);
35
36   static String JavaDoc insertSQL = null;
37   static String JavaDoc deleteSQL = null;
38   static String JavaDoc selectSQL = null;
39   static String JavaDoc selectByPublisherSQL = null;
40   static String JavaDoc verifyOwnershipSQL = null;
41   static String JavaDoc selectPublisherSQL = null;
42
43   static
44   {
45     // buffer used to build SQL statements
46
StringBuffer JavaDoc sql = null;
47
48     // build insertSQL
49
sql = new StringBuffer JavaDoc(150);
50     sql.append("INSERT INTO BUSINESS_ENTITY (");
51     sql.append("BUSINESS_KEY,");
52     sql.append("AUTHORIZED_NAME,");
53     sql.append("PUBLISHER_ID,");
54     sql.append("OPERATOR,");
55     sql.append("LAST_UPDATE) ");
56     sql.append("VALUES (?,?,?,?,?)");
57     insertSQL = sql.toString();
58
59     // build deleteSQL
60
sql = new StringBuffer JavaDoc(100);
61     sql.append("DELETE FROM BUSINESS_ENTITY ");
62     sql.append("WHERE BUSINESS_KEY=?");
63     deleteSQL = sql.toString();
64
65     // build selectSQL
66
sql = new StringBuffer JavaDoc(200);
67     sql.append("SELECT ");
68     sql.append("AUTHORIZED_NAME,");
69     sql.append("OPERATOR ");
70     sql.append("FROM BUSINESS_ENTITY ");
71     sql.append("WHERE BUSINESS_KEY=?");
72     selectSQL = sql.toString();
73
74     // build selectByPublisherIDSQL
75
sql = new StringBuffer JavaDoc(200);
76     sql.append("SELECT ");
77     sql.append("BUSINESS_KEY ");
78     sql.append("FROM BUSINESS_ENTITY ");
79     sql.append("WHERE PUBLISHER_ID=?");
80     selectByPublisherSQL = sql.toString();
81
82     // build verifyOwnershipSQL
83
sql = new StringBuffer JavaDoc(200);
84     sql.append("SELECT ");
85     sql.append("* ");
86     sql.append("FROM BUSINESS_ENTITY ");
87     sql.append("WHERE BUSINESS_KEY=? ");
88     sql.append("AND PUBLISHER_ID=?");
89     verifyOwnershipSQL = sql.toString();
90
91     // build selectPublisherSQL
92
sql = new StringBuffer JavaDoc(200);
93     sql.append("SELECT ");
94     sql.append("PUBLISHER_ID ");
95     sql.append("FROM BUSINESS_ENTITY ");
96     sql.append("WHERE BUSINESS_KEY=?");
97     selectPublisherSQL = sql.toString();
98   }
99
100   /**
101    * Insert new row into the BUSINESS_ENTITY table.
102    *
103    * @param business object holding values to be inserted
104    * @param publisherID
105    * @param connection connection
106    * @throws java.sql.SQLException
107    */

108   public static void insert(BusinessEntity business,String JavaDoc publisherID,Connection JavaDoc connection)
109     throws java.sql.SQLException JavaDoc
110   {
111     PreparedStatement JavaDoc statement = null;
112     Timestamp JavaDoc timeStamp = new Timestamp JavaDoc(System.currentTimeMillis());
113
114     try
115     {
116       statement = connection.prepareStatement(insertSQL);
117       statement.setString(1,business.getBusinessKey());
118       statement.setString(2,business.getAuthorizedName());
119       statement.setString(3,publisherID);
120       statement.setString(4,business.getOperator());
121       statement.setTimestamp(5,timeStamp);
122
123       log.debug("insert into BUSINESS_ENTITY table:\n\n\t" + insertSQL +
124         "\n\t BUSINESS_KEY=" + business.getBusinessKey() +
125         "\n\t AUTHORIZED_NAME=" + business.getAuthorizedName() +
126         "\n\t PUBLISHER_ID=" + publisherID +
127         "\n\t OPERATOR=" + business.getOperator() +
128         "\n\t LAST_UPDATE=" + timeStamp.getTime() + "\n");
129
130       // execute
131
statement.executeUpdate();
132     }
133     finally
134     {
135       try {
136         statement.close();
137       }
138       catch (Exception JavaDoc e)
139       {
140         log.warn("An Exception was encountered while attempting to close " +
141           "the Insert BusinessEntity PreparedStatement: "+e.getMessage(),e);
142       }
143     }
144   }
145
146   /**
147    * Delete row from the BUSINESS_ENTITY table.
148    *
149    * @param businessKey key value
150    * @param connection JDBC Connection
151    * @throws java.sql.SQLException
152    */

153   public static void delete(String JavaDoc businessKey,Connection JavaDoc connection)
154     throws java.sql.SQLException JavaDoc
155   {
156     PreparedStatement JavaDoc statement = null;
157
158     try
159     {
160       // prepare the delete
161
statement = connection.prepareStatement(deleteSQL);
162       statement.setString(1,businessKey.toString());
163
164       log.debug("delete from BUSINESS_ENTITY table:\n\n\t" + deleteSQL +
165         "\n\t BUSINESS_KEY=" + businessKey.toString() + "\n");
166
167       // execute
168
statement.executeUpdate();
169     }
170     finally
171     {
172       try {
173         statement.close();
174       }
175       catch (Exception JavaDoc e)
176       {
177         log.warn("An Exception was encountered while attempting to close " +
178           "the Delete BusinessEntity PreparedStatement: "+e.getMessage(),e);
179       }
180     }
181   }
182
183   /**
184    * Select one row from the BUSINESS_ENTITY table.
185    *
186    * @param businessKey key value
187    * @param connection JDBC Connection
188    * @throws java.sql.SQLException
189    */

190   public static BusinessEntity select(String JavaDoc businessKey,Connection JavaDoc connection)
191     throws java.sql.SQLException JavaDoc
192   {
193     BusinessEntity business = null;
194     PreparedStatement JavaDoc statement = null;
195     ResultSet JavaDoc resultSet = null;
196
197     try
198     {
199       statement = connection.prepareStatement(selectSQL);
200       statement.setString(1,businessKey.toString());
201
202       log.debug("select from BUSINESS_ENTITY table:\n\n\t" + selectSQL +
203         "\n\t BUSINESS_KEY=" + businessKey.toString() + "\n");
204
205       resultSet = statement.executeQuery();
206       if (resultSet.next())
207       {
208         business = new BusinessEntity();
209         business.setBusinessKey(businessKey);
210         business.setAuthorizedName(resultSet.getString(1));//("AUTHORIZED_NAME"));
211
business.setOperator(resultSet.getString(2));//("OPERATOR"));
212
}
213
214       return business;
215     }
216     finally
217     {
218       try {
219         resultSet.close();
220         statement.close();
221       }
222       catch (Exception JavaDoc e)
223       {
224         log.warn("An Exception was encountered while attempting to close " +
225           "the Select BusinessEntity ResultSet and PreparedStatement: "+e.getMessage(),e);
226       }
227     }
228   }
229
230   /**
231    * Select all BusinessKeys from the business_entities table for a given
232    * 'publisherID' value.
233    *
234    * @param publisherID The user id of the BusinessEntity owner.
235    * @param connection JDBC connection
236    * @return Vector A Vector of BusinessKeys
237    * @throws java.sql.SQLException
238    */

239   public static Vector JavaDoc selectByPublisherID(String JavaDoc publisherID,Connection JavaDoc connection)
240     throws java.sql.SQLException JavaDoc
241   {
242     Vector JavaDoc keyList = new Vector JavaDoc();
243     PreparedStatement JavaDoc statement = null;
244     ResultSet JavaDoc resultSet = null;
245
246     try
247     {
248       // create a statement to query with
249
statement = connection.prepareStatement(selectByPublisherSQL);
250       statement.setString(1,publisherID);
251
252       log.debug("select from BUSINESS_ENTITY table:\n\n\t" + selectByPublisherSQL +
253         "\n\t PUBLISHER_ID=" + publisherID + "\n");
254
255       // execute the statement
256
resultSet = statement.executeQuery();
257       while (resultSet.next())
258         keyList.add(resultSet.getString(1));//("BUSINESS_KEY"));
259

260       return keyList;
261     }
262     finally
263     {
264       try {
265         resultSet.close();
266         statement.close();
267       }
268       catch (Exception JavaDoc e)
269       {
270         log.warn("An Exception was encountered while attempting to close " +
271           "the Select BusinessEntity ResultSet and PreparedStatement: "+e.getMessage(),e);
272       }
273     }
274   }
275
276   /**
277    * Verify that 'publisherID' has the authority to update or delete
278    * BusinessEntity identified by the businessKey parameter
279    *
280    * @param businessKey
281    * @param publisherID
282    * @param connection
283    * @throws java.sql.SQLException
284    */

285   public static boolean verifyOwnership(String JavaDoc businessKey,String JavaDoc publisherID,Connection JavaDoc connection)
286     throws java.sql.SQLException JavaDoc
287   {
288     if ((businessKey == null) || (publisherID == null))
289       return false;
290
291     boolean authorized = false;
292     PreparedStatement JavaDoc statement = null;
293     ResultSet JavaDoc resultSet = null;
294
295     try
296     {
297       statement = connection.prepareStatement(verifyOwnershipSQL);
298       statement.setString(1,businessKey);
299       statement.setString(2,publisherID);
300
301       log.debug("checking ownership of BUSINESS_ENTITY:\n\n\t" + verifyOwnershipSQL +
302         "\n\t BUSINESS_KEY=" + businessKey +
303         "\n\t PUBLISHER_ID=" + publisherID + "\n");
304
305       resultSet = statement.executeQuery();
306       if (resultSet.next())
307         authorized = true;
308
309       return authorized;
310     }
311     finally
312     {
313       try {
314         resultSet.close();
315         statement.close();
316       }
317       catch (Exception JavaDoc e) { /* ignored */ }
318     }
319   }
320
321   /**
322    * Return the 'publisherID' for the BusinessEntity identified by the
323    * businessKey parameter. Retusn null if the business entity key does
324    * not represent a valid BuisnessEntity or if no publisherID is specified
325    * for that particular BusinessEntity.
326    *
327    * @param businessKey
328    * @param connection
329    * @return publisherID or null if no publisherID is available.
330    * @throws java.sql.SQLException
331    */

332   public static String JavaDoc selectPublisherID(String JavaDoc businessKey,Connection JavaDoc connection)
333     throws java.sql.SQLException JavaDoc
334   {
335     if (businessKey == null)
336       return null;
337
338     String JavaDoc publisherID = null;
339     PreparedStatement JavaDoc statement = null;
340     ResultSet JavaDoc resultSet = null;
341
342     try
343     {
344       statement = connection.prepareStatement(selectPublisherSQL);
345       statement.setString(1,businessKey);
346
347       log.debug("fetching publishers ID for BUSINESS_ENTITY:\n\n\t" + selectPublisherSQL +
348         "\n\t BUSINESS_KEY=" + businessKey + "\n");
349
350       resultSet = statement.executeQuery();
351       if (resultSet.next())
352         publisherID = resultSet.getString(1);//("PUBLISHER_ID");
353

354       return publisherID;
355     }
356     finally
357     {
358       try {
359         resultSet.close();
360         statement.close();
361       }
362       catch (Exception JavaDoc e) { /* ignored */ }
363     }
364   }
365 }
366
Popular Tags