KickJava   Java API By Example, From Geeks To Geeks.

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


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 BusinessCategoryTable
31 {
32   // private reference to the jUDDI logger
33
private static Log log = LogFactory.getLog(BusinessCategoryTable.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_CATEGORY (");
46     sql.append("BUSINESS_KEY,");
47     sql.append("CATEGORY_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("CATEGORY_ID ");
61     sql.append("FROM BUSINESS_CATEGORY ");
62     sql.append("WHERE BUSINESS_KEY=? ");
63     sql.append("ORDER BY CATEGORY_ID");
64     selectSQL = sql.toString();
65
66     // build deleteSQL
67
sql = new StringBuffer JavaDoc(100);
68     sql.append("DELETE FROM BUSINESS_CATEGORY ");
69     sql.append("WHERE BUSINESS_KEY=?");
70     deleteSQL = sql.toString();
71   }
72
73   /**
74    * Insert new row into the BUSINESS_CATEGORY 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 categoryID = 0; categoryID < listSize; categoryID++)
96       {
97         KeyedReference keyRef = (KeyedReference) keyRefs.elementAt(categoryID);
98
99         // extract values to insert
100
String JavaDoc tModelKeyValue = null;
101         if (keyRef.getTModelKey() != null)
102           tModelKeyValue = keyRef.getTModelKey().toString();
103
104         // set the values
105
statement.setInt(2, categoryID);
106         statement.setString(3, tModelKeyValue);
107         statement.setString(4, keyRef.getKeyName());
108         statement.setString(5, keyRef.getKeyValue());
109
110         log.debug(
111           "insert into BUSINESS_CATEGORY table:\n\n\t"
112             + insertSQL
113             + "\n\t BUSINESS_KEY="
114             + businessKey.toString()
115             + "\n\t CATEGORY_ID="
116             + categoryID
117             + "\n\t TMODEL_KEY_REF="
118             + tModelKeyValue
119             + "\n\t KEY_NAME="
120             + keyRef.getKeyName()
121             + "\n\t KEY_VALUE="
122             + keyRef.getKeyValue()
123             + "\n");
124
125         // execute
126
statement.executeUpdate();
127       }
128     }
129     finally
130     {
131       try
132       {
133         statement.close();
134       }
135       catch (Exception JavaDoc e)
136       { /* ignored */
137       }
138     }
139   }
140
141   /**
142    * Select all rows from the BUSINESS_CATEGORY table for a given BusinessKey.
143    *
144    * @param businessKey BusinessKey
145    * @param connection JDBC connection
146    * @throws java.sql.SQLException
147    */

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

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