1 package oracle.toplink.essentials.exceptions; 3 4 import java.util.*; 5 import oracle.toplink.essentials.exceptions.i18n.ExceptionMessageGenerator; 6 7 10 public class ConversionException extends TopLinkException { 11 protected Class classToConvertTo; 12 protected transient Object sourceObject; 13 public final static int COULD_NOT_BE_CONVERTED = 3001; 14 public final static int COULD_NOT_BE_CONVERTED_EXTENDED = 3002; 15 public final static int INCORRECT_DATE_FORMAT = 3003; 16 public final static int INCORRECT_TIME_FORMAT = 3004; 17 public final static int INCORRECT_TIMESTAMP_FORMAT = 3005; 18 public final static int COULD_NOT_CONVERT_TO_BYTE_ARRAY = 3006; 19 public final static int COULD_NOT_BE_CONVERTED_TO_CLASS = 3007; 20 public final static int INCORRECT_DATE_TIME_FORMAT = 3008; 21 22 26 protected ConversionException(String message, Object sourceObject, Class classToConvertTo, Exception exception) { 27 super(message, exception); 28 setSourceObject(sourceObject); 29 setClassToConvertTo(classToConvertTo); 30 } 31 32 public static ConversionException couldNotBeConverted(Object mapping, Object descriptor, ConversionException exception) { 36 Object sourceObject = exception.getSourceObject(); 37 Class javaClass = exception.getClassToConvertTo(); 38 Exception original = (Exception )exception.getInternalException(); 39 40 Object [] args = { sourceObject, sourceObject.getClass(), mapping, descriptor, javaClass }; 41 String message = ExceptionMessageGenerator.buildMessage(ConversionException.class, COULD_NOT_BE_CONVERTED_EXTENDED, args); 42 ConversionException conversionException = new ConversionException(message, sourceObject, javaClass, original); 43 conversionException.setStackTrace(exception.getStackTrace()); 44 conversionException.setErrorCode(COULD_NOT_BE_CONVERTED_EXTENDED); 45 return conversionException; 46 } 47 48 public static ConversionException couldNotBeConverted(Object object, Class javaClass) { 49 Object [] args = { object, object.getClass(), javaClass }; 50 String message = ExceptionMessageGenerator.buildMessage(ConversionException.class, COULD_NOT_BE_CONVERTED, args); 51 ConversionException conversionException = new ConversionException(message, object, javaClass, null); 52 conversionException.setErrorCode(COULD_NOT_BE_CONVERTED); 53 return conversionException; 54 } 55 56 public static ConversionException couldNotBeConverted(Object object, Class javaClass, Exception exception) { 57 Object [] args = { object, object.getClass(), javaClass }; 58 String message = ExceptionMessageGenerator.buildMessage(ConversionException.class, COULD_NOT_BE_CONVERTED, args); 59 ConversionException conversionException = new ConversionException(message, object, javaClass, exception); 60 conversionException.setErrorCode(COULD_NOT_BE_CONVERTED); 61 return conversionException; 62 } 63 64 public static ConversionException couldNotBeConvertedToClass(Object object, Class javaClass, Exception exception) { 65 Object [] args = { object, object.getClass(), javaClass }; 66 String message = ExceptionMessageGenerator.buildMessage(ConversionException.class, COULD_NOT_BE_CONVERTED_TO_CLASS, args); 67 ConversionException conversionException = new ConversionException(message, object, javaClass, exception); 68 conversionException.setErrorCode(COULD_NOT_BE_CONVERTED_TO_CLASS); 69 return conversionException; 70 } 71 72 public static ConversionException couldNotConvertToByteArray(Object object) { 73 Object [] args = { object }; 74 String message = ExceptionMessageGenerator.buildMessage(ConversionException.class, COULD_NOT_CONVERT_TO_BYTE_ARRAY, args); 75 ConversionException conversionException = new ConversionException(message, object, byte[].class, null); 76 conversionException.setErrorCode(COULD_NOT_CONVERT_TO_BYTE_ARRAY); 77 return conversionException; 78 } 79 80 public static ConversionException incorrectDateFormat(String dateString) { 81 Object [] args = { dateString }; 82 String message = ExceptionMessageGenerator.buildMessage(ConversionException.class, INCORRECT_DATE_FORMAT, args); 83 ConversionException conversionException = new ConversionException(message, dateString, java.sql.Date .class, null); 84 conversionException.setErrorCode(INCORRECT_DATE_FORMAT); 85 return conversionException; 86 } 87 88 public static ConversionException incorrectTimeFormat(String timeString) { 89 Object [] args = { timeString }; 90 String message = ExceptionMessageGenerator.buildMessage(ConversionException.class, INCORRECT_TIME_FORMAT, args); 91 ConversionException conversionException = new ConversionException(message, timeString, java.sql.Time .class, null); 92 conversionException.setErrorCode(INCORRECT_TIME_FORMAT); 93 return conversionException; 94 } 95 96 public static ConversionException incorrectTimestampFormat(String timestampString) { 97 Object [] args = { timestampString }; 98 String message = ExceptionMessageGenerator.buildMessage(ConversionException.class, INCORRECT_TIMESTAMP_FORMAT, args); 99 ConversionException conversionException = new ConversionException(message, timestampString, java.sql.Timestamp .class, null); 100 conversionException.setErrorCode(INCORRECT_TIMESTAMP_FORMAT); 101 return conversionException; 102 } 103 104 public static ConversionException incorrectDateTimeFormat(String dateTimeString) { 105 Object [] args = { dateTimeString }; 106 String message = ExceptionMessageGenerator.buildMessage(ConversionException.class, INCORRECT_DATE_TIME_FORMAT, args); 107 ConversionException conversionException = new ConversionException(message, dateTimeString, Calendar.class, null); 108 conversionException.setErrorCode(INCORRECT_DATE_TIME_FORMAT); 109 return conversionException; 110 } 111 112 116 public Class getClassToConvertTo() { 117 return classToConvertTo; 118 } 119 120 124 public Object getSourceObject() { 125 return sourceObject; 126 } 127 128 132 public void setClassToConvertTo(Class classToConvertTo) { 133 this.classToConvertTo = classToConvertTo; 134 } 135 136 140 public void setSourceObject(Object sourceObject) { 141 this.sourceObject = sourceObject; 142 } 143 } 144 | Popular Tags |