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

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.List;

import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.mm.pictograms.Shape;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jpt.jpa.core.JpaProject;
import org.eclipse.jpt.jpa.core.context.java.JavaPersistentType;
import org.eclipse.jpt.jpa.core.context.persistence.PersistenceUnit;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.JPADiagramEditorPlugin;

public class JPACheckSum {
	
	private static JPACheckSum instance = null;
	private static MessageDigest md5 = null;
	
	public static synchronized JPACheckSum INSTANCE() {
		if (instance == null) {
			try {
				md5 = MessageDigest.getInstance("MD5");	//$NON-NLS-1$
			} catch (NoSuchAlgorithmException e) {
				JPADiagramEditorPlugin.logError("Couldn't create instance of MD5", e); //$NON-NLS-1$
				return null;
			}
			instance = new JPACheckSum();
		}
		return instance; 
	}
		
	public String getSavedShapeMD5(Shape sh) {	
		String checkSumString = Graphiti.getPeService().getPropertyValue(sh, JPAEditorConstants.PROP_ENTITY_CHECKSUM);
		if (checkSumString == null)
			return "";	//$NON-NLS-1$
		return checkSumString;
	}
	
	public void assignEntityShapesMD5Strings(Diagram d, JpaProject jpaProject) {
		PersistenceUnit pu = JpaArtifactFactory.instance().getPersistenceUnit(jpaProject);
		List<Shape> picts = d.getChildren();
		if (picts.size() == 0)
			return;
		Iterator<Shape> it = picts.iterator();
		while (it.hasNext()) {
			Shape pict = it.next();
			String name = Graphiti.getPeService().getPropertyValue(pict, JPAEditorConstants.PROP_ENTITY_CLASS_NAME);
			JavaPersistentType jpt = JpaArtifactFactory.instance().getJPT(name, pu);
			String hash = "";	//$NON-NLS-1$
			if (jpt != null) {
				ICompilationUnit cu = JPAEditorUtil.getCompilationUnit(jpt);
				hash = generateCompilationUnitMD5String(cu);
			}
			Graphiti.getPeService().setPropertyValue(pict, JPAEditorConstants.PROP_ENTITY_CHECKSUM, hash);
		}
	}

		
	public String generateCompilationUnitMD5String(ICompilationUnit cu) {
		String src = null;
		try {
			src = cu.getSource();
		} catch (JavaModelException e) {
			JPADiagramEditorPlugin.logError("Can't get the source of the compilation unit", e); //$NON-NLS-1$	
			return null;
		}
		byte[] res = md5.digest(src.getBytes());
		StringBuilder sb = new StringBuilder();
		for (byte b : res)
			sb.append(Byte.toString(b));
		return sb.toString();		
	}
	
	public boolean isEntityModelChanged(Shape sh, JpaProject jpaProject) {
		if (sh == null)
			return true; 
		PersistenceUnit pu = JpaArtifactFactory.instance().getPersistenceUnit(jpaProject);
		String name = Graphiti.getPeService().getPropertyValue(sh, JPAEditorConstants.PROP_ENTITY_CLASS_NAME);
		JavaPersistentType jpt = JpaArtifactFactory.instance().getJPT(name, pu);
		String savedMD5 = getSavedShapeMD5(sh);	
		String actualMD5 = "";	//$NON-NLS-1$
		if (jpt != null) {
			ICompilationUnit cu = JPAEditorUtil.getCompilationUnit(jpt);
			actualMD5 = generateCompilationUnitMD5String(cu);
		}
		return !savedMD5.equals(actualMD5);
	}
	
	public boolean isModelDifferentFromDiagram(Diagram d, JpaProject jpaProject) {
		List<Shape> picts = d.getChildren();
		if (picts.size() == 0)
			return false;
		Iterator<Shape> it = picts.iterator();
		while (it.hasNext()) {
			Shape pict = it.next();
			if (isEntityModelChanged(pict, jpaProject))
				return true;
		}
		return false;
	}
}

Back to the top