001: /**
002: * Copyright (C) 2001-2005 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.metadata;
018:
019: import java.util.Iterator;
020:
021: import org.objectweb.speedo.api.SpeedoRuntimeException;
022:
023: /**
024: * Defines a persistent field inherited from an ancestor. The mapping of this
025: * persistent field is redefined in the current persistent class. This meta
026: * object references the inherited field and defines its new mapping.
027: *
028: * @author S.Chassande-Barrioz
029: */
030: public class SpeedoCommonField extends SpeedoElement {
031:
032: private static final long serialVersionUID = 1326599649531880606L;
033:
034: /**
035: * name of the persistent field
036: */
037: public String name;
038:
039: /**
040: * Meta object of the class containing the field.
041: */
042: public SpeedoClass moClass;
043:
044: /**
045: * is the column(s) containing the value of the field.
046: * - If the field has a primitive type (long, int, java.lang.Integer,
047: * java.lang.String, java.util.Date, ...), the value must be store on a
048: * single column (columns.lenght < 2).
049: * - If the field is a reference to a Class or a generic class (Collection,
050: * Set, ...), this field describes the columns containing the reference.
051: *
052: * In both cases the column(s) containing the value can be found in another
053: * table than the main table (moClass.mainTable).
054: * The value of this columns can depend on the reverse field (if there is
055: * one).
056: * @see SpeedoClass#mainTable
057: * @see #join
058: */
059: public SpeedoColumn[] columns;
060:
061: /**
062: * In case of the columns is in another table than the main table of the
063: * class, this field defines the join to reach this external table.
064: * This join instance is common for each columns corresponding to this
065: * persistent field. This join must be also listed into
066: * #moClass.joinToExtTables.
067: * This field is null when this field is mapped on a column included into
068: * the main table.
069: * @see #columns
070: * @see SpeedoClass#mainTable
071: * @see SpeedoClass#joinToExtTables
072: */
073: public SpeedoJoin join;
074:
075: /**
076: * Type of the tuple represented by this field if it is a tuple
077: */
078: public SpeedoTuple jdoTuple;
079:
080: public String getSourceDesc() {
081: StringBuffer sb = new StringBuffer();
082: sb.append("field '").append(name);
083: sb.append("' in class '").append(moClass.getFQName());
084: sb.append("' in desc '");
085: sb.append(moClass.moPackage.xmlDescriptor.xmlFile).append("'");
086: return sb.toString();
087: }
088:
089: public void addColumn(SpeedoColumn col) {
090: if (col == null) {
091: throw new SpeedoRuntimeException(
092: "try to add a null column into " + getSourceDesc());
093: }
094: columns = (SpeedoColumn[]) addInArray(col, columns,
095: SpeedoColumn[].class);
096: }
097:
098: public SpeedoColumn getJoinColumn(SpeedoField joinedField) {
099: if (join == null) {
100: return null;
101: }
102: if (join.columns.size() == 1) {
103: return ((SpeedoJoinColumn) join.columns.get(0)).column;
104: }
105: for (Iterator it = join.columns.iterator(); it.hasNext();) {
106: SpeedoJoinColumn joincol = (SpeedoJoinColumn) it.next();
107: if (joinedField.name.equals(joincol.targetField)
108: || joinedField.columns[0].name
109: .equals(joincol.targetColumn)) {
110: return joincol.column;
111: }
112: }
113: return null;
114: }
115:
116: public SpeedoColumn getFKColumn(String pkColName) {
117: for (int i = 0; i < columns.length; i++) {
118: if (pkColName.equals(columns[i].targetColumn)) {
119: return columns[i];
120: }
121: }
122: return null;
123: }
124:
125: public SpeedoJoinColumn getFKJoinColumn(String pkColName) {
126: for (int i = 0; i < join.columns.size(); i++) {
127: SpeedoJoinColumn jc = (SpeedoJoinColumn) join.columns
128: .get(i);
129: if (pkColName.equals(jc.targetColumn)) {
130: return jc;
131: }
132: }
133: return null;
134: }
135:
136: public final String getFQFieldName() {
137: return moClass.getFQName() + "." + name;
138: }
139:
140: public String printColumns() {
141: StringBuffer sb = new StringBuffer();
142: sb.append("[");
143: if (columns != null) {
144: for (int i = 0; i < columns.length; i++) {
145: sb.append("\n\t").append(columns[i].toString());
146: }
147: }
148: sb.append("]");
149: return sb.toString();
150: }
151: }
|