Skip to main content
summaryrefslogtreecommitdiffstats
blob: e3cf8900461df3313882c4108f35f5fec89d47b9 (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
/*******************************************************************************
 * 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;

import java.util.Iterator;

import org.eclipse.jpt.core.MappingKeys;
import org.eclipse.jpt.core.context.ColumnMapping;
import org.eclipse.jpt.core.context.Embeddable;
import org.eclipse.jpt.core.context.Entity;
import org.eclipse.jpt.core.context.JoinColumn;
import org.eclipse.jpt.core.context.PersistentAttribute;
import org.eclipse.jpt.core.context.PersistentType;
import org.eclipse.jpt.core.context.RelationshipMapping;
import org.eclipse.jpt.core.context.java.JavaPersistentAttribute;

public class MappingTools
{
	/**
	 * Default join table name from the JPA spec:
	 * 	The concatenated names of the two associated primary
	 * 	entity tables, separated by a underscore.
	 * 
	 * [owning table name]_[target table name]
	 */
	public static String buildJoinTableDefaultName(RelationshipMapping relationshipMapping) {
		if (!relationshipMapping.isRelationshipOwner()) {
			return null;
		}
		String owningTableName = relationshipMapping.getTypeMapping().getTableName();
		if (owningTableName == null) {
			return null;
		}
		Entity targetEntity = relationshipMapping.getResolvedTargetEntity();
		if (targetEntity == null) {
			return null;
		}
		String targetTableName = targetEntity.getTableName();
		if (targetTableName == null) {
			return null;
		}
		return owningTableName + '_' + targetTableName;
	}

	/**
	 * return the join column's default name;
	 * which is typically <attribute name>_<referenced column name>
	 * but, if we don't have an attribute name (e.g. in a unidirectional
	 * OneToMany or ManyToMany) is
	 * <target entity name>_<referenced column name>
	 */
	// <attribute name>_<referenced column name>
	//     or
	// <target entity name>_<referenced column name>
	public static String buildJoinColumnDefaultName(JoinColumn joinColumn) {
		if (joinColumn.getOwner().joinColumnsSize() != 1) {
			return null;
		}
		String prefix = joinColumn.getOwner().getAttributeName();
		if (prefix == null) {
			prefix = targetEntityName(joinColumn);
		}
		if (prefix == null) {
			return null;
		}
		// TODO not sure which of these is correct...
		// (the spec implies that the referenced column is always the
		// primary key column of the target entity)
		// String targetColumn = this.targetPrimaryKeyColumnName();
		String targetColumn = joinColumn.getReferencedColumnName();
		if (targetColumn == null) {
			return null;
		}
		return prefix + '_' + targetColumn;
	}

	/**
	 * return the name of the target entity
	 */
	protected static String targetEntityName(JoinColumn joinColumn) {
		Entity targetEntity = joinColumn.getOwner().getTargetEntity();
		return (targetEntity == null) ? null : targetEntity.getName();
	}

	public static String buildJoinColumnDefaultReferencedColumnName(JoinColumn joinColumn) {
		if (joinColumn.getOwner().joinColumnsSize() != 1) {
			return null;
		}
		return targetPrimaryKeyColumnName(joinColumn);
	}

	/**
	 * return the name of the single primary key column of the target entity
	 */
	protected static String targetPrimaryKeyColumnName(JoinColumn joinColumn) {
		Entity targetEntity = joinColumn.getOwner().getTargetEntity();
		return (targetEntity == null) ? null : targetEntity.getPrimaryKeyColumnName();
	}
	
	
	public static Embeddable getEmbeddableFor(JavaPersistentAttribute persistentAttribute) {
		String qualifiedTypeName = persistentAttribute.getResourcePersistentAttribute().getQualifiedTypeName();
		if (qualifiedTypeName == null) {
			return null;
		}
		PersistentType persistentType = persistentAttribute.getPersistenceUnit().getPersistentType(qualifiedTypeName);
		if (persistentType != null) {
			if (persistentType.getMappingKey() == MappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY) {
				return (Embeddable) persistentType.getMapping();
			}
		}
		return null;
	}
	
	public static ColumnMapping getColumnMapping(String attributeName, Embeddable embeddable) {
		if (attributeName == null || embeddable == null) {
			return null;
		}
		for (Iterator<PersistentAttribute> stream = embeddable.getPersistentType().allAttributes(); stream.hasNext();) {
			PersistentAttribute persAttribute = stream.next();
			if (attributeName.equals(persAttribute.getName())) {
				if (persAttribute.getMapping() instanceof ColumnMapping) {
					return (ColumnMapping) persAttribute.getMapping();
				}
			}
		}
		return null;		
	}
}

Back to the top