Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4ff0a0f1303fccb4cb7281808b207f8f74f79e48 (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
/*******************************************************************************
 * Copyright (c) 2006, 2008 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.context.java;

import java.util.List;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jpt.core.MappingKeys;
import org.eclipse.jpt.core.context.TypeMapping;
import org.eclipse.jpt.core.context.java.JavaAttributeMapping;
import org.eclipse.jpt.core.context.java.JavaPersistentAttribute;
import org.eclipse.jpt.core.internal.validation.DefaultJpaValidationMessages;
import org.eclipse.jpt.core.internal.validation.JpaValidationMessages;
import org.eclipse.jpt.core.resource.java.JavaResourceNode;
import org.eclipse.jpt.core.resource.java.JavaResourcePersistentAttribute;
import org.eclipse.jpt.core.utility.TextRange;
import org.eclipse.jpt.db.Table;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;


public abstract class AbstractJavaAttributeMapping<T extends JavaResourceNode> extends AbstractJavaJpaContextNode
	implements JavaAttributeMapping
{
	protected JavaResourcePersistentAttribute resourcePersistentAttribute;
	

	protected AbstractJavaAttributeMapping(JavaPersistentAttribute parent) {
		super(parent);
	}
	
	@SuppressWarnings("unchecked")
	protected T getMappingResource() {
		if (isDefault()) {
			return (T) this.resourcePersistentAttribute.getNullMappingAnnotation(getAnnotationName());
		}
		return (T) this.resourcePersistentAttribute.getMappingAnnotation(getAnnotationName());
	}
	
	public GenericJavaPersistentAttribute getPersistentAttribute() {
		return (GenericJavaPersistentAttribute) this.getParent();
	}

	protected JavaResourcePersistentAttribute getResourcePersistentAttribute() {
		return this.resourcePersistentAttribute;
	}
	
	/**
	 * the persistent attribute can tell whether there is a "specified" mapping
	 * or a "default" one
	 */
	public boolean isDefault() {
		return this.getPersistentAttribute().mappingIsDefault(this);
	}

	protected boolean embeddableOwned() {
		return this.getTypeMapping().getKey() == MappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY;
	}
	
	protected boolean entityOwned() {
		return this.getTypeMapping().getKey() == MappingKeys.ENTITY_TYPE_MAPPING_KEY;
	}
	
	public TypeMapping getTypeMapping() {
		return this.getPersistentAttribute().getTypeMapping();
	}

	public String getAttributeName() {
		return this.getPersistentAttribute().getName();
	}
	
	public Table getDbTable(String tableName) {
		return getTypeMapping().getDbTable(tableName);
	}
	
	public TextRange getValidationTextRange(CompilationUnit astRoot) {
		TextRange textRange = null;
		T mappingResource = getMappingResource();
		if (mappingResource != null) {
			textRange = mappingResource.getTextRange(astRoot);
		}
		return (textRange != null) ? textRange : this.getPersistentAttribute().getValidationTextRange(astRoot);
	}
	
	public String getPrimaryKeyColumnName() {
		return null;
	}

	public boolean isOverridableAttributeMapping() {
		return false;
	}

	public boolean isOverridableAssociationMapping() {
		return false;
	}

	public boolean isIdMapping() {
		return false;
	}

	public void initialize(JavaResourcePersistentAttribute resourcePersistentAttribute) {
		this.resourcePersistentAttribute = resourcePersistentAttribute;
		initialize(getMappingResource());
	}

	protected void initialize(T mappingResource) {
		
	}

	public void update(JavaResourcePersistentAttribute resourcePersistentAttribute) {
		this.resourcePersistentAttribute = resourcePersistentAttribute;
		this.update(getMappingResource());
	}
	
	protected void update(T mappingResource) {
		
	}

	
	//************ Validation *************************
	
	@Override
	public void addToMessages(List<IMessage> messages, CompilationUnit astRoot) {
		super.addToMessages(messages, astRoot);
		
		addModifierMessages(messages, astRoot);
		addInvalidMappingMessage(messages, astRoot);
	}
	
	protected void addModifierMessages(List<IMessage> messages, CompilationUnit astRoot) {
		GenericJavaPersistentAttribute attribute = this.getPersistentAttribute();
		if (attribute.getMappingKey() == MappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY) {
			return;
		}
		
		if ( this.resourcePersistentAttribute.isForField()) {
			if (this.resourcePersistentAttribute.isFinal()) {
				messages.add(
					DefaultJpaValidationMessages.buildMessage(
						IMessage.HIGH_SEVERITY,
						JpaValidationMessages.PERSISTENT_ATTRIBUTE_FINAL_FIELD,
						new String[] {attribute.getName()},
						attribute, attribute.getValidationTextRange(astRoot))
				);
			}
			
			if (this.resourcePersistentAttribute.isPublic()) {
				messages.add(
					DefaultJpaValidationMessages.buildMessage(
						IMessage.HIGH_SEVERITY,
						JpaValidationMessages.PERSISTENT_ATTRIBUTE_PUBLIC_FIELD,
						new String[] {attribute.getName()},
						attribute, attribute.getValidationTextRange(astRoot))
				);
			}
		}
	}
	
	protected void addInvalidMappingMessage(List<IMessage> messages, CompilationUnit astRoot) {
		if (! getTypeMapping().attributeMappingKeyAllowed(this.getKey())) {
			messages.add(
				DefaultJpaValidationMessages.buildMessage(
					IMessage.HIGH_SEVERITY,
					JpaValidationMessages.PERSISTENT_ATTRIBUTE_INVALID_MAPPING,
					new String[] {this.getPersistentAttribute().getName()},
					this, this.getValidationTextRange(astRoot))
			);
		}
	}
	
}

Back to the top