Skip to main content
summaryrefslogtreecommitdiffstats
blob: d1aec207e9d7d218b3f1569498423fb2726da9c2 (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
/*******************************************************************************
 *  Copyright (c) 2010  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.core.internal.context.orm;

import java.util.List;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IType;
import org.eclipse.jpt.core.context.AccessType;
import org.eclipse.jpt.core.context.java.JavaIdClassReference;
import org.eclipse.jpt.core.context.java.JavaPersistentType;
import org.eclipse.jpt.core.context.orm.EntityMappings;
import org.eclipse.jpt.core.context.orm.OrmIdClassReference;
import org.eclipse.jpt.core.context.orm.OrmPersistentType;
import org.eclipse.jpt.core.context.orm.OrmTypeMapping;
import org.eclipse.jpt.core.internal.context.AbstractXmlContextNode;
import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType;
import org.eclipse.jpt.core.resource.orm.OrmFactory;
import org.eclipse.jpt.core.resource.orm.XmlClassReference;
import org.eclipse.jpt.core.resource.orm.XmlIdClassContainer;
import org.eclipse.jpt.core.resource.orm.XmlTypeMapping;
import org.eclipse.jpt.core.utility.TextRange;
import org.eclipse.jpt.utility.internal.iterables.EmptyIterable;
import org.eclipse.jpt.utility.internal.iterables.SingleElementIterable;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
import org.eclipse.wst.validation.internal.provisional.core.IReporter;

public class GenericOrmIdClassReference
	extends AbstractXmlContextNode
	implements OrmIdClassReference
{
	protected String specifiedIdClassName;
	
	protected String defaultIdClassName;

	protected JavaPersistentType idClass;
	
	
	public GenericOrmIdClassReference(OrmTypeMapping parent, JavaIdClassReference javaIdClassReference) {
		super(parent);
		this.specifiedIdClassName = buildSpecifiedIdClassName();
		this.defaultIdClassName = buildDefaultIdClassName(javaIdClassReference);
		this.idClass = buildIdClass();
	}
	
	
	protected OrmTypeMapping getTypeMapping() {
		return (OrmTypeMapping) getParent();
	}
	
	protected OrmPersistentType getPersistentType() {
		return getTypeMapping().getPersistentType();
	}
	
	
	// **************** PersistentType.Owner impl *****************************
	
	public AccessType getOverridePersistentTypeAccess() {
		return getPersistentType().getAccess();
	}
	
	public AccessType getDefaultPersistentTypeAccess() {
		// this shouldn't be needed, since we've specified an override access, but just to be safe ...
		return getPersistentType().getAccess();
	}
	
	
	// **************** IdClassReference impl *********************************
	
	public String getSpecifiedIdClassName() {
		return this.specifiedIdClassName;
	}
	
	public void setSpecifiedIdClassName(String newClassName) {
		String oldClassName = this.specifiedIdClassName;
		this.specifiedIdClassName = newClassName;
		if (valuesAreDifferent(oldClassName, newClassName)) {
			if (getIdXmlClassRef() != null) {
				getIdXmlClassRef().setClassName(newClassName);
				if (getIdXmlClassRef().isUnset()) {
					removeIdClassElement();
				}
			}
			else if (newClassName != null) {
				addIdClassElement();
				getIdXmlClassRef().setClassName(newClassName);
			}
		}
		firePropertyChanged(SPECIFIED_ID_CLASS_NAME_PROPERTY, oldClassName, newClassName);
	}
	
	protected void setSpecifiedIdClassName_(String newClassName) {
		String oldClassName = this.specifiedIdClassName;
		this.specifiedIdClassName = newClassName;
		firePropertyChanged(SPECIFIED_ID_CLASS_NAME_PROPERTY, oldClassName, newClassName);
	}
	
	protected String buildSpecifiedIdClassName() {
		XmlClassReference idXmlClassRef = this.getIdXmlClassRef();
		return (idXmlClassRef == null) ? null : idXmlClassRef.getClassName();
	}
	
	public String getDefaultIdClassName() {
		return this.defaultIdClassName;
	}
	
	protected void setDefaultIdClassName_(String newClassName) {
		String oldClassName = this.defaultIdClassName;
		this.defaultIdClassName = newClassName;
		firePropertyChanged(DEFAULT_ID_CLASS_NAME_PROPERTY, oldClassName, newClassName);
	}
	
	protected String buildDefaultIdClassName(JavaIdClassReference javaIdClassReference) {
		return (javaIdClassReference == null) ? null : javaIdClassReference.getFullyQualifiedIdClassName();
	}
	
	public String getIdClassName() {
		return (this.specifiedIdClassName == null) ? this.defaultIdClassName : this.specifiedIdClassName;
	}
	
	public boolean isSpecified() {
		return getIdClassName() != null;
	}
	
	public JavaPersistentType getIdClass() {
		return this.idClass;
	}
	
	protected void setIdClass_(JavaPersistentType newIdClass) {
		JavaPersistentType oldIdClass = this.idClass;
		this.idClass = newIdClass;
		firePropertyChanged(ID_CLASS_PROPERTY, oldIdClass, newIdClass);
	}
	
	protected JavaPersistentType buildIdClass() {
		JavaResourcePersistentType resourceIdClass = getResourceIdClass();
		return (resourceIdClass == null) ? 
				null : this.buildIdClass(resourceIdClass);
	}
	
	protected JavaPersistentType buildIdClass(JavaResourcePersistentType resourceClass) {
		return getJpaFactory().buildJavaPersistentType(this, resourceClass);
	}
	
	protected XmlTypeMapping getResourceTypeMapping() {
		return getTypeMapping().getResourceTypeMapping();
	}
	
	protected XmlIdClassContainer getResourceIdClassContainer() {
		return (XmlIdClassContainer) getResourceTypeMapping();
	}
	
	protected XmlClassReference getIdXmlClassRef() {
		return this.getResourceIdClassContainer().getIdClass();
	}
	
	protected void addIdClassElement() {
		getResourceIdClassContainer().setIdClass(OrmFactory.eINSTANCE.createXmlClassReference());		
	}
	
	protected void removeIdClassElement() {
		getResourceIdClassContainer().setIdClass(null);
	}
	
	protected JavaResourcePersistentType getResourceIdClass() {
		XmlClassReference idXmlClassRef = this.getIdXmlClassRef();
		if (idXmlClassRef == null) {
			return null;
		}

		String className = idXmlClassRef.getClassName();
		if (className == null) {
			return null;
		}
		
		return this.getEntityMappings().resolveJavaResourcePersistentType(className);
	}
	
	protected EntityMappings getEntityMappings() {
		return (EntityMappings) getMappingFileRoot();
	}

	public char getIdClassEnclosingTypeSeparator() {
		return '$';
	}
	
	public void update(JavaIdClassReference javaIdClassReference) {
		setDefaultIdClassName_(buildDefaultIdClassName(javaIdClassReference));
		setSpecifiedIdClassName_(buildSpecifiedIdClassName());
		updateIdClass();
	}
	
	protected void updateIdClass() {
		JavaResourcePersistentType resourceIdClass = getResourceIdClass();
		if (resourceIdClass == null) {
			setIdClass_(null);
		}
		else { 
			if (this.idClass == null || this.idClass.getResourcePersistentType() != resourceIdClass) {
				setIdClass_(buildIdClass(resourceIdClass));
			}
			else {
				this.idClass.update(resourceIdClass);
			}
		}
	}


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

	public Iterable<ReplaceEdit> createRenameTypeEdits(IType originalType, String newName) {
		if (this.isFor(originalType.getFullyQualifiedName('.'))) {
			return new SingleElementIterable<ReplaceEdit>(this.createRenameEdit(originalType, newName));
		}
		return EmptyIterable.instance();
	}

	protected ReplaceEdit createRenameEdit(IType originalType, String newName) {
		return getIdXmlClassRef().createRenameEdit(originalType, newName);
	}

	protected boolean isFor(String typeName) {
		if (this.idClass != null && this.idClass.isFor(typeName)) {
			return true;
		}
		return false;
	}

	public Iterable<ReplaceEdit> createMoveTypeEdits(IType originalType, IPackageFragment newPackage) {
		if (this.isFor(originalType.getFullyQualifiedName('.'))) {
			return new SingleElementIterable<ReplaceEdit>(this.createRenamePackageEdit(newPackage.getElementName()));
		}
		return EmptyIterable.instance();
	}

	public Iterable<ReplaceEdit> createRenamePackageEdits(IPackageFragment originalPackage, String newName) {
		if (this.isIn(originalPackage)) {
			return new SingleElementIterable<ReplaceEdit>(this.createRenamePackageEdit(newName));
		}
		return EmptyIterable.instance();
	}

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

	protected boolean isIn(IPackageFragment originalPackage) {
		if (this.idClass != null && this.idClass.isIn(originalPackage)) {
			return true;
		}
		return false;
	}


	// **************** validation ********************************************
	
	public TextRange getValidationTextRange() {
		XmlClassReference idXmlClassRef = getIdXmlClassRef();
		return (idXmlClassRef == null) ?
				this.getTypeMapping().getValidationTextRange() :
				idXmlClassRef.getClassNameTextRange();
	}
	
	@Override
	public void validate(List<IMessage> messages, IReporter reporter) {
		super.validate(messages, reporter);
		// most validation is done "holistically" from the type mapping level
	}
}

Back to the top