Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7b49b72f8c087661d930684ba0671715f6d7e6b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*******************************************************************************
 * Copyright (c) 2006, 2007 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.db.internal;

import java.text.Collator;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.datatools.connectivity.sqm.core.rte.ICatalogObject;
import org.eclipse.datatools.connectivity.sqm.core.rte.ICatalogObjectListener;
import org.eclipse.datatools.modelbase.sql.constraints.PrimaryKey;
import org.eclipse.datatools.modelbase.sql.tables.BaseTable;
import org.eclipse.jpt.utility.internal.CollectionTools;
import org.eclipse.jpt.utility.internal.NameTools;
import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.jpt.utility.internal.iterators.TransformationIterator;

/**
 *  Wrap a DTP Table
 */
public final class Table extends DTPWrapper implements Comparable<Table> {
	final Schema schema;
	final org.eclipse.datatools.modelbase.sql.tables.Table dtpTable;
	private ICatalogObjectListener tableListener;
	
	private Set<Column> columns;  // lazy-initialized
	private Set<Column> primaryKeyColumns;  // lazy-initialized
	private Set<ForeignKey> foreignKeys;  // lazy-initialized
	private Set<Column> foreignKeyColumns;  // lazy-initialized
	
	// ********** constructors **********

	Table(Schema schema, org.eclipse.datatools.modelbase.sql.tables.Table dtpTable) {
		super();
		this.schema = schema;
		this.dtpTable = dtpTable;
		this.initialize();
	}


	// ********** behavior **********

	private void initialize() {
		if( this.connectionIsOnline()) {
			this.tableListener = this.buildTableListener();
			this.addCatalogObjectListener(( ICatalogObject) this.dtpTable, this.tableListener);
		}
	}
	
	@Override
	protected boolean connectionIsOnline() {
		return this.schema.connectionIsOnline();
	}
	
	private ICatalogObjectListener buildTableListener() {
       return new ICatalogObjectListener() {
    	    public void notifyChanged( final ICatalogObject table, final int eventType) {     
    			if( table == Table.this.dtpTable) {	    	    	
    				Table.this.refresh();
	    			Table.this.schema.tableChanged( Table.this, eventType);
    			}
    	    }
        };
    }

    void refresh() {
		this.disposeColumns();
		
    	this.columns = null;
    	this.primaryKeyColumns = null;
    	this.foreignKeys = null;
    	this.foreignKeyColumns = null;
    }

	@Override
	protected void dispose() {
		this.removeCatalogObjectListener(( ICatalogObject) this.dtpTable, this.tableListener);
		
		this.disposeColumns();
		this.disposeForeignKey();
	}

	private void disposeColumns() {
		if( this.columns != null) {
			for( Iterator<Column> stream = this.columns(); stream.hasNext(); ) {
				stream.next().dispose();
			}
		}
	}

	private void disposeForeignKey() {
		if( this.foreignKeys != null) {
			for( Iterator<ForeignKey> stream = this.foreignKeys(); stream.hasNext(); ) {
				stream.next().dispose();
			}
		}
	}

	// ********** queries **********

	@Override
	public String getName() {
		return this.dtpTable.getName();
	}
	
	boolean isCaseSensitive() {
		return this.schema.isCaseSensitive();
	}

	public String shortJavaClassName() {
		String jName = this.getName();
		if ( ! this.isCaseSensitive()) {
			jName = StringTools.capitalize(jName.toLowerCase());
		}
		return NameTools.convertToJavaIdentifier(jName);
	}

	public boolean matchesShortJavaClassName(String shortJavaClassName) {
		return this.isCaseSensitive() ?
			this.getName().equals(shortJavaClassName)
		:
			this.getName().equalsIgnoreCase(shortJavaClassName);
	}

	public String javaFieldName() {
		String jName = this.getName();
		if ( ! this.isCaseSensitive()) {
			jName = jName.toLowerCase();
		}
		return NameTools.convertToJavaIdentifier(jName);
	}

	boolean wraps(org.eclipse.datatools.modelbase.sql.tables.Table table) {
		return this.dtpTable == table;
	}

	/**
	 * return the table for the specified DTP table
	 */
	Table table(org.eclipse.datatools.modelbase.sql.tables.Table table) {
		return this.schema.table(table);
	}

	// ***** columns

	private synchronized Set<Column> getColumns() {
		if (this.columns == null) {
			this.columns = this.buildColumns();
		}
		return this.columns;
	}

	@SuppressWarnings("unchecked")
	private Collection<org.eclipse.datatools.modelbase.sql.tables.Column> dtpColumns() {
		return this.dtpTable.getColumns();
	}

	private Set<Column> buildColumns() {
		Collection<org.eclipse.datatools.modelbase.sql.tables.Column> dtpColumns = this.dtpColumns();
		Set<Column> result = new HashSet<Column>(dtpColumns.size());
		for (org.eclipse.datatools.modelbase.sql.tables.Column c : dtpColumns) {
			result.add(new Column(this, c));
		}
		return result;
	}

	public Iterator<Column> columns() {
		return this.getColumns().iterator();
	}

	public int columnsSize() {
		return this.getColumns().size();
	}

	public boolean columnsContains(Column column) {
		return this.getColumns().contains(column);
	}

	public Iterator<String> columnNames() {
		return new TransformationIterator<Column, String>(this.columns()) {
			@Override
			protected String transform(Column next) {
				 return next.getName();
			}
		};
	}

