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.AddressLine;
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 AddressLineTable
33  {
34    // private reference to the jUDDI logger
35    private static Log log = LogFactory.getLog(AddressLineTable.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    {
44      tablePrefix = Config.getStringProperty(
45         RegistryEngine.PROPNAME_TABLE_PREFIX,RegistryEngine.DEFAULT_TABLE_PREFIX);
46      // buffer used to build SQL statements
47      StringBuffer sql = null;
48  
49      // build insertSQL
50      sql = new StringBuffer(150);
51      sql.append("INSERT INTO ").append(tablePrefix).append("ADDRESS_LINE (");
52      sql.append("BUSINESS_KEY,");
53      sql.append("CONTACT_ID,");
54      sql.append("ADDRESS_ID,");
55      sql.append("ADDRESS_LINE_ID,");
56      sql.append("LINE,");
57      sql.append("KEY_NAME,");
58      sql.append("KEY_VALUE) ");
59      sql.append("VALUES (?,?,?,?,?,?,?)");
60      insertSQL = sql.toString();
61  
62      // build selectSQL
63      sql = new StringBuffer(200);
64      sql.append("SELECT ");
65      sql.append("LINE,");
66      sql.append("KEY_NAME,");
67      sql.append("KEY_VALUE, ");
68      sql.append("ADDRESS_LINE_ID ");
69      sql.append("FROM ").append(tablePrefix).append("ADDRESS_LINE ");
70      sql.append("WHERE BUSINESS_KEY=? ");
71      sql.append("AND CONTACT_ID=? ");
72      sql.append("AND ADDRESS_ID=? ");
73      sql.append("ORDER BY ADDRESS_LINE_ID");
74      selectSQL = sql.toString();
75  
76      // build deleteSQL
77      sql = new StringBuffer(100);
78      sql.append("DELETE FROM "+ tablePrefix + "ADDRESS_LINE ");
79      sql.append("WHERE BUSINESS_KEY=?");
80      deleteSQL = sql.toString();
81    }
82  
83    /***
84     * Insert new row into the ADDRESS_LINE table.
85     *
86     * @param businessKey BusinessKey to the BusinessEntity object that owns the Contact to be inserted
87     * @param contactID The unique ID generated when saving the parent Contact instance.
88     * @param addressID The unique ID generated when saving the parent Address instance.
89     * @param lineList Enumeration of AddressLines objects holding values to be inserted.
90     * @param connection JDBC connection
91     * @throws java.sql.SQLException
92     */
93    public static void insert(String businessKey,int contactID,int addressID,Vector lineList,Connection connection)
94      throws java.sql.SQLException
95    {
96      if ((lineList == null) || (lineList.size() == 0))
97        return; // everything is valid but no elements to insert
98  
99      PreparedStatement statement = null;
100 
101     try
102     {
103       statement = connection.prepareStatement(insertSQL);
104       statement.setString(1,businessKey.toString());
105       statement.setInt(2,contactID);
106       statement.setInt(3,addressID);
107 
108       int listSize = lineList.size();
109       for (int lineID=0; lineID<listSize; lineID++)
110       {
111         AddressLine line = (AddressLine)lineList.elementAt(lineID);
112         statement.setInt(4,lineID);
113         statement.setString(5,line.getLineValue());
114         statement.setString(6,line.getKeyName());
115         statement.setString(7,line.getKeyValue());
116 
117         if (log.isDebugEnabled()) {
118             log.debug("insert into "+ tablePrefix + "ADDRESS_LINE table:\n\n\t" + insertSQL +
119               "\n\t BUSINESS_KEY=" + businessKey.toString() +
120               "\n\t CONTACT_ID=" + contactID +
121               "\n\t ADDRESS_ID=" + addressID +
122               "\n\t ADDRESS_LINE_ID=" + lineID +
123               "\n\t LINE=" + line.getLineValue() +
124               "\n\t KEY_NAME=" + line.getKeyName() +
125               "\n\t KEY_VALUE=" + line.getKeyValue() + "\n");
126         }
127 
128         statement.executeUpdate();
129       }
130     }
131     finally
132     {
133       try {
134         statement.close();
135       }
136       catch (Exception e) { /* ignored */ }
137     }
138   }
139 
140   /***
141    * Select all rows from the CONTACT table for a given BusinessKey.
142    *
143    * @param  businessKey BusinessKey
144    * @param  contactID Unique ID representing the parent Contact instance
145    * @param  connection JDBC connection
146    * @throws java.sql.SQLException
147    */
148   public static Vector select(String businessKey,int contactID,int addressID,Connection connection)
149     throws java.sql.SQLException
150   {
151     Vector lineList = new Vector();
152     PreparedStatement statement = null;
153     ResultSet resultSet = null;
154 
155     try
156     {
157       // create a statement to query with
158       statement = connection.prepareStatement(selectSQL);
159       statement.setString(1,businessKey.toString());
160       statement.setInt(2,contactID);
161       statement.setInt(3,addressID);
162 
163       if (log.isDebugEnabled()) {
164           log.debug("select from "+ tablePrefix + "ADDRESS_LINE table:\n\n\t" + selectSQL +
165             "\n\t BUSINESS_KEY=" + businessKey.toString() +
166             "\n\t CONTACT_KEY=" + contactID +
167             "\n\t ADDRESS_ID=" + addressID + "\n");
168       }
169 
170       // execute the statement
171       resultSet = statement.executeQuery();
172       while (resultSet.next())
173       {
174         AddressLine line = new AddressLine();
175         line.setLineValue(resultSet.getString(1));//("LINE")
176         line.setKeyName(resultSet.getString(2));//("KEY_NAME"));
177         line.setKeyValue(resultSet.getString(3));//("KEY_VALUE"));
178         lineList.add(line);
179       }
180 
181       return lineList;
182     }
183     finally
184     {
185       try {
186         resultSet.close();
187         statement.close();
188       }
189       catch (Exception e) { /* ignored */ }
190     }
191   }
192 
193   /***
194    * Delete multiple rows from the ADDRESS_LINE table that are assigned to the
195    * 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("delete from "+ tablePrefix + "ADDRESS_LINE table:\n\n\t" + deleteSQL +
214             "\n\t BUSINESS_KEY=" + businessKey.toString() + "\n");
215       }
216 
217       // execute
218       statement.executeUpdate();
219     }
220     finally
221     {
222       try {
223         statement.close();
224       }
225       catch (Exception e) { /* ignored */ }
226     }
227   }
228 }