Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eb4cdb0d200c27714a07e76bb64f8b56cde4515a (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
360
361
362
363
364
365
366
367
368
369
370
/*******************************************************************************
 * Copyright (c) 2013, 2015 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.jpa.core.internal.context.orm;

import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IType;
import org.eclipse.jpt.common.core.internal.utility.EmptyTextRange;
import org.eclipse.jpt.common.core.internal.utility.JavaProjectTools;
import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement.AstNodeType;
import org.eclipse.jpt.common.core.resource.java.JavaResourceType;
import org.eclipse.jpt.common.core.utility.TextRange;
import org.eclipse.jpt.common.utility.internal.ClassNameTools;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
import org.eclipse.jpt.jpa.core.context.ManagedType;
import org.eclipse.jpt.jpa.core.context.PersistentType;
import org.eclipse.jpt.jpa.core.context.java.JavaManagedType;
import org.eclipse.jpt.jpa.core.context.orm.EntityMappings;
import org.eclipse.jpt.jpa.core.context.orm.OrmManagedType;
import org.eclipse.jpt.jpa.core.resource.orm.XmlManagedType;
import org.eclipse.jpt.jpa.core.validation.JptJpaCoreValidationMessages;
import org.eclipse.text.edits.DeleteEdit;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
import org.eclipse.wst.validation.internal.provisional.core.IReporter;

/**
 * specified <code>orm.xml</code> managed type:<ul>

 * <li>Java managed type
 * </ul>
 */
public abstract class AbstractOrmManagedType<P extends EntityMappings>
	extends AbstractOrmXmlContextModel<P>
	implements OrmManagedType
{
	protected XmlManagedType xmlManagedType;

	protected String class_;

	protected String name;

	protected JavaManagedType javaManagedType;


	protected AbstractOrmManagedType(P parent, XmlManagedType xmlManagedType) {
		super(parent);
		this.xmlManagedType = xmlManagedType;
		this.class_ = this.xmlManagedType.getClassName();
		this.name = this.buildName(); 
		// 'javaManagedType' is resolved in the update
	}


	// ********** synchronize/update **********

	@Override
	public void synchronizeWithResourceModel() {
		super.synchronizeWithResourceModel();
		this.setClass_(this.xmlManagedType.getClassName());
		this.setName(this.buildName());
		this.syncJavaManagedType();
	}

	@Override
	public void update(IProgressMonitor monitor) {
		super.update(monitor);
		this.updateJavaManagedType(monitor);
	}

	public XmlManagedType getXmlManagedType() {
		return this.xmlManagedType;
	}


	// ********** class **********

	public String getClass_() {
		return this.class_;
	}

	public void setClass(String class_) {
		this.setClass_(class_);
		this.xmlManagedType.setClassName(class_);
	}

	protected void setClass_(String class_) {
		String old = this.class_;
		this.class_ = class_;
		this.firePropertyChanged(CLASS_PROPERTY, old, class_);
	}


	// ********** name **********

	public String getName() {
		return this.name;
	}

	protected void setName(String name) {
		String old = this.name;
		this.name = name;
		if (this.firePropertyChanged(NAME_PROPERTY, old, name)) {
			// clear out the Java managed type here, it will be rebuilt during "update"
			if (this.javaManagedType != null) {
				this.setJavaManagedType(null);
			}
		}
	}

	protected String buildName() {
		return this.getEntityMappings().qualify(this.class_);		
	}

	public String getSimpleName(){
		String className = this.getName();
		return StringTools.isBlank(className) ? null : ClassNameTools.simpleName(className);
	}

	public String getTypeQualifiedName() {
		String className = this.class_;
		if (className == null) {
			return null;
		}
		int lastPeriod = className.lastIndexOf('.');
		className = (lastPeriod == -1) ? className : className.substring(lastPeriod + 1);
		className = className.replace('$', '.');
		return className;
	}

	protected TextRange getClassTextRange() {
		return this.getValidationTextRange(this.getXmlManagedType().getClassTextRange());
	}


	// ********** Java managed type **********

	public JavaManagedType getJavaManagedType() {
		return this.javaManagedType;
	}

	protected void setJavaManagedType(JavaManagedType javaManagedType) {
		ManagedType old = this.javaManagedType;
		this.javaManagedType = javaManagedType;
		this.firePropertyChanged(JAVA_MANAGED_TYPE_PROPERTY, old, javaManagedType);
	}

	/**
	 * If the managed type's name changes during <em>update</em>, 
	 * the Java managed type will be cleared out in
	 * {@link #setName(String)}. If we get here and
	 * the Java managed type is present, we can
	 * <em>sync</em> it. In some circumstances it will be obsolete
	 * since the name is changed during update (the class name or
	 * the entity mapping's package affect the name)
	 *
	 * @see #updateJavaManagedType()
	 */
	protected void syncJavaManagedType() {
		if (this.javaManagedType != null) {
			this.javaManagedType.synchronizeWithResourceModel();
		}
	}

	/**
	 * @see #syncJavaManagedType()
	 */
	protected void updateJavaManagedType(IProgressMonitor monitor) {
		if (this.getName() == null) {
			if (this.javaManagedType != null) {
				this.setJavaManagedType(null);
			}			
		}
		else {
			JavaResourceType resourceType = this.resolveJavaResourceType();
			if (this.javaManagedType == null) {
				this.setJavaManagedType(this.buildJavaManagedType(resourceType));
			}
			else {
				// bug 379051 using == here because it is possible that the names are the same, 
				// but the location has changed: the java resource type has moved from "external" 
				// to part of the jpa project's jpa files. 
				if (this.javaManagedType.getJavaResourceType() == resourceType) {
					this.javaManagedType.update(monitor);
				} else {
					this.setJavaManagedType(this.buildJavaManagedType(resourceType));
				}
			}
		}
	}

	/**
	 * Return null it's an enum; don't build a JavaManagedType
	 * @see #updateJavaManagedType()
	 */
	protected JavaResourceType resolveJavaResourceType() {
		if (this.name == null) {
			return null;
		}
		return (JavaResourceType) this.getJpaProject().getJavaResourceType(this.name, AstNodeType.TYPE);
	}

	protected abstract JavaManagedType buildJavaManagedType(JavaResourceType jrt);


	public JavaResourceType getJavaResourceType() {
		return (this.javaManagedType == null) ? null : this.javaManagedType.getJavaResourceType();
	}

	public TextRange getValidationTextRange() {
		// this should never be null; 
		TextRange textRange = this.getXmlManagedType().getValidationTextRange();
		//*return an Empty text range because validation sometimes run concurrently
		//with the code adding the type mapping to xml; the IDOMNode might not
		//be set when this is called. Brian's batch update changes in 3.2 should
		//fix this problem.  bug 358745
		return (textRange != null) ? textRange : EmptyTextRange.instance();
	}

	public TextRange getFullTextRange() {
		return this.getXmlManagedType().getFullTextRange();
	}

	public boolean containsOffset(int textOffset) {
		return this.getXmlManagedType().containsOffset(textOffset);
	}


	// ********** completion proposals **********

	@Override
	public Iterable<String> getCompletionProposals(int pos) {
		Iterable<String> result = super.getCompletionProposals(pos);
		if (result != null) {
			return result;
		}
		if (this.classNameTouches(pos)) {
			return this.getCandidateClassNames();
		}
		return null;
	}

	protected Iterable<String> getCandidateClassNames() {
		return JavaProjectTools.getJavaClassNames(this.getJavaProject());
	}

	protected boolean classNameTouches(int pos) {
		return this.getXmlManagedType().classNameTouches(pos);
	}


	//*********** refactoring ***********

	public Iterable<DeleteEdit> createDeleteTypeEdits(IType type) {
		return this.isFor(type.getFullyQualifiedName('.')) ?
				IterableTools.singletonIterable(this.createDeleteTypeEdit()) :
				IterableTools.<DeleteEdit>emptyIterable();
	}
	
	protected DeleteEdit createDeleteTypeEdit() {
		return this.getXmlManagedType().createDeleteEdit();
	}

	public Iterable<ReplaceEdit> createRenameTypeEdits(IType originalType, String newName) {
		return this.isFor(originalType.getFullyQualifiedName('.')) ?
				IterableTools.singletonIterable(this.createRenameTypeEdit(originalType, newName)) :
				IterableTools.<ReplaceEdit>emptyIterable();
	}
	
	protected ReplaceEdit createRenameTypeEdit(IType originalType, String newName) {
		return this.getXmlManagedType().createRenameTypeEdit(originalType, newName);
	}

	public Iterable<ReplaceEdit> createMoveTypeEdits(IType originalType, IPackageFragment newPackage) {
		return this.isFor(originalType.getFullyQualifiedName('.')) ?
				IterableTools.singletonIterable(this.createRenamePackageEdit(newPackage.getElementName())) :
				IterableTools.<ReplaceEdit>emptyIterable();
	}

	public Iterable<ReplaceEdit> createRenamePackageEdits(IPackageFragment originalPackage, String newName) {
		return this.isIn(originalPackage) ?
				IterableTools.singletonIterable(this.createRenamePackageEdit(newName)) :
				IterableTools.<ReplaceEdit>emptyIterable();
	}

	protected ReplaceEdit createRenamePackageEdit(String newName) {
		return this.getXmlManagedType().createRenamePackageEdit(newName);
	}


	// ********** validation **********

	@Override
	public void validate(List<IMessage> messages, IReporter reporter) {
		super.validate(messages, reporter);
		this.validateClass(messages);
	}

	protected void validateClass(List<IMessage> messages) {
		if (StringTools.isBlank(this.class_)) {
			messages.add(
				this.buildValidationMessage(
					this.getClassTextRange(),
					JptJpaCoreValidationMessages.MANAGED_TYPE_UNSPECIFIED_CLASS
				)
			);
			return;
		}
		this.validateClassResolves(messages);
	}

	protected void validateClassResolves(List<IMessage> messages) {
		if (this.javaManagedType == null) {
			messages.add(
				this.buildValidationMessage(
					this.getClassTextRange(),
					JptJpaCoreValidationMessages.MANAGED_TYPE_UNRESOLVED_CLASS,
					this.getName()
				)
			);
		}
	}


	// ********** misc **********

	protected EntityMappings getEntityMappings() {
		return this.parent;
	}

	public String getDefaultPackage() {
		return this.getEntityMappings().getDefaultPersistentTypePackage();
	}

	public boolean isFor(String typeName) {
		return ObjectTools.equals(typeName, this.getName());
	}

	public boolean isIn(IPackageFragment packageFragment) {
		String packageName = this.getPackageName();
		if (ObjectTools.equals(packageName, packageFragment.getElementName())) {
			return true;
		}
		return false;
	}

	protected String getPackageName() {
		String className = this.class_;
		if (className == null) {
			return null;
		}
		int lastPeriod = className.lastIndexOf('.');
		return (lastPeriod == -1) ? this.getDefaultPackage() : className.substring(0, lastPeriod);
	}

	@Override
	public void toString(StringBuilder sb) {
		sb.append(this.getName());
	}
}

Back to the top