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 BusinessCategoryTable
33  {
34    // private reference to the jUDDI logger
35    private static Log log = LogFactory.getLog(BusinessCategoryTable.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("BUSINESS_CATEGORY (");
51      sql.append("BUSINESS_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("BUSINESS_CATEGORY ");
67      sql.append("WHERE BUSINESS_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("BUSINESS_CATEGORY ");
74      sql.append("WHERE BUSINESS_KEY=?");
75      deleteSQL = sql.toString();
76    }
77  
78    /***
79     * Insert new row into the BUSINESS_CATEGORY table.
80     *
81     * @param businessKey BusinessKey to the parent BusinessEntity 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 businessKey,
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, businessKey.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 + "BUSINESS_CATEGORY table:\n\n\t"
118                 + insertSQL
119                 + "\n\t BUSINESS_KEY="
120                 + businessKey.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         // execute
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 BUSINESS_CATEGORY table for a given BusinessKey.
150    *
151    * @param  businessKey BusinessKey
152    * @param  connection JDBC connection
153    * @throws java.sql.SQLException
154    */
155   public static Vector select(String businessKey, 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, businessKey.toString());
167       
168       if (log.isDebugEnabled()) {
169           log.debug(
170             "select from " + tablePrefix + "BUSINESS_CATEGORY table:\n\n\t"
171               + selectSQL
172               + "\n\t BUSINESS_KEY="
173               + businessKey.toString()
174               + "\n");
175       }
176 
177       // execute the statement
178       resultSet = statement.executeQuery();
179 
180       KeyedReference keyRef = null;
181       while (resultSet.next())
182       {
183         keyRef = new KeyedReference();
184         keyRef.setTModelKey(resultSet.getString(1));//("TMODEL_KEY_REF"));
185         keyRef.setKeyName(resultSet.getString(2));//("KEY_NAME"));
186         keyRef.setKeyValue(resultSet.getString(3));//("KEY_VALUE"));
187         keyRefList.add(keyRef);
188         keyRef = null;
189       }
190 
191       return keyRefList;
192     }
193     finally
194     {
195       try
196       {
197         resultSet.close();
198         statement.close();
199       }
200       catch (Exception e)
201       { /* ignored */
202       }
203     }
204   }
205 
206   /***
207    * Delete multiple rows from the BUSINESS_CATEGORY table that are assigned to the
208    * BusinessKey specified.
209    *
210    * @param  businessKey BusinessKey
211    * @param  connection JDBC connection
212    * @throws java.sql.SQLException
213    */
214   public static void delete(String businessKey, Connection connection)
215     throws java.sql.SQLException
216   {
217     PreparedStatement statement = null;
218 
219     try
220     {
221       // prepare the delete
222       statement = connection.prepareStatement(deleteSQL);
223       statement.setString(1, businessKey.toString());
224 
225       if (log.isDebugEnabled()) {
226           log.debug(
227             "delete from " + tablePrefix + "BUSINESS_CATEGORY table:\n\n\t"
228               + deleteSQL
229               + "\n\t BUSINESS_KEY="
230               + businessKey.toString()
231               + "\n");
232       }
233 
234       // execute
235       statement.executeUpdate();
236     }
237     finally
238     {
239       try
240       {
241         statement.close();
242       }
243       catch (Exception e)
244       { /* ignored */
245       }
246     }
247   }
248 }