Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 37c3cb7d0c3eeb7ae42c66520363def358997ade (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
/*******************************************************************************
 * Copyright (c) 2006, 2009 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);
			// and the model is rebuilt when the connection connects or disconnects
			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();
	}

	/**
	 * Return the database object identified by the specified identifier. If
	 * the identifier is "delimited" (typically with double-quotes), it will be
	 * used without any folding. If the name is "regular" (i.e. not delimited),
	 * it will be folded to the appropriate case (typically uppercase).
	 * This is called by whenever we need to find a component by identifier
	 * (e.g. {{@link org.eclipse.jpt.db.Table#getColumnForIdentifier(String)}).
	 */
	<T extends DatabaseObject> T selectDatabaseObjectForIdentifier(Iterable<T> databaseObjects, String identifier) {
		return this.selectDatabaseObjectNamed(databaseObjects, this.convertIdentifierToName(identifier));
	}

	private String convertIdentifierToName(String identifier) {
		return this.getConnectionProfile().convertIdentifierToName(identifier);
	}

	/**
	 * Convenience method.
	 */
	<T extends DatabaseObject> T selectDatabaseObjectNamed(Iterable<T> databaseObjects, String name) {
		for (T dbObject : databaseObjects) {
			if (dbObject.getName().equals(name)) {
				return dbObject;
			}
		}
		return null;
	}

	/**
	 * Examples:<ul>
	 * <li>Oracle etc.<ul><code>
	 *     <li>Table(FOO) vs. "Foo" => null
	 *     <li>Table(BAR) vs. "Foo" => "BAR"
	 *     <li>Table(Foo) vs. "Foo" => "\"Foo\""
	 *     <li>Table(Bar) vs. "Foo" => "\"Bar\""
	 * </code></ul>
	 * <li>PostgreSQL etc.<ul><code>
	 *     <li>Table(foo) vs. "Foo" => null
	 *     <li>Table(bar) vs. "Foo" => "bar"
	 *     <li>Table(Foo) vs. "Foo" => "\"Foo\""
	 *     <li>Table(Bar) vs. "Foo" => "\"Bar\""
	 * </code></ul>
	 * <li>SQL Server etc.<ul><code>
	 *     <li>Table(Foo) vs. "Foo" => null
	 *     <li>Table(foo) vs. "Foo" => "foo"
	 *     <li>Table(bar) vs. "Foo" => "bar"
	 *     <li>Table(Bar) vs. "Foo" => "Bar"
	 * </code></ul>
	 * </ul>
	 */
	public String getIdentifier(String defaultName) {
		return this.getDatabase().convertNameToIdentifier(this.getName(), defaultName);
	}

	/**
	 * Examples:<ul>
	 * <li>Oracle etc.<ul><code>
	 *     <li>Table(FOO) => "FOO"
	 *     <li>Table(Foo) => "\"Foo\""
	 *     <li>Table(foo) => "\"foo\""
	 *     <li>Table(foo++) => "\"foo++\""
	 *     <li>Table(f"o) => "\"f\"\"o\"" (i.e. "f""o")
	 * </code></ul>
	 * <li>PostgreSQL etc.<ul><code>
	 *     <li>Table(FOO) => "\"FOO\""
	 *     <li>Table(Foo) => "\"Foo\""
	 *     <li>Table(foo) => "foo"
	 *     <li>Table(foo++) => "\"foo++\""
	 *     <li>Table(f"o) => "\"f\"\"o\"" (i.e. "f""o")
	 * </code></ul>
	 * <li>SQL Server etc.<ul><code>
	 *     <li>Table(FOO) => "FOO"
	 *     <li>Table(Foo) => "Foo"
	 *     <li>Table(foo) => "foo"
	 *     <li>Table(foo++) => "\"foo++\""
	 *     <li>Table(f"o) => "\"f\"\"o\"" (i.e. "f""o")
	 * </code></ul>
	 * </ul>
	 */
	public String getIdentifier() {
		return this.convertNameToIdentifier(this.getName());
	}

	String convertNameToIdentifier(String name) {
		return this.getConnectionProfile().convertNameToIdentifier(name);
	}

	@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