- Timestamp:
- 02/04/10 21:56:24 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
MetadataBeans/trunk/src/main/java/org/eobjects/metamodel/schema/Schema.java
r886 r887 1 /**2 * This file is part of MetaModel.3 *4 * MetaModel is free software: you can redistribute it and/or modify5 * it under the terms of the GNU General Public License as published by6 * the Free Software Foundation, either version 3 of the License, or7 * (at your option) any later version.8 *9 * MetaModel is distributed in the hope that it will be useful,10 * but WITHOUT ANY WARRANTY; without even the implied warranty of11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12 * GNU General Public License for more details.13 *14 * You should have received a copy of the GNU General Public License15 * along with MetaModel. If not, see <http://www.gnu.org/licenses/>.16 */17 1 package org.eobjects.metamodel.schema; 18 19 import java.io.Serializable;20 import java.util.ArrayList;21 import java.util.Collection;22 import java.util.List;23 24 import org.apache.commons.lang.builder.CompareToBuilder;25 import org.apache.commons.lang.builder.EqualsBuilder;26 import org.apache.commons.lang.builder.HashCodeBuilder;27 import org.apache.commons.lang.builder.ToStringBuilder;28 import org.apache.commons.lang.builder.ToStringStyle;29 2 30 3 /** … … 34 7 * @see Table 35 8 */ 36 public class Schema implements Serializable, Comparable<Schema> { 37 38 private static final long serialVersionUID = 4465197783868238863L; 39 40 protected String _name; 41 protected List<Table> _tables = new ArrayList<Table>(); 42 43 public Schema() { 44 } 45 46 public Schema(String name) { 47 this(); 48 _name = name; 49 } 50 51 public Schema(String name, Table... tables) { 52 this(name); 53 setTables(tables); 54 } 9 public interface Schema extends Comparable<Schema> { 55 10 56 11 /** 57 12 * @return the name of the schema 58 13 */ 59 public String getName() { 60 return _name; 61 } 62 63 public Schema setName(String name) { 64 _name = name; 65 for (Table table : _tables) { 66 table.fireSchemaChanged(); 67 } 68 return this; 69 } 14 public String getName(); 70 15 71 16 /** 72 17 * @return the number of tables that reside in the schema 73 18 */ 74 public int getTableCount() { 75 return _tables.size(); 76 } 19 public int getTableCount(); 77 20 78 public int getTableCount(TableType type) { 79 return getTables(type).length; 80 } 21 public int getTableCount(TableType type); 81 22 82 23 /** 83 24 * @return the tables that reside in the schema 84 25 */ 85 public Table[] getTables() { 86 return _tables.toArray(new Table[_tables.size()]); 87 } 26 public Table[] getTables(); 88 27 89 public Table[] getTables(TableType type) { 90 List<Table> result = new ArrayList<Table>(); 91 for (Table table : _tables) { 92 if (table.getType() == type) { 93 result.add(table); 94 } 95 } 96 return result.toArray(new Table[result.size()]); 97 } 98 99 public Schema setTables(Collection<Table> tables) { 100 _tables.clear(); 101 for (Table table : tables) { 102 _tables.add(table); 103 } 104 return this; 105 } 106 107 public Schema setTables(Table... tables) { 108 _tables.clear(); 109 for (Table table : tables) { 110 _tables.add(table); 111 } 112 return this; 113 } 114 115 public Schema addTable(Table table) { 116 _tables.add(table); 117 return this; 118 } 119 120 public Schema removeTable(Table table) { 121 _tables.remove(table); 122 return this; 123 } 28 public Table[] getTables(TableType type); 124 29 125 30 /** … … 131 36 * found. 132 37 */ 133 public Table getTableByName(String tableName) { 134 if (tableName != null) { 135 List<Table> foundTables = new ArrayList<Table>(); 136 // Search for table matches, case insensitive. 137 for (Table table : _tables) { 138 if (tableName.equalsIgnoreCase(table.getName())) { 139 foundTables.add(table); 140 } 141 } 142 int numTables = foundTables.size(); 143 if (numTables == 0) { 144 return null; 145 } else if (numTables == 1) { 146 return foundTables.get(0); 147 } else { 148 // If more matches are found, search case sensitive 149 for (Table table : foundTables) { 150 if (tableName.equals(table.getName())) { 151 return table; 152 } 153 } 154 } 155 } 156 return null; 157 } 38 public Table getTableByName(String tableName); 158 39 159 public Relationship[] getRelationships() { 160 List<Relationship> result = new ArrayList<Relationship>(); 161 for (Table table : _tables) { 162 Relationship[] relations = table.getRelationships(); 163 for (int i = 0; i < relations.length; i++) { 164 Relationship relation = relations[i]; 165 boolean found = false; 166 for (Relationship existingRelation : result) { 167 if (relation == existingRelation) { 168 found = true; 169 break; 170 } 171 } 40 public Relationship[] getRelationships(); 172 41 173 if (!found) { 174 result.add(relation); 175 } 176 } 177 } 178 return result.toArray(new Relationship[result.size()]); 179 } 42 public int getRelationshipCount(); 180 43 181 public int getRelationshipCount() { 182 return getRelationships().length; 183 } 44 public int compareTo(Schema that); 184 45 185 @Override 186 public String toString() { 187 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 188 .append("name", _name).toString(); 189 } 46 public String[] getTableNames(); 190 47 191 @Override192 public int hashCode() {193 HashCodeBuilder hcb = new HashCodeBuilder();194 hcb.append(_name);195 hcb.append(_tables.size());196 return hcb.toHashCode();197 }198 199 @Override200 public boolean equals(Object obj) {201 if (obj == this) {202 return true;203 }204 if (obj instanceof Schema) {205 Schema that = (Schema) obj;206 return new EqualsBuilder().append(this.getName(), that.getName())207 .append(this.getTables(), that.getTables()).isEquals();208 }209 return false;210 }211 212 public int compareTo(Schema that) {213 CompareToBuilder ctb = new CompareToBuilder();214 ctb.append(this.getName(), that.getName());215 ctb.append(this.getTableCount(), that.getTableCount());216 return ctb.toComparison();217 }218 219 public String[] getTableNames() {220 ArrayList<String> result = new ArrayList<String>();221 Table[] tables = getTables();222 for (Table table : tables) {223 result.add(table.getName());224 }225 return result.toArray(new String[result.size()]);226 }227 48 }
Note: See TracChangeset
for help on using the changeset viewer.
