Skip to main content
summaryrefslogtreecommitdiffstats
blob: 365d2e5263e2c491baca0169ac55a49baec04b4f (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
/*******************************************************************************
 * Copyright (c) 2009, 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.jpa.core.internal.resource.java.binary;

import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jpt.common.core.utility.TextRange;
import org.eclipse.jpt.common.utility.internal.ArrayTools;
import org.eclipse.jpt.jpa.core.jpa2.resource.java.RelationshipMapping2_0Annotation;
import org.eclipse.jpt.jpa.core.resource.java.CascadeType;
import org.eclipse.jpt.jpa.core.resource.java.FetchType;
import org.eclipse.jpt.jpa.core.resource.java.JavaResourcePersistentAttribute;

/**
 * javax.persistence.ManyToMany
 * javax.persistence.ManyToOne
 * javax.persistence.OneToMany
 * javax.persistence.OneToOne
 */
abstract class BinaryRelationshipMappingAnnotation
	extends BinaryAnnotation
	implements RelationshipMapping2_0Annotation
{
	String targetEntity;
	FetchType fetch;
	boolean cascadeAll;
	boolean cascadeMerge;
	boolean cascadePersist;
	boolean cascadeRefresh;
	boolean cascadeRemove;
	boolean cascadeDetach; //added in JPA 2.0


	BinaryRelationshipMappingAnnotation(JavaResourcePersistentAttribute parent, IAnnotation jdtAnnotation) {
		super(parent, jdtAnnotation);
		this.targetEntity = this.buildTargetEntity();
		this.fetch = this.buildFetch();
		this.initializeCascadeTypes(this.buildCascadeTypes());
	}
	
	protected void initializeCascadeTypes(CascadeType[] cascadeTypes) {
		this.cascadeAll = ArrayTools.contains(cascadeTypes, CascadeType.ALL);
		this.cascadeMerge = ArrayTools.contains(cascadeTypes, CascadeType.MERGE);
		this.cascadePersist = ArrayTools.contains(cascadeTypes, CascadeType.PERSIST);
		this.cascadeRefresh = ArrayTools.contains(cascadeTypes, CascadeType.REFRESH);
		this.cascadeRemove = ArrayTools.contains(cascadeTypes, CascadeType.REMOVE);
		this.cascadeDetach = ArrayTools.contains(cascadeTypes, CascadeType.DETACH);
	}

	@Override
	public void update() {
		super.update();
		this.setTargetEntity_(this.buildTargetEntity());
		this.setFetch_(this.buildFetch());
		this.updateCascadeTypes(this.buildCascadeTypes());
	}

	protected void updateCascadeTypes(CascadeType[] cascadeTypes) {
		this.setCascadeAll_(ArrayTools.contains(cascadeTypes, CascadeType.ALL));
		this.setCascadeMerge_(ArrayTools.contains(cascadeTypes, CascadeType.MERGE));
		this.setCascadePersist_(ArrayTools.contains(cascadeTypes, CascadeType.PERSIST));
		this.setCascadeRefresh_(ArrayTools.contains(cascadeTypes, CascadeType.REFRESH));
		this.setCascadeRemove_(ArrayTools.contains(cascadeTypes, CascadeType.REMOVE));
		this.setCascadeDetach_(ArrayTools.contains(cascadeTypes, CascadeType.DETACH));
	}

	// ********** RelationshipMappingAnnotation implementation **********

	// ***** target entity
	public String getTargetEntity() {
		return this.targetEntity;
	}

	public void setTargetEntity(String targetEntity) {
		throw new UnsupportedOperationException();
	}

	private void setTargetEntity_(String targetEntity) {
		String old = this.targetEntity;
		this.targetEntity = targetEntity;
		this.firePropertyChanged(TARGET_ENTITY_PROPERTY, old, targetEntity);
	}

	private String buildTargetEntity() {
		return (String) this.getJdtMemberValue(this.getTargetEntityElementName());
	}

	abstract String getTargetEntityElementName();

	public TextRange getTargetEntityTextRange(CompilationUnit astRoot) {
		throw new UnsupportedOperationException();
	}

	// ***** fully-qualified target entity class name
	public String getFullyQualifiedTargetEntityClassName() {
		return this.targetEntity;
	}

	// ***** fetch
	public FetchType getFetch() {
		return this.fetch;
	}

	public void setFetch(FetchType fetch) {
		throw new UnsupportedOperationException();
	}

	private void setFetch_(FetchType fetch) {
		FetchType old = this.fetch;
		this.fetch = fetch;
		this.firePropertyChanged(FETCH_PROPERTY, old, fetch);
	}

	private FetchType buildFetch() {
		return FetchType.fromJavaAnnotationValue(this.getJdtMemberValue(this.getFetchElementName()));
	}

	abstract String getFetchElementName();

	public TextRange getFetchTextRange(CompilationUnit astRoot) {
		throw new UnsupportedOperationException();
	}

	// ***** cascade
	private CascadeType[] buildCascadeTypes() {
		return CascadeType.fromJavaAnnotationValues(this.getJdtMemberValues(this.getCascadeElementName()));
	}

	abstract String getCascadeElementName();

	public TextRange getCascadeTextRange(CompilationUnit astRoot) {
		throw new UnsupportedOperationException();
	}

	// ***** cascade all
	public boolean isCascadeAll() {
		return this.cascadeAll;
	}

	public void setCascadeAll(boolean cascadeAll) {
		throw new UnsupportedOperationException();
	}

	private void setCascadeAll_(boolean cascadeAll) {
		boolean old = this.cascadeAll;
		this.cascadeAll = cascadeAll;
		this.firePropertyChanged(CASCADE_ALL_PROPERTY, old, cascadeAll);
	}

	// ***** cascade merge
	public boolean isCascadeMerge() {
		return this.cascadeMerge;
	}

	public void setCascadeMerge(boolean cascadeMerge) {
		throw new UnsupportedOperationException();
	}

	private void setCascadeMerge_(boolean cascadeMerge) {
		boolean old = this.cascadeMerge;
		this.cascadeMerge = cascadeMerge;
		this.firePropertyChanged(CASCADE_MERGE_PROPERTY, old, cascadeMerge);
	}

	// ***** cascade persist
	public boolean isCascadePersist() {
		return this.cascadePersist;
	}

	public void setCascadePersist(boolean cascadePersist) {
		throw new UnsupportedOperationException();
	}

	private void setCascadePersist_(boolean cascadePersist) {
		boolean old = this.cascadePersist;
		this.cascadePersist = cascadePersist;
		this.firePropertyChanged(CASCADE_PERSIST_PROPERTY, old, cascadePersist);
	}

	// ***** cascade refresh
	public boolean isCascadeRefresh() {
		return this.cascadeRefresh;
	}

	public void setCascadeRefresh(boolean cascadeRefresh) {
		throw new UnsupportedOperationException();
	}

	private void setCascadeRefresh_(boolean cascadeRefresh) {
		boolean old = this.cascadeRefresh;
		this.cascadeRefresh = cascadeRefresh;
		this.firePropertyChanged(CASCADE_REFRESH_PROPERTY, old, cascadeRefresh);
	}

	// ***** cascade remove
	public boolean isCascadeRemove() {
		return this.cascadeRemove;
	}

	public void setCascadeRemove(boolean cascadeRemove) {
		throw new UnsupportedOperationException();
	}

	private void setCascadeRemove_(boolean cascadeRemove) {
		boolean old = this.cascadeRemove;
		this.cascadeRemove = cascadeRemove;
		this.firePropertyChanged(CASCADE_REMOVE_PROPERTY, old, cascadeRemove);
	}

	// ***** cascade detach - JPA 2.0
	public boolean isCascadeDetach() {
		return this.cascadeDetach;
	}

	public void setCascadeDetach(boolean cascadeDetach) {
		throw new UnsupportedOperationException();
	}

	private void setCascadeDetach_(boolean cascadeDetach) {
		boolean old = this.cascadeDetach;
		this.cascadeDetach = cascadeDetach;
		this.firePropertyChanged(CASCADE_DETACH_PROPERTY, old, cascadeDetach);
	}

}

Back to the top