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

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

The newest version!
/**
 *  This file is part of MetaModel.
 *
 *  MetaModel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MetaModel 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MetaModel.  If not, see .
 */
package dk.eobjects.metamodel.schema;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import dk.eobjects.metamodel.JdbcDataContextStrategy;

public class JdbcSchema extends Schema {

	private static final long serialVersionUID = 7543633400859277467L;
	private transient JdbcDataContextStrategy _strategy;
	private transient boolean _relationsLoaded = false;

	public JdbcSchema(String name, JdbcDataContextStrategy strategy) {
		super(name);
		_strategy = strategy;
	}

	public void loadRelations() {
		if (!_relationsLoaded) {
			_relationsLoaded = true;
			if (_strategy != null) {
				_strategy.loadRelations(this);
			}
		}
	}

	public Schema toSerializableForm() {
		// Trigger the lazy-loading of the full schema, including relationships,
		// indexes and all columns
		Schema schema = new Schema(_name);
		Table[] tables = getTables();
		for (int i = 0; i < tables.length; i++) {
			Table origTable = tables[i];
			Table newTable = new Table(origTable.getName(),
					origTable.getType(), schema);
			newTable.setQuote(origTable.getQuote());
			newTable.setRemarks(origTable.getRemarks());
			Column[] columns = origTable.getColumns();
			for (int j = 0; j < columns.length; j++) {
				Column origColumn = columns[j];
				Column newColumn = new Column(origColumn.getName(), origColumn
						.getType(), newTable, origColumn.getColumnNumber(),
						origColumn.isNullable());
				newColumn.setNativeType(origColumn.getNativeType());
				newColumn.setIndexed(origColumn.isIndexed());
				newColumn.setQuote(origColumn.getQuote());
				newColumn.setRemarks(origColumn.getRemarks());
				newColumn.setColumnSize(origColumn.getColumnSize());
				newTable.addColumn(newColumn);
			}
			schema.addTable(newTable);
		}

		Relationship[] relationships = getRelationships();
		for (int i = 0; i < relationships.length; i++) {
			Relationship origRelationship = relationships[i];
			Column[] origPrimaryColumns = origRelationship.getPrimaryColumns();
			Column[] newPrimaryColumns = new Column[origPrimaryColumns.length];
			for (int j = 0; j < origPrimaryColumns.length; j++) {
				Column column = origPrimaryColumns[j];
				Table table = column.getTable();
				newPrimaryColumns[j] = schema.getTableByName(table.getName())
						.getColumnByName(column.getName());
			}

			Column[] origForeignColumns = origRelationship.getForeignColumns();
			Column[] newForeignColumns = new Column[origForeignColumns.length];
			for (int j = 0; j < origForeignColumns.length; j++) {
				Column column = origForeignColumns[j];
				Table table = column.getTable();
				newForeignColumns[j] = schema.getTableByName(table.getName())
						.getColumnByName(column.getName());
			}

			Relationship.createRelationship(newPrimaryColumns,
					newForeignColumns);
		}
		return schema;
	}

	/**
	 * Called by the Java Serialization API to serialize the object.
	 */
	private void writeObject(ObjectOutputStream stream) throws IOException {
		stream.writeObject(toSerializableForm());
	}

	/**
	 * Called by the Java Serialization API to deserialize the object.
	 */
	private void readObject(ObjectInputStream stream) throws IOException,
			ClassNotFoundException {
		Schema schema = (Schema) stream.readObject();
		setName(schema.getName());
		setTables(schema.getTables());
		_relationsLoaded = true;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy