Skip to main content
summaryrefslogtreecommitdiffstats
blob: 31914682523fa9158d4a355f1f9b308eff3c9eab (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*******************************************************************************
 * Copyright (c) 2009, 2015 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.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jpt.common.core.JptResourceType;
import org.eclipse.jpt.common.core.internal.utility.ContentTypeTools;
import org.eclipse.jpt.common.core.resource.java.JavaResourceAbstractType;
import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement;
import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement.AstNodeType;
import org.eclipse.jpt.common.core.resource.java.JavaResourcePackageFragmentRoot;
import org.eclipse.jpt.common.core.resource.java.JavaResourceType;
import org.eclipse.jpt.common.core.utility.TextRange;
import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
import org.eclipse.jpt.common.utility.internal.predicate.PredicateAdapter;
import org.eclipse.jpt.common.utility.predicate.Predicate;
import org.eclipse.jpt.jpa.core.context.AccessType;
import org.eclipse.jpt.jpa.core.context.PersistentType;
import org.eclipse.jpt.jpa.core.context.java.JarFile;
import org.eclipse.jpt.jpa.core.context.java.JavaManagedType;
import org.eclipse.jpt.jpa.core.context.java.JavaManagedTypeDefinition;
import org.eclipse.jpt.jpa.core.context.java.JavaPersistentType;
import org.eclipse.jpt.jpa.core.context.java.JavaSpecifiedPersistentAttribute;
import org.eclipse.jpt.jpa.core.context.persistence.JarFileRef;
import org.eclipse.jpt.jpa.core.internal.context.java.JavaPersistentTypeDefinition;
import org.eclipse.jpt.jpa.core.internal.context.persistence.AbstractPersistenceXmlContextModel;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
import org.eclipse.wst.validation.internal.provisional.core.IReporter;

/**
 * Context JAR file
 */
public class GenericJarFile
	extends AbstractPersistenceXmlContextModel<JarFileRef>
	implements JarFile, JavaPersistentType.Parent
{
	protected final JavaResourcePackageFragmentRoot jarResourcePackageFragmentRoot;

	protected final ContextCollectionContainer<JavaManagedType, JavaResourceType> javaManagedTypeContainer;


	// ********** constructor/initialization **********

	public GenericJarFile(JarFileRef parent, JavaResourcePackageFragmentRoot jarResourcePackageFragmentRoot) {
		super(parent);
		this.jarResourcePackageFragmentRoot = jarResourcePackageFragmentRoot;
		this.javaManagedTypeContainer = this.buildJavaManagedTypeContainer();
	}


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

	@Override
	public void synchronizeWithResourceModel(IProgressMonitor monitor) {
		super.synchronizeWithResourceModel(monitor);
		this.syncJavaManagedTypes(monitor);
	}

	@Override
	public void update(IProgressMonitor monitor) {
		super.update(monitor);
		this.updateModels(this.getJavaManagedTypes(), monitor);
	}

	public JavaResourcePackageFragmentRoot getJarResourcePackageFragmentRoot() {
		return this.jarResourcePackageFragmentRoot;
	}


	// ********** JpaContextNode implementation **********

	@Override
	public JptResourceType getResourceType() {
		return ContentTypeTools.getResourceType(JavaResourcePackageFragmentRoot.JAR_CONTENT_TYPE);
	}


	// ********** Java managed types **********

	public JavaManagedType getManagedType(String typeName) {
		for (JavaManagedType mt : this.getJavaManagedTypes()) {
			if (mt.getName().equals(typeName)) {
				return mt;
			}
		}
		return null;
	}

	public Iterable<JavaManagedType> getJavaManagedTypes() {
		return this.javaManagedTypeContainer;
	}

	public int getJavaManagedTypesSize() {
		return this.javaManagedTypeContainer.size();
	}

	protected void syncJavaManagedTypes(IProgressMonitor monitor) {
		this.javaManagedTypeContainer.synchronizeWithResourceModel(monitor);
	}

	protected void addJavaManagedType(JavaResourceType jrt) {
		this.javaManagedTypeContainer.addContextElement(getJavaManagedTypesSize(), jrt);
	}

	protected void removeJavaManagedType(JavaManagedType javaManagedType) {
		this.javaManagedTypeContainer.remove(javaManagedType);
	}

	//only accept types, enums aren't valid for JPA
	protected Iterable<JavaResourceType> getJavaResourceTypes() {
		return IterableTools.downCast(
				IterableTools.filter(
					this.getJavaResourceAbstractTypes(),
					new JavaResourceAnnotatedElement.AstNodeTypeEquals(AstNodeType.TYPE)
				)
			);
	}

	/**
	 * the resource JAR holds only annotated types, so we can use them all for
	 * building the context types
	 */
	protected Iterable<JavaResourceAbstractType> getJavaResourceAbstractTypes() {
		return this.jarResourcePackageFragmentRoot.getTypes();
	}

	protected JavaManagedType buildJavaManagedType(JavaResourceType jrt, JavaManagedTypeDefinition managedTypeDefinition) {
		return managedTypeDefinition.buildContextManagedType(this, jrt, this.getJpaFactory());
	}

	protected Iterable<JavaManagedTypeDefinition> getJavaManagedTypeDefinitions() {
		return this.getJpaPlatform().getJavaManagedTypeDefinitions();
	}

	protected JavaManagedTypeDefinition getJavaManagedTypeDefinition(JavaResourceType jrt) {
		for (JavaManagedTypeDefinition managedTypeDefinition : this.getJavaManagedTypeDefinitions()) {
			if (jrt.isAnnotatedWithAnyOf(managedTypeDefinition.getAnnotationNames(this.getJpaProject()))) {
				return managedTypeDefinition;
			}
		}
		return JavaPersistentTypeDefinition.instance();
	}

	protected JavaManagedType buildJavaManagedType(JavaResourceType jrt) {
		return getJavaManagedTypeDefinition(jrt).buildContextManagedType(this, jrt, getJpaFactory());
	}

	protected ContextCollectionContainer<JavaManagedType, JavaResourceType> buildJavaManagedTypeContainer() {
		return this.buildSpecifiedContextCollectionContainer(JAVA_MANAGED_TYPES_COLLECTION, new JavaManagedTypeContainerAdapter());
	}

	/**
	 * Java managed type container adapter
	 */
	public class JavaManagedTypeContainerAdapter
		extends AbstractContainerAdapter<JavaManagedType, JavaResourceType>
	{
		public JavaManagedType buildContextElement(JavaResourceType resourceElement) {
			return GenericJarFile.this.buildJavaManagedType(resourceElement);
		}
		public Iterable<JavaResourceType> getResourceElements() {
			return GenericJarFile.this.getJavaResourceTypes();
		}
		public JavaResourceType extractResourceElement(JavaManagedType contextElement) {
			return contextElement.getJavaResourceType();
		}
	}


	// ********** ManagedTypeContainer implementation **********

	public Iterable<JavaManagedType> getManagedTypes() {
		return this.getJavaManagedTypes();
	}


	// ********** PersistentTypeContainer implementation **********

	public JavaPersistentType getPersistentType(String typeName) {
		JavaManagedType managedType = this.getManagedType(typeName);
		return (managedType.getManagedTypeType() == PersistentType.class) ? (JavaPersistentType) managedType : null;
	}

	public Iterable<JavaPersistentType> getPersistentTypes() {
		return IterableTools.downCast(IterableTools.filter(this.getManagedTypes(), TYPE_IS_PERSISTENT_TYPE));
	}

	protected static final Predicate<JavaManagedType> TYPE_IS_PERSISTENT_TYPE = new TypeIsPersistentType();

	public static class TypeIsPersistentType
		extends PredicateAdapter<JavaManagedType>
	{
		@Override
		public boolean evaluate(JavaManagedType mt) {
			return mt.getManagedTypeType() == PersistentType.class;
		}
	}


	// ********** JavaPersistentType.Parent implementation **********

	public AccessType getDefaultPersistentTypeAccess() {
		return this.getPersistenceUnit().getDefaultAccess();
	}

	public AccessType getOverridePersistentTypeAccess() {
		// no access type at this level overrides any local access type specification
		return null;
	}

	public void attributeChanged(JavaSpecifiedPersistentAttribute attribute) {
		// NOP
	}


	// ********** JpaNode implementation **********

	protected JarFileRef getJarFileRef() {
		return this.parent;
	}

	@Override
	public IResource getResource() {
		return this.jarResourcePackageFragmentRoot.getFile();
	}


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

	@Override
	public void validate(List<IMessage> messages, IReporter reporter) {
		super.validate(messages, reporter);
		// TODO validate 'javaManagedTypes'
	}

	public boolean isIn(org.eclipse.core.resources.IFolder folder) {
		IResource member = folder.findMember(this.jarResourcePackageFragmentRoot.getFile().getName());
		IFile file = this.jarResourcePackageFragmentRoot.getFile();
		return member != null && file != null && member.equals(file);		
	}

	public TextRange getValidationTextRange() {
		return this.getJarFileRef().getValidationTextRange();
	}
}

Back to the top