KickJava   Java API By Example, From Geeks To Geeks.

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


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.service.BusinessService;
27
28 /**
29  * @author Steve Viens (sviens@apache.org)
30  */

31 class BusinessServiceTable
32 {
33   // private reference to the jUDDI logger
34
private static Log log = LogFactory.getLog(BusinessServiceTable.class);
35
36   static String JavaDoc insertSQL = null;
37   static String JavaDoc deleteSQL = null;
38   static String JavaDoc selectSQL = null;
39   static String JavaDoc selectByBusinessKeySQL = null;
40   static String JavaDoc deleteByBusinessKeySQL = null;
41   static String JavaDoc verifyOwnershipSQL = 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(100);
50     sql.append("INSERT INTO BUSINESS_SERVICE (");
51     sql.append("BUSINESS_KEY,");
52     sql.append("SERVICE_KEY,");
53     sql.append("LAST_UPDATE) ");
54     sql.append("VALUES (?,?,?)");
55     insertSQL = sql.toString();
56
57     // build deleteSQL
58
sql = new StringBuffer JavaDoc(100);
59     sql.append("DELETE FROM BUSINESS_SERVICE ");
60     sql.append("WHERE SERVICE_KEY=?");
61     deleteSQL = sql.toString();
62
63     // build selectSQL
64
sql = new StringBuffer JavaDoc(100);
65     sql.append("SELECT ");
66     sql.append("BUSINESS_KEY ");
67     sql.append("FROM BUSINESS_SERVICE ");
68     sql.append("WHERE SERVICE_KEY=?");
69     selectSQL = sql.toString();
70
71     // build selectByBusinessKeySQL
72
sql = new StringBuffer JavaDoc(200);
73     sql.append("SELECT ");
74     sql.append("SERVICE_KEY ");
75     sql.append("FROM BUSINESS_SERVICE ");
76     sql.append("WHERE BUSINESS_KEY=?");
77     selectByBusinessKeySQL = sql.toString();
78
79     // build deleteByBusinessKeySQL
80
sql = new StringBuffer JavaDoc(100);
81     sql.append("DELETE FROM BUSINESS_SERVICE ");
82     sql.append("WHERE BUSINESS_KEY=?");
83     deleteByBusinessKeySQL = sql.toString();
84
85     // build verifyOwnershipSQL
86
sql = new StringBuffer JavaDoc(200);
87     sql.append("SELECT ");
88     sql.append("* ");
89     sql.append("FROM BUSINESS_ENTITY e, BUSINESS_SERVICE s ");
90     sql.append("WHERE e.BUSINESS_KEY = s.BUSINESS_KEY ");
91     sql.append("AND s.SERVICE_KEY=? ");
92     sql.append("AND e.PUBLISHER_ID=?");
93     verifyOwnershipSQL = sql.toString();
94   }
95
96   /**
97    * Insert new row into the BUSINESS_ENTITIES table.
98    *
99    * @param service object holding values to be inserted
100    * @param connection JDBC connection
101    * @throws java.sql.SQLException
102    */

103   public static void insert(BusinessService service,Connection JavaDoc connection)
104     throws java.sql.SQLException JavaDoc
105   {
106     PreparedStatement JavaDoc statement = null;
107     Timestamp JavaDoc timeStamp = new Timestamp JavaDoc(System.currentTimeMillis());
108
109     try
110     {
111       statement = connection.prepareStatement(insertSQL);
112       statement.setString(1,service.getBusinessKey().toString());
113       statement.setString(2,service.getServiceKey().toString());
114       statement.setTimestamp(3,timeStamp);
115
116       log.debug("insert into BUSINESS_SERVICE table:\n\n\t" + insertSQL +
117         "\n\t BUSINESS_KEY=" + service.getBusinessKey().toString() +
118         "\n\t SERVICE_KEY=" + service.getServiceKey().toString() +
119         "\n\t LAST_UPDATE=" + timeStamp.getTime() + "\n");
120
121       statement.executeUpdate();
122     }
123     finally
124     {
125       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
126     }
127   }
128
129   /**
130    * Delete row from the BUSINESS_SERVICE table.
131    *
132    * @param serviceKey Primary key value
133    * @param connection JDBC connection
134    * @throws java.sql.SQLException
135    */

136   public static void delete(String JavaDoc serviceKey,Connection JavaDoc connection)
137     throws java.sql.SQLException JavaDoc
138   {
139     PreparedStatement JavaDoc statement = null;
140
141     try
142     {
143       // prepare
144
statement = connection.prepareStatement(deleteSQL);
145       statement.setString(1,serviceKey.toString());
146
147       log.debug("delete from BUSINESS_SERVICE table:\n\n\t" + deleteSQL +
148         "\n\t SERVICE_KEY=" + serviceKey.toString() + "\n");
149
150       // execute
151
statement.executeUpdate();
152     }
153     finally
154     {
155       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
156     }
157   }
158
159   /**
160    * Select one row from the BUSINESS_SERVICE table.
161    *
162    * @param serviceKey primary key value
163    * @param connection JDBC connection
164    * @throws java.sql.SQLException
165    */

