001: /*
002: * Copyright 2006 Davide Deidda
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package it.biobytes.ammentos.fieldtypes;
018:
019: import it.biobytes.ammentos.Field;
020: import it.biobytes.ammentos.FieldType;
021: import it.biobytes.ammentos.PersistenceException;
022:
023: import java.lang.reflect.InvocationTargetException;
024: import java.lang.reflect.Method;
025: import java.sql.PreparedStatement;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028: import java.sql.Types;
029:
030: /**
031: * <P>Enum specific field type. This field type can encapsulate any java enum
032: * constants.</P>
033: *
034: * @author Mark Bednarczyk
035: */
036: public class EnumType implements FieldType {
037:
038: private Class mappedClass;
039: private Method valueOfMethod;
040: private Method valuesMethod;
041:
042: public EnumType(Class mappedClass) throws Exception {
043: this .mappedClass = mappedClass;
044:
045: if (mappedClass.isEnum() == false) {
046: throw new IllegalArgumentException("mappedClass "
047: + mappedClass
048: + " needs to be declared using enum statement");
049: }
050:
051: valueOfMethod = mappedClass.getDeclaredMethod("valueOf",
052: String.class);
053: valuesMethod = mappedClass.getDeclaredMethod("values");
054: }
055:
056: public void setParamValue(Object fieldValue,
057: PreparedStatement pstmt, int paramIndex)
058: throws SQLException {
059: pstmt.setString(paramIndex, fieldValue.toString());
060: }
061:
062: /**
063: * <P>Method which invokes the Enum.valueOf(String) static method.
064: * All enums support this static method.</P>
065: * @param str string value to be converted into an enum using the
066: * Enum.valueOf(String) method.
067: *
068: * @return a new instance of the object. Since these are enum singleton
069: * class, this is a globally shared instance of the enum instance.
070: */
071: private Object valueOf(String str) {
072: try {
073: return valueOfMethod.invoke(null, str);
074:
075: } catch (SecurityException e) {
076: // TODO Auto-generated catch block
077: e.printStackTrace();
078: } catch (IllegalArgumentException e) {
079: // TODO Auto-generated catch block
080: e.printStackTrace();
081: } catch (IllegalAccessException e) {
082: // TODO Auto-generated catch block
083: e.printStackTrace();
084: } catch (InvocationTargetException e) {
085: // TODO Auto-generated catch block
086: e.printStackTrace();
087: }
088:
089: return null;
090: }
091:
092: /**
093: * <P>Method which invokes the Enum.valueOf(String) static method.
094: * All enums support this static method.</P>
095: * @param str string value to be converted into an enum using the
096: * Enum.valueOf(String) method.
097: *
098: * @return a new instance of the object. Since these are enum singleton
099: * class, this is a globally shared instance of the enum instance.
100: */
101: private Object[] values() {
102: try {
103: return (Object[]) valuesMethod.invoke(null);
104:
105: } catch (SecurityException e) {
106: // TODO Auto-generated catch block
107: e.printStackTrace();
108: } catch (IllegalArgumentException e) {
109: // TODO Auto-generated catch block
110: e.printStackTrace();
111: } catch (IllegalAccessException e) {
112: // TODO Auto-generated catch block
113: e.printStackTrace();
114: } catch (InvocationTargetException e) {
115: // TODO Auto-generated catch block
116: e.printStackTrace();
117: }
118:
119: return null;
120: }
121:
122: public Object loadValue(ResultSet rs, Field field)
123: throws SQLException {
124: return valueOf(rs.getString(field.getName()));
125: }
126:
127: public Object parseValue(String stringValue)
128: throws PersistenceException {
129: return valueOf(stringValue);
130: }
131:
132: public String formatValue(Object format) {
133: return format.toString();
134: }
135:
136: public Object generateValue() throws PersistenceException {
137: return new PersistenceException(
138: "unable to generate automatic value for enum type of "
139: + mappedClass);
140: }
141:
142: public boolean isNumeric() {
143: return false;
144: }
145:
146: public Object[] getPossibleValues() {
147: return values();
148: }
149:
150: public Class getMappedClass() {
151: return this .mappedClass;
152: }
153:
154: public Object addValues(Object arg0, Object arg1) {
155: return null;
156: }
157:
158: public int getSqlType() {
159: return Types.VARCHAR;
160: }
161: }
|