KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > datastore > jdbc > AddressTable


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 JavaDoc;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.juddi.datatype.Address;
26
27 /**
28  * @author Steve Viens (sviens@apache.org)
29  */

30 class AddressTable
31 {
32   // private reference to the jUDDI logger
33
private static Log log = LogFactory.getLog(AddressTable.class);
34
35   static String JavaDoc insertSQL = null;
36   static String JavaDoc selectSQL = null;
37   static String JavaDoc deleteSQL = null;
38
39   static {
40       // buffer used to build SQL statements
41
StringBuffer JavaDoc sql = null;
42
43       // build insertSQL
44
sql = new StringBuffer JavaDoc(150);
45       sql.append("INSERT INTO ADDRESS (");
46       sql.append("BUSINESS_KEY,");
47       sql.append("CONTACT_ID,");
48       sql.append("ADDRESS_ID,");
49       sql.append("USE_TYPE,");
50       sql.append("SORT_CODE,");
51       sql.append("TMODEL_KEY) ");
52       sql.append("VALUES (?,?,?,?,?,?)");
53       insertSQL = sql.toString();
54
55       // build selectSQL
56
sql = new StringBuffer JavaDoc(200);
57       sql.append("SELECT ");
58       sql.append("USE_TYPE,");
59       sql.append("SORT_CODE,");
60       sql.append("TMODEL_KEY, ");
61       sql.append("ADDRESS_ID ");
62       sql.append("FROM ADDRESS ");
63       sql.append("WHERE BUSINESS_KEY=? ");
64       sql.append("AND CONTACT_ID=? ");
65       sql.append("ORDER BY ADDRESS_ID");
66       selectSQL = sql.toString();
67
68       // build deleteSQL
69
sql = new StringBuffer JavaDoc(100);
70       sql.append("DELETE FROM ADDRESS ");
71       sql.append("WHERE BUSINESS_KEY=?");
72       deleteSQL = sql.toString();
73   }
74
75   /**
76    * Insert new row into the ADDRESS table.
77    *
78    * @param businessKey String to the BusinessEntity object that owns the Contact to be inserted
79    * @param contactID The unique ID generated when saving the parent Contact instance.
80    * @param addrList Collection of Address objects to be inserted
81    * @param connection JDBC connection
82    * @throws java.sql.SQLException
83    */

84   public static void insert(String JavaDoc businessKey,int contactID,Vector JavaDoc addrList,Connection JavaDoc connection)
85     throws java.sql.SQLException JavaDoc
86   {
87     if ((addrList == null) || (addrList.size() == 0))
88       return; // everything is valid but no elements to insert
89

90     PreparedStatement JavaDoc statement = null;
91   
92     try
93     {
94       statement = connection.prepareStatement(insertSQL);
95       statement.setString(1, businessKey.toString());
96       statement.setInt(2, contactID);
97   
98       int listSize = addrList.size();
99       for (int addressID = 0; addressID < listSize; addressID++)
100       {
101         Address address = (Address) addrList.elementAt(addressID);
102         statement.setInt(3, addressID);
103         statement.setString(4, address.getUseType());
104         statement.setString(5, address.getSortCode());
105         statement.setString(6, address.getTModelKey());
106   
107         log.debug(
108           "insert into ADDRESS table:\n\n\t"
109             + insertSQL
110             + "\n\t BUSINESS_KEY="
111             + businessKey.toString()
112             + "\n\t CONTACT_ID="
113             + contactID
114             + "\n\t ADDRESS_ID="
115             + addressID
116             + "\n\t USE_TYPE="
117             + address.getUseType()
118             + "\n\t SORT_CODE="
119             + address.getSortCode()
120             + "\n\t TMODEL_KEY="
121             + address.getTModelKey()
122             + "\n");
123   
124         statement.executeUpdate();
125       }
126     }
127     finally
128     {
129       try {
130         statement.close();
131       }
132       catch (Exception JavaDoc e) { /* ignored */ }
133     }
134   }
135
136   /**
137    * Select all rows from the CONTACT table for a given BusinessKey.
138    *
139    * @param businessKey String
140    * @param contactID Unique ID representing the parent Contact instance
141    * @param connection JDBC connection
142    * @throws java.sql.SQLException
143    */

144   public static Vector JavaDoc select(String JavaDoc businessKey,int contactID,Connection JavaDoc connection)
145     throws java.sql.SQLException JavaDoc
146   {
147     Vector JavaDoc addrList = new Vector JavaDoc();
148     PreparedStatement JavaDoc statement = null;
149     ResultSet JavaDoc resultSet = null;
150
151     try
152     {
153       // create a statement to query with
154
statement = connection.prepareStatement(selectSQL);
155       statement.setString(1, businessKey.toString());
156       statement.setInt(2, contactID);
157
158       log.debug(
159         "select from ADDRESS table:\n\n\t"
160           + selectSQL
161           + "\n\t BUSINESS_KEY="
162           + businessKey.toString()
163           + "\n\t CONTACT_ID="
164           + contactID
165           + "\n");
166
167       // execute the statement
168
resultSet = statement.executeQuery();
169       while (resultSet.next())
170       {
171         Address address = new Address();
172         address.setUseType(resultSet.getString(1));//("USE_TYPE"));
173
address.setSortCode(resultSet.getString(2));//("SORT_CODE"));
174
address.setTModelKey(resultSet.getString(3));//("TMODEL_KEY"));
175
addrList.add(address);
176         address = null;
177       }
178
179       return addrList;
180     }
181     finally
182     {
183       try {
184         resultSet.close();
185         statement.close();
186       }
187       catch (Exception JavaDoc e) { /* ignored */ }
188     }
189   }
190
191   /**
192    * Delete multiple rows from the ADDRESS table that are assigned to the
193    * BusinessKey specified.
194    *
195    * @param businessKey String
196    * @param connection JDBC connection
197    * @throws java.sql.SQLException
198    */

199   public static void delete(String JavaDoc businessKey, Connection JavaDoc connection)
200     throws java.sql.SQLException JavaDoc
201   {
202     PreparedStatement JavaDoc statement = null;
203
204     try
205     {
206       // prepare the delete
207
statement = connection.prepareStatement(deleteSQL);
208       statement.setString(1, businessKey.toString());
209
210       log.debug(
211         "delete from ADDRESS table:\n\n\t"
212           + deleteSQL
213           + "\n\t BUSINESS_KEY="
214           + businessKey.toString()
215           + "\n");
216
217       // execute
218
statement.executeUpdate();
219     }
220     finally
221     {
222       try {
223         statement.close();
224       }
225       catch (Exception JavaDoc e) { /* ignored */ }
226     }
227   }
228 }
Popular Tags