1 package org.j2ee.dao.errors;
2
3 //sql
4 import java.sql.SQLException;
5
6 //Hibernate
7 import net.sf.hibernate.*;
8 import net.sf.hibernate.JDBCException;
9 import net.sf.hibernate.ObjectNotFoundException;
10
11 // log4j
12 import org.apache.log4j.Logger;
13
14 // DAO
15 import org.j2ee.dao.DAOErrorHandler;
16
17 /***
18 * This abstrac class is used to convert a database dependant representation of
19 * SQL Error Code to a list of known error codes defined into the interface DAOErrorHandler
20 *
21 * Last modified $Date: 2004/05/18 10:50:14 $
22 * @version $Revision: 1.1.1.1 $
23 * @author mochoa (mochoa@users.sourceforge.net)
24 *
25 * @see DAOErrorHandler
26 */
27 public abstract class AbstractErrorMapper implements ErrorMapper {
28 private static Logger log = Logger.getLogger(AbstractErrorMapper.class);
29
30 public AbstractErrorMapper() {
31 }
32
33 /***
34 *
35 * This do not work perfectly, see
36 * http://www.jguru.com/faq/view.jsp?EID=46397
37 * @param error, value returned by getError method of SQLException
38 * @return an internal representation for commons error codes
39 * @see DAOErrorHandler for a complete list of error code to return
40 */
41 public abstract int DBErrorConvert(int error);
42
43 public int DBErrorMapper(Throwable t) {
44 if (t==null)
45 return DAOErrorHandler.UNKNOWN_ERROR_CODE;
46 if (t instanceof SQLException) {
47 SQLException sqe = (SQLException)t;
48 log.debug("getErrorCode: "+sqe.getErrorCode());
49 log.debug("getSQLState : "+sqe.getSQLState());
50 return DBErrorConvert(sqe.getErrorCode());
51 } else if (t instanceof JDBCException) {
52 JDBCException jdbce = (JDBCException)t;
53 log.debug("getErrorCode: "+jdbce.getErrorCode());
54 log.debug("getSQLState : "+jdbce.getSQLState());
55 return DBErrorConvert(jdbce.getErrorCode());
56 } else if (t instanceof ObjectNotFoundException) {
57 ObjectNotFoundException onfe = (ObjectNotFoundException)t;
58 log.debug("ObjectNotFoundException.");
59 return DAOErrorHandler.NO_DATA_FOUND;
60 } else if (t instanceof HibernateException) {
61 if (t.getMessage().indexOf("row not found")>0)
62 return DAOErrorHandler.NO_DATA_FOUND;
63 else
64 return DAOErrorHandler.UNKNOWN_ERROR_CODE;
65 } else
66 return DAOErrorHandler.UNKNOWN_ERROR_CODE;
67 }
68 }
This page was automatically generated by Maven