Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fc89c741b9365c05fa1728aed51c210c5225e5da (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
/*******************************************************************************
 * <copyright>
 *
 * Copyright (c) 2005, 2010 SAP AG.
 * 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:
 *    Stefan Dimov - initial API, implementation and documentation
 *
 * </copyright>
 *
 *******************************************************************************/
package org.eclipse.jpt.jpadiagrameditor.ui.internal.relations;

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.PersistentType;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.IJPAEditorFeatureProvider;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorConstants;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorUtil;

public class IsARelation {

	protected final static String SEPARATOR = ";isA;";							//$NON-NLS-1$
	public final static String IS_A_CONNECTION_PROP_KEY = "is_is_a_connection";	//$NON-NLS-1$
	
	
	protected PersistentType subclass;
	protected PersistentType superclass;
	
	public IsARelation(PersistentType subclass, 
					   PersistentType superclass) {
		this.subclass = subclass;
		this.superclass = superclass;
	}
	
	public IsARelation(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 ((startObj == null) || (endObj == null))
			throw new NullPointerException("Some of the connection ends is null");	//$NON-NLS-1$
		if (!(startObj instanceof PersistentType) || !(endObj instanceof PersistentType))
			throw new IllegalArgumentException();
		this.subclass = (PersistentType)startObj;
		this.superclass = (PersistentType)endObj;
	}
	
	
	public PersistentType getSubclass() {
		return subclass; 
	}
	
	public PersistentType getSuperclass() {
		return superclass; 
	}
	
	public static boolean isIsAConnection(Connection conn) {
		String val = JPAEditorUtil.getPeUtil().getPropertyValue(conn, IS_A_CONNECTION_PROP_KEY);
		return (Boolean.TRUE.toString().equals(val));
	}


	public String getId() {
		return generateId(subclass, superclass);
	}
	
	public static String generateId(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 ((startObj == null) || (endObj == null))
			return null;
		if (!(startObj instanceof PersistentType) || !(endObj instanceof PersistentType))
			return null;
		PersistentType startJpt = (PersistentType)startObj;
		PersistentType endJpt = (PersistentType)endObj;
		return generateId(startJpt, endJpt);
	}
	
	private static String generateId(PersistentType startJpt, PersistentType endJpt) {
		return JPAEditorConstants.IS_A_RELATION_ID_PREFIX + 
				startJpt.getName() + SEPARATOR + endJpt.getName();
	}
	
	@Override
	public boolean equals(Object otherRel) {
		if (!IsARelation.class.isInstance(otherRel))
			return false;
		return getId().equals(((IsARelation)otherRel).getId());
	}
	
	@Override
	public int hashCode() {
		return getId().hashCode();
	}

}

Back to the top