1 /*** $Id: AbstractJdbcDAOAction.java,v 1.1.1.1 2004/05/18 10:50:12 mochoa Exp $
2 * */
3 package org.j2ee.dao;
4
5 //sql
6 import java.sql.PreparedStatement;
7 import java.sql.CallableStatement;
8 import java.sql.ResultSet;
9 import java.sql.SQLException;
10
11 // DAO
12 import org.j2ee.dao.util.GenericErrorHandler;
13
14 /***
15 * <br>
16 * Based on an example of book <i>Code Notes for J2EE, Edited by GREGORY BRILL,
17 * e-ISBN 0-679-64727-9</i>
18 * <br>
19 * Last modified $Date: 2004/05/18 10:50:12 $
20 * @version $Revision: 1.1.1.1 $
21 * @author jvlio (jvlio@users.sourceforge.net)*/
22 public abstract class AbstractJdbcDAOAction implements JdbcDAOAction {
23
24 PreparedStatement pstmt = null;
25 Exception exception = null;
26 private boolean caching = false;
27 private int stmtType = QUERY;
28 private DAOErrorHandler errorHandler = new GenericErrorHandler();
29
30 public AbstractJdbcDAOAction() { setStmtType(QUERY); }
31 public AbstractJdbcDAOAction(int stmtType) { setStmtType(stmtType); }
32
33 /***
34 * REDEFINE THIS METHOD ON EVERY SUBCLASS
35 * Set the private attribute, the transfer object.
36 * Concrete implementation of this interface must be properly
37 * implement this method to provide an uniform interface for all the actions.
38 * @param to (The Transfer Object used by the action)
39 */
40 public void setTO(Object to) {
41 } ;
42
43 /***
44 * REDEFINE THIS METHOD ON EVERY SUBCLASS
45 * The transfer object used or setted by the action.
46 * @return the transfer object-
47 */
48 public Object getTO() {
49 return null;
50 };
51
52 /***
53 * This method allows to the action to set a class which manage application
54 * errors such as integrity constraint violated.
55 * @param handler a instance with the application code to manage the exception
56 * @see DAOErrorHandler interface for a list of the code passed to method
57 */
58 public void setErrorHandler(DAOErrorHandler handler) {
59 errorHandler = handler;
60 }
61
62 public abstract String getSQL();
63 public Object[] getParameters() { return null; }
64 public int[] getDataTypesForParams() { return null; }
65 public int[] getDataTypesForOutParams() { return null; }
66
67 public void cachPreparedStatement(PreparedStatement pstmt) {
68 if (this.isCaching()) this.pstmt=pstmt;
69 };
70 public PreparedStatement getPreparedStatement() { return pstmt; }
71
72 public int getStmtType() { return stmtType; }
73 public void setStmtType(int stmtType) { this.stmtType=stmtType; }
74 public boolean isQuery() { return (stmtType==QUERY); }
75 public boolean isUpdate() { return (stmtType==UPDATE); }
76 public boolean isCall() { return (stmtType==CALL_PROCEDURE || stmtType==CALL_FUNCTION); }
77
78 public void handleResults(ResultSet rs) throws SQLException {};
79 public void handleResults(int rowsAffected) throws SQLException {};
80 public void handleResults(CallableStatement cstmt) throws SQLException {};
81
82 public void execute(DAOActionProcessor processor) throws DAOException {
83 try {
84 processor.process(this);
85 } catch (DAOException de) {
86 if (errorHandler!=null) {// Call to the error handler to manage applications errors
87 int internaleErrorCode = DAOErrorHandler.UNKNOWN_ERROR_CODE;
88 Throwable originalException = de.getCause();
89 if (originalException!=null)
90 internaleErrorCode = processor.getErrorMapper().DBErrorMapper(originalException);
91 errorHandler.handleError(this,processor,de,internaleErrorCode);
92 } else // No error handler pass the exception as is
93 throw de;
94 }
95 }
96
97 public boolean isCaching() { return caching; }
98 public void setCaching(boolean caching) { this.caching = caching; }
99 }
This page was automatically generated by Maven