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