Skip to main content
summaryrefslogtreecommitdiffstats
blob: f5a90336b766c48a7769461f213ca21fa8cbd8cc (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
/*******************************************************************************
 * Copyright (c) 2008, 2011 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.eclipselink.core.internal.context.java;

import java.util.List;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jpt.common.core.utility.TextRange;
import org.eclipse.jpt.jpa.core.context.java.JavaTypeMapping;
import org.eclipse.jpt.jpa.core.internal.context.java.AbstractJavaJpaContextNode;
import org.eclipse.jpt.jpa.core.resource.java.JavaResourcePersistentType;
import org.eclipse.jpt.jpa.eclipselink.core.context.java.JavaEclipseLinkConverterContainer;
import org.eclipse.jpt.jpa.eclipselink.core.resource.java.EclipseLinkNamedConverterAnnotation;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
import org.eclipse.wst.validation.internal.provisional.core.IReporter;

public class JavaEclipseLinkConverterContainerImpl
	extends AbstractJavaJpaContextNode
	implements JavaEclipseLinkConverterContainer
{
	protected JavaEclipseLinkCustomConverter customConverter;
	protected JavaEclipseLinkObjectTypeConverter objectTypeConverter;
	protected JavaEclipseLinkStructConverter structConverter;
	protected JavaEclipseLinkTypeConverter typeConverter;


	public JavaEclipseLinkConverterContainerImpl(JavaTypeMapping parent) {
		super(parent);
		this.customConverter = this.buildCustomConverter();
		this.objectTypeConverter = this.buildObjectTypeConverter();
		this.structConverter = this.buildStructConverter();
		this.typeConverter = this.buildTypeConverter();
	}


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

	@Override
	public void synchronizeWithResourceModel() {
		super.synchronizeWithResourceModel();
		this.syncCustomConverter();
		this.syncObjectTypeConverter();
		this.syncStructConverter();
		this.syncTypeConverter();
	}

	@Override
	public void update() {
		super.update();
		if (this.customConverter != null) {
			this.customConverter.update();
		}
		if (this.objectTypeConverter != null) {
			this.objectTypeConverter.update();
		}
		if (this.structConverter != null) {
			this.structConverter.update();
		}
		if (this.typeConverter != null) {
			this.typeConverter.update();
		}
	}


	// ********** custom converter **********

	public JavaEclipseLinkCustomConverter getCustomConverter() {
		return this.customConverter;
	}

	public JavaEclipseLinkCustomConverter addCustomConverter() {
		if (this.customConverter != null) {
			throw new IllegalStateException("custom converter already exists: " + this.customConverter); //$NON-NLS-1$
		}
		JavaEclipseLinkCustomConverter converter = this.buildNewConverter(this.getCustomConverterAdapter());
		this.setCustomConverter_(converter);
		return converter;
	}

	public void removeCustomConverter() {
		if (this.customConverter == null) {
			throw new IllegalStateException("custom converter is null"); //$NON-NLS-1$
		}
		this.getCustomConverterAdapter().removeConverterAnnotation(this.getResourcePersistentType());
		this.setCustomConverter_(null);
	}

	protected void setCustomConverter_(JavaEclipseLinkCustomConverter converter) {
		JavaEclipseLinkCustomConverter old = this.customConverter;
		this.customConverter = converter;
		this.firePropertyChanged(CUSTOM_CONVERTER_PROPERTY, old, converter);
	}

	protected JavaEclipseLinkCustomConverter buildCustomConverter() {
		return this.buildConverter(this.getCustomConverterAdapter());
	}

	protected void syncCustomConverter() {
		EclipseLinkNamedConverterAnnotation annotation = this.getCustomConverterAdapter().getConverterAnnotation(this.getResourcePersistentType());
		if (annotation == null) {
			if (this.customConverter != null) {
				this.setCustomConverter_(null);
			}
		} else {
			if ((this.customConverter != null) && (this.customConverter.getConverterAnnotation() == annotation)) {
				this.customConverter.synchronizeWithResourceModel();
			} else {
				JavaEclipseLinkCustomConverter converter = this.buildConverter(this.getCustomConverterAdapter(), annotation);
				this.setCustomConverter_(converter);
			}
		}
	}

	protected JavaEclipseLinkCustomConverter.Adapter getCustomConverterAdapter() {
		return JavaEclipseLinkCustomConverter.Adapter.instance();
	}


	// ********** object type converter **********

	public JavaEclipseLinkObjectTypeConverter getObjectTypeConverter() {
		return this.objectTypeConverter;
	}

	public JavaEclipseLinkObjectTypeConverter addObjectTypeConverter() {
		if (this.objectTypeConverter != null) {
			throw new IllegalStateException("object type converter already exists: " + this.objectTypeConverter); //$NON-NLS-1$
		}
		JavaEclipseLinkObjectTypeConverter converter = this.buildNewConverter(this.getObjectTypeConverterAdapter());
		this.setObjectTypeConverter_(converter);
		return converter;
	}

	public void removeObjectTypeConverter() {
		if (this.objectTypeConverter == null) {
			throw new IllegalStateException("object type converter is null"); //$NON-NLS-1$
		}
		this.getObjectTypeConverterAdapter().removeConverterAnnotation(this.getResourcePersistentType());
		this.setObjectTypeConverter_(null);
	}

	protected void setObjectTypeConverter_(JavaEclipseLinkObjectTypeConverter converter) {
		JavaEclipseLinkObjectTypeConverter old = this.objectTypeConverter;
		this.objectTypeConverter = converter;
		this.firePropertyChanged(OBJECT_TYPE_CONVERTER_PROPERTY, old, converter);
	}

	protected JavaEclipseLinkObjectTypeConverter buildObjectTypeConverter() {
		return this.buildConverter(this.getObjectTypeConverterAdapter());
	}

	protected void syncObjectTypeConverter() {
		EclipseLinkNamedConverterAnnotation annotation = this.getObjectTypeConverterAdapter().getConverterAnnotation(this.getResourcePersistentType());
		if (annotation == null) {
			if (this.objectTypeConverter != null) {
				this.setObjectTypeConverter_(null);
			}
		} else {
			if ((this.objectTypeConverter != null) && (this.objectTypeConverter.getConverterAnnotation() == annotation)) {
				this.objectTypeConverter.synchronizeWithResourceModel();
			} else {
				JavaEclipseLinkObjectTypeConverter converter = this.buildConverter(this.getObjectTypeConverterAdapter(), annotation);
				this.setObjectTypeConverter_(converter);
			}
		}
	}

	protected JavaEclipseLinkObjectTypeConverter.Adapter getObjectTypeConverterAdapter() {
		return JavaEclipseLinkObjectTypeConverter.Adapter.instance();
	}


	// ********** struct converter **********

	public JavaEclipseLinkStructConverter getStructConverter() {
		return this.structConverter;
	}

	public JavaEclipseLinkStructConverter addStructConverter() {
		if (this.structConverter != null) {
			throw new IllegalStateException("struct converter already exists: " + this.structConverter); //$NON-NLS-1$
		}
		JavaEclipseLinkStructConverter converter = this.buildNewConverter(this.getStructConverterAdapter());
		this.setStructConverter_(converter);
		return converter;
	}

	public void removeStructConverter() {
		if (this.structConverter == null) {
			throw new IllegalStateException("struct converter is null"); //$NON-NLS-1$
		}
		this.getStructConverterAdapter().removeConverterAnnotation(this.getResourcePersistentType());
		this.setStructConverter_(null);
	}

	protected void setStructConverter_(JavaEclipseLinkStructConverter converter) {
		JavaEclipseLinkStructConverter old = this.structConverter;
		this.structConverter = converter;
		this.firePropertyChanged(STRUCT_CONVERTER_PROPERTY, old, converter);
	}

	protected JavaEclipseLinkStructConverter buildStructConverter() {
		return this.buildConverter(this.getStructConverterAdapter());
	}

	protected void syncStructConverter() {
		EclipseLinkNamedConverterAnnotation annotation = this.getStructConverterAdapter().getConverterAnnotation(this.getResourcePersistentType());
		if (annotation == null) {
			if (this.structConverter != null) {
				this.setStructConverter_(null);
			}
		} else {
			if ((this.structConverter != null) && (this.structConverter.getConverterAnnotation() == annotation)) {
				this.structConverter.synchronizeWithResourceModel();
			} else {
				JavaEclipseLinkStructConverter converter = this.buildConverter(this.getStructConverterAdapter(), annotation);
				this.setStructConverter_(converter);
			}
		}
	}

	protected JavaEclipseLinkStructConverter.Adapter getStructConverterAdapter() {
		return JavaEclipseLinkStructConverter.Adapter.instance();
	}


	// ********** object type converter **********

	public JavaEclipseLinkTypeConverter getTypeConverter() {
		return this.typeConverter;
	}

	public JavaEclipseLinkTypeConverter addTypeConverter() {
		if (this.typeConverter != null) {
			throw new IllegalStateException("type converter already exists: " + this.typeConverter); //$NON-NLS-1$
		}
		JavaEclipseLinkTypeConverter converter = this.buildNewConverter(this.getTypeConverterAdapter());
		this.setTypeConverter_(converter);
		return converter;
	}

	public void removeTypeConverter() {
		if (this.typeConverter == null) {
			throw new IllegalStateException("type converter is null"); //$NON-NLS-1$
		}
		this.getTypeConverterAdapter().removeConverterAnnotation(this.getResourcePersistentType());
		this.setTypeConverter_(null);
	}

	protected void setTypeConverter_(JavaEclipseLinkTypeConverter converter) {
		JavaEclipseLinkTypeConverter old = this.typeConverter;
		this.typeConverter = converter;
		this.firePropertyChanged(TYPE_CONVERTER_PROPERTY, old, converter);
	}

	protected JavaEclipseLinkTypeConverter buildTypeConverter() {
		return this.buildConverter(this.getTypeConverterAdapter());
	}

	protected void syncTypeConverter() {
		EclipseLinkNamedConverterAnnotation annotation = this.getTypeConverterAdapter().getConverterAnnotation(this.getResourcePersistentType());
		if (annotation == null) {
			if (this.typeConverter != null) {
				this.setTypeConverter_(null);
			}
		} else {
			if ((this.typeConverter != null) && (this.typeConverter.getConverterAnnotation() == annotation)) {
				this.typeConverter.synchronizeWithResourceModel();
			} else {
				JavaEclipseLinkTypeConverter converter = this.buildConverter(this.getTypeConverterAdapter(), annotation);
				this.setTypeConverter_(converter);
			}
		}
	}

	protected JavaEclipseLinkTypeConverter.Adapter getTypeConverterAdapter() {
		return JavaEclipseLinkTypeConverter.Adapter.instance();
	}


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

	@Override
	public JavaTypeMapping getParent() {
		return (JavaTypeMapping) super.getParent();
	}

	protected JavaTypeMapping getTypeMapping() {
		return this.getParent();
	}

	protected JavaResourcePersistentType getResourcePersistentType() {
		return this.getTypeMapping().getResourcePersistentType();
	}

	@SuppressWarnings("unchecked")
	protected <C extends JavaEclipseLinkConverter<?>> C buildConverter(C.Adapter adapter) {
		return (C) adapter.buildConverter(this.getResourcePersistentType(), this);
	}

	@SuppressWarnings("unchecked")
	protected <C extends JavaEclipseLinkConverter<?>> C buildConverter(C.Adapter adapter, EclipseLinkNamedConverterAnnotation converterAnnotation) {
		return (C) adapter.buildConverter(converterAnnotation, this);
	}

	@SuppressWarnings("unchecked")
	protected <C extends JavaEclipseLinkConverter<?>> C buildNewConverter(C.Adapter adapter) {
		return (C) adapter.buildNewConverter(this.getResourcePersistentType(), this);
	}


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

	@Override
	public void validate(List<IMessage> messages, IReporter reporter, CompilationUnit astRoot) {
		super.validate(messages, reporter, astRoot);
		if (this.customConverter != null) {
			this.customConverter.validate(messages, reporter, astRoot);
		}
		if (this.objectTypeConverter != null) {
			this.objectTypeConverter.validate(messages, reporter, astRoot);
		}
		if (this.typeConverter != null) {
			this.typeConverter.validate(messages, reporter, astRoot);
		}
		if (this.structConverter != null) {
			this.structConverter.validate(messages, reporter, astRoot);
		}
	}

	public TextRange getValidationTextRange(CompilationUnit astRoot) {
		TextRange textRange = this.getResourcePersistentType().getTextRange(astRoot);
		return (textRange != null) ? textRange : this.getTypeMapping().getValidationTextRange(astRoot);
	}
}

Back to the top