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 TModelIdentifierTable
33  {
34    // private reference to the jUDDI logger
35    private static Log log = LogFactory.getLog(TModelIdentifierTable.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("TMODEL_IDENTIFIER (");
52      sql.append("TMODEL_KEY,");
53      sql.append("IDENTIFIER_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("IDENTIFIER_ID ");
67      sql.append("FROM ").append(tablePrefix).append("TMODEL_IDENTIFIER ");
68      sql.append("WHERE TMODEL_KEY=? ");
69      sql.append("ORDER BY IDENTIFIER_ID");
70      selectSQL = sql.toString();
71  
72      // build deleteSQL
73      sql = new StringBuffer(100);
74      sql.append("DELETE FROM ").append(tablePrefix).append("TMODEL_IDENTIFIER ");
75      sql.append("WHERE TMODEL_KEY=?");
76      deleteSQL = sql.toString();
77    }
78  
79    /***
80     * Insert new row into the TMODEL_IDENTIFIER table.
81     *
82     * @param tModelKey String to the parent TModelEntity 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 tModelKey,
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, tModelKey.toString());
99  
100       int listSize = keyRefs.size();
101       for (int identifierID = 0; identifierID < listSize; identifierID++)
102       {
103         KeyedReference keyRef =
104           (KeyedReference) keyRefs.elementAt(identifierID);
105 
106         // extract values to insert
107         String tModelKeyValue = null;
108         if (keyRef.getTModelKey() != null)
109           tModelKeyValue = keyRef.getTModelKey().toString();
110 
111         // set the values
112         statement.setInt(2, identifierID);
113         statement.setString(3, tModelKeyValue);
114         statement.setString(4, keyRef.getKeyName());
115         statement.setString(5, keyRef.getKeyValue());
116 
117         if (log.isDebugEnabled()) {
118             log.debug(
119               "insert into " + tablePrefix + "TMODEL_IDENTIFIER table:\n\n\t"
120                 + insertSQL
121                 + "\n\t BUSINESS_KEY="
122                 + tModelKey.toString()
123                 + "\n\t IDENTIFIER_ID="
124                 + identifierID
125                 + "\n\t TMODEL_KEY_REF="
126                 + tModelKeyValue
127                 + "\n\t KEY_NAME="
128                 + keyRef.getKeyName()
129                 + "\n\t KEY_VALUE="
130                 + keyRef.getKeyValue()
131                 + "\n");
132         }
133 
134         // insert!
135         statement.executeUpdate();
136       }
137     }
138     finally
139     {
140       try
141       {
142         statement.close();
143       }
144       catch (Exception e)
145       { /* ignored */
146       }
147     }
148   }
149 
150   /***
151    * Select all rows from the TMODEL_IDENTIFIER table for a given TModelKey.
152    *
153    * @param  tModelKey String
154    * @param  connection JDBC connection
155    * @throws java.sql.SQLException
156    */
157   public static Vector select(String tModelKey, Connection connection)
158     throws java.sql.SQLException
159   {
160     Vector keyRefList = new Vector();
161     PreparedStatement statement = null;
162     ResultSet resultSet = null;
163 
164     try
165     {
166       // create a statement to query with
167       statement = connection.prepareStatement(selectSQL);
168       statement.setString(1, tModelKey.toString());
169 
170       if (log.isDebugEnabled()) {
171           log.debug(
172             "select from " + tablePrefix + "TMODEL_IDENTIFIER table:\n\n\t"
173               + selectSQL
174               + "\n\t TMODEL_KEY="
175               + tModelKey.toString()
176               + "\n");
177       }
178 
179       // execute the statement
180       resultSet = statement.executeQuery();
181       while (resultSet.next())
182       {
183         KeyedReference 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       }
189 
190       return keyRefList;
191     }
192     finally
193     {
194       try
195       {
196         resultSet.close();
197         statement.close();
198       }
199       catch (Exception e)
200       { /* ignored */
201       }
202     }
203   }
204 
205   /***
206    * Delete multiple rows from the TMODEL_IDENTIFIER table that are assigned to the
207    * TModelKey specified.
208    *
209    * @param  tModelKey String
210    * @param  connection JDBC connection
211    * @throws java.sql.SQLException
212    */
213   public static void delete(String tModelKey, Connection connection)
214     throws java.sql.SQLException
215   {
216     PreparedStatement statement = null;
217 
218     try
219     {
220       // prepare
221       statement = connection.prepareStatement(deleteSQL);
222       statement.setString(1, tModelKey.toString());
223 
224       if (log.isDebugEnabled()) {
225           log.debug(
226             "delete from " + tablePrefix + "TMODEL_IDENTIFIER table:\n\n\t"
227               + deleteSQL
228               + "\n\t TMODEL_KEY="
229               + tModelKey.toString()
230               + "\n");
231       }
232 
233       // execute
234       statement.executeUpdate();
235     }
236     finally
237     {
238       try
239       {
240         statement.close();
241       }
242       catch (Exception e)
243       { /* ignored */
244       }
245     }
246   }
247 }