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