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

import org.eclipse.jpt.common.utility.internal.CollectionTools;
import org.eclipse.jpt.common.utility.internal.Transformer;
import org.eclipse.jpt.common.utility.internal.iterables.CompositeIterable;
import org.eclipse.jpt.common.utility.internal.iterables.FilteringIterable;
import org.eclipse.jpt.common.utility.internal.iterables.SubIterableWrapper;
import org.eclipse.jpt.common.utility.internal.iterables.TransformationIterable;
import org.eclipse.jpt.jpa.core.MappingKeys;
import org.eclipse.jpt.jpa.core.context.AttributeMapping;
import org.eclipse.jpt.jpa.core.context.ReadOnlyTable;
import org.eclipse.jpt.jpa.core.context.TypeMapping;
import org.eclipse.jpt.jpa.core.jpa2.context.DerivedIdentity2_0;
import org.eclipse.jpt.jpa.core.jpa2.context.MapsIdDerivedIdentityStrategy2_0;
import org.eclipse.jpt.jpa.core.jpa2.context.SingleRelationshipMapping2_0;

/**
 * Gather some of the behavior common to the Java and XML models. :-(
 */
public class TypeMappingTools {

	// ********** single relationship mappings **********

	/**
	 * Return whether the specified attribute is a derived ID for the specified
	 * type mapping.
	 */
	public static boolean attributeIsDerivedId(TypeMapping typeMapping, String attributeName) {
		if (attributeName == null) {
			return false;
		}
		// the attribute name may be qualified - we test only the first attribute name
		int dotIndex = attributeName.indexOf('.');
		attributeName = (dotIndex == -1) ? attributeName : attributeName.substring(0, dotIndex);
		return CollectionTools.contains(getMapsIdDerivedIdAttributeNames(typeMapping), attributeName);
	}

	/**
	 * Return the names of all the ID attributes derived from
	 * single-relationship mappings for the specified type mapping
	 * (i.e. the names of the ID attributes that are specified by a single-
	 * relationship mapping's "maps ID" setting).
	 */
	protected static Iterable<String> getMapsIdDerivedIdAttributeNames(TypeMapping typeMapping) {
		return new TransformationIterable<MapsIdDerivedIdentityStrategy2_0, String>(getMapsIdDerivedIdentityStrategies(typeMapping)) {
				@Override
				protected String transform(MapsIdDerivedIdentityStrategy2_0 strategy) {
					return strategy.getIdAttributeName();
				}
			};
	}

	/**
	 * Return all the single-relationship "maps ID" derived identity strategies
	 * for the specified type mapping.
	 */
	protected static Iterable<MapsIdDerivedIdentityStrategy2_0> getMapsIdDerivedIdentityStrategies(TypeMapping typeMapping) {
		return new TransformationIterable<DerivedIdentity2_0, MapsIdDerivedIdentityStrategy2_0>(getMapsIdDerivedIdentities(typeMapping)) {
			@Override
			protected MapsIdDerivedIdentityStrategy2_0 transform(DerivedIdentity2_0 derivedIdentity) {
				return derivedIdentity.getMapsIdDerivedIdentityStrategy();
			}
		};
	}

	/**
	 * Return all the single-relationship "maps ID" derived identities for the
	 * specified type mapping.
	 */
	protected static Iterable<DerivedIdentity2_0> getMapsIdDerivedIdentities(TypeMapping typeMapping) {
		return new FilteringIterable<DerivedIdentity2_0>(getDerivedIdentities(typeMapping)) {
				@Override
				protected boolean accept(DerivedIdentity2_0 derivedIdentity) {
					return derivedIdentity.usesMapsIdDerivedIdentityStrategy();
				}
			};
	}

	/**
	 * Return all the single-relationship derived identities for the specified
	 * type mapping.
	 */
	protected static Iterable<DerivedIdentity2_0> getDerivedIdentities(TypeMapping typeMapping) {
		return new TransformationIterable<SingleRelationshipMapping2_0, DerivedIdentity2_0>(getSingleRelationshipMappings(typeMapping)) {
			@Override
			protected DerivedIdentity2_0 transform(SingleRelationshipMapping2_0 mapping) {
				return mapping.getDerivedIdentity();
			}
		};
	}

	/**
	 * Return all the single-relationship attribute mappings for the specified
	 * type mapping.
	 */
	protected static Iterable<SingleRelationshipMapping2_0> getSingleRelationshipMappings(TypeMapping typeMapping) {
		return new SubIterableWrapper<AttributeMapping, SingleRelationshipMapping2_0>(getSingleRelationshipMappings_(typeMapping));
	}

	@SuppressWarnings("unchecked")
	protected static Iterable<AttributeMapping> getSingleRelationshipMappings_(TypeMapping typeMapping) {
		return new CompositeIterable<AttributeMapping>(
					typeMapping.getAllAttributeMappings(MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY),
					typeMapping.getAllAttributeMappings(MappingKeys.MANY_TO_ONE_ATTRIBUTE_MAPPING_KEY)
				);
	}


	// ********** attribute mappings transformer **********

	public static final Transformer<TypeMapping, Iterable<AttributeMapping>> ATTRIBUTE_MAPPINGS_TRANSFORMER = new AttributeMappingsTransformer();
	static class AttributeMappingsTransformer
		implements Transformer<TypeMapping, Iterable<AttributeMapping>>
	{
		public Iterable<AttributeMapping> transform(TypeMapping mapping) {
			return mapping.getAttributeMappings();
		}
	}


	// ********** associated tables transformer **********

	public static final Transformer<TypeMapping, Iterable<ReadOnlyTable>> ASSOCIATED_TABLES_TRANSFORMER = new AssociatedTablesTransformer();
	static class AssociatedTablesTransformer
		implements Transformer<TypeMapping, Iterable<ReadOnlyTable>>
	{
		public Iterable<ReadOnlyTable> transform(TypeMapping mapping) {
			return mapping.getAssociatedTables();
		}
	}


	// ********** overridable attribute names transformer **********

	public static final Transformer<TypeMapping, Iterable<String>> OVERRIDABLE_ATTRIBUTE_NAMES_TRANSFORMER = new OverridableAttributeNamesTransformer();
	static class OverridableAttributeNamesTransformer
		implements Transformer<TypeMapping, Iterable<String>>
	{
		public Iterable<String> transform(TypeMapping mapping) {
			return mapping.getOverridableAttributeNames();
		}
	}


	// ********** overridable association names transformer **********

	public static final Transformer<TypeMapping, Iterable<String>> OVERRIDABLE_ASSOCIATION_NAMES_TRANSFORMER = new OverridableAssociationNamesTransformer();
	static class OverridableAssociationNamesTransformer
		implements Transformer<TypeMapping, Iterable<String>>
	{
		public Iterable<String> transform(TypeMapping mapping) {
			return mapping.getOverridableAssociationNames();
		}
	}


	// ********** constructor **********

	/**
	 * Suppress default constructor, ensuring non-instantiability.
	 */
	private TypeMappingTools() {
		super();
		throw new UnsupportedOperationException();
	}
}

Back to the top