source: ether_2012/persistence/interface/com/ether/PersistenceHelper.java @ 319

Last change on this file since 319 was 319, checked in by vmipsl, 12 years ago

Import du projet Ether pour le nouveau look 2012

File size: 3.5 KB
Line 
1package com.ether;
2
3import java.util.Collection;
4import com.ether.PersistenceException;
5
6import org.apache.commons.logging.Log;
7import org.apache.commons.logging.LogFactory;
8import org.springframework.dao.CleanupFailureDataAccessException;
9import org.springframework.dao.DataAccessException;
10
11public final class PersistenceHelper
12{
13    /**
14     * Returns an unique result from a collection of results.
15     *
16     * @throws PersistenceException : if more than one result is contained in the collection an exception is thrown.
17     */
18    public static <O> O uniqueResult(final Collection<O> results)
19            throws PersistenceException
20    {
21        if (results == null || results.isEmpty())
22            return null;
23        else if (results.size() > 1)
24            throw new PersistenceException(PersistenceException.PersistenceCode.INVALID_API_USAGE, "unique result expected !");
25        else
26            return results.iterator().next();
27    }
28
29    public static <O> O firstResult(final Collection<O> results)
30    {
31        if (results == null || results.isEmpty()) return null;
32        return results.iterator().next();
33    }
34 
35    /**
36     * Converts an exception to a DAO exception.
37     *
38     * @param exception : the exception.
39     * @return the DAO exception.
40     */
41    public static PersistenceException convert(final Exception exception)
42    {
43        // Propagates the exception if exception is persistence exception
44        if (exception instanceof PersistenceException)
45            return (PersistenceException) exception;
46
47        // Rewraps exception if data access exception
48        final PersistenceException.PersistenceCode code;
49
50        if (exception instanceof DataAccessException)
51        {
52            if (exception instanceof CleanupFailureDataAccessException)
53            {
54                code = PersistenceException.PersistenceCode.CLEANUP_FAILURE;
55            }
56            else if (exception instanceof org.springframework.dao.ConcurrencyFailureException)
57            {
58                code = PersistenceException.PersistenceCode.CONCURRENCY_FAILURE;
59            }
60            else if (exception instanceof org.springframework.dao.DataAccessResourceFailureException)
61            {
62                code = PersistenceException.PersistenceCode.RESOURCE_FAILURE;
63            }
64            else if (exception instanceof org.springframework.dao.DataIntegrityViolationException)
65            {
66                code = PersistenceException.PersistenceCode.INTEGRITY_VIOLATION;
67            }
68            else if (exception instanceof org.springframework.dao.DataRetrievalFailureException)
69            {
70                code = PersistenceException.PersistenceCode.RETRIEVAL_FAILURE;
71            }
72            else if (exception instanceof org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException)
73            {
74                code = PersistenceException.PersistenceCode.DATA_SOURCE_LOOKUP_FAILURE;
75            }
76            else if (exception instanceof org.springframework.dao.InvalidDataAccessApiUsageException)
77            {
78                code = PersistenceException.PersistenceCode.INVALID_API_USAGE;
79            }
80            else if (exception instanceof org.springframework.dao.PermissionDeniedDataAccessException)
81            {
82                code = PersistenceException.PersistenceCode.PERMISSION_DENIED;
83            }
84            else
85            {
86                code = PersistenceException.PersistenceCode.UNKNOWN_FAILURE;
87            }
88        }
89        else
90        {
91            code = PersistenceException.PersistenceCode.UNKNOWN_FAILURE;
92        }
93
94        final StringBuilder message = new StringBuilder();
95        message.append(exception.getMessage());
96
97        final Throwable cause = exception.getCause();
98        if (null != cause)
99        {
100            message.append(" : ").append(cause.getMessage());
101        }
102
103        return new PersistenceException(code, message.toString(),
104                exception);
105    }
106
107    private static final Log LOGGER = LogFactory.getLog(PersistenceHelper.class);
108}
Note: See TracBrowser for help on using the repository browser.