1 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 36 public abstract class DataAccessUtils { 37 38 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 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 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 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 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 174 public static int intResult(Collection results) 175 throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { 176 177 return ((Number ) objectResult(results, Number .class)).intValue(); 178 } 179 180 193 public static long longResult(Collection results) 194 throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException { 195 196 return ((Number ) objectResult(results, Number .class)).longValue(); 197 } 198 199 200 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 } 217 | Popular Tags |