	public boolean containsColumnNamed(String name) {
		return this.columnNamed(name) != null;
	}

	public Column columnNamed(String name) {
		return this.isCaseSensitive() ? this.columnNamedInternal(name) : this.columnNamedIgnoreCase(name);
	}
	
	private Column columnNamedInternal(String name) {
		for (Iterator<Column> stream = this.columns(); stream.hasNext(); ) {
			Column column = stream.next();
			if (column.getName().equals(name)) {
				return column;
			}
		}
		return null;
	}

	private Column columnNamedIgnoreCase(String name) {
		for (Iterator<Column> stream = this.columns(); stream.hasNext(); ) {
			Column column = stream.next();
			if (StringTools.stringsAreEqualIgnoreCase(column.getName(), name)) {
				return column;
			}
		}
		return null;
	}
	
	/**
	 * return the column for the specified dtp column
	 */
	Column column(org.eclipse.datatools.modelbase.sql.tables.Column dtpColumn) {
		if (dtpColumn.getTable() != this.dtpTable) {
			return this.schema.column(dtpColumn);
		}
		for (Iterator<Column> stream = this.columns(); stream.hasNext(); ) {
			Column column = stream.next();
			if (column.wraps(dtpColumn)) {
				return column;
			}
		}
		throw new IllegalArgumentException("invalid dtp column: " + dtpColumn);
	}


	// ***** primaryKeyColumns

	private synchronized Set<Column> getPrimaryKeyColumns() {
		if (this.primaryKeyColumns == null) {
			this.primaryKeyColumns = this.buildPrimaryKeyColumns();
		}
		return this.primaryKeyColumns;
	}

	@SuppressWarnings("unchecked")
	private Collection<org.eclipse.datatools.modelbase.sql.tables.Column> columns(PrimaryKey pk) {
		return pk.getMembers();
	}

	private Set<Column> buildPrimaryKeyColumns() {
		if ( ! (this.dtpTable instanceof BaseTable)) {
			return Collections.emptySet();
		}
		PrimaryKey pk = ((BaseTable) this.dtpTable).getPrimaryKey();
		if (pk == null) {
			// no PK was defined
			return Collections.emptySet();
		}
		Collection<org.eclipse.datatools.modelbase.sql.tables.Column> pkColumns = this.columns(pk);
		Set<Column> result = new HashSet<Column>(pkColumns.size());
		for (org.eclipse.datatools.modelbase.sql.tables.Column pkColumn : pkColumns) {
			result.add(this.column(pkColumn));
		}
		return result;
	}

	public Iterator<Column> primaryKeyColumns() {
		return this.getPrimaryKeyColumns().iterator();
	}

	public int primaryKeyColumnsSize() {
		return this.getPrimaryKeyColumns().size();
	}

	public boolean primaryKeyColumnsContains(Column column) {
		return this.getPrimaryKeyColumns().contains(column);
	}


	// ***** foreignKeys

	private synchronized Set<ForeignKey> getForeignKeys() {
		if (this.foreignKeys == null) {
			this.foreignKeys = this.buildForeignKeys();
		}
		return this.foreignKeys;
	}

	@SuppressWarnings("unchecked")
	private Collection<org.eclipse.datatools.modelbase.sql.constraints.ForeignKey> dtpForeignKeys() {
		return ((BaseTable) this.dtpTable).getForeignKeys();
	}

	private Set<ForeignKey> buildForeignKeys() {
		if ( ! (this.dtpTable instanceof BaseTable)) {
			return Collections.emptySet();
		}
		Collection<org.eclipse.datatools.modelbase.sql.constraints.ForeignKey> dtpForeignKeys = this.dtpForeignKeys();
		Set<ForeignKey> result = new HashSet<ForeignKey>(dtpForeignKeys.size());
		for (org.eclipse.datatools.modelbase.sql.constraints.ForeignKey dtpForeignKey : dtpForeignKeys) {
			result.add(new ForeignKey(this, dtpForeignKey));
		}
		return result;
	}

	public Iterator<ForeignKey> foreignKeys() {
		return this.getForeignKeys().iterator();
	}

	public int foreignKeysSize() {
		return this.getForeignKeys().size();
	}


	// ***** foreignKeyColumns

	private synchronized Set<Column> getForeignKeyColumns() {
		if (this.foreignKeyColumns == null) {
			this.foreignKeyColumns = this.buildForeignKeyColumns();
		}
		return this.foreignKeyColumns;
	}

	private Set<Column> buildForeignKeyColumns() {
		if ( ! (this.dtpTable instanceof BaseTable)) {
			return Collections.emptySet();
		}
		Set<Column> result = new HashSet<Column>(this.columnsSize());
		for (Iterator<ForeignKey> stream = this.foreignKeys(); stream.hasNext(); ) {
			CollectionTools.addAll(result, stream.next().baseColumns());
		}
		return result;
	}

	public Iterator<Column> foreignKeyColumns() {
		return this.getForeignKeyColumns().iterator();
	}

	public int foreignKeyColumnsSize() {
		return this.getForeignKeyColumns().size();
	}

	public boolean foreignKeyColumnsContains(Column column) {
		return this.getForeignKeyColumns().contains(column);
	}

	// ********** Comparable implementation **********

	public int compareTo( Table table) {
		return Collator.getInstance().compare( this.getName(), table.getName());
	}
}

Back to the top