KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > catalog > dao > PointbaseCatalogDAO


1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any
21 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
25 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
27 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
28 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
29 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
30 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
31 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that Software is not designed, licensed or intended
34 * for use in the design, construction, operation or maintenance of
35 * any nuclear facility.
36 */

37
38 package com.sun.j2ee.blueprints.catalog.dao;
39
40 import java.sql.Connection JavaDoc;
41 import java.sql.PreparedStatement JavaDoc;
42 import java.sql.ResultSet JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import javax.sql.DataSource JavaDoc;
45 import java.util.Locale JavaDoc;
46 import java.util.ArrayList JavaDoc;
47
48
49 import com.sun.j2ee.blueprints.util.tracer.Debug;
50 import com.sun.j2ee.blueprints.util.dao.*;
51 import com.sun.j2ee.blueprints.catalog.*;
52
53
54 /**
55  * This class implements CatalogDAO for Pointbase database.
56  * This class encapsulates all the JDBC calls .
57  * The logic of inserting/fetching/updating/deleting the data in
58  * relational database tables is implemented here.
59  */

60 public class PointbaseCatalogDAO implements CatalogDAO {
61     
62     private final static String JavaDoc SELECT_LODGINGS_QUERY_STR = "SELECT "+
63     "lodgingid, name, description, price, location" +" FROM "+ DatabaseNames.LODGING_TABLE +
64     " WHERE location = ? AND locale = ?";
65     private final static String JavaDoc SELECT_LODGING_QUERY_STR = "SELECT "+
66     "lodgingid, name, description, price, location" +" FROM "+ DatabaseNames.LODGING_TABLE +
67     " WHERE lodgingid = ? AND locale = ?";
68     private final static String JavaDoc SELECT_TRANSPORTATIONS_QUERY_STR = "SELECT "+
69     "transportationid, name, description, imageuri, price, origin, destination,carrier,departuretime, arrivaltime, class"+" FROM " + DatabaseNames.TRANSPORTATION_TABLE +
70     " WHERE origin = ? AND destination = ? AND locale = ?";
71     private final static String JavaDoc SELECT_TRANSPORTATION_QUERY_STR = "SELECT "+
72     "transportationid, name, description, imageuri, price, origin, destination,carrier,departuretime, arrivaltime, class"+" FROM " + DatabaseNames.TRANSPORTATION_TABLE +
73     " WHERE transportationid = ? AND locale = ?";
74     private final static String JavaDoc SELECT_ACTIVITIES_QUERY_STR = "SELECT "+
75     "activityid, name, description, price, location "+" FROM " + DatabaseNames.ACTIVITY_TABLE +
76     " WHERE location = ? AND locale = ?";
77     private final static String JavaDoc SELECT_ACTIVITY_QUERY_STR = "SELECT "+
78     "activityid, name, description, price, location "+" FROM " + DatabaseNames.ACTIVITY_TABLE +
79     " WHERE activityid = ? AND locale = ?";
80     private final static String JavaDoc SELECT_ADVENTURE_PACKAGE_QUERY_STR = "SELECT "+
81     "name, description, price, location, lodgingid "+" FROM " + DatabaseNames.PACKAGE_TABLE +
82     " WHERE packageid = ? AND locale = ?";
83     private final static String JavaDoc SELECT_ACTIVITYLIST_QUERY_STR = "SELECT "+
84     "activityid"+" FROM " + DatabaseNames.ACTIVITYLIST_TABLE +
85     " WHERE packageid = ? AND locale = ?";
86     
87     public PointbaseCatalogDAO() {
88     }
89     
90     public ArrayList JavaDoc getLodgings(String JavaDoc location, Locale JavaDoc locale)
91     throws CatalogDAOException, DAOSystemException {
92
93         PreparedStatement JavaDoc stmt = null;
94         ResultSet JavaDoc result = null;
95         Connection JavaDoc dbConnection = null;
96         
97         ArrayList JavaDoc lodgings = new ArrayList JavaDoc();
98         try {
99             dbConnection = DAOUtils.getDBConnection(JNDINames.CATALOG_DATASOURCE);
100             stmt = dbConnection.prepareStatement(SELECT_LODGINGS_QUERY_STR);
101             stmt.setString(1, location.trim());
102             stmt.setString(2, locale.toString().trim());
103             result = stmt.executeQuery();
104             if ( !result.next() )
105                 throw new CatalogDAOException(
106                 "No data found for " + location +" , "+locale.toString());
107             do {
108                 int i = 1;
109                 String JavaDoc lodgingId = result.getString(i++);
110                 String JavaDoc name = result.getString(i++);
111                 String JavaDoc description = result.getString(i++);
112                 double price = result.getDouble(i++);
113                 location = result.getString(i++);
114                 lodgings.add(new Lodging(lodgingId , name, description, price, location));
115             }
116             while(result.next());
117             
118             return(lodgings );
119         } catch(SQLException JavaDoc se) {
120             throw new DAOSystemException("SQLException while getting " +
121             "lodging details; location = " + location +" and locale = "+locale.toString()+" \n", se);
122         } finally {
123             DAOUtils.closeResultSet(result);
124             DAOUtils.closeStatement(stmt);
125             DAOUtils.closeConnection(dbConnection);
126         }
127     }
128     
129     
130     
131     public Lodging getLodging(String JavaDoc id, Locale JavaDoc locale)
132     throws CatalogDAOException, DAOSystemException {
133
134         PreparedStatement JavaDoc stmt = null;
135         ResultSet JavaDoc result = null;
136         Connection JavaDoc dbConnection = null;
137         
138         try {
139             dbConnection = DAOUtils.getDBConnection(JNDINames.CATALOG_DATASOURCE);
140             stmt = dbConnection.prepareStatement(SELECT_LODGING_QUERY_STR);
141             stmt.setString(1, id.trim());
142             stmt.setString(2, locale.toString().trim());
143             result = stmt.executeQuery();
144             if ( !result.next() )
145                 throw new CatalogDAOException(
146                 "No data found for " + id +" , "+locale.toString());
147             int i = 1;
148             String JavaDoc lodgingId = result.getString(i++);
149             String JavaDoc name = result.getString(i++);
150             String JavaDoc description = result.getString(i++);
151             double price = result.getDouble(i++);
152             String JavaDoc location = result.getString(i++);
153             return new Lodging(lodgingId , name, description, price, location);
154         } catch(SQLException JavaDoc se) {
155             throw new DAOSystemException("SQLException while getting " +
156             "lodging details; id = " + id +" and locale = "+locale.toString()+" \n", se);
157         } finally {
158             DAOUtils.closeResultSet(result);
159             DAOUtils.closeStatement(stmt);
160             DAOUtils.closeConnection(dbConnection);
161         }
162     }
163     
164     public AdventurePackage getAdventurePackage(String JavaDoc packageId, Locale JavaDoc locale)
165     throws CatalogDAOException, DAOSystemException {
166
167         PreparedStatement JavaDoc stmt = null;
168         PreparedStatement JavaDoc stmt2 = null;
169         ResultSet JavaDoc result = null;
170         ResultSet JavaDoc result2 = null;
171         Connection JavaDoc dbConnection = null;
172         
173         ArrayList JavaDoc transportations = new ArrayList JavaDoc();
174         try {
175             dbConnection = DAOUtils.getDBConnection(JNDINames.CATALOG_DATASOURCE);
176             stmt = dbConnection.prepareStatement(SELECT_ADVENTURE_PACKAGE_QUERY_STR);
177             stmt.setString(1, packageId.trim());
178             stmt.setString(2, locale.toString().trim());
179             result = stmt.executeQuery();
180             if ( !result.next() )
181                 throw new CatalogDAOException(
182                 "No data found for " + packageId +" , "+locale.toString());
183             int i = 1;
184             String JavaDoc name = result.getString(i++).trim();
185             String JavaDoc description = result.getString(i++).trim();
186             double price = result.getDouble(i++);
187             String JavaDoc location = result.getString(i++).trim();
188             String JavaDoc lodgingId = result.getString(i++).trim();
189             ArrayList JavaDoc activities = new ArrayList JavaDoc();
190             // Getting the activites
191
stmt2 = dbConnection.prepareStatement(SELECT_ACTIVITYLIST_QUERY_STR);
192             stmt2.setString(1, packageId);
193             stmt2.setString(2, locale.toString().trim());
194             result2 = stmt2.executeQuery();
195             
196             while (result2.next()) {
197                 String JavaDoc activityId = result2.getString(1).trim();
198                 activities.add(activityId);
199             }
200             AdventurePackage ap = new AdventurePackage(packageId,
201             name, description, location, lodgingId, price, activities);
202             return(ap);
203         } catch(SQLException JavaDoc se) {
204             throw new DAOSystemException("SQLException while getting " +
205             "AdventurePackage details; origin = " + packageId +", and locale = "+locale.toString()+" \n", se);
206         } finally {
207             DAOUtils.closeResultSet(result);
208             DAOUtils.closeStatement(stmt);
209             DAOUtils.closeResultSet(result2);
210             DAOUtils.closeStatement(stmt2);
211             DAOUtils.closeConnection(dbConnection);
212         }
213     }
214     
215     public ArrayList JavaDoc getTransportations(String JavaDoc origin, String JavaDoc destination, Locale JavaDoc locale)
216     throws CatalogDAOException, DAOSystemException {
217
218         PreparedStatement JavaDoc stmt = null;
219         ResultSet JavaDoc result = null;
220         Connection JavaDoc dbConnection = null;
221         
222         ArrayList JavaDoc transportations = new ArrayList JavaDoc();
223         try {
224             dbConnection = DAOUtils.getDBConnection(JNDINames.CATALOG_DATASOURCE);
225             stmt = dbConnection.prepareStatement(SELECT_TRANSPORTATIONS_QUERY_STR);
226             stmt.setString(1, origin.trim());
227             stmt.setString(2, destination.trim());
228             stmt.setString(3, locale.toString().trim());
229             result = stmt.executeQuery();
230             if ( !result.next() )
231                 throw new CatalogDAOException(
232                 "No data found for " + origin +" , "+ destination +" , "+locale.toString());
233             do {
234                 int i = 1;
235                 String JavaDoc transportId = result.getString(i++);
236                 String JavaDoc name = result.getString(i++);
237                 String JavaDoc description = result.getString(i++);
238                 String JavaDoc imageURI = result.getString(i++);
239                 double price = result.getDouble(i++);
240                 origin = result.getString(i++);
241                 destination = result.getString(i++);
242                 String JavaDoc carrier = result.getString(i++);
243                 String JavaDoc departureTime = result.getString(i++);
244                 String JavaDoc arrivalTime = result.getString(i++);
245                 String JavaDoc travelClass = result.getString(i++);
246                 transportations.add(new Transportation(transportId , name, description, imageURI, price,origin,destination,carrier,departureTime,arrivalTime,travelClass));
247             }
248             while(result.next());
249             
250             return(transportations);
251         } catch(SQLException JavaDoc se) {
252             throw new DAOSystemException("SQLException while getting " +
253             "Transportation details; origin = " + origin +" , "+"destination = "+destination+" and locale = "+locale.toString()+" \n", se);
254         } finally {
255             DAOUtils.closeResultSet(result);
256             DAOUtils.closeStatement(stmt);
257             DAOUtils.closeConnection(dbConnection);
258         }
259     }
260     
261     public Transportation getTransportation(String JavaDoc id, Locale JavaDoc locale)
262     throws CatalogDAOException, DAOSystemException {
263
264   PreparedStatement JavaDoc stmt = null;
265         ResultSet JavaDoc result = null;
266         Connection JavaDoc dbConnection = null;
267         
268         try {
269             dbConnection = DAOUtils.getDBConnection(JNDINames.CATALOG_DATASOURCE);
270             stmt = dbConnection.prepareStatement(SELECT_TRANSPORTATION_QUERY_STR);
271             stmt.setString(1, id.trim());
272             stmt.setString(2, locale.toString().trim());
273             result = stmt.executeQuery();
274             if ( !result.next() )
275                 throw new CatalogDAOException(
276                 "No data found for " + id +" , "+locale.toString());
277             int i = 1;
278             String JavaDoc transportId = result.getString(i++);
279             String JavaDoc name = result.getString(i++);
280             String JavaDoc description = result.getString(i++);
281             String JavaDoc imageURI = result.getString(i++);
282             double price = result.getDouble(i++);
283             String JavaDoc origin = result.getString(i++);
284             String JavaDoc destination = result.getString(i++);
285             String JavaDoc carrier = result.getString(i++);
286             String JavaDoc departureTime = result.getString(i++);
287             String JavaDoc arrivalTime = result.getString(i++);
288             String JavaDoc travelClass = result.getString(i++);
289             return new Transportation(transportId , name, description, imageURI,
290             price, origin, destination, carrier, departureTime, arrivalTime, travelClass);
291         } catch(SQLException JavaDoc se) {
292             throw new DAOSystemException("SQLException while getting " +
293             "Transportation details; id = " + id +" and locale = "+locale.toString()+" \n", se);
294         } finally {
295             DAOUtils.closeResultSet(result);
296             DAOUtils.closeStatement(stmt);
297             DAOUtils.closeConnection(dbConnection);
298         }
299     }
300     
301     
302     public ArrayList JavaDoc getActivities(String JavaDoc location, Locale JavaDoc locale)
303     throws CatalogDAOException, DAOSystemException {
304
305   PreparedStatement JavaDoc stmt = null;
306         ResultSet JavaDoc result = null;
307         Connection JavaDoc dbConnection = null;
308         
309         ArrayList JavaDoc activities = new ArrayList JavaDoc();
310         try {
311             dbConnection = DAOUtils.getDBConnection(JNDINames.CATALOG_DATASOURCE);
312             stmt = dbConnection.prepareStatement(SELECT_ACTIVITIES_QUERY_STR);
313             stmt.setString(1, location.trim());
314             stmt.setString(2, locale.toString().trim());
315             result = stmt.executeQuery();
316             if ( !result.next() )
317                 throw new CatalogDAOException(
318                 "No data dound for " + location +" , "+locale.toString());
319             do {
320                 int i = 1;
321                 String JavaDoc activityId = result.getString(i++);
322                 String JavaDoc name = result.getString(i++);
323                 String JavaDoc description = result.getString(i++);
324                 double price = result.getDouble(i++);
325                 location = result.getString(i++);
326                 activities.add(new Activity(activityId , name, description, price,location));
327             }
328             while(result.next());
329             
330             return(activities);
331         } catch(SQLException JavaDoc se) {
332             throw new DAOSystemException("SQLException while getting " +
333             "Activity details; location = " + location +" and locale = "+locale.toString()+" \n", se);
334         } finally {
335             DAOUtils.closeResultSet(result);
336             DAOUtils.closeStatement(stmt);
337             DAOUtils.closeConnection(dbConnection);
338         }
339     }
340     
341     
342     public Activity getActivity(String JavaDoc id, Locale JavaDoc locale)
343     throws CatalogDAOException, DAOSystemException {
344
345   PreparedStatement JavaDoc stmt = null;
346         ResultSet JavaDoc result = null;
347         Connection JavaDoc dbConnection = null;
348         
349         try {
350             dbConnection = DAOUtils.getDBConnection(JNDINames.CATALOG_DATASOURCE);
351             stmt = dbConnection.prepareStatement(SELECT_ACTIVITY_QUERY_STR);
352             stmt.setString(1, id.trim());
353             stmt.setString(2, locale.toString().trim());
354             result = stmt.executeQuery();
355             if ( !result.next() )
356                 throw new CatalogDAOException(
357                 "No data dound for " + id +" , "+locale.toString());
358             int i = 1;
359             String JavaDoc activityId = result.getString(i++);
360             String JavaDoc name = result.getString(i++);
361             String JavaDoc description = result.getString(i++);
362             double price = result.getDouble(i++);
363             String JavaDoc location = result.getString(i++);
364             return new Activity(activityId , name, description, price,location);
365         } catch(SQLException JavaDoc se) {
366             throw new DAOSystemException("SQLException while getting " +
367             "Activity details; id = " + id +" and locale = "+locale.toString()+" \n", se);
368         } finally {
369             DAOUtils.closeResultSet(result);
370             DAOUtils.closeStatement(stmt);
371             DAOUtils.closeConnection(dbConnection);
372         }
373     }
374 }
375
Popular Tags