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