Skip to main content
summaryrefslogtreecommitdiffstats
blob: e769866140616de45530a8d402a5d9c8ec7097e8 (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
/*******************************************************************************
 * Copyright (c) 2011 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
 * which accompanies this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Oracle - initial API and implementation
 *
 ******************************************************************************/
package org.eclipse.jpt.jpa.core.jpql.spi;

import java.util.HashMap;
import java.util.Map;
import org.eclipse.jpt.jpa.core.context.AttributeMapping;
import org.eclipse.jpt.jpa.core.context.TypeMapping;
import org.eclipse.persistence.jpa.jpql.spi.IManagedType;
import org.eclipse.persistence.jpa.jpql.spi.IMapping;
import org.eclipse.persistence.jpa.jpql.spi.IMappingBuilder;
import org.eclipse.persistence.jpa.jpql.spi.IType;
import org.eclipse.persistence.jpa.jpql.util.iterator.CloneIterator;
import org.eclipse.persistence.jpa.jpql.util.iterator.IterableIterator;

/**
 * The abstract definition of {@link IManagedType} defined for wrapping the design-time mapped class
 * object.
 *
 * Provisional API: This interface is part of an interim API that is still under development and
 * expected to change significantly before reaching stability. It is available at this early stage
 * to solicit feedback from pioneering adopters on the understanding that any code that uses this
 * API will almost certainly be broken (repeatedly) as the API evolves.
 *
 * @version 3.1
 * @since 3.0
 * @author Pascal Filion
 */
public abstract class JpaManagedType implements IManagedType {

	/**
	 * The design-time model object wrapped by this class.
	 */
	private final TypeMapping managedType;

	/**
	 * The builder that is responsible to create the {@link IMapping} wrapping a persistent attribute
	 * or property.
	 */
	private IMappingBuilder<AttributeMapping> mappingBuilder;

	/**
	 * The cached collection of {@link IMapping mappings} that prevent rebuilding them every time one
	 * is requested.
	 */
	private Map<String, IMapping> mappings;

	/**
	 * The provider of JPA managed types.
	 */
	private final JpaManagedTypeProvider provider;

	/**
	 * The cached type of this managed type.
	 */
	private IType type;

	/**
	 * Creates a new <code>JDeveloperManagedType</code>.
	 *
	 * @param managedType The provider of JPA managed types
	 * @param mappedClass The design-time model object wrapped by this class
	 * @param mappingBuilder The builder that is responsible to create the {@link IMapping} wrapping
	 * a persistent attribute or property
	 */
	protected JpaManagedType(JpaManagedTypeProvider provider,
	                         TypeMapping managedType,
	                         IMappingBuilder<AttributeMapping> mappingBuilder) {

		super();
		this.provider       = provider;
		this.managedType    = managedType;
		this.mappingBuilder = mappingBuilder;
	}

	/**
	 * Creates the external form that needs to wrap the given {@link AttributeMapping}. By default,
	 * the call is delegated to {@link IMappingBuilder}.
	 *
	 * @param mapping The mapping to wrap with a {@link IMapping}
	 * @return A new concrete instance of {@link IMapping}
	 */
	protected IMapping buildMapping(AttributeMapping mapping) {
		return mappingBuilder.buildMapping(this, mapping);
	}

	/**
	 * Creates an external form for each {@link AttributeMapping} and stores them by using their
	 * mapping name.
	 *
	 * @return The mappings mapped by their name
	 */
	protected Map<String, IMapping> buildMappings() {
		Map<String, IMapping> mappings = new HashMap<String, IMapping>();
		for (AttributeMapping mapping  : managedType.getAllAttributeMappings()) {
			mappings.put(mapping.getName(), buildMapping(mapping));
		}
		return mappings;
	}

	/**
	 * {@inheritDoc}
	 */
	public int compareTo(IManagedType managedType) {
		return getType().getName().compareTo(managedType.getType().getName());
	}

	/**
	 * Returns the encapsulated model object.
	 *
	 * @return The managed type wrapped by this external form
	 */
	protected TypeMapping getManagedType() {
		return managedType;
	}

	/**
	 * Returns the builder that is responsible to create the {@link IMapping} wrapping a persistent
	 * attribute or property.
	 *
	 * @return The concrete implementation of {@link IMappingBuilder}
	 */
	protected IMappingBuilder<AttributeMapping> getMappingBuilder() {
		return mappingBuilder;
	}

	/**
	 * {@inheritDoc}
	 */
	public IMapping getMappingNamed(String name) {
		initializeMappings();
		return mappings.get(name);
	}

	/**
	 * {@inheritDoc}
	 */
	public JpaManagedTypeProvider getProvider() {
		return provider;
	}

	/**
	 * {@inheritDoc}
	 */
	public IType getType() {
		if (type == null) {
			type = provider.getTypeRepository().getType(managedType.getPersistentType().getName());
		}
		return type;
	}

	/**
	 * Initializes this managed type.
	 */
	protected void initializeMappings() {
		if (mappings == null) {
			mappings = buildMappings();
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public IterableIterator<IMapping> mappings() {
		initializeMappings();
		return new CloneIterator<IMapping>(mappings.values());
	}
}

Back to the top