Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2db8014c835fd308c2827ff52eda50f8c92bcf33 (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
/*******************************************************************************
 * Copyright (c) 2007, 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.jpa1.context.java;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jpt.common.utility.internal.CollectionTools;
import org.eclipse.jpt.common.utility.internal.iterators.EmptyIterator;
import org.eclipse.jpt.common.utility.internal.iterators.FilteringIterator;
import org.eclipse.jpt.jpa.core.MappingKeys;
import org.eclipse.jpt.jpa.core.context.TypeMapping;
import org.eclipse.jpt.jpa.core.context.java.JavaAttributeOverrideContainer;
import org.eclipse.jpt.jpa.core.context.java.JavaEmbeddedIdMapping;
import org.eclipse.jpt.jpa.core.context.java.JavaPersistentAttribute;
import org.eclipse.jpt.jpa.core.internal.context.TypeMappingTools;
import org.eclipse.jpt.jpa.core.internal.context.java.AbstractJavaBaseEmbeddedMapping;
import org.eclipse.jpt.jpa.core.internal.validation.DefaultJpaValidationMessages;
import org.eclipse.jpt.jpa.core.internal.validation.JpaValidationMessages;
import org.eclipse.jpt.jpa.core.jpa2.context.EmbeddedIdMapping2_0;
import org.eclipse.jpt.jpa.core.resource.java.EmbeddedIdAnnotation;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
import org.eclipse.wst.validation.internal.provisional.core.IReporter;

/**
 * Java embedded ID mapping
 */
public class GenericJavaEmbeddedIdMapping
	extends AbstractJavaBaseEmbeddedMapping<EmbeddedIdAnnotation>
	implements EmbeddedIdMapping2_0, JavaEmbeddedIdMapping
{
	/* 2.0 feature - a relationship may map this embedded id */
	protected boolean mappedByRelationship;


	public GenericJavaEmbeddedIdMapping(JavaPersistentAttribute parent) {
		super(parent);
	}


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

	@Override
	public void update() {
		super.update();
		this.setMappedByRelationship(this.buildMappedByRelationship());
	}


	// ********** mapped by relationship **********

	public boolean isMappedByRelationship() {
		return this.mappedByRelationship;
	}

	protected void setMappedByRelationship(boolean value) {
		boolean old = this.mappedByRelationship;
		this.mappedByRelationship = value;
		this.firePropertyChanged(MAPPED_BY_RELATIONSHIP_PROPERTY, old, value);
	}

	protected boolean buildMappedByRelationship() {
		return this.isJpa2_0Compatible() && this.buildMappedByRelationship_();
	}

	protected boolean buildMappedByRelationship_() {
		return CollectionTools.contains(this.getMappedByRelationshipAttributeNames(), this.getName());
	}

	protected Iterable<String> getMappedByRelationshipAttributeNames() {
		return TypeMappingTools.getMappedByRelationshipAttributeNames(this.getTypeMapping());
	}


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

	public String getKey() {
		return MappingKeys.EMBEDDED_ID_ATTRIBUTE_MAPPING_KEY;
	}

	@Override
	protected String getAnnotationName() {
		return EmbeddedIdAnnotation.ANNOTATION_NAME;
	}

	@Override
	protected Iterator<String> embeddableOverridableAttributeMappingNames() {
		return (this.mappedByRelationship) ?
			EmptyIterator.<String>instance() :
			super.embeddableOverridableAttributeMappingNames();
	}

	@Override
	protected JavaAttributeOverrideContainer.Owner buildAttributeOverrideContainerOwner() {
		return new AttributeOverrideContainerOwner();
	}


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

	@Override
	public void validate(List<IMessage> messages, IReporter reporter, CompilationUnit astRoot) {
		super.validate(messages, reporter, astRoot);

		// [JPA 2.0] if the embedded id is mapped by a relationship, then any specified
		// attribute overrides are in error
		// (in JPA 1.0, this will obviously never be reached)
		if (this.mappedByRelationship
				&& (this.attributeOverrideContainer.specifiedOverridesSize() > 0)) {
			messages.add(
				DefaultJpaValidationMessages.buildMessage(
					IMessage.HIGH_SEVERITY,
					JpaValidationMessages.EMBEDDED_ID_MAPPING_MAPPED_BY_RELATIONSHIP_AND_ATTRIBUTE_OVERRIDES_SPECIFIED,
					EMPTY_STRING_ARRAY,
					this.attributeOverrideContainer,
					this.attributeOverrideContainer.getValidationTextRange(astRoot)
				)
			);
		}
	}

	// ********** attribute override container owner *********

	protected class AttributeOverrideContainerOwner
		extends AbstractJavaBaseEmbeddedMapping<EmbeddedIdAnnotation>.AttributeOverrideContainerOwner
	{
		@Override
		public Iterator<String> allOverridableNames() {
			return GenericJavaEmbeddedIdMapping.this.isMappedByRelationship() ?
					EmptyIterator.<String>instance() :
					super.allOverridableNames();
		}

		@Override
		protected Iterator<String> allOverridableAttributeNames_(TypeMapping typeMapping) {
			final Set<String> mappedByRelationshipAttributeNames = this.buildMappedByRelationshipAttributeNames();
			if (mappedByRelationshipAttributeNames.isEmpty()) {
				return super.allOverridableAttributeNames_(typeMapping);
			}
			return new FilteringIterator<String>(super.allOverridableAttributeNames_(typeMapping)) {
				@Override
				protected boolean accept(String attributeName) {
					// overridable names are (usually?) qualified with a container mapping,
					// which may also be the one mapped by a relationship
					int dotIndex = attributeName.indexOf('.');
					String qualifier = (dotIndex > 0) ? attributeName.substring(0, dotIndex) : attributeName;
					return ! mappedByRelationshipAttributeNames.contains(qualifier);
				}
			};
		}

		protected Set<String> buildMappedByRelationshipAttributeNames() {
			return CollectionTools.set(GenericJavaEmbeddedIdMapping.this.getMappedByRelationshipAttributeNames());
		}
	}
}

Back to the top