KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Vector JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.juddi.datatype.DiscoveryURL;
26
27 /**
28  * @author Steve Viens (sviens@apache.org)
29  */

30 class DiscoveryURLTable
31 {
32   // private reference to the jUDDI logger
33
private static Log log = LogFactory.getLog(DiscoveryURLTable.class);
34
35   static String JavaDoc insertSQL = null;
36   static String JavaDoc selectSQL = null;
37   static String JavaDoc deleteSQL = null;
38
39   static {
40     // buffer used to build SQL statements
41
StringBuffer JavaDoc sql = null;
42
43     // build insertSQL
44
sql = new StringBuffer JavaDoc(150);
45     sql.append("INSERT INTO DISCOVERY_URL (");
46     sql.append("BUSINESS_KEY,");
47     sql.append("DISCOVERY_URL_ID,");
48     sql.append("USE_TYPE,");
49     sql.append("URL) ");
50     sql.append("VALUES (?,?,?,?)");
51     insertSQL = sql.toString();
52
53     // build selectSQL
54
sql = new StringBuffer JavaDoc(200);
55     sql.append("SELECT ");
56     sql.append("USE_TYPE,");
57     sql.append("URL, ");
58     sql.append("DISCOVERY_URL_ID ");
59     sql.append("FROM DISCOVERY_URL ");
60     sql.append("WHERE BUSINESS_KEY=? ");
61     sql.append("ORDER BY DISCOVERY_URL_ID");
62     selectSQL = sql.toString();
63
64     // build deleteSQL
65
sql = new StringBuffer JavaDoc(100);
66     sql.append("DELETE FROM DISCOVERY_URL ");
67     sql.append("WHERE BUSINESS_KEY=?");
68     deleteSQL = sql.toString();
69   }
70
71   /**
72    * Insert new row into the DISCOVERY_URL table.
73    *
74    * @param businessKey String to the BusinessEntity object that owns
75    * the Description to be inserted
76    * @param urlList Vector of Description objects holding values to be inserted
77    * @param connection JDBC connection
78    * @throws java.sql.SQLException
79    */

80   public static void insert(
81     String JavaDoc businessKey,
82     Vector JavaDoc urlList,
83     Connection JavaDoc connection)
84     throws java.sql.SQLException JavaDoc
85   {
86     if ((urlList == null) || (urlList.size() == 0))
87       return; // everything is valid but no elements to insert
88

89     PreparedStatement JavaDoc statement = null;
90
91     try
92     {
93       statement = connection.prepareStatement(insertSQL);
94       statement.setString(1, businessKey.toString());
95
96       int listSize = urlList.size();
97       for (int urlID = 0; urlID < listSize; urlID++)
98       {
99         DiscoveryURL url = (DiscoveryURL) urlList.elementAt(urlID);
100         String JavaDoc urlValue = url.getValue();
101
102         log.debug(
103           "insert into DISCOVERY_URL table:\n\n\t"
104             + insertSQL
105             + "\n\t BUSINESS_KEY="
106             + businessKey.toString()
107             + "\n\t DISCOVERY_URL_ID="
108             + urlID
109             + "\n\t USE_TYPE="
110             + url.getUseType()
111             + "\n\t URL="
112             + urlValue
113             + "\n");
114
115         statement.setInt(2, urlID);
116         statement.setString(3, url.getUseType());
117         statement.setString(4, urlValue);
118         statement.executeUpdate();
119       }
120     }
121     finally
122     {
123       try
124       {
125         statement.close();
126       }
127       catch (Exception JavaDoc e)
128       { /* ignored */
129       }
130     }
131   }
132
133   /**
134    * Select all rows from the DISCOVERY_URL table for a given BusinessKey.
135    *
136    * @param businessKey String
137    * @param connection JDBC connection
138    * @throws java.sql.SQLException
139    */

140   public static Vector JavaDoc select(String JavaDoc businessKey, Connection JavaDoc connection)
141     throws java.sql.SQLException JavaDoc
142   {
143     Vector JavaDoc urlList = new Vector JavaDoc();
144     PreparedStatement JavaDoc statement = null;
145     ResultSet JavaDoc resultSet = null;
146
147     try
148     {
149       // create a statement to query with
150
statement = connection.prepareStatement(selectSQL);
151       statement.setString(1, businessKey.toString());
152
153       log.debug(
154         "select from DISCOVERY_URL table:\n\n\t"
155           + selectSQL
156           + "\n\t BUSINESS_KEY="
157           + businessKey.toString()
158           + "\n");
159
160       // execute the statement
161
resultSet = statement.executeQuery();
162       while (resultSet.next())
163       {
164         DiscoveryURL url = new DiscoveryURL();
165         url.setUseType(resultSet.getString(1));//("USE_TYPE"));
166
url.setValue(resultSet.getString(2));//("URL"));
167
urlList.add(url);
168       }
169
170       return urlList;
171     }
172     finally
173     {
174       try
175       {
176         resultSet.close();
177         statement.close();
178       }
179       catch (Exception JavaDoc e)
180       { /* ignored */
181       }
182     }
183   }
184
185   /**
186    * Delete multiple rows from the DISCOVERY_URL table that are assigned to the
187    * BusinessKey specified.
188    *
189    * @param businessKey String
190    * @param connection JDBC connection
191    * @throws java.sql.SQLException
192    */

193   public static void delete(String JavaDoc businessKey, Connection JavaDoc connection)
194     throws java.sql.SQLException JavaDoc
195   {
196     PreparedStatement JavaDoc statement = null;
197
198     try
199     {
200       // prepare the delete
201
statement = connection.prepareStatement(deleteSQL);
202       statement.setString(1, businessKey.toString());
203
204       log.debug(
205         "delete from DISCOVERY_URL table:\n\n\t"
206           + deleteSQL
207           + "\n\t BUSINESS_KEY="
208           + businessKey.toString()
209           + "\n");
210
211       // execute
212
statement.executeUpdate();
213     }
214     finally
215     {
216       try
217       {
218         statement.close();
219       }
220       catch (Exception JavaDoc e)
221       { /* ignored */
222       }
223     }
224   }
225 }
226
Popular Tags