
dk.eobjects.metamodel.schema.Schema Maven / Gradle / Ivy
/**
* 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.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
/**
* Represents a schema and it's metadata. Schemas represent a collection of
* tables.
*
* @see Table
*/
public class Schema implements Serializable, Comparable {
private static final long serialVersionUID = 4465197783868238863L;
private String _name;
private List _tables = new ArrayList();
public Schema() {
}
public Schema(String name) {
this();
setName(name);
}
public Schema(String name, Table... tables) {
this(name);
setTables(tables);
}
/**
* @return the name of the schema
*/
public String getName() {
return _name;
}
public Schema setName(String name) {
_name = name;
return this;
}
/**
* @return the number of tables that reside in the schema
*/
public int getTableCount() {
return _tables.size();
}
public int getTableCount(TableType type) {
return getTables(type).length;
}
/**
* @return the tables that reside in the schema
*/
public Table[] getTables() {
return _tables.toArray(new Table[_tables.size()]);
}
public Table[] getTables(TableType type) {
List result = new ArrayList();
for (Table table : _tables) {
if (table.getType() == type) {
result.add(table);
}
}
return result.toArray(new Table[result.size()]);
}
public Schema setTables(Collection tables) {
_tables.clear();
for (Table table : tables) {
_tables.add(table);
}
return this;
}
public Schema setTables(Table... tables) {
_tables.clear();
for (Table table : tables) {
_tables.add(table);
}
return this;
}
public Schema addTable(Table table) {
_tables.add(table);
return this;
}
public Schema removeTable(Table table) {
_tables.remove(table);
return this;
}
/**
* Convenience method for retrieving a table by it's name
*
* @param tableName
* the name of the table to retrieve
* @return the table with the given name. Returns null if no such table is
* found.
*/
public Table getTableByName(String tableName) {
if (tableName != null) {
List foundTables = new ArrayList();
// Search for table matches, case insensitive.
for (Table table : _tables) {
if (tableName.equalsIgnoreCase(table.getName())) {
foundTables.add(table);
}
}
int numTables = foundTables.size();
if (numTables == 0) {
return null;
} else if (numTables == 1) {
return foundTables.get(0);
} else {
// If more matches are found, search case sensitive
for (Table table : foundTables) {
if (tableName.equals(table.getName())) {
return table;
}
}
}
}
return null;
}
public Relationship[] getRelationships() {
Set result = new TreeSet();
for (Table table : _tables) {
Relationship[] relations = table.getRelationships();
for (int i = 0; i < relations.length; i++) {
Relationship relation = relations[i];
result.add(relation);
}
}
return result.toArray(new Relationship[result.size()]);
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
.append("name", _name).toString();
}
@Override
public int hashCode() {
HashCodeBuilder hcb = new HashCodeBuilder();
hcb.append(_name);
hcb.append(_tables.size());
return hcb.toHashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof Schema) {
Schema that = (Schema) obj;
return new EqualsBuilder().append(this.getName(), that.getName())
.append(this.getTables(), that.getTables()).isEquals();
}
return false;
}
public int compareTo(Schema that) {
CompareToBuilder ctb = new CompareToBuilder();
ctb.append(this.getName(), that.getName());
ctb.append(this.getTableCount(), that.getTableCount());
return ctb.toComparison();
}
public String[] getTableNames() {
ArrayList result = new ArrayList();
Table[] tables = getTables();
for (Table table : tables) {
result.add(table.getName());
}
return result.toArray(new String[result.size()]);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy