/**
* Copyright (C) 2001-2005 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.objectweb.speedo.metadata;
import java.util.Iterator;
import org.objectweb.speedo.api.SpeedoRuntimeException;
/**
* Defines a persistent field inherited from an ancestor. The mapping of this
* persistent field is redefined in the current persistent class. This meta
* object references the inherited field and defines its new mapping.
*
* @author S.Chassande-Barrioz
*/
public class SpeedoCommonField extends SpeedoElement {
private static final long serialVersionUID = 1326599649531880606L;
/**
* name of the persistent field
*/
public String name;
/**
* Meta object of the class containing the field.
*/
public SpeedoClass moClass;
/**
* is the column(s) containing the value of the field.
* - If the field has a primitive type (long, int, java.lang.Integer,
* java.lang.String, java.util.Date, ...), the value must be store on a
* single column (columns.lenght < 2).
* - If the field is a reference to a Class or a generic class (Collection,
* Set, ...), this field describes the columns containing the reference.
*
* In both cases the column(s) containing the value can be found in another
* table than the main table (moClass.mainTable).
* The value of this columns can depend on the reverse field (if there is
* one).
* @see SpeedoClass#mainTable
* @see #join
*/
public SpeedoColumn[] columns;
/**
* In case of the columns is in another table than the main table of the
* class, this field defines the join to reach this external table.
* This join instance is common for each columns corresponding to this
* persistent field. This join must be also listed into
* #moClass.joinToExtTables.
* This field is null when this field is mapped on a column included into
* the main table.
* @see #columns
* @see SpeedoClass#mainTable
* @see SpeedoClass#joinToExtTables
*/
public SpeedoJoin join;
/**
* Type of the tuple represented by this field if it is a tuple
*/
public SpeedoTuple jdoTuple;
public String getSourceDesc() {
StringBuffer sb = new StringBuffer();
sb.append("field '").append(name);
sb.append("' in class '").append(moClass.getFQName());
sb.append("' in desc '");
sb.append(moClass.moPackage.xmlDescriptor.xmlFile).append("'");
return sb.toString();
}
public void addColumn(SpeedoColumn col) {
if (col == null) {
throw new SpeedoRuntimeException(
"try to add a null column into " + getSourceDesc());
}
columns = (SpeedoColumn[]) addInArray(col, columns, SpeedoColumn[].class);
}
public SpeedoColumn getJoinColumn(SpeedoField joinedField) {
if (join == null) {
return null;
}
if (join.columns.size() == 1) {
return ((SpeedoJoinColumn) join.columns.get(0)).column;
}
for (Iterator it = join.columns.iterator(); it.hasNext();) {
SpeedoJoinColumn joincol = (SpeedoJoinColumn) it.next();
if (joinedField.name.equals(joincol.targetField)
|| joinedField.columns[0].name.equals(joincol.targetColumn)) {
return joincol.column;
}
}
return null;
}
public SpeedoColumn getFKColumn(String pkColName) {
for (int i = 0; i < columns.length; i++) {
if (pkColName.equals(columns[i].targetColumn)) {
return columns[i];
}
}
return null;
}
public SpeedoJoinColumn getFKJoinColumn(String pkColName) {
for (int i = 0; i < join.columns.size(); i++) {
SpeedoJoinColumn jc = (SpeedoJoinColumn) join.columns.get(i);
if (pkColName.equals(jc.targetColumn)) {
return jc;
}
}
return null;
}
public final String getFQFieldName() {
return moClass.getFQName() + "." + name;
}
public String printColumns() {
StringBuffer sb = new StringBuffer();
sb.append("[");
if (columns != null) {
for (int i = 0; i < columns.length; i++) {
sb.append("\n\t").append(columns[i].toString());
}
}
sb.append("]");
return sb.toString();
}
}