1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.Address;
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 AddressTable
33 {
34
35 private static Log log = LogFactory.getLog(AddressTable.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
46 StringBuffer sql = null;
47
48
49 sql = new StringBuffer(150);
50 sql.append("INSERT INTO ").append(tablePrefix).append("ADDRESS (");
51 sql.append("BUSINESS_KEY,");
52 sql.append("CONTACT_ID,");
53 sql.append("ADDRESS_ID,");
54 sql.append("USE_TYPE,");
55 sql.append("SORT_CODE,");
56 sql.append("TMODEL_KEY) ");
57 sql.append("VALUES (?,?,?,?,?,?)");
58 insertSQL = sql.toString();
59
60
61 sql = new StringBuffer(200);
62 sql.append("SELECT ");
63 sql.append("USE_TYPE,");
64 sql.append("SORT_CODE,");
65 sql.append("TMODEL_KEY, ");
66 sql.append("ADDRESS_ID ");
67 sql.append("FROM ").append(tablePrefix).append("ADDRESS ");
68 sql.append("WHERE BUSINESS_KEY=? ");
69 sql.append("AND CONTACT_ID=? ");
70 sql.append("ORDER BY ADDRESS_ID");
71 selectSQL = sql.toString();
72
73
74 sql = new StringBuffer(100);
75 sql.append("DELETE FROM ").append(tablePrefix).append("ADDRESS ");
76 sql.append("WHERE BUSINESS_KEY=?");
77 deleteSQL = sql.toString();
78 }
79
80 /***
81 * Insert new row into the ADDRESS table.
82 *
83 * @param businessKey String to the BusinessEntity object that owns the Contact to be inserted
84 * @param contactID The unique ID generated when saving the parent Contact instance.
85 * @param addrList Collection of Address objects to be inserted
86 * @param connection JDBC connection
87 * @throws java.sql.SQLException
88 */
89 public static void insert(String businessKey,int contactID,Vector addrList,Connection connection)
90 throws java.sql.SQLException
91 {
92 if ((addrList == null) || (addrList.size() == 0))
93 return;
94
95 PreparedStatement statement = null;
96
97 try
98 {
99 statement = connection.prepareStatement(insertSQL);
100 statement.setString(1, businessKey.toString());
101 statement.setInt(2, contactID);
102
103 int listSize = addrList.size();
104 for (int addressID = 0; addressID < listSize; addressID++)
105 {
106 Address address = (Address) addrList.elementAt(addressID);
107 statement.setInt(3, addressID);
108 statement.setString(4, address.getUseType());
109 statement.setString(5, address.getSortCode());
110 statement.setString(6, address.getTModelKey());
111
112 if (log.isDebugEnabled()) {
113 log.debug(
114 "insert into " + tablePrefix + "ADDRESS table:\n\n\t"
115 + insertSQL
116 + "\n\t BUSINESS_KEY="
117 + businessKey.toString()
118 + "\n\t CONTACT_ID="
119 + contactID
120 + "\n\t ADDRESS_ID="
121 + addressID
122 + "\n\t USE_TYPE="
123 + address.getUseType()
124 + "\n\t SORT_CODE="
125 + address.getSortCode()
126 + "\n\t TMODEL_KEY="
127 + address.getTModelKey()
128 + "\n");
129 }
130
131 statement.executeUpdate();
132 }
133 }
134 finally
135 {
136 try {
137 statement.close();
138 }
139 catch (Exception e) {
140 }
141 }
142
143 /***
144 * Select all rows from the CONTACT table for a given BusinessKey.
145 *
146 * @param businessKey String
147 * @param contactID Unique ID representing the parent Contact instance
148 * @param connection JDBC connection
149 * @throws java.sql.SQLException
150 */
151 public static Vector select(String businessKey,int contactID,Connection connection)
152 throws java.sql.SQLException
153 {
154 Vector addrList = new Vector();
155 PreparedStatement statement = null;
156 ResultSet resultSet = null;
157
158 try
159 {
160
161 statement = connection.prepareStatement(selectSQL);
162 statement.setString(1, businessKey.toString());
163 statement.setInt(2, contactID);
164
165 if (log.isDebugEnabled()) {
166 log.debug(
167 "select from " + tablePrefix + "ADDRESS table:\n\n\t"
168 + selectSQL
169 + "\n\t BUSINESS_KEY="
170 + businessKey.toString()
171 + "\n\t CONTACT_ID="
172 + contactID
173 + "\n");
174 }
175
176
177 resultSet = statement.executeQuery();
178 while (resultSet.next())
179 {
180 Address address = new Address();
181 address.setUseType(resultSet.getString(1));
182 address.setSortCode(resultSet.getString(2));
183 address.setTModelKey(resultSet.getString(3));
184 addrList.add(address);
185 address = null;
186 }
187
188 return addrList;
189 }
190 finally
191 {
192 try {
193 resultSet.close();
194 statement.close();
195 }
196 catch (Exception e) {
197 }
198 }
199
200 /***
201 * Delete multiple rows from the ADDRESS table that are assigned to the
202 * BusinessKey specified.
203 *
204 * @param businessKey String
205 * @param connection JDBC connection
206 * @throws java.sql.SQLException
207 */
208 public static void delete(String businessKey, Connection connection)
209 throws java.sql.SQLException
210 {
211 PreparedStatement statement = null;
212
213 try
214 {
215
216 statement = connection.prepareStatement(deleteSQL);
217 statement.setString(1, businessKey.toString());
218
219 if (log.isDebugEnabled()) {
220 log.debug(
221 "delete from " + tablePrefix + "ADDRESS table:\n\n\t"
222 + deleteSQL
223 + "\n\t BUSINESS_KEY="
224 + businessKey.toString()
225 + "\n");
226 }
227
228
229 statement.executeUpdate();
230 }
231 finally
232 {
233 try {
234 statement.close();
235 }
236 catch (Exception e) {
237 }
238 }
239 }