1 /*
2 * Copyright 2002-2006 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.springframework.dao.support;
18
19 import java.util.Collection;
20
21 import org.springframework.dao.DataAccessException;
22 import org.springframework.dao.EmptyResultDataAccessException;
23 import org.springframework.dao.IncorrectResultSizeDataAccessException;
24 import org.springframework.dao.TypeMismatchDataAccessException;
25 import org.springframework.util.Assert;
26 import org.springframework.util.CollectionUtils;
27 import org.springframework.util.NumberUtils;
28
29 /**
30 * Miscellaneous utility methods for DAO implementations.
31 * Useful with any data access technology.
32 *
33 * @author Juergen Hoeller
34 * @since 1.0.2
35 */
36 public abstract class DataAccessUtils {
37
38 /**
39 * Return a single result object from the given Collection.
40 * <p>Returns <code>null</code> if 0 result objects found;
41 * throws an exception if more than 1 element found.
42 * @param results the result Collection (can be <code>null</code>)
43 * @return the single result object, or <code>null</code> if none
44 * @throws IncorrectResultSizeDataAccessException if more than one
45 * element has been found in the given Collection
46 */
47 public static Object singleResult(Collection results) throws IncorrectResultSizeDataAccessException {
48 int size = (results != null ? results.size() : 0);
49 if (size == 0) {
50 return null;
51 }
52 if (results.size() > 1) {
53 throw new IncorrectResultSizeDataAccessException(1, size);
54 }
55 return results.iterator().next();
56 }
57
58 /**
59 * Return a single result object from the given Collection.
60 * <p>Throws an exception if 0 or more than 1 element found.
61 * @param results the result Collection (can be <code>null</code>)
62 * @return the single result object
63 * @throws IncorrectResultSizeDataAccessException if more than one
64 * element has been found in the given Collection
65 * @throws EmptyResultDataAccessException if no element at all
66 * has been found in the given Collection
67 */
68 public static Object requiredSingleResult(Collection results) throws IncorrectResultSizeDataAccessException {
69 int size = (results != null ? results.size() : 0);
70 if (size == 0) {
71 throw new EmptyResultDataAccessException(1);
72 }
73 if (results.size() > 1) {
74 throw new IncorrectResultSizeDataAccessException(1, size);
75 }
76 return results.iterator().next();
77 }
78
79 /**
80 * Return a unique result object from the given Collection.
81 * <p>Returns <code>null</code> if 0 result objects found;
82 * throws an exception if more than 1 instance found.
83 * @param results the result Collection (can be <code>null</code>)
84 * @return the unique result object, or <code>null</code> if none
85 * @throws IncorrectResultSizeDataAccessException if more than one
86 * result object has been found in the given Collection
87 * @see org.springframework.util.CollectionUtils#hasUniqueObject
88 */
89 public static Object uniqueResult(Collection results) throws IncorrectResultSizeDataAccessException {
90 int size = (results != null ? results.size() : 0);
91 if (size == 0) {
92 return null;
93 }
94 if (!CollectionUtils.hasUniqueObject(results)) {
95 throw new IncorrectResultSizeDataAccessException(1, size);
96 }
97 return results.iterator().next();
98 }
99
100 /**
101 * Return a unique result object from the given Collection.
102 * <p>Throws an exception if 0 or more than 1 instance found.
103 * @param results the result Collection (can be <code>null</code>)
104 * @return the unique result object
105 * @throws IncorrectResultSizeDataAccessException if more than one
106 * result object has been found in the given Collection
107 * @throws EmptyResultDataAccessException if no result object at all
108 * has been found in the given Collection
109 * @see org.springframework.util.CollectionUtils#hasUniqueObject
110 */
111 public static Object requiredUniqueResult(Collection results) throws IncorrectResultSizeDataAccessException {
112 int size = (results != null ? results.size() : 0);
113 if (size == 0) {
114 throw new EmptyResultDataAccessException(1);
115 }
116 if (!CollectionUtils.hasUniqueObject(results)) {
117 throw new IncorrectResultSizeDataAccessException(1, size);
118 }
119 return results.iterator().next();
120 }
121
122 /**
123 * Return a unique result object from the given Collection.
124 * Throws an exception if 0 or more than 1 result objects found,
125 * of if the unique result object is not convertable to the
126 * specified required type.
127 * @param results the result Collection (can be <code>null</code>)
128 * @return the unique result object
129 * @throws IncorrectResultSizeDataAccessException if more than one
130 * result object has been found in the given Collection
131 * @throws EmptyResultDataAccessException if no result object
132 * at all has been found in the given Collection
133 * @throws TypeMismatchDataAccessException if the unique object does
134 * not match the specified required type
135 */
136 public static Object objectResult(Collection results, Class requiredType)
137 throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException {
138
139 Object result = requiredUniqueResult(results);
140 if (requiredType != null && !requiredType.isInstance(result)) {
141 if (String.class.equals(requiredType)) {
142 result = result.toString();
143 }
144 else if (Number.class.isAssignableFrom(requiredType) && Number.class.isInstance(result)) {
145 try {
146 result = NumberUtils.convertNumberToTargetClass(((Number) result), requiredType);
147 }
148 catch (IllegalArgumentException ex) {
149 throw new TypeMismatchDataAccessException(ex.getMessage());
150 }
151 }
152 else {
153 throw new TypeMismatchDataAccessException(
154 "Result object is of type [" + result.getClass().getName() +
155 "] and could not be converted to required type [" + requiredType.getName() + "]");
156 }
157 }
158 return result;
159 }
160
161 /**
162 * Return a unique int result from the given Collection.
163 * Throws an exception if 0 or more than 1 result objects found,
164 * of if the unique result object is not convertable to an int.
165 * @param results the result Collection (can be <code>null</code>)
166 * @return the unique int result
167 * @throws IncorrectResultSizeDataAccessException if more than one
168 * result object has been found in the given Collection
169 * @throws EmptyResultDataAccessException if no result object
170 * at all has been found in the given Collection
171 * @throws TypeMismatchDataAccessException if the unique object
172 * in the collection is not convertable to an int
173 */
174 public static int intResult(Collection results)
175 throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException {
176
177 return ((Number) objectResult(results, Number.class)).intValue();
178 }
179
180 /**
181 * Return a unique long result from the given Collection.
182 * Throws an exception if 0 or more than 1 result objects found,
183 * of if the unique result object is not convertable to a long.
184 * @param results the result Collection (can be <code>null</code>)
185 * @return the unique long result
186 * @throws IncorrectResultSizeDataAccessException if more than one
187 * result object has been found in the given Collection
188 * @throws EmptyResultDataAccessException if no result object
189 * at all has been found in the given Collection
190 * @throws TypeMismatchDataAccessException if the unique object
191 * in the collection is not convertable to a long
192 */
193 public static long longResult(Collection results)
194 throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException {
195
196 return ((Number) objectResult(results, Number.class)).longValue();
197 }
198
199
200 /**
201 * Return a translated exception if this is appropriate,
202 * otherwise return the input exception.
203 * @param rawException exception we may wish to translate
204 * @param pet PersistenceExceptionTranslator to use to perform the translation
205 * @return a translated exception if translation is possible, or
206 * the raw exception if it is not
207 */
208 public static RuntimeException translateIfNecessary(
209 RuntimeException rawException, PersistenceExceptionTranslator pet) {
210
211 Assert.notNull(pet, "PersistenceExceptionTranslator must not be null");
212 DataAccessException dex = pet.translateExceptionIfPossible(rawException);
213 return (dex != null ? dex : rawException);
214 }
215
216 }