Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8d27c6252e072601c5310964cd4e7b90bd7c65e6 (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
/*******************************************************************************
 * Copyright (c) 2011, 2013 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;

import java.util.List;

import org.eclipse.jpt.jpa.core.context.AttributeMapping;
import org.eclipse.jpt.jpa.core.context.Entity;
import org.eclipse.jpt.common.core.internal.utility.ValidationMessageTools;
import org.eclipse.jpt.jpa.core.validation.JptJpaCoreValidationMessages;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
import org.eclipse.wst.validation.internal.provisional.core.IReporter;

public abstract class AbstractEntityPrimaryKeyValidator extends
		AbstractPrimaryKeyValidator {

	protected AbstractEntityPrimaryKeyValidator(Entity entity) {
		super(entity);
	}

	protected Entity entity() {
		return (Entity) this.typeMapping();
	}
	
	public boolean validate(List<IMessage> messages, IReporter reporter) {
		// if an entity is non-root, it is not allowed to define primary keys
		if (! entity().isRootEntity()) {
			validatePrimaryKeyForNonRoot(messages, reporter);
		}
		else {
			validatePrimaryKeyForRoot(messages, reporter);
		}
		return true;
	}
	
	protected void validatePrimaryKeyForNonRoot(List<IMessage> messages, IReporter reporter) {
		validateNonRootEntityDoesNotSpecifyIdClass(messages, reporter);
		validateNonRootEntityDoesNotSpecifyPrimaryKeyAttributes(messages, reporter);
	}
	
	protected void validatePrimaryKeyForRoot(List<IMessage> messages, IReporter reporter) {
		validatePrimaryKeyIsNotRedefined(messages, reporter);
		validateIdClassIsUsedIfNecessary(messages, reporter);
		
		// if the primary key is not defined on an ancestor, it must be defined here
		if (! definesPrimaryKey(typeMapping())) {
			messages.add(
					ValidationMessageTools.buildValidationMessage(
						entity().getResource(),
						entity().getValidationTextRange(),
						JptJpaCoreValidationMessages.ENTITY_NO_PK
					)
				);
		}
		
		// if primary key is composite, it may either use an id class or embedded id, not both
		validateOneOfIdClassOrEmbeddedIdIsUsed(messages, reporter);
		// ... and only one embedded id
		validateOneEmbeddedId(messages, reporter);
		// ... and not both id and embedded id
		validateOneOfEmbeddedOrIdIsUsed(messages, reporter);
		
		validateMapsIdMappings(messages, reporter);
		
		if (specifiesIdClass()) {
			validateIdClass(idClassReference().getIdClass(), messages, reporter);
		}
	}
	
	protected void validateNonRootEntityDoesNotSpecifyIdClass(List<IMessage> messages, IReporter reporter) {
		if (idClassReference().isSpecified()) {
			messages.add(
					ValidationMessageTools.buildValidationMessage(
						entity().getResource(),
						idClassReference().getValidationTextRange(),
						JptJpaCoreValidationMessages.ENTITY_NON_ROOT_ID_CLASS_SPECIFIED
					)
				);
		}
	}
	
	protected void validateNonRootEntityDoesNotSpecifyPrimaryKeyAttributes(List<IMessage> messages, IReporter reporter) {
		for (AttributeMapping each : getPrimaryKeyMappingsDefinedLocally(typeMapping())) {
			messages.add(
					ValidationMessageTools.buildValidationMessage(
						each.getResource(),
						getAttributeMappingTextRange(each),
						JptJpaCoreValidationMessages.ENTITY_NON_ROOT_ID_ATTRIBUTE_SPECIFIED
					)
				);
		}
	}
}

Back to the top