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