KickJava   Java API By Example, From Geeks To Geeks.

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


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

30 class ServiceDescTable
31 {
32   // private reference to the jUDDI logger
33
private static Log log = LogFactory.getLog(ServiceDescTable.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 SERVICE_DESCR (");
46     sql.append("SERVICE_KEY,");
47     sql.append("SERVICE_DESCR_ID,");
48     sql.append("LANG_CODE,");
49     sql.append("DESCR) ");
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("LANG_CODE,");
57     sql.append("DESCR, ");
58     sql.append("SERVICE_DESCR_ID ");
59     sql.append("FROM SERVICE_DESCR ");
60     sql.append("WHERE SERVICE_KEY=? ");
61     sql.append("ORDER BY SERVICE_DESCR_ID");
62     selectSQL = sql.toString();
63
64     // build deleteSQL
65
sql = new StringBuffer JavaDoc(100);
66     sql.append("DELETE FROM SERVICE_DESCR ");
67     sql.append("WHERE SERVICE_KEY=?");
68     deleteSQL = sql.toString();
69   }
70
71   /**
72    * Insert new row into the SERVICE_DESCR table.
73    *
74    * @param serviceKey String to the BusinessEntity object that owns the Description to be inserted
75    * @param descList Vector of Description objects holding values to be inserted
76    * @param connection JDBC connection
77    * @throws java.sql.SQLException
78    */

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

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

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

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