All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dk.eobjects.metamodel.schema.Relationship Maven / Gradle / Ivy

Go to download

The eobjects.dk MetaModel is a common domain model, query-engine and optimizer for different kinds of datastores.

The newest version!
/*
 * Copyright 2008 eobjects.dk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dk.eobjects.metamodel.schema;

import java.io.Serializable;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * Represents a relationship between two tables where one set of columns is the
 * primary key, and another set is the foreign key. Relationship is unlike most
 * of the MetaModel an immutable type. The immutability help ensure integrity of
 * object-relationships. To create relationsips use the
 * createRelationship method and to remove them use the
 * remove method.
 * 
 * @see Table
 * @see Column
 */
public class Relationship implements Serializable, Comparable {

	private static final long serialVersionUID = 238786848828528822L;
	private Table _primaryTable;
	private Column[] _primaryColumns;
	private Table _foreignTable;
	private Column[] _foreignColumns;

	/**
	 * Factory method to create relations between two tables by specifying which
	 * columns from the tables that enforce the relationship.
	 * 
	 * @param primaryColumns
	 *            the columns from the primary key table
	 * @param foreignColumns
	 *            the columns from the foreign key table
	 * @return the relation created
	 */
	public static Relationship createRelationship(Column[] primaryColumns,
			Column[] foreignColumns) {
		Table primaryTable = checkSameTable(primaryColumns);
		Table foreignTable = checkSameTable(foreignColumns);
		Relationship relation = new Relationship(primaryTable, primaryColumns,
				foreignTable, foreignColumns);
		primaryTable.addRelationship(relation);

		// Ticket #144: Some tables have relations with them selves and then the
		// relationship should only be added once.
		if (foreignTable != primaryTable) {
			foreignTable.addRelationship(relation);
		}
		return relation;
	}

	private static Table checkSameTable(Column[] columns) {
		if (columns == null || columns.length == 0) {
			throw new IllegalArgumentException(
					"At least one key-column must exist on both "
							+ "primary and foreign side for "
							+ "a relation to exist.");
		}
		Table table = null;
		for (int i = 0; i < columns.length; i++) {
			Column column = columns[i];
			if (i == 0) {
				table = column.getTable();
			} else {
				if (table != column.getTable()) {
					throw new IllegalArgumentException(
							"Key-columns did not have same table");
				}
			}
		}
		return table;
	}

	public void remove() {
		_primaryTable.removeRelationship(this);
		_foreignTable.removeRelationship(this);
		_primaryColumns = null;
		_primaryTable = null;
		_foreignColumns = null;
		_foreignTable = null;
	}

	/**
	 * Prevent external instantiation
	 */
	private Relationship(Table primaryTable, Column[] primaryColumns,
			Table foreignTable, Column[] foreignColumns) {
		_primaryTable = primaryTable;
		_primaryColumns = primaryColumns;
		_foreignTable = foreignTable;
		_foreignColumns = foreignColumns;
	}

	public Table getPrimaryTable() {
		return _primaryTable;
	}

	public Column[] getPrimaryColumns() {
		return _primaryColumns;
	}

	public Table getForeignTable() {
		return _foreignTable;
	}

	public Column[] getForeignColumns() {
		return _foreignColumns;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Relationship[");
		sb.append("primaryTable=" + _primaryTable.getName());
		Column[] columns = getPrimaryColumns();
		sb.append(",primaryColumns={");
		for (int i = 0; i < columns.length; i++) {
			if (i != 0) {
				sb.append(",");
			}
			sb.append(columns[i].getName());
		}
		sb.append("}");
		sb.append(",foreignTable=" + _foreignTable.getName());
		columns = getForeignColumns();
		sb.append(",foreignColumns={");
		for (int i = 0; i < columns.length; i++) {
			if (i != 0) {
				sb.append(",");
			}
			sb.append(columns[i].getName());
		}
		sb.append("}");
		sb.append("]");
		return sb.toString();
	}

	public int compareTo(Relationship that) {
		CompareToBuilder ctb = new CompareToBuilder();
		ctb.append(this.getPrimaryTable(), that.getPrimaryTable());
		ctb.append(this.getForeignTable(), that.getForeignTable());
		ctb.append(this.getPrimaryColumns(), that.getPrimaryColumns());
		ctb.append(this.getForeignColumns(), that.getForeignColumns());
		return ctb.toComparison();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (obj instanceof Relationship) {
			Relationship that = (Relationship) obj;
			EqualsBuilder eb = new EqualsBuilder();
			eb.append(this.getPrimaryColumns(), that.getPrimaryColumns());
			eb.append(this.getForeignColumns(), that.getForeignColumns());
			return eb.isEquals();
		}
		return false;
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(_foreignColumns).append(
				_primaryColumns).toHashCode();
	}

	/**
	 * @param column
	 * @return true if this relation contains the specified primary and foreign
	 *         columns as a part of the relation
	 */
	public boolean containsColumnPair(Column pkColumn, Column fkColumn) {
		if (pkColumn != null && fkColumn != null) {
			for (int i = 0; i < _primaryColumns.length; i++) {
				if (pkColumn.equals(_primaryColumns[i])
						&& fkColumn.equals(_foreignColumns[i])) {
					return true;
				}
			}
		}
		return false;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy