001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.jdbc.schema;
020:
021: import java.io.Serializable;
022: import java.util.Map;
023: import java.util.TreeMap;
024:
025: import org.apache.commons.lang.StringUtils;
026:
027: /**
028: * Represents a database schema.
029: *
030: * @author Abe White
031: */
032: public class Schema implements Comparable, Serializable {
033:
034: private String _name = null;
035: private SchemaGroup _group = null;
036: private Map _tableMap = null;
037: private Map _seqMap = null;
038:
039: // cache
040: private Table[] _tables = null;
041: private Sequence[] _seqs = null;
042:
043: /**
044: * Default constructor.
045: */
046: public Schema() {
047: }
048:
049: /**
050: * Constructor.
051: *
052: * @param name the schema name, if any
053: * @param group the schema's owning group
054: */
055: public Schema(String name, SchemaGroup group) {
056: setName(name);
057: _group = group;
058: }
059:
060: /**
061: * Called when the schema is removed from its group. Invalidates the
062: * schema and removes all its member tables.
063: */
064: void remove() {
065: Table[] tabs = getTables();
066: for (int i = 0; i < tabs.length; i++)
067: removeTable(tabs[i]);
068: Sequence[] seqs = getSequences();
069: for (int i = 0; i < seqs.length; i++)
070: removeSequence(seqs[i]);
071: _group = null;
072: }
073:
074: /**
075: * Return the schema's group.
076: */
077: public SchemaGroup getSchemaGroup() {
078: return _group;
079: }
080:
081: /**
082: * Return the name of the schema, or null if none.
083: */
084: public String getName() {
085: return _name;
086: }
087:
088: /**
089: * Set the name of the schema. This method can only be used for schemas
090: * not attached to a group.
091: */
092: public void setName(String name) {
093: if (getSchemaGroup() != null)
094: throw new IllegalStateException();
095: _name = StringUtils.trimToNull(name);
096: }
097:
098: /**
099: * Return the schema's tables.
100: */
101: public Table[] getTables() {
102: if (_tables == null)
103: _tables = (_tableMap == null) ? new Table[0]
104: : (Table[]) _tableMap.values().toArray(
105: new Table[_tableMap.size()]);
106: return _tables;
107: }
108:
109: /**
110: * Return the table with the given name, or null if none.
111: */
112: public Table getTable(String name) {
113: if (name == null || _tableMap == null)
114: return null;
115: return (Table) _tableMap.get(name.toUpperCase());
116: }
117:
118: /**
119: * Add a table to the schema.
120: */
121: public Table addTable(String name) {
122: SchemaGroup group = getSchemaGroup();
123: Table tab;
124: if (group != null) {
125: group.addName(name, true);
126: tab = group.newTable(name, this );
127: } else
128: tab = new Table(name, this );
129: if (_tableMap == null)
130: _tableMap = new TreeMap();
131: _tableMap.put(name.toUpperCase(), tab);
132: _tables = null;
133: return tab;
134: }
135:
136: /**
137: * Remove the given table from the schema.
138: *
139: * @return true if the table was removed, false if not in the schema
140: */
141: public boolean removeTable(Table tab) {
142: if (tab == null || _tableMap == null)
143: return false;
144:
145: Table cur = (Table) _tableMap.get(tab.getName().toUpperCase());
146: if (!cur.equals(tab))
147: return false;
148:
149: _tableMap.remove(tab.getName().toUpperCase());
150: _tables = null;
151: SchemaGroup group = getSchemaGroup();
152: if (group != null)
153: group.removeName(tab.getName());
154: tab.remove();
155: return true;
156: }
157:
158: /**
159: * Import a table from another schema. Note that this method does
160: * <strong>not</strong> import foreign keys, indexes, or unique constraints.
161: */
162: public Table importTable(Table table) {
163: if (table == null)
164: return null;
165:
166: Table copy = addTable(table.getName());
167: Column[] cols = table.getColumns();
168: for (int i = 0; i < cols.length; i++)
169: copy.importColumn(cols[i]);
170:
171: copy.importPrimaryKey(table.getPrimaryKey());
172: return copy;
173: }
174:
175: /**
176: * Return the schema's sequences.
177: */
178: public Sequence[] getSequences() {
179: if (_seqs == null)
180: _seqs = (_seqMap == null) ? new Sequence[0]
181: : (Sequence[]) _seqMap.values().toArray(
182: new Sequence[_seqMap.size()]);
183: return _seqs;
184: }
185:
186: /**
187: * Return the sequence with the given name, or null if none.
188: */
189: public Sequence getSequence(String name) {
190: if (name == null || _seqMap == null)
191: return null;
192: return (Sequence) _seqMap.get(name.toUpperCase());
193: }
194:
195: /**
196: * Add a sequence to the schema.
197: */
198: public Sequence addSequence(String name) {
199: SchemaGroup group = getSchemaGroup();
200: Sequence seq;
201: if (group != null) {
202: group.addName(name, true);
203: seq = group.newSequence(name, this );
204: } else
205: seq = new Sequence(name, this );
206: if (_seqMap == null)
207: _seqMap = new TreeMap();
208: _seqMap.put(name.toUpperCase(), seq);
209: _seqs = null;
210: return seq;
211: }
212:
213: /**
214: * Remove the given sequence from the schema.
215: *
216: * @return true if the sequence was removed, false if not in the schema
217: */
218: public boolean removeSequence(Sequence seq) {
219: if (seq == null || _seqMap == null)
220: return false;
221:
222: Sequence cur = (Sequence) _seqMap.get(seq.getName()
223: .toUpperCase());
224: if (!cur.equals(seq))
225: return false;
226:
227: _seqMap.remove(seq.getName().toUpperCase());
228: _seqs = null;
229: SchemaGroup group = getSchemaGroup();
230: if (group != null)
231: group.removeName(seq.getName());
232: seq.remove();
233: return true;
234: }
235:
236: /**
237: * Import a sequence from another schema.
238: */
239: public Sequence importSequence(Sequence seq) {
240: if (seq == null)
241: return null;
242:
243: Sequence copy = addSequence(seq.getName());
244: copy.setInitialValue(seq.getInitialValue());
245: copy.setIncrement(seq.getIncrement());
246: copy.setAllocate(seq.getAllocate());
247: return copy;
248: }
249:
250: public int compareTo(Object other) {
251: String name = getName();
252: String otherName = ((Schema) other).getName();
253: if (name == null && otherName == null)
254: return 0;
255: if (name == null)
256: return 1;
257: if (otherName == null)
258: return -1;
259: return name.compareTo(otherName);
260: }
261:
262: public String toString() {
263: return getName();
264: }
265: }
|