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.business.Contact;
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 ContactTable
33  {
34    // private reference to the jUDDI logger
35    private static Log log = LogFactory.getLog(ContactTable.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 (");
51      sql.append("BUSINESS_KEY,");
52      sql.append("CONTACT_ID,");
53      sql.append("USE_TYPE,");
54      sql.append("PERSON_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("USE_TYPE,");
62      sql.append("PERSON_NAME, ");
63      sql.append("CONTACT_ID ");
64      sql.append("FROM ").append(tablePrefix).append("CONTACT ");
65      sql.append("WHERE BUSINESS_KEY=? ");
66      sql.append("ORDER BY CONTACT_ID");
67      selectSQL = sql.toString();
68  
69      // build deleteSQL
70      sql = new StringBuffer(100);
71      sql.append("DELETE FROM ").append(tablePrefix).append("CONTACT ");
72      sql.append("WHERE BUSINESS_KEY=?");
73      deleteSQL = sql.toString();
74    }
75  
76    /***
77     * Insert new row into the CONTACT table.
78     *
79     * @param businessKey BusinessKey to the BusinessEntity object that owns the Contact to be inserted
80     * @param contactList Vector of Contact 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 contactList,
87      Connection connection)
88      throws java.sql.SQLException
89    {
90      if ((contactList == null) || (contactList.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 = contactList.size();
101       for (int contactID = 0; contactID < listSize; contactID++)
102       {
103         Contact contact = (Contact) contactList.elementAt(contactID);
104 
105         statement.setInt(2, contactID);
106         statement.setString(3, contact.getUseType());
107         statement.setString(4, contact.getPersonNameValue());
108 
109         if (log.isDebugEnabled()) {
110             log.debug(
111               "insert into " + tablePrefix + "CONTACT table:\n\n\t"
112                 + insertSQL
113                 + "\n\t BUSINESS_KEY="
114                 + businessKey.toString()
115                 + "\n\t CONTACT_ID="
116                 + contactID
117                 + "\n\t USE_TYPE="
118                 + contact.getUseType()
119                 + "\n\t PERSON_NAME="
120                 + contact.getPersonNameValue()
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 CONTACT 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 contactList = 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 + "CONTACT 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         Contact contact = new Contact();
173         contact.setUseType(resultSet.getString(1));//("USE_TYPE"));
174         contact.setPersonNameValue(resultSet.getString(2));//("PERSON_NAME"));
175         contactList.add(contact);
176       }
177 
178       return contactList;
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 CONTACT table that are assigned to the
195    * BusinessKey specified.
196    *
197    * @param  businessKey BusinessKey
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
209       statement = connection.prepareStatement(deleteSQL);
210       statement.setString(1, businessKey.toString());
211 
212       if (log.isDebugEnabled()) {
213           log.debug(
214             "delete from " + tablePrefix + "CONTACT 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 }