Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2440179665f751305d569532c0d6bf8c2768c394 (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
/*******************************************************************************
 * <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.command;

import java.util.Locale;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.graphiti.ui.editor.IDiagramContainerUI;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jpt.common.utility.command.Command;
import org.eclipse.jpt.jpa.core.context.PersistentType;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.JPADiagramEditorPlugin;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.provider.IJPAEditorFeatureProvider;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JPAEditorUtil;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.util.JpaArtifactFactory;
import org.eclipse.ui.IWorkbenchSite;

public class DeleteAttributeCommand implements Command {
	
	private PersistentType jpt;
	private String attributeName;
	private IJPAEditorFeatureProvider fp;
	
	public DeleteAttributeCommand(PersistentType jpt, String attributeName,
							IJPAEditorFeatureProvider fp) {
		super();
		this.jpt = jpt;
		this.attributeName = attributeName;
		this.fp = fp;		
	}

	public void execute() {
		JpaArtifactFactory.instance().removeOrmPersistentAttribute(jpt, attributeName);

		try {
		IJavaProject jp = JavaCore.create(jpt.getJpaProject().getProject());
		IType javaType = jp.findType(jpt.getName());
		
		String attrNameWithCapitalLetter = attributeName.substring(0, 1)
				.toUpperCase(Locale.ENGLISH) + attributeName.substring(1);
			
		IMethod getAttributeMethod = findGetterMethod(javaType,
				attrNameWithCapitalLetter);
		
		String typeSignature = getReturnedType(getAttributeMethod);

		deleteGetterMethod(typeSignature, getAttributeMethod);
		deleteField(javaType);
		deleteSetterMethod(javaType, attrNameWithCapitalLetter,
					typeSignature);

		IWorkbenchSite ws = ((IDiagramContainerUI) fp.getDiagramTypeProvider().getDiagramBehavior().getDiagramContainer()).getSite();
		JPAEditorUtil.organizeImports(javaType.getCompilationUnit(), ws);

		jpt.getJavaResourceType().getJavaResourceCompilationUnit().synchronizeWithJavaSource();
		jpt.synchronizeWithResourceModel();
		jpt.update();
		
		} catch (JavaModelException e) {
			JPADiagramEditorPlugin
					.logError(
							"Cannnot delete attribute with name " + attributeName, e); //$NON-NLS-1$				
		}

	}

	private String getReturnedType(IMethod getAttributeMethod)
			throws JavaModelException {
		String typeSignature = null;
		if ((getAttributeMethod != null) && getAttributeMethod.exists()) {
			typeSignature = getAttributeMethod.getReturnType();
		}
		return typeSignature;
	}

	private IMethod findGetterMethod(IType javaType,
			String attrNameWithCapitalLetter) {
		String getterPrefix = "get"; 			//$NON-NLS-1$
		String methodName = getterPrefix + attrNameWithCapitalLetter;
		IMethod getAttributeMethod = javaType.getMethod(methodName,
				new String[0]);
		if (!getAttributeMethod.exists()) {
			getterPrefix = "is";	//$NON-NLS-1$
		}
		methodName = getterPrefix + attrNameWithCapitalLetter; 	//$NON-NLS-1$
		getAttributeMethod = javaType.getMethod(methodName, new String[0]);
		return getAttributeMethod;
	}

	private void deleteSetterMethod(IType javaType,
			String attrNameWithCapitalLetter, String typeSignature)
			throws JavaModelException {
		String methodName = "set" + attrNameWithCapitalLetter; //$NON-NLS-1$
		if(typeSignature != null) {
			IMethod setAttributeMethod = javaType.getMethod(methodName,
					new String[] { typeSignature });
			if ((setAttributeMethod != null) && setAttributeMethod.exists())
				setAttributeMethod.delete(true, new NullProgressMonitor());
		}
	}

	private void deleteGetterMethod(String typeSignature, IMethod getAttributeMethod) throws JavaModelException {
		if (getAttributeMethod != null && getAttributeMethod.exists()) {
			getAttributeMethod.delete(true, new NullProgressMonitor());
		}
	}

	private void deleteField(IType javaType) throws JavaModelException {
		IField attributeField = javaType.getField(attributeName);
		if (attributeField != null) {
			if (!attributeField.exists()) {
				attributeField = javaType.getField(JPAEditorUtil.revertFirstLetterCase(attributeName));
			}
			attributeField.delete(true, new NullProgressMonitor());
		}
	}
	
}

Back to the top