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 package org.apache.openjpa.jdbc.meta.strats;
20
21 import java.lang.reflect.Method;
22
23 import org.apache.openjpa.jdbc.kernel.JDBCStore;
24 import org.apache.openjpa.jdbc.meta.ValueMapping;
25 import org.apache.openjpa.jdbc.schema.Column;
26 import org.apache.openjpa.jdbc.schema.ColumnIO;
27 import org.apache.openjpa.meta.JavaTypes;
28 import org.apache.openjpa.util.MetaDataException;
29
30 /**
31 * Value handler for JDK1.5 enum field types.
32 *
33 * @nojavadoc
34 */
35 public class EnumValueHandler
36 extends AbstractValueHandler {
37
38 private Enum[] _vals = null;
39 private boolean _ordinal = false;
40
41 /**
42 * Whether to store the enum value as its ordinal.
43 */
44 public boolean getStoreOrdinal() {
45 return _ordinal;
46 }
47
48 /**
49 * Whether to store the enum value as its ordinal.
50 */
51 public void setStoreOrdinal(boolean ordinal) {
52 _ordinal = ordinal;
53 }
54
55 public Column[] map(ValueMapping vm, String name, ColumnIO io,
56 boolean adapt) {
57 // all enum classes have a static method called 'values()'
58 // that returns an array of all the enum values
59 try {
60 Method m = vm.getType().getMethod("values", (Class[]) null);
61 _vals = (Enum[]) m.invoke(null, (Object[]) null);
62 } catch (Exception e) {
63 throw new MetaDataException().setCause(e);
64 }
65
66 Column col = new Column();
67 col.setName(name);
68 if (_ordinal)
69 col.setJavaType(JavaTypes.SHORT);
70 else {
71 // look for the longest enum value name; use 20 as min length to
72 // leave room for future long names
73 int len = 20;
74 for (int i = 0; i < _vals.length; i++)
75 len = Math.max(_vals[i].name().length(), len);
76
77 col.setJavaType(JavaTypes.STRING);
78 col.setSize(len);
79 }
80 return new Column[]{ col };
81 }
82
83 public boolean isVersionable() {
84 return true;
85 }
86
87 public Object toDataStoreValue(ValueMapping vm, Object val,
88 JDBCStore store) {
89 if (val == null)
90 return null;
91 if (_ordinal)
92 return new Integer(((Enum) val).ordinal());
93 return ((Enum) val).name();
94 }
95
96 public Object toObjectValue(ValueMapping vm, Object val) {
97 if (val == null)
98 return null;
99 if (_ordinal)
100 return _vals[((Number) val).intValue()];
101 return Enum.valueOf(vm.getType(), (String) val);
102 }
103 }