1 /*** $Id: GenericErrorHandler.java,v 1.1.1.1 2004/05/18 10:50:17 mochoa Exp $ */
2 package org.j2ee.dao.util;
3
4 // log4j
5 import org.apache.log4j.Logger;
6
7 // DAO
8 import org.j2ee.dao.DAOAction;
9 import org.j2ee.dao.DAOActionProcessor;
10 import org.j2ee.dao.DAOErrorHandler;
11 import org.j2ee.dao.DAOException;
12 import org.j2ee.dao.DAONoDataFoundException;
13 import org.j2ee.dao.DAOParentKeyNotFoundException;
14 import org.j2ee.dao.DAOCheckConstraintFailedException;
15 import org.j2ee.dao.DAOUniqueConstraintFailedException;
16 import org.j2ee.dao.DAORestrictUpdateFailedException;
17
18 /***
19 * This class is a Generic ErrorHandler setted by default by AbstractHibernateDAOAction and
20 * AbstractJdbcDAOAction as Error Handler.
21 * You can extend this class to provided custonized behaviour for
22 * the list of catched exceptions.
23 *
24 * Last modified $Date: 2004/05/18 10:50:17 $
25 * @version $Revision: 1.1.1.1 $
26 * @author mochoa (mochoa@users.sourceforge.net)
27 *
28 * @see org.j2ee.dao.AbstractHibernateDAOAction and
29 * @see org.j2ee.dao.AbstractJdbcDAOAction
30 */
31 public class GenericErrorHandler implements DAOErrorHandler {
32 private static Logger log = Logger.getLogger(GenericErrorHandler.class);
33
34 public GenericErrorHandler() {
35 }
36
37 /***
38 * raised when Hibernate or Toplink methed Load fail (Not Applicable for JDBC)
39 * @param d (The action, which throw the exception)
40 * @param p (The current processor, usefull if you want tray to execute some action)
41 * @param e (Original exception, the SQLException or Hibernate Exception is available through e.getCause()
42 * @see DAOException
43 */
44 public void noDataFound(DAOAction d, DAOActionProcessor p, Throwable e) throws DAOException {
45 log.error("No Data Found",e);
46 throw new DAONoDataFoundException("Throwing No Data Found Exception",e);
47 }
48
49 /***
50 * raised when the parent key not found
51 * @param d (The action, which throw the exception)
52 * @param p (The current processor, usefull if you want tray to execute some action)
53 * @param e (Original exception, the SQLException or Hibernate Exception is available through e.getCause()
54 * @see DAOException
55 */
56 public void parentKeyNotFound(DAOAction d, DAOActionProcessor p, Throwable e) throws DAOException {
57 log.error("Parent Key Not Found",e);
58 throw new DAOParentKeyNotFoundException("Throwing Parent Key Not Found",e);
59 }
60
61 /***
62 * raised when some check constraint fail
63 * @param d (The action, which throw the exception)
64 * @param p (The current processor, usefull if you want tray to execute some action)
65 * @param e (Original exception, the SQLException or Hibernate Exception is available through e.getCause()
66 * @see DAOException
67 */
68 public void checkConstraintFailed(DAOAction d, DAOActionProcessor p, Throwable e) throws DAOException {
69 log.error("Check Constraint Failed",e);
70 throw new DAOCheckConstraintFailedException("Throwing Check Constraint Failed",e);
71 }
72
73 /***
74 * raised when some unique constraint fail
75 * @param d (The action, which throw the exception)
76 * @param p (The current processor, usefull if you want tray to execute some action)
77 * @param e (Original exception, the SQLException or Hibernate Exception is available through e.getCause()
78 * @see DAOException
79 */
80 public void uniqueConstraintFailed(DAOAction d, DAOActionProcessor p, Throwable e) throws DAOException {
81 log.error("Unique Constraint Failed",e);
82 throw new DAOUniqueConstraintFailedException("Throwing Unique Constraint Failed",e);
83 }
84
85 /***
86 * raised when you try to delete or update and the foreign key fail. (cascade delete not enable for example)
87 * @param d (The action, which throw the exception)
88 * @param p (The current processor, usefull if you want tray to execute some action)
89 * @param e (Original exception, the SQLException or Hibernate Exception is available through e.getCause()
90 * @see DAOException
91 */
92 public void restrictUpdateDelete(DAOAction d, DAOActionProcessor p, Throwable e) throws DAOException {
93 log.error("Restrict Update/Delete Failed",e);
94 throw new DAORestrictUpdateFailedException("Throwing Restrict Update Failed",e);
95 }
96
97 public void handleError(DAOAction d, DAOActionProcessor p, Throwable e, int internalErrorCode) throws DAOException {
98 switch(internalErrorCode) {
99 case DAOErrorHandler.OK:break;
100 case DAOErrorHandler.NO_DATA_FOUND:noDataFound(d,p,e);break;
101 case DAOErrorHandler.PARENT_KEY_NOT_FOUND:parentKeyNotFound(d,p,e);break;
102 case DAOErrorHandler.CHECK_CONSTRAINT_FAILED:checkConstraintFailed(d,p,e);break;
103 case DAOErrorHandler.UNIQUE_CONSTRAINT_FAILED:uniqueConstraintFailed(d,p,e);break;
104 case DAOErrorHandler.RESTRICT_UPDATE_DELETE:restrictUpdateDelete(d,p,e);break;
105 default:throw (DAOException)e;
106 }
107 }
108 }
This page was automatically generated by Maven