Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8e9547d0e31f3370540838fd5a91f27da1b66671 (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
/*******************************************************************************
 * <copyright>
 *
 * Copyright (c) 2012 SAP AG and others.
 * 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:
 *    Petya Sabeva - initial API, implementation and documentation
 *
 * </copyright>
 *
 *******************************************************************************/

package org.eclipse.jpt.jpadiagrameditor.ui.internal.relations;

import java.util.Hashtable;

import org.eclipse.graphiti.mm.pictograms.Anchor;
import org.eclipse.graphiti.mm.pictograms.Connection;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.jpt.jpa.core.context.PersistentAttribute;
import org.eclipse.jpt.jpa.core.context.PersistentType;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.IJPAEditorFeatureProvider;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorConstants;

public abstract class HasReferanceRelation {
	protected final static String SEPARATOR = ";hasReference;";							//$NON-NLS-1$

	protected PersistentType embeddingEntity;
	protected PersistentType embeddable;
	
	public final static Hashtable<HasReferenceType, String> relTypeToIdPart = new Hashtable<HasReferenceType, String>(); 

	private PersistentAttribute embeddedAnnotatedAttribute;
	
	public static enum HasReferenceType {
		SINGLE, COLLECTION
	}
	
	static {
		relTypeToIdPart.put(HasReferenceType.SINGLE, "1-1;"); //$NON-NLS-1$
		relTypeToIdPart.put(HasReferenceType.COLLECTION, "1-N;"); //$NON-NLS-1$
	}
	
	public HasReferanceRelation(PersistentType embeddingEntity, 
					   PersistentType embeddable) {
		this.embeddingEntity = embeddingEntity;
		this.embeddable = embeddable;
	}
	
	public HasReferanceRelation(IJPAEditorFeatureProvider fp, Connection conn) {
		Anchor start = conn.getStart();
		Anchor end = conn.getEnd();
		Object startObj = fp.getBusinessObjectForPictogramElement((ContainerShape)start.eContainer());
		Object endObj = fp.getBusinessObjectForPictogramElement((ContainerShape)end.eContainer());
		if ((endObj == null) || (startObj == null))
			throw new NullPointerException("Some of the connection ends is null");	//$NON-NLS-1$
		if (!(endObj instanceof PersistentType) || !(startObj instanceof PersistentType))
			throw new IllegalArgumentException();
		this.embeddingEntity = (PersistentType)startObj;
		this.embeddable = (PersistentType)endObj;
	}
	
	
	public PersistentType getEmbeddable() {
		return embeddable; 
	}
	
	public PersistentType getEmbeddingEntity() {
		return embeddingEntity; 
	}

	public String getId() {
		return generateId(embeddingEntity, embeddable, embeddedAnnotatedAttribute.getName(), getReferenceType());
	}

	public static String generateId(PersistentType startJpt, PersistentType endJpt, String embeddedAttributeName, HasReferenceType relType) {		
		return JPAEditorConstants.HAS_REFERENCE_RELATION_ID_PREFIX +
				startJpt.getName() + SEPARATOR + relTypeToIdPart.get(relType) + endJpt.getName()+ "-" + embeddedAttributeName; //$NON-NLS-1$
	}

	@Override
	public boolean equals(Object otherRel) {
		if (!HasReferanceRelation.class.isInstance(otherRel))
			return false;
		return getId().equals(((HasReferanceRelation)otherRel).getId());
	}
	
	@Override
	public int hashCode() {
		return getId().hashCode();
	}

	public abstract HasReferenceType getReferenceType();

	public PersistentAttribute getEmbeddedAnnotatedAttribute() {
		return embeddedAnnotatedAttribute;
	}

	public void setEmbeddedAnnotatedAttribute(PersistentAttribute embeddedAnnotatedAttribute) {
		this.embeddedAnnotatedAttribute = embeddedAnnotatedAttribute;
	}
}

Back to the top