KickJava   Java API By Example, From Geeks To Geeks.

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


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.AddressLine;
26
27 /**
28  * @author Steve Viens (sviens@apache.org)
29  */

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

88   public static void insert(String JavaDoc businessKey,int contactID,int addressID,Vector JavaDoc lineList,Connection JavaDoc connection)
89     throws java.sql.SQLException JavaDoc
90   {
91     if ((lineList == null) || (lineList.size() == 0))
92       return; // everything is valid but no elements to insert
93

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

141   public static Vector JavaDoc select(String JavaDoc businessKey,int contactID,int addressID,Connection JavaDoc connection)
142     throws java.sql.SQLException JavaDoc
143   {
144     Vector JavaDoc lineList = new Vector JavaDoc();
145     PreparedStatement JavaDoc statement = null;
146     ResultSet JavaDoc resultSet = null;
147
148     try
149     {
150       // create a statement to query with
151
statement = connection.prepareStatement(selectSQL);
152       statement.setString(1,businessKey.toString());
153       statement.setInt(2,contactID);
154       statement.setInt(3,addressID);
155
156       log.debug("select from ADDRESS_LINE table:\n\n\t" + selectSQL +
157         "\n\t BUSINESS_KEY=" + businessKey.toString() +
158         "\n\t CONTACT_KEY=" + contactID +
159         "\n\t ADDRESS_ID=" + addressID + "\n");
160
161       // execute the statement
162
resultSet = statement.executeQuery();
163       while (resultSet.next())
164       {
165         AddressLine line = new AddressLine();
166         line.setLineValue(resultSet.getString(1));//("LINE")
167
line.setKeyName(resultSet.getString(2));//("KEY_NAME"));
168
line.setKeyValue(resultSet.getString(3));//("KEY_VALUE"));
169
lineList.add(line);
170       }
171
172       return lineList;
173     }
174     finally
175     {
176       try {
177         resultSet.close();
178         statement.close();
179       }
180       catch (Exception JavaDoc e) { /* ignored */ }
181     }
182   }
183
184   /**
185    * Delete multiple rows from the ADDRESS_LINE table that are assigned to the
186    * BusinessKey specified.
187    *
188    * @param businessKey String
189    * @param connection JDBC connection
190    * @throws java.sql.SQLException
191    */

192   public static void delete(String JavaDoc businessKey,Connection JavaDoc connection)
193     throws java.sql.SQLException JavaDoc
194   {
195     PreparedStatement JavaDoc statement = null;
196
197     try
198     {
199       // prepare the delete
200
statement = connection.prepareStatement(deleteSQL);
201       statement.setString(1,businessKey.toString());
202
203       log.debug("delete from ADDRESS_LINE table:\n\n\t" + deleteSQL +
204         "\n\t BUSINESS_KEY=" + businessKey.toString() + "\n");
205
206       // execute
207
statement.executeUpdate();
208     }
209     finally
210     {
211       try {
212         statement.close();
213       }
214       catch (Exception JavaDoc e) { /* ignored */ }
215     }
216   }
217 }
218
Popular Tags