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 BusinessIdentifierTable
33  {
34    // private reference to the jUDDI logger
35    private static Log log = LogFactory.getLog(BusinessIdentifierTable.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_IDENTIFIER (");
51      sql.append("BUSINESS_KEY,");
52      sql.append("IDENTIFIER_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("IDENTIFIER_ID ");
66      sql.append("FROM ").append(tablePrefix).append("BUSINESS_IDENTIFIER ");
67      sql.append("WHERE BUSINESS_KEY=? ");
68      sql.append("ORDER BY IDENTIFIER_ID");
69      selectSQL = sql.toString();
70  
71      // build deleteSQL
72      sql = new StringBuffer(100);
73      sql.append("DELETE FROM ").append(tablePrefix).append("BUSINESS_IDENTIFIER ");
74      sql.append("WHERE BUSINESS_KEY=?");
75      deleteSQL = sql.toString();
76    }
77  
78    /***
79     * Insert new row into the BUSINESS_IDENTIFIER 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 identifierID = 0; identifierID < listSize; identifierID++)
101       {
102         KeyedReference keyRef =
103           (KeyedReference) keyRefs.elementAt(identifierID);
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, identifierID);
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 + "BUSINESS_IDENTIFIER table:\n\n\t"
119                 + insertSQL
120                 + "\n\t BUSINESS_KEY="
121                 + businessKey.toString()
122                 + "\n\t IDENTIFIER_ID="
123                 + identifierID
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         // execute
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 BUSINESS_IDENTIFIER table for a given BusinessKey.
151    *
152    * @param  businessKey BusinessKey
153    * @param  connection JDBC connection
154    * @throws java.sql.SQLException
155    */
156   public static Vector select(String businessKey, 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, businessKey.toString());
168 
169       if (log.isDebugEnabled()) {
170           log.debug(
171             "select from " + tablePrefix + "BUSINESS_IDENTIFIER table:\n\n\t"
172               + selectSQL
173               + "\n\t BUSINESS_KEY="
174               + businessKey.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 BUSINESS_IDENTIFIER table that are assigned
206    * to the BusinessKey specified.
207    *
208    * @param  businessKey BusinessKey
209    * @param  connection JDBC connection
210    * @throws java.sql.SQLException
211    */
212   public static void delete(String businessKey, 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, businessKey.toString());
222 
223       if (log.isDebugEnabled()) {
224           log.debug(
225             "delete from " + tablePrefix + "BUSINESS_IDENTIFIER table:\n\n\t"
226               + deleteSQL
227               + "\n\t BUSINESS_KEY="
228               + businessKey.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 }