Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bed57df93bffe41f48fb1b7909c5b403d60bc200 (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
/*******************************************************************************
 * Copyright (c) 2006, 2008 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 org.eclipse.datatools.connectivity.sqm.core.rte.ICatalogObject;
import org.eclipse.datatools.connectivity.sqm.core.rte.ICatalogObjectListener;
import org.eclipse.datatools.connectivity.sqm.core.rte.RefreshManager;
import org.eclipse.jpt.db.DatabaseObject;
import org.eclipse.jpt.utility.internal.StringTools;

/**
 *  DTP Catalog Object Wrapper base class
 */
abstract class DTPDatabaseObjectWrapper
	implements DTPDatabaseObject
{
	// we need a way to get to the connection profile
	private final DTPDatabaseObject parent;

	// listen for the "catalog object" being refreshed
	private final ICatalogObjectListener catalogObjectListener;

	// listen for this DTP catalog object to refresh
	final ICatalogObject catalogObject;


	// ********** construction/initialization **********

	DTPDatabaseObjectWrapper(DTPDatabaseObject parent, Object dtpObject) {
		super();
		this.parent = parent;
		if (this.getConnectionProfile().isConnected()) {
			// we only listen to "live" connections (as opposed to "off-line" connections)
			this.catalogObject = (ICatalogObject) dtpObject;
			this.catalogObjectListener = this.buildCatalogObjectListener();
			if (this.getConnectionProfile().hasAnyListeners()) {
				this.startListening();
			}
		} else {
			this.catalogObject = null;
			this.catalogObjectListener = null;
		}
	}

	private ICatalogObjectListener buildCatalogObjectListener() {
		return new ICatalogObjectListener() {
			public void notifyChanged(ICatalogObject dmElement, int eventType) {
				if (dmElement == DTPDatabaseObjectWrapper.this.catalogObject) {
					// 'eventType' doesn't seem to be very useful, so drop it
					DTPDatabaseObjectWrapper.this.catalogObjectChanged();
				}
			}
		};
	}

	// typically, notify the connection profile something has changed
	void catalogObjectChanged() {
		this.clear();
	}

	/**
	 * The DTP object has changed, clear the wrapper's state so it will be
	 * synchronized on-demand.
	 */
	abstract void clear();



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

	DTPDatabaseObject getParent() {
		return this.parent;
	}

	public DTPConnectionProfileWrapper getConnectionProfile() {
		return this.parent.getConnectionProfile();
	}

	public DTPDatabaseWrapper getDatabase() {
		return this.getConnectionProfile().getDatabase();
	}

	/**
	 * This is called by whenever we need to find a component by name
	 * (e.g. Table.getColumnNamed(String)). We channel all the calls to the
	 * connection profile, which then delegates to the JPA platform-
	 * supplied "database finder".
	 */
	<T extends DatabaseObject> T getDatabaseObjectNamed(T[] databaseObjects, String name) {
		return this.getConnectionProfile().getDatabaseObjectNamed(databaseObjects, name);
	}

	/**
	 * Examples:
	 * Oracle etc.
	 *     Table(FOO) vs. "Foo" => null
	 *     Table(BAR) vs. "Foo" => "BAR"
	 *     Table(Foo) vs. "Foo" => "\"Foo\""
	 *     Table(Bar) vs. "Foo" => "\"Bar\""
	 *     
	 * PostgreSQL etc.
	 *     Table(foo) vs. "Foo" => null
	 *     Table(bar) vs. "Foo" => "bar"
	 *     Table(Foo) vs. "Foo" => "\"Foo\""
	 *     Table(Bar) vs. "Foo" => "\"Bar\""
	 *     
	 * SQL Server etc.
	 *     Table(Foo) vs. "Foo" => null
	 *     Table(foo) vs. "Foo" => "foo"
	 *     Table(bar) vs. "Foo" => "bar"
	 *     Table(Bar) vs. "Foo" => "Bar"
	 */
	public String getAnnotationIdentifier(String javaIdentifier) {
		return this.getAnnotationIdentifier(javaIdentifier, this.getName());
	}

	String getAnnotationIdentifier(String javaIdentifier, String dbIdentifier) {
		if (this.getDatabase().vendorFoldsToUppercase()) {
			if (StringTools.stringIsUppercase(dbIdentifier)) {
				return dbIdentifier.equalsIgnoreCase(javaIdentifier) ? null : dbIdentifier;
			}
			return this.getDatabase().delimitIdentifier(dbIdentifier);
		}
		if (this.getDatabase().vendorFoldsToLowercase()) {
			if (StringTools.stringIsLowercase(dbIdentifier)) {
				return dbIdentifier.equalsIgnoreCase(javaIdentifier) ? null : dbIdentifier;
			}
			return this.getDatabase().delimitIdentifier(dbIdentifier);
		}
		if (this.getDatabase().vendorDoesNotFold()) {
			return dbIdentifier.equals(javaIdentifier) ? null : dbIdentifier;
		}
		throw new IllegalStateException("unknown vendor folding: " + this.getDatabase().getVendor()); //$NON-NLS-1$
	}

	/**
	 * Examples:
	 * Oracle etc.
	 *     Table(FOO) => "FOO"
	 *     Table(Foo) => "\"Foo\""
	 *     Table(foo) => "\"foo\""
	 *     
	 * PostgreSQL etc.
	 *     Table(foo) => "foo"
	 *     Table(Foo) => "\"Foo\""
	 *     Table(FOO) => "\"FOO\""
	 *     
	 * SQL Server etc.
	 *     Table(Foo) => "Foo"
	 *     Table(foo) => "foo"
	 *     Table(FOO) => "FOO"
	 */
	public String getAnnotationIdentifier() {
		String name = this.getName();
		if (this.getDatabase().vendorFoldsToUppercase()) {
			return StringTools.stringIsUppercase(name) ? name : this.getDatabase().delimitIdentifier(name);
		}
		if (this.getDatabase().vendorFoldsToLowercase()) {
			return StringTools.stringIsLowercase(name) ? name : this.getDatabase().delimitIdentifier(name);
		}
		if (this.getDatabase().vendorDoesNotFold()) {
			return name;
		}
		throw new IllegalStateException("unknown vendor folding: " + this.getDatabase().getVendor()); //$NON-NLS-1$
	}

	@Override
	public String toString() {
		return StringTools.buildToStringFor(this, this.getName());
	}


	// ********** listening to DTP database object **********

	// this should only be called when the connection profile is "live" and has listeners
	void startListening() {
		this.checkListener();
		RefreshManager.getInstance().AddListener(this.catalogObject, this.catalogObjectListener);
	}

	// this should only be called when the connection profile is "live" and has no listeners
	void stopListening() {
		this.checkListener();
        RefreshManager.getInstance().removeListener(this.catalogObject, this.catalogObjectListener);
	}

	private void checkListener() {
		if (this.catalogObjectListener == null) {
			throw new IllegalStateException("the catalog listener is null");  //$NON-NLS-1$
		}
	}

}

Back to the top