01: /*
02: * $Id: RegisterCustomerOkayImpl.java,v 1.8 2007/03/12 10:46:15 agoubard Exp $
03: */
04: package com.mycompany.petstore.api;
05:
06: import com.mycompany.petstore.types.Salutation;
07: import java.sql.ResultSet;
08: import java.sql.SQLException;
09: import java.sql.Statement;
10:
11: /**
12: * Implementation of the <code>RegisterCustomer</code> function.
13: *
14: * <p>Description: Register a new client.
15: *
16: * @version $Revision: 1.8 $ $Date: 2007/03/12 10:46:15 $
17: * @author John Doe (<a href="mailto:john.doe@mycompany.com">john.doe@mycompany.com</a>)
18: */
19: public final class RegisterCustomerOkayImpl extends
20: RegisterCustomerOkay {
21:
22: /**
23: * Constructs a new <code>RegisterCustomerOkayImpl</code> instance.
24: *
25: * @param api
26: * the API to which this function belongs, guaranteed to be not
27: * <code>null</code>.
28: */
29: public RegisterCustomerOkayImpl(APIImpl api) {
30: super (api);
31: }
32:
33: /**
34: * Calls this function. If the function fails, it may throw any kind of
35: * exception. All exceptions will be handled by the caller.
36: *
37: * @param request
38: * the request, never <code>null</code>.
39: *
40: * @return
41: * the result of the function call, should never be <code>null</code>.
42: *
43: * @throws Throwable
44: * if anything went wrong.
45: */
46: public Result call(Request request) throws Throwable {
47: try {
48: Statement statement = _databaseConnection.getConnection()
49: .createStatement();
50: ResultSet rsLogin = statement
51: .executeQuery("SELECT password FROM CUSTOMERS WHERE email='"
52: + request.getEmail() + "'");
53: if (rsLogin.next()) {
54: return new AlreadyRegisteredResult();
55: }
56: } catch (SQLException sqlEx) {
57: return new DatabaseFailureResult();
58: }
59: try {
60: Statement statement = _databaseConnection.getConnection()
61: .createStatement();
62: statement
63: .executeUpdate("INSERT INTO CUSTOMERS (email, password, gender, firstname, lastname, address, phonenumber) "
64: + "VALUES ('"
65: + request.getEmail()
66: + "', '"
67: + request.getPassword()
68: + "', '"
69: + (request.getSalutation() == Salutation.MALE ? "M"
70: : "F")
71: + "', '"
72: + request.getFirstName()
73: + "', '"
74: + request.getLastName()
75: + "', '"
76: + request.getAddress()
77: + "', '"
78: + (request.isSetPhoneNumber() ? request
79: .getPhoneNumber() : "") + "')");
80: } catch (SQLException sqlEx) {
81: return new DatabaseFailureResult();
82: }
83: SuccessfulResult result = new SuccessfulResult();
84: return result;
85: }
86: }
|