View Javadoc
1 /*** 2 * $Id: AbstractHibernateDAOAction.java,v 1.1.1.1 2004/05/18 10:50:12 mochoa Exp $ 3 * */ 4 5 package org.j2ee.dao; 6 import java.util.List; 7 8 // DAO 9 import org.j2ee.dao.util.GenericErrorHandler; 10 11 12 /*** 13 * <br> 14 * Based on an example of book <i>Code Notes for J2EE, Edited by GREGORY BRILL, 15 * e-ISBN 0-679-64727-9</i> 16 * <br> 17 * Last modified $Date: 2004/05/18 10:50:12 $ 18 * @version $Revision: 1.1.1.1 $ 19 * @author mochoa (mochoa@users.sourceforge.net)*/ 20 public abstract class AbstractHibernateDAOAction implements HibernateDAOAction { 21 22 /*** 23 * Define if SQL o HQL queries should be cached 24 */ 25 private boolean caching = false; 26 27 /*** 28 * Operation of this action, default HQL_QUERY 29 * @see HibernateAction 30 */ 31 private int stmtType = HQL_QUERY; 32 33 /*** 34 * ErrorHandler used by this action 35 */ 36 private DAOErrorHandler errorHandler = new GenericErrorHandler(); 37 38 /*** 39 * Default constructor, set HQL operation as default 40 */ 41 public AbstractHibernateDAOAction() { setStmtType(HQL_QUERY); } 42 43 /*** 44 * Specialized constructor 45 * @param stmtTypeValue type of operation of this action 46 */ 47 public AbstractHibernateDAOAction(int stmtTypeValue) { setStmtType(stmtTypeValue); } 48 49 /*** 50 * REDEFINE THIS METHOD ON EVERY SUBCLASS 51 * Set the private attribute, the transfer object. 52 * Concrete implementation of this interface must be properly 53 * implement this method to provide an uniform interface for all the actions. 54 * @param to (The Transfer Object used by the action) 55 */ 56 public void setTO(Object to) { 57 } 58 59 /*** 60 * REDEFINE THIS METHOD ON EVERY SUBCLASS 61 * The transfer object used or setted by the action. 62 * @return the transfer object 63 */ 64 public Object getTO() { 65 return null; 66 } 67 68 /*** 69 * This method allows to the action to set a class which manage application 70 * errors such as integrity constraint violated. 71 * @param handler a instance with the application code to manage the exception 72 * @see DAOErrorHandler interface for a list of the code passed to method 73 */ 74 public void setErrorHandler(DAOErrorHandler handler) { 75 errorHandler = handler; 76 } 77 78 /*** 79 * Implement this method if you are writting a SQL based Action 80 * @return SQL string to be executed by the action 81 */ 82 public String getSQL() { 83 return null; 84 } 85 86 /*** 87 * Implement this method if you are writting a HQL based Action 88 * @return HQL String to be executed 89 */ 90 public String getHQL() { 91 return null; 92 } 93 94 /*** 95 * @return an Object array with the parameters values for the HQL or SQL query 96 */ 97 public Object[] getParameters() { 98 return null; 99 } 100 101 /*** 102 * @return an String array with the names of the parameters declared 103 * into the HQL o SQL query 104 */ 105 public String[] getNamesForParams() { 106 return null; 107 } 108 109 /*** 110 * Hibernate requieres a class for every object returned by an SQL query 111 * @return array of Class objects return by the SQL query 112 */ 113 public Class [] getReturnClassForSqlQuery() { 114 return null; 115 } 116 117 /*** 118 * Alias used into the SQL query with the sintax {alias} 119 * @return an array of alias used into the SQL query 120 */ 121 public String[] getReturnAliasForSqlQuery() { 122 return null; 123 } 124 125 /*** 126 * getter for private attribute 127 * @return stmtType 128 */ 129 public int getStmtType() { 130 return stmtType; 131 } 132 133 /*** 134 * setter method for private attribute 135 * @param stmtType value 136 */ 137 public void setStmtType(int stmtType) { 138 this.stmtType = stmtType; 139 } 140 141 /*** 142 * @return true if the action is based on SQL query 143 */ 144 public boolean isSqlQuery() { 145 return (stmtType == SQL_QUERY); 146 } 147 148 /*** 149 * @return true if the action is based on HQL query 150 */ 151 public boolean isHqlQuery() { 152 return (stmtType == HQL_QUERY); 153 } 154 155 /*** 156 * @return true if the action is an Hibernate save operation 157 */ 158 public boolean isSave() { 159 return (stmtType == SAVE); 160 } 161 162 /*** 163 * @return true if the action is an Hibernate saveOrUpdate operation 164 */ 165 public boolean isUpdate() { 166 return (stmtType == UPDATE); 167 } 168 169 /*** 170 * @return true if the action is an Hibernate save operation 171 */ 172 public boolean isDelete() { 173 return (stmtType == DELETE); 174 } 175 176 /*** 177 * @return true if the action is an Hibernate load operation 178 */ 179 public boolean isLoad() { 180 return (stmtType == LOAD); 181 } 182 183 /*** 184 * Called by the Processor after an SQL or HQL succefull operation 185 * @param rs List with the object results 186 * @throws Exception generated by this method 187 */ 188 public void handleResults(List rs) throws Exception { 189 } 190 191 /*** 192 * Called by the Processor after an Hibernate succefull load operation 193 * @param to the object loaded 194 * @throws Exception 195 */ 196 public void handleResult(Object to) throws Exception { 197 } 198 199 /*** 200 * Called by the Processor after an Hibernate succefull 201 * save,update,delete operation 202 * @throws Exception generated by this method 203 */ 204 public void handleResult() throws Exception { 205 } 206 207 /*** 208 * Execute this action using the given processor 209 * If there are exception, try to conver it with the Error Mapper and then 210 * call to Error Handler of this aciont 211 * @param processor which will execute the action 212 * @throws DAOException generated by this method 213 */ 214 public void execute(DAOActionProcessor processor) throws DAOException { 215 try { 216 processor.process(this); 217 } catch (DAOException de) { 218 if (errorHandler != null) { 219 // Call to the error handler to manage applications errors 220 int internaleErrorCode = DAOErrorHandler.UNKNOWN_ERROR_CODE; 221 Throwable originalException = de.getCause(); 222 if (originalException != null) { 223 internaleErrorCode = 224 processor.getErrorMapper().DBErrorMapper(originalException); 225 } 226 errorHandler.handleError(this,processor,de,internaleErrorCode); 227 } else // No error handler pass the exception as is 228 throw de; 229 } 230 } 231 232 /*** 233 * @return true if this HQL or SQL operation should be cached by the processor 234 */ 235 public boolean isCaching() { 236 return caching; 237 } 238 239 /*** 240 * Defines if the HQL or SQL query should be cached by the processor 241 * @param caching 242 */ 243 public void setCaching(boolean caching) { 244 this.caching = caching; 245 } 246 }

This page was automatically generated by Maven