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;
20:
21: import org.apache.openjpa.jdbc.meta.strats.NoneVersionStrategy;
22: import org.apache.openjpa.jdbc.meta.strats.SuperclassVersionStrategy;
23: import org.apache.openjpa.jdbc.schema.Column;
24: import org.apache.openjpa.jdbc.schema.Index;
25: import org.apache.openjpa.jdbc.schema.SchemaGroup;
26: import org.apache.openjpa.jdbc.schema.Table;
27:
28: /**
29: * Information about the mapping from a version indicator to the schema, in
30: * raw form. The columns and tables used in mapping info will not be part of
31: * the {@link SchemaGroup} used at runtime. Rather, they will be structs
32: * with the relevant pieces of information filled in.
33: *
34: * @author Abe White
35: */
36: public class VersionMappingInfo extends MappingInfo {
37:
38: /**
39: * Return the columns set for this version, based on the given templates.
40: */
41: public Column[] getColumns(Version version, Column[] tmplates,
42: boolean adapt) {
43: Table table = version.getClassMapping().getTable();
44: version.getMappingRepository().getMappingDefaults()
45: .populateColumns(version, table, tmplates);
46: return createColumns(version, null, tmplates, table, adapt);
47: }
48:
49: /**
50: * Return the index to set on the version columns, or null if none.
51: */
52: public Index getIndex(Version version, Column[] cols, boolean adapt) {
53: Index idx = null;
54: if (cols.length > 0)
55: idx = version.getMappingRepository().getMappingDefaults()
56: .getIndex(version, cols[0].getTable(), cols);
57: return createIndex(version, null, idx, cols, adapt);
58: }
59:
60: /**
61: * Synchronize internal information with the mapping data for the given
62: * version.
63: */
64: public void syncWith(Version version) {
65: clear(false);
66:
67: ClassMapping cls = version.getClassMapping();
68: Column[] cols = version.getColumns();
69:
70: setColumnIO(version.getColumnIO());
71: syncColumns(version, cols, false);
72: syncIndex(version, version.getIndex());
73:
74: if (version.getStrategy() == null
75: || version.getStrategy() instanceof SuperclassVersionStrategy)
76: return;
77:
78: // explicit version strategy if:
79: // - unmapped class and version mapped
80: // - mapped base class
81: // - mapped subclass that doesn't rely on superclass version
82: String strat = version.getStrategy().getAlias();
83: if ((!cls.isMapped() && !NoneVersionStrategy.ALIAS
84: .equals(strat))
85: || (cls.isMapped() && cls
86: .getJoinablePCSuperclassMapping() == null))
87: setStrategy(strat);
88: }
89: }
|