001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.jdbc.meta.strats;
020:
021: import java.sql.SQLException;
022:
023: import org.apache.openjpa.jdbc.kernel.JDBCFetchConfiguration;
024: import org.apache.openjpa.jdbc.kernel.JDBCStore;
025: import org.apache.openjpa.jdbc.meta.ClassMapping;
026: import org.apache.openjpa.jdbc.meta.ClassMappingInfo;
027: import org.apache.openjpa.jdbc.schema.Column;
028: import org.apache.openjpa.jdbc.schema.PrimaryKey;
029: import org.apache.openjpa.jdbc.schema.Table;
030: import org.apache.openjpa.jdbc.sql.Row;
031: import org.apache.openjpa.jdbc.sql.RowManager;
032: import org.apache.openjpa.jdbc.sql.Select;
033: import org.apache.openjpa.kernel.OpenJPAStateManager;
034: import org.apache.openjpa.lib.util.Localizer;
035: import org.apache.openjpa.meta.JavaTypes;
036: import org.apache.openjpa.meta.ValueStrategies;
037: import org.apache.openjpa.util.MetaDataException;
038:
039: /**
040: * Mapping for when the class maps all fields to its own table.
041: *
042: * @author Abe White
043: */
044: public class FullClassStrategy extends AbstractClassStrategy {
045:
046: public static final String ALIAS = "full";
047:
048: private static final Localizer _loc = Localizer
049: .forPackage(FullClassStrategy.class);
050:
051: public String getAlias() {
052: return ALIAS;
053: }
054:
055: public void map(boolean adapt) {
056: if (cls.getEmbeddingMetaData() != null)
057: throw new MetaDataException(_loc.get("not-full", cls));
058:
059: ClassMapping sup = cls.getMappedPCSuperclassMapping();
060: ClassMappingInfo info = cls.getMappingInfo();
061: if (sup != null && info.isJoinedSubclass())
062: throw new MetaDataException(_loc.get("not-full", cls));
063:
064: info.assertNoJoin(cls, true);
065: info.assertNoForeignKey(cls, !adapt);
066: info.assertNoIndex(cls, false);
067: info.assertNoUnique(cls, false);
068:
069: // find class table
070: Table table = info.getTable(cls, adapt);
071:
072: // find primary key column
073: Column[] pkCols = null;
074: if (cls.getIdentityType() == cls.ID_DATASTORE) {
075: Column id = new Column();
076: id.setName("id");
077: id.setJavaType(JavaTypes.LONG);
078: id.setComment("datastore id");
079: if (cls.getIdentityStrategy() == ValueStrategies.AUTOASSIGN)
080: id.setAutoAssigned(true);
081: id.setNotNull(true);
082: pkCols = info.getDataStoreIdColumns(cls,
083: new Column[] { id }, table, adapt);
084: cls.setPrimaryKeyColumns(pkCols);
085: cls.setColumnIO(info.getColumnIO());
086: }
087: cls.setTable(table);
088:
089: // add a primary key if we don't have one already
090: PrimaryKey pk = table.getPrimaryKey();
091: if (pk == null) {
092: String pkname = null;
093: if (adapt)
094: pkname = cls.getMappingRepository()
095: .getMappingDefaults().getPrimaryKeyName(cls,
096: table);
097: pk = table.addPrimaryKey(pkname);
098: pk.setLogical(!adapt);
099: if (pkCols != null)
100: pk.setColumns(pkCols);
101: }
102:
103: // set joinable
104: if (cls.getIdentityType() == ClassMapping.ID_DATASTORE)
105: cls.setJoinable(cls.getPrimaryKeyColumns()[0],
106: new IdentityJoinable(cls));
107: }
108:
109: public boolean supportsEagerSelect(Select sel,
110: OpenJPAStateManager sm, JDBCStore store, ClassMapping base,
111: JDBCFetchConfiguration fetch) {
112: return false;
113: }
114:
115: public void insert(OpenJPAStateManager sm, JDBCStore store,
116: RowManager rm) throws SQLException {
117: Row row = rm
118: .getRow(cls.getTable(), Row.ACTION_INSERT, sm, true);
119: if (cls.getIdentityType() == cls.ID_DATASTORE)
120: row.setPrimaryKey(cls.getColumnIO(), sm);
121: }
122:
123: public void update(OpenJPAStateManager sm, JDBCStore store,
124: RowManager rm) throws SQLException {
125: Row row = rm.getRow(cls.getTable(), Row.ACTION_UPDATE, sm,
126: false);
127: if (row != null)
128: row.wherePrimaryKey(sm);
129: }
130:
131: public void delete(OpenJPAStateManager sm, JDBCStore store,
132: RowManager rm) throws SQLException {
133: Row row = rm
134: .getRow(cls.getTable(), Row.ACTION_DELETE, sm, true);
135: row.wherePrimaryKey(sm);
136: }
137:
138: public boolean isPrimaryKeyObjectId(boolean hasAll) {
139: return true;
140: }
141: }
|