01: /*
02: * $Id: LoginOkayImpl.java,v 1.8 2007/03/12 10:46:15 agoubard Exp $
03: */
04: package com.mycompany.petstore.api;
05:
06: import java.sql.ResultSet;
07: import java.sql.SQLException;
08: import java.sql.Statement;
09: import java.util.Iterator;
10:
11: /**
12: * Implementation of the <code>LoginOkay</code> function.
13: *
14: * @version $Revision: 1.8 $ $Date: 2007/03/12 10:46:15 $
15: * @author TODO
16: */
17: public final class LoginOkayImpl extends LoginOkay {
18:
19: /**
20: * Constructs a new <code>LoginOkayImpl</code> instance.
21: *
22: * @param api
23: * the API to which this function belongs, guaranteed to be not
24: * <code>null</code>.
25: */
26: public LoginOkayImpl(APIImpl api) {
27: super (api);
28: }
29:
30: /**
31: * Calls this function. If the function fails, it may throw any kind of
32: * exception. All exceptions will be handled by the caller.
33: *
34: * @param request
35: * the request, never <code>null</code>.
36: *
37: * @return
38: * the result of the function call, should never be <code>null</code>.
39: *
40: * @throws Throwable
41: * if anything went wrong.
42: */
43: public Result call(Request request) throws Throwable {
44: try {
45: Statement statement = _databaseConnection.getConnection()
46: .createStatement();
47: ResultSet rsLogin = statement
48: .executeQuery("SELECT password FROM CUSTOMERS WHERE email='"
49: + request.getEmail() + "'");
50: if (rsLogin.next()) {
51: String databasePassword = rsLogin.getString(1).trim();
52: if (!request.getPassword().equals(databasePassword)) {
53: return new IncorrectPasswordResult();
54: }
55: } else {
56: return new UnknownLoginResult();
57: }
58: } catch (SQLException sqlEx) {
59: DatabaseFailureResult databaseFailure = new DatabaseFailureResult();
60: databaseFailure.setDetails(sqlEx.getMessage());
61: return databaseFailure;
62: }
63:
64: // The customer successfully logged in.
65: _sessionManager.removeProperty("password");
66: _sessionManager.setProperty(_sessionManager.getSessionId(),
67: Boolean.TRUE);
68: SuccessfulResult result = new SuccessfulResult();
69: return result;
70: }
71: }
|