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.jdbc.meta.strats;
20:
21: import java.util.Map;
22: import java.util.HashMap;
23:
24: import org.apache.openjpa.meta.JavaTypes;
25: import org.apache.openjpa.jdbc.schema.Column;
26: import serp.util.Numbers;
27:
28: /**
29: * Uses a version number for optimistic versioning.
30: *
31: * @author Abe White
32: */
33: public class NumberVersionStrategy extends ColumnVersionStrategy {
34:
35: public static final String ALIAS = "version-number";
36:
37: private Number _initial = Numbers.valueOf(1);
38:
39: /**
40: * Set the initial value for version column. Defaults to 1.
41: */
42: public void setInitialValue(int initial) {
43: _initial = Numbers.valueOf(initial);
44: }
45:
46: /**
47: * Return the initial value for version column. Defaults to 1.
48: */
49: public int getInitialValue() {
50: return _initial.intValue();
51: }
52:
53: public String getAlias() {
54: return ALIAS;
55: }
56:
57: protected int getJavaType() {
58: return JavaTypes.INT;
59: }
60:
61: protected Object nextVersion(Object version) {
62: if (version == null)
63: return _initial;
64: return Numbers.valueOf(((Number) version).intValue() + 1);
65: }
66:
67: public Map getBulkUpdateValues() {
68: Column[] cols = vers.getColumns();
69: Map map = new HashMap(cols.length);
70: for (int i = 0; i < cols.length; i++)
71: map.put(cols[i], cols[i].getName() + " + 1");
72: return map;
73: }
74: }
|