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
021package org.granite.tide.data;
022
023import java.util.HashMap;
024import java.util.Map;
025
026import javax.persistence.EntityExistsException;
027import javax.persistence.EntityNotFoundException;
028import javax.persistence.NoResultException;
029import javax.persistence.NonUniqueResultException;
030import javax.persistence.OptimisticLockException;
031import javax.persistence.PersistenceException;
032import javax.persistence.RollbackException;
033import javax.persistence.TransactionRequiredException;
034
035import org.granite.messaging.service.ExceptionConverter;
036import org.granite.messaging.service.ServiceException;
037
038
039public class PersistenceExceptionConverter implements ExceptionConverter {
040    
041    public static final String ENTITY_EXISTS = "Persistence.EntityExists";
042    public static final String ENTITY_NOT_FOUND = "Persistence.EntityNotFound";
043    public static final String NON_UNIQUE_RESULT = "Persistence.NonUnique";
044    public static final String NO_RESULT = "Persistence.NoResult";
045    public static final String OPTIMISTIC_LOCK = "Persistence.OptimisticLock";
046    public static final String TRANSACTION_REQUIRED = "Persistence.TransactionRequired";
047    public static final String ROLLBACK = "Persistence.Rollback";
048    public static final String OTHER = "Persistence.Error";
049    
050
051    public boolean accepts(Throwable t, Throwable finalException) {
052        return t.getClass().equals(EntityExistsException.class) 
053            || t.getClass().equals(EntityNotFoundException.class)
054            || t.getClass().equals(NonUniqueResultException.class)
055            || t.getClass().equals(NoResultException.class)
056            || t.getClass().equals(OptimisticLockException.class)
057            || t.getClass().equals(TransactionRequiredException.class)
058            || t.getClass().equals(RollbackException.class)
059            || PersistenceException.class.isAssignableFrom(t.getClass());
060    }
061
062    public ServiceException convert(Throwable t, String detail, Map<String, Object> extendedData) {
063        String error = null;
064        Map<String, Object> ex = null;
065        if (t.getClass().equals(EntityExistsException.class))
066            error = ENTITY_EXISTS;
067        else if (t.getClass().equals(EntityNotFoundException.class))
068            error = ENTITY_NOT_FOUND;
069        else if (t.getClass().equals(NonUniqueResultException.class))
070            error = NON_UNIQUE_RESULT;
071        else if (t.getClass().equals(NoResultException.class))
072            error = NO_RESULT;
073        else if (t.getClass().equals(OptimisticLockException.class)) {
074            error = OPTIMISTIC_LOCK;
075            ex = new HashMap<String, Object>();
076            ex.put("entity", ((OptimisticLockException)t).getEntity());
077        }
078        else if (t.getClass().equals(TransactionRequiredException.class))
079            error = TRANSACTION_REQUIRED;
080        else if (t.getClass().equals(RollbackException.class))
081            error = ROLLBACK;
082        else
083            error = OTHER; 
084        
085        ServiceException se = new ServiceException(error, t.getMessage(), detail, t);
086        if (ex != null && !ex.isEmpty())
087            se.getExtendedData().putAll(ex);
088        return se;
089    }
090
091}