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.OverviewDoc;
26  import org.apache.juddi.datatype.binding.InstanceDetails;
27  import org.apache.juddi.datatype.binding.InstanceParms;
28  import org.apache.juddi.datatype.binding.TModelInstanceInfo;
29  import org.apache.juddi.registry.RegistryEngine;
30  import org.apache.juddi.util.Config;
31  
32  /***
33   * @author Steve Viens (sviens@apache.org)
34   */
35  class TModelInstanceInfoTable
36  {
37    // private reference to the jUDDI logger
38    private static Log log = LogFactory.getLog(TModelInstanceInfoTable.class);
39  
40    static String insertSQL = null;
41    static String selectSQL = null;
42    static String deleteSQL = null;
43    static String tablePrefix = "";
44    
45    static
46    {
47     tablePrefix = Config.getStringProperty(
48         RegistryEngine.PROPNAME_TABLE_PREFIX,RegistryEngine.DEFAULT_TABLE_PREFIX);
49      // buffer used to build SQL statements
50      StringBuffer sql = null;
51  
52      // build insertSQL
53      sql = new StringBuffer(150);
54      sql.append("INSERT INTO ").append(tablePrefix).append("TMODEL_INSTANCE_INFO (");
55      sql.append("BINDING_KEY,");
56      sql.append("TMODEL_INSTANCE_INFO_ID,");
57      sql.append("TMODEL_KEY, ");
58      sql.append("OVERVIEW_URL,");
59      sql.append("INSTANCE_PARMS) ");
60      sql.append("VALUES (?,?,?,?,?)");
61      insertSQL = sql.toString();
62  
63      // build selectSQL
64      sql = new StringBuffer(200);
65      sql.append("SELECT ");
66      sql.append("TMODEL_KEY,");
67      sql.append("OVERVIEW_URL,");
68      sql.append("INSTANCE_PARMS, ");
69      sql.append("TMODEL_INSTANCE_INFO_ID ");
70      sql.append("FROM ").append(tablePrefix).append("TMODEL_INSTANCE_INFO ");
71      sql.append("WHERE BINDING_KEY=? ");
72      sql.append("ORDER BY TMODEL_INSTANCE_INFO_ID");
73      selectSQL = sql.toString();
74  
75      // build deleteSQL
76      sql = new StringBuffer(100);
77      sql.append("DELETE FROM ").append(tablePrefix).append("TMODEL_INSTANCE_INFO ");
78      sql.append("WHERE BINDING_KEY=?");
79      deleteSQL = sql.toString();
80    }
81  
82    /***
83     * Insert new row into the TMODEL_INSTANCE_INFO table.
84     *
85     * @param bindingKey String to the BusinessEntity object that owns the Contact to be inserted
86     * @param infoList Vector of Contact objects holding values to be inserted
87     * @param connection JDBC connection
88     * @throws java.sql.SQLException
89     */
90    public static void insert(
91      String bindingKey,
92      Vector infoList,
93      Connection connection)
94      throws java.sql.SQLException
95    {
96      if ((infoList == null) || (infoList.size() == 0))
97        return; // everything is valid but no elements to insert
98  
99      PreparedStatement statement = null;
100 
101     try
102     {
103       statement = connection.prepareStatement(insertSQL);
104       statement.setString(1, bindingKey.toString());
105 
106       int listSize = infoList.size();
107       for (int infoID = 0; infoID < listSize; infoID++)
108       {
109         String tModelKey = null;
110         String overURL = null;
111         String instParms = null;
112 
113         TModelInstanceInfo info =
114           (TModelInstanceInfo) infoList.elementAt(infoID);
115         if (info.getTModelKey() != null)
116           tModelKey = info.getTModelKey().toString();
117 
118         InstanceDetails details = info.getInstanceDetails();
119         if (details != null)
120         {
121           if (details.getOverviewDoc() != null)
122             overURL = details.getOverviewDoc().getOverviewURLString();
123 
124           if (details.getInstanceParms() != null)
125             instParms = details.getInstanceParms().getValue();
126         }
127 
128         // insert sequence number
129         statement.setInt(2, infoID);
130         statement.setString(3, tModelKey);
131         statement.setString(4, overURL);
132         statement.setString(5, instParms);
133         
134         if (log.isDebugEnabled()) {
135             log.debug(
136               "insert into " + tablePrefix + "TMODEL_INSTANCE_INFO table:\n\n\t"
137                 + insertSQL
138                 + "\n\t BINDING_KEY="
139                 + bindingKey.toString()
140                 + "\n\t TMODEL_INSTANCE_INFO_ID="
141                 + infoID
142                 + "\n\t TMODEL_KEY="
143                 + tModelKey
144                 + "\n\t OVERVIEW_URL="
145                 + overURL
146                 + "\n\t INSTANCE_PARMS="
147                 + instParms
148                 + "\n");
149         }
150 
151         statement.executeUpdate();
152       }
153     }
154     finally
155     {
156       try
157       {
158         statement.close();
159       }
160       catch (Exception e)
161       { /* ignored */
162       }
163     }
164   }
165 
166   /***
167    * Select all rows from the TMODEL_INST_INFO table for a given BusinessKey.
168    *
169    * @param bindingKey String
170    * @param connection JDBC connection
171    * @throws java.sql.SQLException
172    */
173   public static Vector select(String bindingKey, Connection connection)
174     throws java.sql.SQLException
175   {
176     Vector infoList = new Vector();
177     PreparedStatement statement = null;
178     ResultSet resultSet = null;
179 
180     try
181     {
182       // create a statement to query with
183       statement = connection.prepareStatement(selectSQL);
184       statement.setString(1, bindingKey.toString());
185 
186       if (log.isDebugEnabled()) {
187           log.debug(
188             "select from " + tablePrefix + "TMODEL_INSTANCE_INFO table:\n\n\t"
189               + selectSQL
190               + "\n\t BINDING_KEY="
191               + bindingKey.toString()
192               + "\n");
193       }
194 
195       // execute the statement
196       resultSet = statement.executeQuery();
197 
198       while (resultSet.next())
199       {
200         String tModelKey = resultSet.getString(1);//("TMODEL_KEY");
201         String overURL = resultSet.getString(2);//("OVERVIEW_URL");
202         String instParms = resultSet.getString(3);//("INSTANCE_PARMS");
203 
204         if (tModelKey != null)
205         {
206           TModelInstanceInfo info = new TModelInstanceInfo();
207           info.setTModelKey(tModelKey);
208 
209           OverviewDoc overviewDoc = null;
210           if (overURL != null)
211           {
212             overviewDoc = new OverviewDoc();
213             overviewDoc.setOverviewURL(overURL);
214           }
215 
216           InstanceParms instanceParms = null;
217           if (instParms != null)
218           {
219             instanceParms = new InstanceParms();
220             instanceParms.setText(instParms);
221           }
222 
223           InstanceDetails details = null;
224           if ((overviewDoc != null) || (instanceParms != null))
225           {
226             details = new InstanceDetails();
227             details.setOverviewDoc(overviewDoc);
228             details.setInstanceParms(instanceParms);
229             info.setInstanceDetails(details);
230           }
231 
232           infoList.add(info);
233         }
234       }
235 
236       return infoList;
237     }
238     finally
239     {
240       try
241       {
242         resultSet.close();
243         statement.close();
244       }
245       catch (Exception e)
246       { /* ignored */
247       }
248     }
249   }
250 
251   /***
252    * Delete multiple rows from the TMODEL_INST_INFO table that are assigned to the
253    * BusinessKey specified.
254    *
255    * @param bindingKey String
256    * @param connection JDBC connection
257    * @throws java.sql.SQLException
258    */
259   public static void delete(String bindingKey, Connection connection)
260     throws java.sql.SQLException
261   {
262     PreparedStatement statement = null;
263 
264     try
265     {
266       // prepare the delete
267       statement = connection.prepareStatement(deleteSQL);
268       statement.setString(1, bindingKey.toString());
269 
270       if (log.isDebugEnabled()) {
271           log.debug(
272             "delete from " + tablePrefix + "TMODEL_INSTANCE_INFO table:\n\n\t"
273               + deleteSQL
274               + "\n\t BINDING_KEY="
275               + bindingKey.toString()
276               + "\n");
277       }
278 
279       // execute
280       statement.executeUpdate();
281     }
282     finally
283     {
284       try
285       {
286         statement.close();
287       }
288       catch (Exception e)
289       { /* ignored */
290       }
291     }
292   }
293 }