Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c35b6b1366a343d8e86cee28c377ceeaaecd1d11 (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
/*******************************************************************************
 * 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.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
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.emf.common.util.EList;

/**
 *  Wrap a DTP Database
 */
public final class DTPDatabaseWrapper extends Database {
	
	final ConnectionProfile profile;
	final org.eclipse.datatools.modelbase.sql.schema.Database dtpDatabase;
	private ICatalogObjectListener databaseListener;
	
	private Set<Catalog> catalogs;  // lazy-initialized
	private Set<Schema> schemata;  // lazy-initialized

	DTPDatabaseWrapper( ConnectionProfile profile, org.eclipse.datatools.modelbase.sql.schema.Database dtpDatabase) {
		super();
		this.dtpDatabase = dtpDatabase;
		this.profile = profile;
		this.initialize();
	}

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

	private void initialize() {
		
		if( this.connectionIsOnline()) {
			this.databaseListener = this.buildDatabaseListener();
			this.addCatalogObjectListener(( ICatalogObject) this.dtpDatabase, this.databaseListener);
		}
	}
	
	@Override
	protected boolean connectionIsOnline() {
		return this.profile.isConnected();
	}
	
	private ICatalogObjectListener buildDatabaseListener() {
	   return new ICatalogObjectListener() {
		    public void notifyChanged( final ICatalogObject database, final int eventType) {     
				if( database == DTPDatabaseWrapper.this.dtpDatabase) {	
					DTPDatabaseWrapper.this.refresh();
					DTPDatabaseWrapper.this.profile.databaseChanged( DTPDatabaseWrapper.this, eventType);
				}
		    }
	    };
	}

	@Override
	void refresh() {
		this.disposeSchemata();
		this.disposeCatalogs();
		
		this.schemata = null;
		this.catalogs = null;
	}
	
	@Override
	void catalogChanged( Catalog catalog, int eventType) {
		this.profile.catalogChanged( catalog, this, eventType);
		return;
	}	
		
	@Override
	void schemaChanged( Schema schema, int eventType) {
		this.profile.schemaChanged( schema, this, eventType);
		return;
	}

	@Override
	void tableChanged( Table table,  Schema schema, int eventType) {
		this.profile.tableChanged( table, schema, this, eventType);
		return;
	}
	
	@Override
	protected void dispose() {
		this.removeCatalogObjectListener(( ICatalogObject) this.dtpDatabase, this.databaseListener);

		this.disposeSchemata();
		this.disposeCatalogs();
	}

	private void disposeSchemata() {
		if( this.schemata != null) {
			for( Iterator<Schema> stream = this.schemata(); stream.hasNext(); ) {
				stream.next().dispose();
			}
		}
	}
	
	private void disposeCatalogs() {
		if( this.catalogs != null) {
			for( Iterator<Catalog> stream = this.catalogs(); stream.hasNext(); ) {
				stream.next().dispose();
			}
		}
	}
	
	// ********** queries **********

	@Override
	public String getName() {

		return this.dtpDatabase.getName();
	}

	@Override
	public String getVendor() {
		
		return this.dtpDatabase.getVendor();
	}
	
	@Override
	public String getVersion() {
		
		return this.dtpDatabase.getVersion();
	}
	
	
	// ***** schemata

	@Override
	synchronized Set<Schema> getSchemata() {
		if( this.schemata == null) {
			this.schemata = this.buildSchemata();
		}
		return this.schemata;
	}

	@SuppressWarnings("unchecked")
	private Set<Schema> buildSchemata() {
		Set<Schema> result;
		if( this.supportsCatalogs()) {
			result = this.getSchemataForCatalogNamed( this.profile.getCatalogName());
		}
		else {
			EList<org.eclipse.datatools.modelbase.sql.schema.Schema> dtpSchemata = this.dtpDatabase.getSchemas();
			result = new HashSet<Schema>( dtpSchemata.size());
			for (org.eclipse.datatools.modelbase.sql.schema.Schema dtpSchema : dtpSchemata) {
				result.add( this.wrap(dtpSchema));
			}
		}
		return result;
	}
	
	// ***** catalogs

	@SuppressWarnings("unchecked")
	@Override
	public boolean supportsCatalogs() {
		EList<org.eclipse.datatools.modelbase.sql.schema.Schema> dtpSchemata = this.dtpDatabase.getSchemas();
		return ( dtpSchemata == null || dtpSchemata.size() == 0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public String getDefaultCatalogName() {
		
		if( !this.supportsCatalogs()) {	// this database doesn't support catalogs
			return "";
		}
		String userName = this.profile.getUserName();
		for (org.eclipse.datatools.modelbase.sql.schema.Catalog dtpCatalog : (EList<org.eclipse.datatools.modelbase.sql.schema.Catalog>) this.dtpDatabase.getCatalogs()) {
			if( dtpCatalog.getName().length() == 0) {	// special catalog that contains all schemata
				return "";
			}
			else if( dtpCatalog.getName().equals( userName)) {
				return userName;		// returns user name as default catalog
			}
		}
		throw new NoSuchElementException();
	}
	
	@Override
	synchronized Set<Catalog> getCatalogs() {
		if( this.catalogs == null) {
			this.catalogs = this.buildCatalogs();
		}
		return this.catalogs;
	}

	@SuppressWarnings("unchecked")
	private Set<Catalog> buildCatalogs() {
		
		EList<org.eclipse.datatools.modelbase.sql.schema.Catalog> dtpCatalogs = this.dtpDatabase.getCatalogs();
		if( dtpCatalogs == null) {
			return Collections.emptySet();
		}
		Set<Catalog> result = new HashSet<Catalog>( dtpCatalogs.size());
		for (org.eclipse.datatools.modelbase.sql.schema.Catalog dtpCatalog : dtpCatalogs) {
			result.add( this.wrap(dtpCatalog));
		}
		return result;
	}
	
	private Set<Schema> getSchemataForCatalogNamed( String catalogName) {

		Catalog catalog = this.catalogNamed( catalogName);
		return ( catalog != null) ? catalog.buildSchemata() : Collections.<Schema>emptySet();
	}
}

Back to the top