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.Name;
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 BusinessNameTable
33  {
34    // private reference to the jUDDI logger
35    private static Log log = LogFactory.getLog(BusinessNameTable.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_NAME (");
51      sql.append("BUSINESS_KEY,");
52      sql.append("BUSINESS_NAME_ID,");
53      sql.append("LANG_CODE,");
54      sql.append("NAME) ");
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("NAME, ");
63      sql.append("BUSINESS_NAME_ID ");
64      sql.append("FROM ").append(tablePrefix).append("BUSINESS_NAME ");
65      sql.append("WHERE BUSINESS_KEY=? ");
66      sql.append("ORDER BY BUSINESS_NAME_ID");
67      selectSQL = sql.toString();
68  
69      // build deleteSQL
70      sql = new StringBuffer(100);
71      sql.append("DELETE FROM ").append(tablePrefix).append("BUSINESS_NAME ");
72      sql.append("WHERE BUSINESS_KEY=?");
73      deleteSQL = sql.toString();
74    }
75  
76    /***
77     * Insert new row into the BUSINESS_NAME table.
78     *
79     * @param  businessKey String to the BusinessEntity object that owns the Contact to be inserted
80     * @param  nameList Vector of Phone 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 nameList,
87      Connection connection)
88      throws java.sql.SQLException
89    {
90      if ((nameList == null) || (nameList.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 = nameList.size();
101       for (int nameID = 0; nameID < listSize; nameID++)
102       {
103         Name name = (Name) nameList.elementAt(nameID);
104 
105         statement.setInt(2, nameID);
106         statement.setString(3, name.getLanguageCode());
107         statement.setString(4, name.getValue());
108 
109         if (log.isDebugEnabled()) {
110             log.debug(
111               "insert into " + tablePrefix + "BUSINESS_NAME table:\n\n\t"
112                 + insertSQL
113                 + "\n\t BUSINESS_KEY="
114                 + businessKey.toString()
115                 + "\n\t BUSINESS_NAME_ID="
116                 + nameID
117                 + "\n\t LANG_CODE="
118                 + name.getLanguageCode()
119                 + "\n\t NAME="
120                 + name.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_NAME table for a given BusinessKey.
141    *
142    * @param  businessKey String
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 nameList = 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 the " + tablePrefix + "BUSINESS_NAME 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       while (resultSet.next())
171       {
172         Name name = new Name();
173         name.setValue(resultSet.getString(2));//("NAME"));
174         name.setLanguageCode(resultSet.getString(1));//("LANG_CODE"));
175         nameList.add(name);
176       }
177 
178       return nameList;
179     }
180     finally
181     {
182       try
183       {
184         resultSet.close();
185         statement.close();
186       }
187       catch (Exception e)
188       { /* ignored */
189       }
190     }
191   }
192 
193   /***
194    * Delete multiple rows from the BUSINESS_NAME table that are assigned
195    * to the BusinessKey specified.
196    *
197    * @param  businessKey String
198    * @param  connection JDBC connection
199    * @throws java.sql.SQLException
200    */
201   public static void delete(String businessKey, Connection connection)
202     throws java.sql.SQLException
203   {
204     PreparedStatement statement = null;
205 
206     try
207     {
208       // prepare the delete
209       statement = connection.prepareStatement(deleteSQL);
210       statement.setString(1, businessKey.toString());
211 
212       if (log.isDebugEnabled()) {
213           log.debug(
214             "delete from the " + tablePrefix + "BUSINESS_NAME table:\n\n\t"
215               + deleteSQL
216               + "\n\t BUSINESS_KEY="
217               + businessKey.toString()
218               + "\n");
219       }
220 
221       // execute
222       statement.executeUpdate();
223     }
224     finally
225     {
226       try
227       {
228         statement.close();
229       }
230       catch (Exception e)
231       { /* ignored */
232       }
233     }
234   }
235 }