KickJava   Java API By Example, From Geeks To Geeks.

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


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

30 class BusinessIdentifierTable
31 {
32   // private reference to the jUDDI logger
33
private static Log log = LogFactory.getLog(BusinessIdentifierTable.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 BUSINESS_IDENTIFIER (");
46     sql.append("BUSINESS_KEY,");
47     sql.append("IDENTIFIER_ID,");
48     sql.append("TMODEL_KEY_REF,");
49     sql.append("KEY_NAME,");
50     sql.append("KEY_VALUE) ");
51     sql.append("VALUES (?,?,?,?,?)");
52     insertSQL = sql.toString();
53
54     // build selectSQL
55
sql = new StringBuffer JavaDoc(200);
56     sql.append("SELECT ");
57     sql.append("TMODEL_KEY_REF,");
58     sql.append("KEY_NAME,");
59     sql.append("KEY_VALUE, ");
60     sql.append("IDENTIFIER_ID ");
61     sql.append("FROM BUSINESS_IDENTIFIER ");
62     sql.append("WHERE BUSINESS_KEY=? ");
63     sql.append("ORDER BY IDENTIFIER_ID");
64     selectSQL = sql.toString();
65
66     // build deleteSQL
67
sql = new StringBuffer JavaDoc(100);
68     sql.append("DELETE FROM BUSINESS_IDENTIFIER ");
69     sql.append("WHERE BUSINESS_KEY=?");
70     deleteSQL = sql.toString();
71   }
72
73   /**
74    * Insert new row into the BUSINESS_IDENTIFIER table.
75    *
76    * @param businessKey BusinessKey to the parent BusinessEntity object.
77    * @param keyRefs A Vector of KeyedReference instances to insert.
78    * @param connection JDBC connection
79    * @throws java.sql.SQLException
80    */

81   public static void insert(
82     String JavaDoc businessKey,
83     Vector JavaDoc keyRefs,
84     Connection JavaDoc connection)
85     throws java.sql.SQLException JavaDoc
86   {
87     PreparedStatement JavaDoc statement = null;
88
89     try
90     {
91       statement = connection.prepareStatement(insertSQL);
92       statement.setString(1, businessKey.toString());
93
94       int listSize = keyRefs.size();
95       for (int identifierID = 0; identifierID < listSize; identifierID++)
96       {
97         KeyedReference keyRef =
98           (KeyedReference) keyRefs.elementAt(identifierID);
99
100         // extract values to insert
101
String JavaDoc tModelKeyValue = null;
102         if (keyRef.getTModelKey() != null)
103           tModelKeyValue = keyRef.getTModelKey().toString();
104
105         // set the values
106
statement.setInt(2, identifierID);
107         statement.setString(3, tModelKeyValue);
108         statement.setString(4, keyRef.getKeyName());
109         statement.setString(5, keyRef.getKeyValue());
110
111         log.debug(
112           "insert into BUSINESS_IDENTIFIER table:\n\n\t"
113             + insertSQL
114             + "\n\t BUSINESS_KEY="
115             + businessKey.toString()
116             + "\n\t IDENTIFIER_ID="
117             + identifierID
118             + "\n\t TMODEL_KEY_REF="
119             + tModelKeyValue
120             + "\n\t KEY_NAME="
121             + keyRef.getKeyName()
122             + "\n\t KEY_VALUE="
123             + keyRef.getKeyValue()
124             + "\n");
125
126         // execute
127
statement.executeUpdate();
128       }
129     }
130     finally
131     {
132       try
133       {
134         statement.close();
135       }
136       catch (Exception JavaDoc e)
137       { /* ignored */
138       }
139     }
140   }
141
142   /**
143    * Select all rows from the BUSINESS_IDENTIFIER table for a given BusinessKey.
144    *
145    * @param businessKey BusinessKey
146    * @param connection JDBC connection
147    * @throws java.sql.SQLException
148    */

149   public static Vector JavaDoc select(String JavaDoc businessKey, Connection JavaDoc connection)
150     throws java.sql.SQLException JavaDoc
151   {
152     Vector JavaDoc keyRefList = new Vector JavaDoc();
153     PreparedStatement JavaDoc statement = null;
154     ResultSet JavaDoc resultSet = null;
155
156     try
157     {
158       // create a statement to query with
159
statement = connection.prepareStatement(selectSQL);
160       statement.setString(1, businessKey.toString());
161
162       log.debug(
163         "select from BUSINESS_IDENTIFIER table:\n\n\t"
164           + selectSQL
165           + "\n\t BUSINESS_KEY="
166           + businessKey.toString()
167           + "\n");
168
169       // execute the statement
170
resultSet = statement.executeQuery();
171       while (resultSet.next())
172       {
173         KeyedReference keyRef = new KeyedReference();
174         keyRef.setTModelKey(resultSet.getString(1));//("TMODEL_KEY_REF"));
175
keyRef.setKeyName(resultSet.getString(2));//("KEY_NAME"));
176
keyRef.setKeyValue(resultSet.getString(3));//("KEY_VALUE"));
177
keyRefList.add(keyRef);
178       }
179
180       return keyRefList;
181     }
182     finally
183     {
184       try
185       {
186         resultSet.close();
187         statement.close();
188       }
189       catch (Exception JavaDoc e)
190       { /* ignored */
191       }
192     }
193   }
194
195   /**
196    * Delete multiple rows from the BUSINESS_IDENTIFIER table that are assigned
197    * to the BusinessKey specified.
198    *
199    * @param businessKey BusinessKey
200    * @param connection JDBC connection
201    * @throws java.sql.SQLException
202    */

203   public static void delete(String JavaDoc businessKey, Connection JavaDoc connection)
204     throws java.sql.SQLException JavaDoc
205   {
206     PreparedStatement JavaDoc statement = null;
207
208     try
209     {
210       // prepare the delete
211
statement = connection.prepareStatement(deleteSQL);
212       statement.setString(1, businessKey.toString());
213
214       log.debug(
215         "delete from BUSINESS_IDENTIFIER table:\n\n\t"
216           + deleteSQL
217           + "\n\t BUSINESS_KEY="
218           + businessKey.toString()
219           + "\n");
220
221       // execute
222
statement.executeUpdate();
223     }
224     finally
225     {
226       try
227       {
228         statement.close();
229       }
230       catch (Exception JavaDoc e)
231       { /* ignored */
232       }
233     }
234   }
235 }
236
Popular Tags