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.Description;
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 InstanceDetailsDescTable
33  {
34    // private reference to the jUDDI logger
35    private static Log log = LogFactory.getLog(InstanceDetailsDescTable.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("INSTANCE_DETAILS_DESCR (");
52      sql.append("BINDING_KEY,");
53      sql.append("TMODEL_INSTANCE_INFO_ID,");
54      sql.append("INSTANCE_DETAILS_DESCR_ID,");
55      sql.append("LANG_CODE,");
56      sql.append("DESCR) ");
57      sql.append("VALUES (?,?,?,?,?)");
58      insertSQL = sql.toString();
59  
60      // build selectSQL
61      sql = new StringBuffer(200);
62      sql.append("SELECT ");
63      sql.append("LANG_CODE,");
64      sql.append("DESCR, ");
65      sql.append("INSTANCE_DETAILS_DESCR_ID ");
66      sql.append("FROM ").append(tablePrefix).append("INSTANCE_DETAILS_DESCR ");
67      sql.append("WHERE BINDING_KEY=? ");
68      sql.append("AND TMODEL_INSTANCE_INFO_ID=? ");
69      sql.append("ORDER BY INSTANCE_DETAILS_DESCR_ID");
70      selectSQL = sql.toString();
71  
72      // build deleteSQL
73      sql = new StringBuffer(100);
74      sql.append("DELETE FROM ").append(tablePrefix).append("INSTANCE_DETAILS_DESCR ");
75      sql.append("WHERE BINDING_KEY=?");
76      deleteSQL = sql.toString();
77    }
78  
79    /***
80     * Insert new row into the INSTANCE_DETAILS_DESCR table.
81     *
82     * @param bindingKey String to the BindingTemplate object
83     * @param tModelInstanceInfoID String to the BusinessEntity object that owns the Contact to be inserted
84     * @param descList Vector of Description objects holding values to be inserted
85     * @param connection JDBC connection
86     * @throws java.sql.SQLException
87     */
88    public static void insert(
89      String bindingKey,
90      int tModelInstanceInfoID,
91      Vector descList,
92      Connection connection)
93      throws java.sql.SQLException
94    {
95      if ((descList == null) || (descList.size() == 0))
96        return; // everything is valid but no elements to insert
97  
98      PreparedStatement statement = null;
99  
100     try
101     {
102       statement = connection.prepareStatement(insertSQL);
103       statement.setString(1, bindingKey.toString());
104       statement.setInt(2, tModelInstanceInfoID);
105 
106       int listSize = descList.size();
107       for (int descID = 0; descID < listSize; descID++)
108       {
109         Description desc = (Description) descList.elementAt(descID);
110 
111         // okay, set the values to be inserted
112         statement.setInt(3, descID); // Sequence Number aka Desc ID
113         statement.setString(4, desc.getLanguageCode());
114         statement.setString(5, desc.getValue());
115 
116         if (log.isDebugEnabled()) {
117             log.debug(
118               "insert into " + tablePrefix + "INSTANCE_DETAILS_DESCR table:\n\n\t"
119                 + insertSQL
120                 + "\n\t BINDING_KEY="
121                 + bindingKey.toString()
122                 + "\n\t TMODEL_INSTANCE_INFO_ID="
123                 + tModelInstanceInfoID
124                 + "\n\t INSTANCE_DETAILS_DESCR_ID="
125                 + descID
126                 + "\n\t LANG_CODE="
127                 + desc.getLanguageCode()
128                 + "\n\t DESCR="
129                 + desc.getValue()
130                 + "\n");
131         }
132 
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 TMODEL_INST_INFO table for a given BusinessKey.
150    *
151    * @param  bindingKey String
152    * @param  tModelInstanceInfoID ID (sequence number) of the parent TModelInstanceInfo object
153    * @param  connection JDBC connection
154    * @throws java.sql.SQLException
155    */
156   public static Vector select(
157     String bindingKey,
158     int tModelInstanceInfoID,
159     Connection connection)
160     throws java.sql.SQLException
161   {
162     Vector descList = new Vector();
163     PreparedStatement statement = null;
164     ResultSet resultSet = null;
165 
166     try
167     {
168       // create a statement to query with
169       statement = connection.prepareStatement(selectSQL);
170       statement.setString(1, bindingKey.toString());
171       statement.setInt(2, tModelInstanceInfoID);
172 
173       if (log.isDebugEnabled()) {
174           log.debug(
175             "select from " + tablePrefix + "INSTANCE_DETAILS_DESCR table:\n\n\t"
176               + selectSQL
177               + "\n\t BINDING_KEY="
178               + bindingKey.toString()
179               + "\n\t TMODEL_INSTANCE_INFO_ID="
180               + tModelInstanceInfoID
181               + "\n");
182       }
183 
184       // execute the statement
185       resultSet = statement.executeQuery();
186       while (resultSet.next())
187       {
188         Description desc = new Description();
189         desc.setLanguageCode(resultSet.getString(1)); //("LANG_CODE"));
190         desc.setValue(resultSet.getString(2)); //("DESCR"));
191         descList.add(desc);
192         desc = null;
193       }
194 
195       return descList;
196     }
197     finally
198     {
199       try
200       {
201         resultSet.close();
202         statement.close();
203       }
204       catch (Exception e)
205       { /* ignored */
206       }
207     }
208   }
209 
210   /***
211    * Delete multiple rows from the INSTANCE_DETAILS_DESCR table that are
212    * assigned to the BindingKey specified.
213    *
214    * @param bindingKey String
215    * @param connection JDBC connection
216    * @throws java.sql.SQLException
217    */
218   public static void delete(String bindingKey, Connection connection)
219     throws java.sql.SQLException
220   {
221     PreparedStatement statement = null;
222 
223     try
224     {
225       // prepare
226       statement = connection.prepareStatement(deleteSQL);
227       statement.setString(1, bindingKey.toString());
228 
229       if (log.isDebugEnabled()) {
230           log.debug(
231             "delete from " + tablePrefix + "INSTANCE_DETAILS_DESCR table:\n\n\t"
232               + deleteSQL
233               + "\n\t BINDING_KEY="
234               + bindingKey.toString()
235               + "\n");
236       }
237 
238       // execute
239       statement.executeUpdate();
240     }
241     finally
242     {
243       try
244       {
245         statement.close();
246       }
247       catch (Exception e)
248       { /* ignored */
249       }
250     }
251   }
252 }