01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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.persistence.jdbc.annotations;
20:
21: import java.awt.*;
22:
23: import org.apache.openjpa.jdbc.kernel.JDBCStore;
24: import org.apache.openjpa.jdbc.meta.ValueMapping;
25: import org.apache.openjpa.jdbc.meta.strats.AbstractValueHandler;
26: import org.apache.openjpa.jdbc.schema.Column;
27: import org.apache.openjpa.jdbc.schema.ColumnIO;
28: import org.apache.openjpa.meta.JavaTypes;
29: import serp.util.Numbers;
30:
31: /**
32: * Handler for {@link Point}. This is a sample of how to create custom
33: * handlers, and is in this package for unit testing purposes.
34: */
35: public class PointHandler extends AbstractValueHandler {
36:
37: /**
38: * Create columns with default names and java type values. Kodo will
39: * fill in the rest of the information and merge in any information given
40: * in the user's mapping data. If the user does not give column names,
41: * Kodo will alter your default names as necessary to avoid conflicts and
42: * meet the database's name limitations.
43: *
44: * @param name default base name for columns
45: */
46: public Column[] map(ValueMapping vm, String name, ColumnIO io,
47: boolean adapt) {
48: Column xcol = new Column();
49: xcol.setName("X" + name);
50: xcol.setJavaType(JavaTypes.INT);
51: Column ycol = new Column();
52: ycol.setName("Y" + name);
53: ycol.setJavaType(JavaTypes.INT);
54: return new Column[] { xcol, ycol };
55: }
56:
57: /**
58: * Return whether the column value is an exact value that can be used
59: * in state-comparison versioning.
60: */
61: public boolean isVersionable() {
62: return true;
63: }
64:
65: /**
66: * Convert the object value to its datastore equivalent.
67: */
68: public Object toDataStoreValue(ValueMapping vm, Object val,
69: JDBCStore store) {
70: if (val == null)
71: return null;
72:
73: Point p = (Point) val;
74: return new Object[] { Numbers.valueOf(p.x),
75: Numbers.valueOf(p.y) };
76: }
77:
78: /**
79: * Convert the datastore value to its object equivalent.
80: */
81: public Object toObjectValue(ValueMapping vm, Object val) {
82: Object[] vals = (Object[]) val;
83: if (vals[0] == null || vals[1] == null)
84: return null;
85:
86: int x = ((Number) vals[0]).intValue();
87: int y = ((Number) vals[1]).intValue();
88: return new Point(x, y);
89: }
90: }
|