KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > types > EnumType


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access.types;
21
22 import java.lang.reflect.Method JavaDoc;
23 import java.sql.CallableStatement JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26
27 import org.apache.cayenne.dba.TypesMapping;
28 import org.apache.cayenne.map.DbAttribute;
29 import org.apache.cayenne.validation.ValidationResult;
30
31 /**
32  * An ExtendedType that handles an enum class. If Enum is mapped to a character column,
33  * its name is used as persistent value; if it is mapped to a numeric column, its ordinal
34  * (i.e. a position in enum class) is used.
35  * <p>
36  * <i>Requires Java 1.5 or newer</i>
37  * </p>
38  *
39  * @since 1.2
40  * @author Andrus Adamchik
41  */

42 public class EnumType implements ExtendedType {
43
44     protected Class JavaDoc enumClass;
45     protected Object JavaDoc[] values;
46
47     public EnumType(Class JavaDoc enumClass) {
48         if (enumClass == null) {
49             throw new IllegalArgumentException JavaDoc("Null enum class");
50         }
51
52         this.enumClass = enumClass;
53
54         try {
55             Method JavaDoc m = enumClass.getMethod("values");
56             this.values = (Object JavaDoc[]) m.invoke(null);
57         }
58         catch (Exception JavaDoc e) {
59             throw new IllegalArgumentException JavaDoc("Class "
60                     + enumClass.getName()
61                     + " is not an Enum", e);
62         }
63     }
64
65     public String JavaDoc getClassName() {
66         return enumClass.getName();
67     }
68
69     /**
70      * @deprecated since 3.0 as validation should not be done at the DataNode level.
71      */

72     public boolean validateProperty(
73             Object JavaDoc source,
74             String JavaDoc property,
75             Object JavaDoc value,
76             DbAttribute dbAttribute,
77             ValidationResult validationResult) {
78
79         return AbstractType.validateNull(
80                 source,
81                 property,
82                 value,
83                 dbAttribute,
84                 validationResult);
85     }
86
87     public void setJdbcObject(
88             PreparedStatement JavaDoc statement,
89             Object JavaDoc value,
90             int pos,
91             int type,
92             int precision) throws Exception JavaDoc {
93
94         if (value instanceof Enum JavaDoc) {
95
96             Enum JavaDoc e = (Enum JavaDoc) value;
97
98             if (TypesMapping.isNumeric(type)) {
99                 statement.setInt(pos, e.ordinal());
100             }
101             else {
102                 statement.setString(pos, e.name());
103             }
104         }
105         else {
106             statement.setNull(pos, type);
107         }
108     }
109
110     public Object JavaDoc materializeObject(ResultSet JavaDoc rs, int index, int type) throws Exception JavaDoc {
111         if (TypesMapping.isNumeric(type)) {
112             int i = rs.getInt(index);
113             return (rs.wasNull() || index < 0) ? null : values[i];
114         }
115         else {
116             String JavaDoc string = rs.getString(index);
117             return string != null ? Enum.valueOf(enumClass, string) : null;
118         }
119     }
120
121     public Object JavaDoc materializeObject(CallableStatement JavaDoc rs, int index, int type)
122             throws Exception JavaDoc {
123
124         if (TypesMapping.isNumeric(type)) {
125             int i = rs.getInt(index);
126             return (rs.wasNull() || index < 0) ? null : values[i];
127         }
128         else {
129             String JavaDoc string = rs.getString(index);
130             return string != null ? Enum.valueOf(enumClass, string) : null;
131         }
132     }
133 }
134
Popular Tags