166   public static BusinessService select(String JavaDoc serviceKey,Connection JavaDoc connection)
167     throws java.sql.SQLException JavaDoc
168   {
169     BusinessService service = null;
170     PreparedStatement JavaDoc statement = null;
171     ResultSet JavaDoc resultSet = null;
172
173     try
174     {
175       statement = connection.prepareStatement(selectSQL);
176       statement.setString(1,serviceKey.toString());
177
178       log.debug("select from BUSINESS_SERVICE table:\n\n\t" + selectSQL +
179         "\n\t SERVICE_KEY=" + serviceKey.toString() + "\n");
180
181       resultSet = statement.executeQuery();
182       if (resultSet.next())
183       {
184         service = new BusinessService();
185         service.setBusinessKey(resultSet.getString(1));//("BUSINESS_KEY"));
186
service.setServiceKey(serviceKey);
187       }
188
189       return service;
190     }
191     finally
192     {
193       try { resultSet.close(); } catch (Exception JavaDoc e) { /* ignored */ }
194       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
195     }
196   }
197
198   /**
199    * Delete multiple rows from the BUSINESS_SERVICE table that are assigned to
200    * the BusinessKey specified.
201    *
202    * @param businessKey BusinessKey
203    * @param connection JDBC connection
204    * @throws java.sql.SQLException
205    */

206   public static void deleteByBusinessKey(String JavaDoc businessKey,Connection JavaDoc connection)
207     throws java.sql.SQLException JavaDoc
208   {
209     PreparedStatement JavaDoc statement = null;
210
211     try
212     {
213       // prepare the delete
214
statement = connection.prepareStatement(deleteByBusinessKeySQL);
215       statement.setString(1,businessKey.toString());
216
217       log.debug("delet from the BUSINESS_SERVICE table:\n\n\t" + deleteByBusinessKeySQL +
218         "\n\t BUSINESS_KEY=" + businessKey.toString() + "\n");
219
220       // execute
221
statement.executeUpdate();
222     }
223     finally
224     {
225       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
226     }
227   }
228
229   /**
230    * Select all rows from the business_service table for
231    * a given BusinessKey.
232    *
233    * @param businessKey BusinessKey
234    * @param connection JDBC connection
235    * @throws java.sql.SQLException
236    */

237   public static Vector JavaDoc selectByBusinessKey(String JavaDoc businessKey,Connection JavaDoc connection)
238     throws java.sql.SQLException JavaDoc
239   {
240     Vector JavaDoc serviceList = new Vector JavaDoc();
241     PreparedStatement JavaDoc statement = null;
242     ResultSet JavaDoc resultSet = null;
243
244     try
245     {
246      // create a statement to query with
247
statement = connection.prepareStatement(selectByBusinessKeySQL);
248       statement.setString(1,businessKey.toString());
249
250       log.debug("select from BUSINESS_SERVICE table:\n\n\t" + selectByBusinessKeySQL +
251         "\n\t BUSINESS_KEY=" + businessKey.toString() + "\n");
252
253       // execute the statement
254
resultSet = statement.executeQuery();
255       while (resultSet.next())
256       {
257         BusinessService service = new BusinessService();
258         service.setBusinessKey(businessKey);
259         service.setServiceKey(resultSet.getString(1));//("SERVICE_KEY"));
260
serviceList.add(service);
261       }
262
263       return serviceList;
264     }
265     finally
266     {
267       try { resultSet.close(); } catch (Exception JavaDoc e) { /* ignored */ }
268       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
269     }
270   }
271
272   /**
273    * Verify that 'authorizedName' has the authority to update or delete
274    * BusinessService identified by the serviceKey parameter
275    *
276    * @param serviceKey
277    * @param publisherID
278    * @param connection
279    * @throws java.sql.SQLException
280    */

281   public static boolean verifyOwnership(String JavaDoc serviceKey,String JavaDoc publisherID,Connection JavaDoc connection)
282     throws java.sql.SQLException JavaDoc
283   {
284     if ((serviceKey == null) || (publisherID == null))
285       return false;
286
287     boolean authorized = false;
288     PreparedStatement JavaDoc statement = null;
289     ResultSet JavaDoc resultSet = null;
290
291     try
292     {
293       statement = connection.prepareStatement(verifyOwnershipSQL);
294       statement.setString(1,serviceKey);
295       statement.setString(2,publisherID);
296
297       log.debug("checking ownership of BUSINESS_SERVICE:\n\n\t" + verifyOwnershipSQL +
298         "\n\t SERVICE_KEY=" + serviceKey +
299         "\n\t PUBLISHER_ID=" + publisherID + "\n");
300
301       resultSet = statement.executeQuery();
302       if (resultSet.next())
303         authorized = true;
304
305       return authorized;
306     }
307     finally
308     {
309       try { resultSet.close(); } catch (Exception JavaDoc e) { /* ignored */ }
310       try { statement.close(); } catch (Exception JavaDoc e) { /* ignored */ }
311     }
312   }
313 }
314
Popular Tags