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