01: /*
02: * $Id: SearchPetOkayImpl.java,v 1.5 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:
10: /**
11: * Implementation of the <code>SearchPetOkay</code> function.
12: *
13: * <p>Description: Get the list of pets starting with the typed letters.
14: *
15: * @version $Revision: 1.5 $ $Date: 2007/03/12 10:46:15 $
16: * @author John Doe (<a href="mailto:john.doe@mycompany.com">john.doe@mycompany.com</a>)
17: */
18: public final class SearchPetOkayImpl extends SearchPetOkay {
19:
20: /**
21: * Constructs a new <code>SearchPetOkayImpl</code> instance.
22: *
23: *
24: * @param api
25: * the API to which this function belongs, guaranteed to be not
26: * <code>null</code>.
27: */
28: public SearchPetOkayImpl(APIImpl api) {
29: super (api);
30: }
31:
32: /**
33: * Calls this function. If the function fails, it may throw any kind of
34: * exception. All exceptions will be handled by the caller.
35: *
36: * @param request
37: * the request, never <code>null</code>.
38: *
39: * @return
40: * the result of the function call, should never be <code>null</code>.
41: *
42: * @throws Throwable
43: * if anything went wrong.
44: */
45: public Result call(Request request) throws Throwable {
46:
47: /* This function can be accessed with the _xins-std calling convention.
48: if (!_sessionManager.getBoolProperty(_sessionManager.getSessionId())) {
49: return new NotLoggedInResult();
50: }*/
51:
52: SuccessfulResult result = new SuccessfulResult();
53: try {
54: Statement statement = _databaseConnection.getConnection()
55: .createStatement();
56: String query = "SELECT id, petname, price, age FROM PETS";
57: if (request.isSetPetName()) {
58: query += " WHERE petname LIKE '" + request.getPetName()
59: + "%'";
60: }
61: ResultSet rsPets = statement.executeQuery(query);
62: while (rsPets.next()) {
63: Pet pet = new Pet();
64: pet.setPetID(Integer.parseInt(rsPets.getString(1)));
65: pet.setPetName(rsPets.getString(2).trim());
66: pet.setPrice(rsPets.getFloat(3));
67: pet.setAge(Byte.parseByte(rsPets.getString(4)));
68: result.addPet(pet);
69: }
70: rsPets.close();
71: } catch (SQLException sqlEx) {
72: sqlEx.printStackTrace();
73: return new DatabaseFailureResult();
74: }
75: return result;
76: }
77: }
|