KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > customer > dao > PointbaseAccountDAO


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.customer.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 javax.naming.InitialContext JavaDoc;
46 import javax.naming.Context JavaDoc;
47 import javax.naming.NamingException JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.Iterator JavaDoc;
50
51
52 import com.sun.j2ee.blueprints.customer.ContactInformation;
53 import com.sun.j2ee.blueprints.customer.Address;
54 import com.sun.j2ee.blueprints.customer.Account;
55 import com.sun.j2ee.blueprints.util.tracer.Debug;
56 import com.sun.j2ee.blueprints.util.dao.DAOUtils;
57 import com.sun.j2ee.blueprints.util.dao.DAOSystemException;
58
59 /**
60  * This class implements AccountDAO for Pointbase database.
61  * This class encapsulates all the JDBC calls made to the Account.
62  * The logic of inserting/fetching/updating/deleting the data in
63  * relational database tables is implemented here.
64  */

65 public class PointbaseAccountDAO implements AccountDAO {
66
67    private final static String JavaDoc INSERT_ACCOUNT_QUERY_STR = "INSERT INTO " +
68             DatabaseNames.ACCOUNT_TABLE
69             + "(userid,email,firstname,lastname,"
70             + "addr1,addr2,city,state,zip,country,"
71             + "phone)" + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
72     
73    private final static String JavaDoc SELECT_USER_ID_QUERY_STR =
74        "SELECT userid FROM " + DatabaseNames.ACCOUNT_TABLE +
75        " WHERE userid = ?";
76
77    private final static String JavaDoc SELECT_ACCOUNT_QUERY_STR = "SELECT "+
78        "userid,email,firstname,lastname,"+
79        "addr1,addr2,city,state,zip,country,phone"+
80        " FROM " + DatabaseNames.ACCOUNT_TABLE + " WHERE userid = ?";
81
82    
83     public PointbaseAccountDAO() {
84     }
85
86     public void create(Account details) throws DAOSystemException,
87                                 AccountDAODupKeyException,
88                                 AccountDAODBUpdateException,
89                                 AccountDAOException {
90         insertAccount(details);
91     }
92
93     public Account getAccount(String JavaDoc userId) throws AccountDAOFinderException,
94                                             DAOSystemException {
95         return(selectAccount(userId));
96     }
97
98
99     private void insertAccount(Account details) throws
100                                  DAOSystemException,
101                                  AccountDAODupKeyException,
102                                  AccountDAODBUpdateException,
103                                  AccountDAOException {
104
105         if (!isValidData(details.getUserId(), details.getContactInformation()))
106             throw new AccountDAOException("Illegal data values for insert");
107         if (userExists(details.getUserId()))
108             throw new AccountDAODupKeyException("Account exists for "+
109                                                 details.getUserId());
110
111         PreparedStatement JavaDoc stmt = null;
112         ContactInformation info = details.getContactInformation();
113
114         Connection JavaDoc dbConnection = null;
115         try {
116             dbConnection = DAOUtils.getDBConnection(JNDINames.CUSTOMER_DATASOURCE);
117             stmt = dbConnection.prepareStatement(INSERT_ACCOUNT_QUERY_STR);
118
119             stmt.setString(1, details.getUserId().trim() );
120             stmt.setString(2, info.getEMail().trim() );
121             stmt.setString(3, info.getGivenName().trim() );
122             stmt.setString(4, info.getFamilyName().trim() );
123             stmt.setString(5, info.getAddress().getStreetName1().trim() );
124
125             if (info.getAddress().getStreetName2() != null)
126                 stmt.setString(6, info.getAddress().getStreetName2().trim() );
127             else
128                 stmt.setString(6, " ");
129                 
130             stmt.setString(7, info.getAddress().getCity().trim() );
131             stmt.setString(8, info.getAddress().getState().trim() );
132             stmt.setString(9, info.getAddress().getZipCode().trim() );
133             stmt.setString(10,info.getAddress().getCountry().trim() );
134             stmt.setString(11, info.getTelephone().trim() );
135
136             int resultCount = stmt.executeUpdate();
137
138             if ( resultCount != 1 ) {
139                 throw new AccountDAODBUpdateException(
140                     "ERROR in ACCOUNT_TABLE INSERT !! resultCount = " +
141                                    resultCount);
142             }
143         } catch(SQLException JavaDoc se) {
144             throw new DAOSystemException("SQLException while inserting new " +
145                       "account; id = " + details.getUserId() + "\n", se);
146         } finally {
147             DAOUtils.closeStatement(stmt);
148             DAOUtils.closeConnection(dbConnection);
149         }
150     }
151
152     private boolean isValidData(String JavaDoc userId, ContactInformation info) {
153         if ( (userId == null) ||
154              ( info.getEMail() == null) ||
155              (info.getGivenName() == null) || (info.getFamilyName() == null)
156              || (info.getAddress().getStreetName1() == null) ||
157              (info.getAddress().getCity() == null) ||
158              (info.getAddress().getState() == null) ||
159              (info.getAddress().getZipCode() == null) ||
160              (info.getAddress().getCountry() == null)
161              || (info.getTelephone() == null) )
162             return (false);
163         else
164             return (true);
165     }
166
167     private boolean userExists (String JavaDoc userId) throws DAOSystemException {
168         PreparedStatement JavaDoc stmt = null;
169         ResultSet JavaDoc result = null;
170         boolean returnValue = false;
171
172         Connection JavaDoc dbConnection = null;
173         try {
174            dbConnection = DAOUtils.getDBConnection(JNDINames.CUSTOMER_DATASOURCE);
175             stmt = dbConnection.prepareStatement(SELECT_USER_ID_QUERY_STR);
176             stmt.setString(1, userId.trim());
177             result = stmt.executeQuery();
178             if ( !result.next() ) {
179                 returnValue = false;
180             } else {
181                 userId = result.getString(1);
182                 returnValue = true;
183             }
184         } catch(SQLException JavaDoc se) {
185             throw new DAOSystemException("SQLException while checking for an"
186                            + " existing user - id -> " + userId + "\n", se);
187         } finally {
188             DAOUtils.closeResultSet(result);
189             DAOUtils.closeStatement(stmt);
190             DAOUtils.closeConnection(dbConnection);
191         }
192         return returnValue;
193     }
194
195
196     private Account selectAccount(String JavaDoc userId) throws
197       DAOSystemException, AccountDAOFinderException {
198
199         PreparedStatement JavaDoc stmt = null;
200         ResultSet JavaDoc result = null;
201
202         Connection JavaDoc dbConnection = null;
203         try {
204             dbConnection = DAOUtils.getDBConnection(JNDINames.CUSTOMER_DATASOURCE);
205             stmt = dbConnection.prepareStatement(SELECT_ACCOUNT_QUERY_STR);
206             stmt.setString(1, userId.trim());
207             result = stmt.executeQuery();
208
209             if ( !result.next() )
210                 throw new AccountDAOFinderException(
211                                   "No record for primary key " + userId);
212
213             int i = 1;
214             userId = result.getString(i++);
215             String JavaDoc email = result.getString(i++);
216             String JavaDoc firstName = result.getString(i++);
217             String JavaDoc lastName = result.getString(i++);
218             String JavaDoc street1 = result.getString(i++);
219             String JavaDoc street2 = result.getString(i++);
220             String JavaDoc city = result.getString(i++);
221             String JavaDoc state = result.getString(i++);
222             String JavaDoc zip = result.getString(i++);
223             String JavaDoc country = result.getString(i++);
224             String JavaDoc phone = result.getString(i++);
225
226             Address addr = new Address(street1, street2, city, state, zip,
227                                country);
228             ContactInformation info =
229                 new ContactInformation(lastName, firstName, phone,
230                                           email, addr);
231             return(new Account(userId, info));
232         } catch(SQLException JavaDoc se) {
233             throw new DAOSystemException("SQLException while getting " +
234                       "account; id = " + userId + " :\n", se);
235         } finally {
236             DAOUtils.closeResultSet(result);
237             DAOUtils.closeStatement(stmt);
238             DAOUtils.closeConnection(dbConnection);
239         }
240     }
241 }
242
Popular Tags