001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.tide.hibernate;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    import org.granite.hibernate.HibernateOptimisticLockException;
027    import org.granite.messaging.service.ExceptionConverter;
028    import org.granite.messaging.service.ExtendedServiceExceptionHandler;
029    import org.granite.messaging.service.ServiceException;
030    import org.hibernate.HibernateException;
031    import org.hibernate.NonUniqueObjectException;
032    import org.hibernate.NonUniqueResultException;
033    import org.hibernate.ObjectNotFoundException;
034    import org.hibernate.StaleObjectStateException;
035    import org.hibernate.StaleStateException;
036    
037    
038    public class HibernateExceptionConverter implements ExceptionConverter {
039        
040        public static final String ENTITY_EXISTS = "Persistence.EntityExists";
041        public static final String ENTITY_NOT_FOUND = "Persistence.EntityNotFound";
042        public static final String NON_UNIQUE_RESULT = "Persistence.NonUnique";
043        public static final String NO_RESULT = "Persistence.NoResult";
044        public static final String OPTIMISTIC_LOCK = "Persistence.OptimisticLock";
045        public static final String TRANSACTION_REQUIRED = "Persistence.TransactionRequired";
046        public static final String ROLLBACK = "Persistence.Rollback";
047        public static final String OTHER = "Persistence.Error";
048        
049    
050        public boolean accepts(Throwable t, Throwable finalException) {
051            // Check if we could not let the JPA converter handle the exception
052            for (Throwable cause = finalException; cause != null && cause != t; cause = ExtendedServiceExceptionHandler.getCause(cause)) {
053                    if (cause.getClass().getName().startsWith("javax.persistence"))
054                            return false;
055            }
056            
057            return t.getClass().equals(NonUniqueObjectException.class)
058                || t.getClass().equals(ObjectNotFoundException.class)
059                || t.getClass().equals(NonUniqueResultException.class)
060                || StaleStateException.class.isAssignableFrom(t.getClass())
061                || HibernateException.class.isAssignableFrom(t.getClass());
062        }
063    
064        public ServiceException convert(Throwable t, String detail, Map<String, Object> extendedData) {
065            String error = null;
066            Map<String, Object> ex = null;
067            if (t.getClass().equals(NonUniqueObjectException.class))
068                error = ENTITY_EXISTS;
069            else if (t.getClass().equals(ObjectNotFoundException.class))
070                error = ENTITY_NOT_FOUND;
071            else if (t.getClass().equals(NonUniqueResultException.class))
072                error = NON_UNIQUE_RESULT;
073            else if (t.getClass().equals(StaleStateException.class) || t.getClass().equals(StaleObjectStateException.class)
074                            || t.getClass().equals(HibernateOptimisticLockException.class)) {
075                error = OPTIMISTIC_LOCK;
076                if (t instanceof HibernateOptimisticLockException) {
077                    ex = new HashMap<String, Object>();
078                    ex.put("entity", ((HibernateOptimisticLockException)t).getEntity());
079                }
080                else if (t instanceof StaleObjectStateException) {
081                    ex = new HashMap<String, Object>();
082                    ex.put("entityName", ((StaleObjectStateException)t).getEntityName());
083                    ex.put("identifier", ((StaleObjectStateException)t).getIdentifier());
084                }
085            }
086            else
087                error = OTHER; 
088            
089            ServiceException se = new ServiceException(error, t.getMessage(), detail, t);
090            if (ex != null && !ex.isEmpty())
091                se.getExtendedData().putAll(ex);
092            return se;
093        }
094    
095    }