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