View Javadoc

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;
19  import java.sql.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.util.Vector;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.juddi.datatype.KeyedReference;
26  import org.apache.juddi.registry.RegistryEngine;
27  import org.apache.juddi.util.Config;
28  
29  /***
30   * @author Steve Viens (sviens@apache.org)
31   */
32  class ServiceCategoryTable
33  {
34    // private reference to the jUDDI logger
35    private static Log log = LogFactory.getLog(ServiceCategoryTable.class);
36  
37    static String insertSQL = null;
38    static String selectSQL = null;
39    static String deleteSQL = null;
40    static String tablePrefix = "";
41    
42    static
43    {
44     tablePrefix = Config.getStringProperty(
45         RegistryEngine.PROPNAME_TABLE_PREFIX,RegistryEngine.DEFAULT_TABLE_PREFIX);
46      // buffer used to build SQL statements
47      StringBuffer sql = null;
48  
49      // build insertSQL
50      sql = new StringBuffer(150);
51      sql.append("INSERT INTO ").append(tablePrefix).append("SERVICE_CATEGORY (");
52      sql.append("SERVICE_KEY,");
53      sql.append("CATEGORY_ID,");
54      sql.append("TMODEL_KEY_REF,");
55      sql.append("KEY_NAME,");
56      sql.append("KEY_VALUE) ");
57      sql.append("VALUES (?,?,?,?,?)");
58      insertSQL = sql.toString();
59  
60      // build selectSQL
61      sql = new StringBuffer(200);
62      sql.append("SELECT ");
63      sql.append("TMODEL_KEY_REF,");
64      sql.append("KEY_NAME,");
65      sql.append("KEY_VALUE, ");
66      sql.append("CATEGORY_ID ");
67      sql.append("FROM ").append(tablePrefix).append("SERVICE_CATEGORY ");
68      sql.append("WHERE SERVICE_KEY=? ");
69      sql.append("ORDER BY CATEGORY_ID");
70      selectSQL = sql.toString();
71  
72      // build deleteSQL
73      sql = new StringBuffer(100);
74      sql.append("DELETE FROM ").append(tablePrefix).append("SERVICE_CATEGORY ");
75      sql.append("WHERE SERVICE_KEY=?");
76      deleteSQL = sql.toString();
77    }
78  
79    /***
80     * Insert new row into the SERVICE_CATEGORY table.
81     *
82     * @param serviceKey String to the parent BusinessService object.
83     * @param keyRefs A Vector of KeyedReference instances to insert.
84     * @param connection JDBC connection
85     * @throws java.sql.SQLException
86     */
87    public static void insert(
88      String serviceKey,
89      Vector keyRefs,
90      Connection connection)
91      throws java.sql.SQLException
92    {
93      PreparedStatement statement = null;
94  
95      try
96      {
97        statement = connection.prepareStatement(insertSQL);
98        statement.setString(1, serviceKey.toString());
99  
100       int listSize = keyRefs.size();
101       for (int categoryID = 0; categoryID < listSize; categoryID++)
102       {
103         KeyedReference keyRef = (KeyedReference) keyRefs.elementAt(categoryID);
104 
105         // extract values to insert
106         String tModelKeyValue = null;
107         if (keyRef.getTModelKey() != null)
108           tModelKeyValue = keyRef.getTModelKey().toString();
109 
110         // set the values
111         statement.setInt(2, categoryID);
112         statement.setString(3, tModelKeyValue);
113         statement.setString(4, keyRef.getKeyName());
114         statement.setString(5, keyRef.getKeyValue());
115 
116         if (log.isDebugEnabled()) {
117             log.debug(
118               "insert into " + tablePrefix + "SERVICE_CATEGORY table:\n\n\t"
119                 + insertSQL
120                 + "\n\t SERVICE_KEY="
121                 + serviceKey.toString()
122                 + "\n\t CATEGORY_ID="
123                 + categoryID
124                 + "\n\t TMODEL_KEY_REF="
125                 + tModelKeyValue
126                 + "\n\t KEY_NAME="
127                 + keyRef.getKeyName()
128                 + "\n\t KEY_VALUE="
129                 + keyRef.getKeyValue()
130                 + "\n");
131         }
132 
133         // insert
134         statement.executeUpdate();
135       }
136     }
137     finally
138     {
139       try
140       {
141         statement.close();
142       }
143       catch (Exception e)
144       { /* ignored */
145       }
146     }
147   }
148 
149   /***
150    * Select all rows from the SERVICE_CATEGORY table for a given ServiceKey.
151    *
152    * @param  serviceKey String
153    * @param  connection JDBC connection
154    * @throws java.sql.SQLException
155    */
156   public static Vector select(String serviceKey, Connection connection)
157     throws java.sql.SQLException
158   {
159     Vector keyRefList = new Vector();
160     PreparedStatement statement = null;
161     ResultSet resultSet = null;
162 
163     try
164     {
165       // create a statement to query with
166       statement = connection.prepareStatement(selectSQL);
167       statement.setString(1, serviceKey.toString());
168 
169       if (log.isDebugEnabled()) {
170           log.debug(
171             "select from " + tablePrefix + "SERVICE_CATEGORY table:\n\n\t"
172               + selectSQL
173               + "\n\t SERVICE_KEY="
174               + serviceKey.toString()
175               + "\n");
176       }
177 
178       // execute the statement
179       resultSet = statement.executeQuery();
180       while (resultSet.next())
181       {
182         KeyedReference keyRef = new KeyedReference();
183         keyRef.setTModelKey(resultSet.getString(1));//("TMODEL_KEY_REF"));
184         keyRef.setKeyName(resultSet.getString(2));//("KEY_NAME"));
185         keyRef.setKeyValue(resultSet.getString(3));//("KEY_VALUE"));
186         keyRefList.add(keyRef);
187       }
188 
189       return keyRefList;
190     }
191     finally
192     {
193       try
194       {
195         resultSet.close();
196         statement.close();
197       }
198       catch (Exception e)
199       { /* ignored */
200       }
201     }
202   }
203 
204   /***
205    * Delete multiple rows from the SERVICE_CATEGORY table that are assigned to the
206    * ServiceKey specified.
207    *
208    * @param  serviceKey String
209    * @param  connection JDBC connection
210    * @throws java.sql.SQLException
211    */
212   public static void delete(String serviceKey, Connection connection)
213     throws java.sql.SQLException
214   {
215     PreparedStatement statement = null;
216 
217     try
218     {
219       // prepare the delete
220       statement = connection.prepareStatement(deleteSQL);
221       statement.setString(1, serviceKey.toString());
222 
223       if (log.isDebugEnabled()) {
224           log.debug(
225             "delete from " + tablePrefix + "SERVICE_CATEGORY table:\n\n\t"
226               + deleteSQL
227               + "\n\t SERVICE_KEY="
228               + serviceKey.toString()
229               + "\n");
230       }
231 
232       // execute
233       statement.executeUpdate();
234     }
235     finally
236     {
237       try
238       {
239         statement.close();
240       }
241       catch (Exception e)
242       { /* ignored */
243       }
244     }
245   }
246 }