Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5b7f308587da573204f9fc695fd782e366c263ec (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
/*******************************************************************************
 *  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.jpa1.context;

import java.util.List;
import org.eclipse.jpt.core.context.BaseJoinColumn;
import org.eclipse.jpt.core.context.PersistentAttribute;
import org.eclipse.jpt.core.internal.context.BaseJoinColumnTextRangeResolver;
import org.eclipse.jpt.core.internal.jpa1.context.BaseColumnTableValidator.TableDescriptionProvider;
import org.eclipse.jpt.core.internal.validation.DefaultJpaValidationMessages;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;

public abstract class BaseJoinColumnValidator extends AbstractNamedColumnValidator
{
	private final BaseJoinColumn.Owner joinColumnOwner;

	protected BaseJoinColumnValidator(
				BaseJoinColumn column,
				BaseJoinColumn.Owner joinColumnOwner,
				BaseJoinColumnTextRangeResolver textRangeResolver,
				TableDescriptionProvider provider) {
		super(column, textRangeResolver, provider);
		this.joinColumnOwner = joinColumnOwner;
	}

	protected BaseJoinColumnValidator(
				PersistentAttribute persistentAttribute,
				BaseJoinColumn column,
				BaseJoinColumn.Owner joinColumnOwner,
				BaseJoinColumnTextRangeResolver textRangeResolver,
				TableDescriptionProvider provider) {
		super(persistentAttribute, column, textRangeResolver, provider);
		this.joinColumnOwner = joinColumnOwner;
	}

	@Override
	public BaseJoinColumn getColumn() {
		return (BaseJoinColumn) super.getColumn();
	}

	@Override
	public BaseJoinColumnTextRangeResolver getTextRangeResolver() {
		return (BaseJoinColumnTextRangeResolver) super.getTextRangeResolver();
	}

	@Override
	//this method will only be called if the table validates correctly
	protected void validateName(List<IMessage> messages) {
		this.validateJoinColumnName(messages);
		this.validateReferencedColumnName(messages);
	}
	
	protected void validateJoinColumnName(List<IMessage> messages) {
		if (this.getColumn().getSpecifiedName() == null && this.joinColumnOwner.joinColumnsSize() > 1) {
			messages.add(this.buildUnspecifiedNameMultipleJoinColumnsMessage());
		}
		else if (this.getColumn().getName() != null){
			super.validateName(messages);
		}
		//If the name is null and there is only one join-column, one of these validation messages will apply
		// 1. target entity does not have a primary key
		// 2. target entity is not specified
		// 3. target entity is not an entity
	}
	
	protected void validateReferencedColumnName(List<IMessage> messages) {
		if (this.getColumn().getSpecifiedReferencedColumnName() == null && this.joinColumnOwner.joinColumnsSize() > 1) {
			messages.add(this.buildUnspecifiedReferencedColumnNameMultipleJoinColumnsMessage());
		}
		else if (this.getColumn().getSpecifiedReferencedColumnName() != null) {
			if (this.getColumn().getReferencedColumnDbTable() != null && ! this.getColumn().isReferencedColumnResolved()) {
				messages.add(this.buildUnresolvedReferencedColumnNameMessage());
			}
		}
		//If the referenced column name is null and there is only one join-column, one of these validation messages will apply
		// 1. target entity does not have a primary key
		// 2. target entity is not specified
		// 3. target entity is not an entity
	}

	protected IMessage buildUnresolvedReferencedColumnNameMessage() {
		if (isPersistentAttributeVirtual()) {
			return this.buildVirtualAttributeUnresolvedReferencedColumnNameMessage();
		}
		return buildUnresolvedReferencedColumnNameMessage(this.getUnresolvedReferencedColumnNameMessage());
	}

	protected abstract String getUnresolvedReferencedColumnNameMessage();

	protected IMessage buildUnresolvedReferencedColumnNameMessage(String message) {
		return DefaultJpaValidationMessages.buildMessage(
			IMessage.HIGH_SEVERITY,
			message,
			new String[] {this.getColumn().getReferencedColumnName(), this.getColumn().getReferencedColumnDbTable().getName()},
			this.getColumn(), 
			this.getTextRangeResolver().getReferencedColumnNameTextRange()
		);
	}

	protected IMessage buildVirtualAttributeUnresolvedReferencedColumnNameMessage() {
		return DefaultJpaValidationMessages.buildMessage(
			IMessage.HIGH_SEVERITY,
			this.getVirtualAttributeUnresolvedReferencedColumnNameMessage(),
			new String[] {getPersistentAttributeName(), this.getColumn().getReferencedColumnName(), this.namedColumn.getDbTable().getName()},
			this.getColumn(), 
			this.getTextRangeResolver().getReferencedColumnNameTextRange()
		);
	}

	protected abstract String getVirtualAttributeUnresolvedReferencedColumnNameMessage();

	protected IMessage buildUnspecifiedNameMultipleJoinColumnsMessage() {
		if (isPersistentAttributeVirtual()) {
			return this.buildVirtualAttributeUnspecifiedNameMultipleJoinColumnsMessage();
		}
		return DefaultJpaValidationMessages.buildMessage(
			IMessage.HIGH_SEVERITY,
			this.getUnspecifiedNameMultipleJoinColumnsMessage(),
			new String[0],
			this.getColumn(), 
			this.getTextRangeResolver().getNameTextRange()
		);
	}
	protected abstract String getUnspecifiedNameMultipleJoinColumnsMessage();

	protected IMessage buildVirtualAttributeUnspecifiedNameMultipleJoinColumnsMessage() {
		return DefaultJpaValidationMessages.buildMessage(
			IMessage.HIGH_SEVERITY,
			this.getVirtualAttributeUnspecifiedNameMultipleJoinColumnsMessage(),
			new String[] {this.getPersistentAttributeName()},
			this.getColumn(), 
			this.getTextRangeResolver().getNameTextRange()
		);
	}

	protected abstract String getVirtualAttributeUnspecifiedNameMultipleJoinColumnsMessage();

	protected IMessage buildUnspecifiedReferencedColumnNameMultipleJoinColumnsMessage() {
		if (isPersistentAttributeVirtual()) {
			return this.buildVirtualAttributeUnspecifiedReferencedColumnNameMultipleJoinColumnsMessage();
		}
		return DefaultJpaValidationMessages.buildMessage(
			IMessage.HIGH_SEVERITY,
			this.getUnspecifiedReferencedColumnNameMultipleJoinColumnsMessage(),
			new String[0],
			this.getColumn(), 
			this.getTextRangeResolver().getReferencedColumnNameTextRange()
		);
	}

	protected abstract String getUnspecifiedReferencedColumnNameMultipleJoinColumnsMessage();

	protected IMessage buildVirtualAttributeUnspecifiedReferencedColumnNameMultipleJoinColumnsMessage() {
		return DefaultJpaValidationMessages.buildMessage(
			IMessage.HIGH_SEVERITY,
			this.getVirtualAttributeUnspecifiedReferencedColumnNameMultipleJoinColumnsMessage(),
			new String[] {this.getPersistentAttributeName()},
			this.getColumn(), 
			this.getTextRangeResolver().getReferencedColumnNameTextRange()
		);
	}

	protected abstract String getVirtualAttributeUnspecifiedReferencedColumnNameMultipleJoinColumnsMessage();
}

Back to the top