001: /**********************************************************************
002: Copyright (c) 2004 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016: Contributors:
017: ...
018: **********************************************************************/package org.jpox.store;
019:
020: import org.jpox.metadata.AbstractMemberMetaData;
021: import org.jpox.metadata.ClassMetaData;
022:
023: /**
024: * Representation of a class/field managed by the StoreManager where the datastore persists objects
025: * into a "DatastoreClass" (table).
026: * <H3>Type of data</H3>
027: * There are 2 constructors, one for specifying a class (FCO) being managed, and one
028: * for specifying a field (SCO) being managed. The "type" is set accordingly to represent
029: * which one is being used.
030: * <H3>Ownership of Tables</H3>
031: * In JPOX, a class can have a table assigned where the class is not the "owner" of the table.
032: * This happens when the class has its fields persisted in the table of another class
033: * - so they share the table.
034: *
035: * @version $Revision: 1.2 $
036: */
037: public class TableStoreData extends StoreData {
038: /** If this class is an implementation of a persistent interface, this attribute will hold the name of the interface. */
039: protected final String interfaceName;
040:
041: /** Name of the datastore object where it is stored. */
042: protected String tableName;
043:
044: /** Whether this class is the owner of the table. */
045: protected final boolean isTableOwner;
046:
047: /** The datastore container (table) where this class/field is persisted. */
048: protected DatastoreContainerObject table;
049:
050: /**
051: * Constructor. To be used when creating for the start mechanism.
052: * @param name Name of the class/field
053: * @param tableName Name of the table associated
054: * @param tableOwner Whether this is the owner
055: * @param type The type (FCO/SCO)
056: * @param interfaceName if this class is an implementation of a persistent interface (multiple persistent interface names
057: * are comma separated), otherwise is null.
058: */
059: public TableStoreData(String name, String tableName,
060: boolean tableOwner, int type, String interfaceName) {
061: super (name, null, type, interfaceName);
062: this .tableName = tableName;
063: this .isTableOwner = tableOwner;
064: this .interfaceName = interfaceName;
065: }
066:
067: /**
068: * Constructor for FCO data.
069: * @param cmd MetaData for the class.
070: * @param table Table where the class is stored.
071: * @param tableOwner Whether the class is the owner of the table.
072: */
073: public TableStoreData(ClassMetaData cmd,
074: DatastoreContainerObject table, boolean tableOwner) {
075: super (cmd.getFullClassName(), cmd, FCO_TYPE, null);
076:
077: this .table = table;
078: this .tableName = (table != null ? table.toString() : null);
079: this .isTableOwner = tableOwner;
080: String interfaces = null;
081: for (int i = 0; i < cmd.getImplementsMetaData().length; i++) {
082: if (interfaces == null) {
083: interfaces = "";
084: } else {
085: interfaces += ",";
086: }
087: interfaces += cmd.getImplementsMetaData()[i].getName();
088: }
089: this .interfaceName = interfaces;
090: }
091:
092: /**
093: * Constructor for SCO data.
094: * @param fmd MetaData for the field.
095: * @param table Table where the field is stored.
096: */
097: public TableStoreData(AbstractMemberMetaData fmd,
098: DatastoreContainerObject table) {
099: super (fmd.getFullFieldName(), fmd, SCO_TYPE, null);
100:
101: this .table = table;
102: this .tableName = (table != null ? table.toString() : null);
103: this .isTableOwner = true; // Always the owner of the table
104: this .interfaceName = table.getStoreManager()
105: .getMetaDataManager().isPersistentInterface(
106: fmd.getType().getName()) ? fmd.getType()
107: .getName() : null;
108: }
109:
110: /**
111: * Accessor for tableName.
112: * @return Returns the tableName.
113: */
114: public String getTableName() {
115: return tableName;
116: }
117:
118: /**
119: * Accessor for whether this class is the owner of the table.
120: * @return Whether it owns the table
121: */
122: public boolean isTableOwner() {
123: return isTableOwner;
124: }
125:
126: /**
127: * Accessor for whether this has a table representation.
128: * @return Whether it has a table
129: */
130: public boolean hasTable() {
131: return (tableName != null);
132: }
133:
134: /**
135: * Accessor for the Table details.
136: * @return The Table
137: **/
138: public DatastoreContainerObject getDatastoreContainerObject() {
139: return table;
140: }
141:
142: /**
143: * Accessor for the identifier for the table.
144: * @return The table identifier
145: **/
146: public DatastoreIdentifier getDatastoreIdentifier() {
147: if (table != null) {
148: return table.getIdentifier();
149: }
150:
151: // If the class doesn't map to a table return null
152: return null;
153: }
154:
155: /**
156: * Convenience to set the table. To be used in cases where the table isn't known
157: * until after the initial create
158: * @param table The table
159: */
160: public void setDatastoreContainerObject(DatastoreClass table) {
161: if (table != null) {
162: tableName = table.toString();
163: this .table = table;
164: }
165: }
166:
167: /**
168: * Method to return this class/field managed object as a string.
169: * @return String version of this class/field managed object.
170: **/
171: public String toString() {
172: if (metadata instanceof ClassMetaData) {
173: ClassMetaData cmd = (ClassMetaData) metadata;
174: if (tableName != null) {
175: return LOCALISER.msg("035004", name, tableName, cmd
176: .getInheritanceMetaData().getStrategyValue()
177: .toString());
178: } else {
179: return LOCALISER.msg("035004", name, "(none)", cmd
180: .getInheritanceMetaData().getStrategyValue()
181: .toString());
182: }
183: } else if (metadata instanceof AbstractMemberMetaData) {
184: return LOCALISER.msg("035005", name, tableName);
185: } else {
186: return LOCALISER.msg("035004", name, tableName);
187: }
188: }
189: }
|