Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1c96d9cdb20e59dc4456eb3e5154728e7e5aa854 (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
/*******************************************************************************
 * Copyright (c) 2007 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.platform;

import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jpt.core.internal.IAttributeMapping;
import org.eclipse.jpt.core.internal.IMappingKeys;
import org.eclipse.jpt.core.internal.ITypeMapping;
import org.eclipse.jpt.core.internal.content.java.JavaPersistentAttribute;
import org.eclipse.jpt.core.internal.content.java.JavaPersistentType;
import org.eclipse.jpt.core.internal.content.orm.XmlAttributeMapping;
import org.eclipse.jpt.core.internal.content.orm.XmlPersistentAttribute;
import org.eclipse.jpt.core.internal.validation.IJpaValidationMessages;
import org.eclipse.jpt.core.internal.validation.JpaValidationMessages;
import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;

public abstract class XmlAttributeContext extends BaseContext
{
	private XmlAttributeMapping xmlAttributeMapping;
	
	private JavaAttributeContext javaAttributeContext;
	
	protected XmlAttributeContext(IContext parentContext, XmlAttributeMapping xmlAttributeMapping) {
		super(parentContext);
		this.xmlAttributeMapping = xmlAttributeMapping;
	}
	
	@Override
	protected void initialize() {
		
	}
	
	public XmlPersistentAttribute getAttribute() {
		return xmlAttributeMapping.getPersistentAttribute();
	}

	public void refreshDefaults(DefaultsContext defaultsContext, IProgressMonitor monitor) {
		JavaPersistentType javaPersistentType = this.xmlAttributeMapping.getPersistentType().findJavaPersistentType();
		String name = this.xmlAttributeMapping.getPersistentAttribute().getName();
		if (name != null && javaPersistentType != null) {
			JavaPersistentAttribute javaPersistentAttribute = javaPersistentType.attributeNamed(name);
			if (javaPersistentAttribute != null) {
				this.javaAttributeContext = 
					(JavaAttributeContext) getPlatform().buildJavaAttributeContext(this, javaPersistentAttribute.getMapping());
			}
		}
		this.xmlAttributeMapping.refreshDefaults(defaultsContext);
	}
	
	protected XmlAttributeMapping attributeMapping() {
		return this.xmlAttributeMapping;
	}
	
	protected IAttributeMapping javaAttributeMapping() {
		if (this.javaAttributeContext != null) {
			return this.javaAttributeContext.getMapping();
		}
		return null;
	}
	
	protected boolean embeddableOwned() {
		return attributeMapping().typeMapping().getKey() == IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY;
	}
	
	protected boolean entityOwned() {
		return attributeMapping().typeMapping().getKey() == IMappingKeys.ENTITY_TYPE_MAPPING_KEY;
	}
	
	public final DefaultsContext wrapDefaultsContext(DefaultsContext defaultsContext) {
		return new DefaultsContextWrapper(defaultsContext) {
			public Object getDefault(String key) {
				return XmlAttributeContext.this.getDefault(key, getWrappedDefaultsContext());
			}
		};
	}
	
	protected Object getDefault(String key, DefaultsContext defaultsContext) {
		return defaultsContext.getDefault(key);
	}
	
	@Override
	public void addToMessages(List<IMessage> messages) {
		super.addToMessages(messages);
		addAttributeMessages(messages);
		addInvalidMappingMessage(messages);
	}
	
	protected void addAttributeMessages(List<IMessage> messages) {
		addUnspecifiedAttributeMessage(messages);
		addUnresolvedAttributeMessage(messages);
		addModifierMessages(messages);
	}
	
	protected void addUnspecifiedAttributeMessage(List<IMessage> messages) {
		XmlPersistentAttribute persistentAttribute = xmlAttributeMapping.getPersistentAttribute();
		if (StringTools.stringIsEmpty(persistentAttribute.getName())) {
			messages.add(
				JpaValidationMessages.buildMessage(
					IMessage.HIGH_SEVERITY,
					IJpaValidationMessages.PERSISTENT_ATTRIBUTE_UNSPECIFIED_NAME,
					persistentAttribute, persistentAttribute.nameTextRange())
			);
		}
	}
	
	protected void addUnresolvedAttributeMessage(List<IMessage> messages) {
		XmlPersistentAttribute persistentAttribute = xmlAttributeMapping.getPersistentAttribute();
		if (! StringTools.stringIsEmpty(persistentAttribute.getName())
				&& persistentAttribute.persistentType().findJdtType() != null
				&& persistentAttribute.getAttribute() == null) {
			messages.add(
				JpaValidationMessages.buildMessage(
					IMessage.HIGH_SEVERITY,
					IJpaValidationMessages.PERSISTENT_ATTRIBUTE_UNRESOLVED_NAME,
					new String[] {persistentAttribute.getName(), persistentAttribute.persistentType().getClass_()},
					persistentAttribute, persistentAttribute.nameTextRange())
			);
		}
	}
	
	protected void addModifierMessages(List<IMessage> messages) {
		XmlPersistentAttribute attribute = xmlAttributeMapping.getPersistentAttribute();
		
		if (attribute.getMapping().getKey() != IMappingKeys.TRANSIENT_ATTRIBUTE_MAPPING_KEY
				&& attribute.getAttribute() != null 
				&& attribute.getAttribute().isField()) {
			int flags;
			try {
				flags = attribute.getAttribute().getJdtMember().getFlags();
			} catch (JavaModelException jme) { 
				/* no error to log, in that case */ 
				return;
			}
			
			if (Flags.isFinal(flags)) {
				messages.add(
					JpaValidationMessages.buildMessage(
						IMessage.HIGH_SEVERITY,
						IJpaValidationMessages.PERSISTENT_ATTRIBUTE_FINAL_FIELD,
						new String[] {attribute.getName()},
						attribute, attribute.validationTextRange())
				);
			}
			
			if (Flags.isPublic(flags)) {
				messages.add(
					JpaValidationMessages.buildMessage(
						IMessage.HIGH_SEVERITY,
						IJpaValidationMessages.PERSISTENT_ATTRIBUTE_PUBLIC_FIELD,
						new String[] {attribute.getName()},
						attribute, attribute.validationTextRange())
				);
				
			}
		}
	}
	
	protected void addInvalidMappingMessage(List<IMessage> messages) {
		IAttributeMapping attributeMapping = attributeMapping();
		ITypeMapping typeMapping = attributeMapping.typeMapping();
		if (! typeMapping.attributeMappingKeyAllowed(attributeMapping.getKey())) {
			messages.add(
				JpaValidationMessages.buildMessage(
					IMessage.HIGH_SEVERITY,
					IJpaValidationMessages.PERSISTENT_ATTRIBUTE_INVALID_MAPPING,
					new String[] {attributeMapping.getPersistentAttribute().getName()},
					attributeMapping, attributeMapping.validationTextRange())
			);
		}
	}
}

Back to the top