Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 636e0872d9922270c8fcacba28b3e3e0f7b9420f (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*******************************************************************************
 * <copyright>
 *
 * Copyright (c) 2005, 2011 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.feature;

import java.text.MessageFormat;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.IDirectEditingContext;
import org.eclipse.graphiti.features.impl.AbstractDirectEditingFeature;
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
import org.eclipse.graphiti.mm.algorithms.Text;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.mm.pictograms.Shape;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jpt.jpa.core.context.java.JavaPersistentAttribute;
import org.eclipse.jpt.jpa.core.context.java.JavaPersistentType;
import org.eclipse.jpt.jpa.core.context.persistence.ClassRef;
import org.eclipse.jpt.jpa.core.context.persistence.PersistenceUnit;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.JPADiagramEditorPlugin;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.i18n.JPAEditorMessages;
import org.eclipse.jpt.jpadiagrameditor.ui.internal.propertypage.JPADiagramPropertyPage;
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;



public class DirectEditJPAEntityFeature extends AbstractDirectEditingFeature {

	private static String allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";	//$NON-NLS-1$

	public DirectEditJPAEntityFeature(IFeatureProvider fp) {
	    super(fp);
	}
	
	public int getEditingType() {
	    return TYPE_TEXT;
	}
	
	@Override
	public boolean canDirectEdit(IDirectEditingContext context) {
		return true;
	}
	
	public String getInitialValue(IDirectEditingContext context) {
	    Shape sh = (Shape)context.getPictogramElement();
	    ContainerShape csh = sh.getContainer();
	    JavaPersistentType jpt = (JavaPersistentType)getBusinessObjectForPictogramElement(csh);
	    return JPAEditorUtil.returnSimpleName(JpaArtifactFactory.instance().getEntityName(jpt));
	}
	
	@Override
	public String checkValueValid(String value, IDirectEditingContext context) {
		//CSN #1305664 2010
		PictogramElement pe = context.getPictogramElement();
	    ContainerShape csh = ((Shape)pe).getContainer();	    
	    JavaPersistentType jpt = (JavaPersistentType) getFeatureProvider().getBusinessObjectForPictogramElement(csh);
        String packageName = Signature.getQualifier(jpt.getName());
	    PersistenceUnit unit = jpt.getPersistenceUnit();
        
	    for(ClassRef classRef : unit.getClassRefs()) {
	    	if(classRef.getClassName().equals(packageName + Signature.C_DOT + value) && !(JPAEditorUtil.getText(jpt).equals(value))){
		    	return MessageFormat.format(JPAEditorMessages.DirectEditJPAEntityFeature_duplicateEntityName, packageName+value);

	    	}
	    }
	    
	    if (value.length() < 1)
	        return JPAEditorMessages.DirectEditJPAEntityFeature_classNameMsg; 
	    if (value.contains(" ")) //$NON-NLS-1$
	        return JPAEditorMessages.DirectEditJPAEntityFeature_scpacesNotAllowedMsg; 
	    if (value.contains("\n")) //$NON-NLS-1$
	        return JPAEditorMessages.DirectEditJPAEntityFeature_lineBreaksNotAllowedMsg;
	    if(value.contains("{") || value.contains("}")) //$NON-NLS-1$ //$NON-NLS-2$
	    	return JPAEditorMessages.DirectEditJPAEntityFeature_bracesNotAllowedMsg;
	    for (int i = 0; i < value.length(); i++) {
	    	if (allowed.indexOf(value.charAt(i)) < 0)
	    		return MessageFormat.format(JPAEditorMessages.DirectEditJPAEntityFeature_invalidSymbolsMsg, value);	
	    }
	    return null;
	}
	
	@Override
	public void setValue(final String value, IDirectEditingContext context) {
	    PictogramElement pe = context.getPictogramElement();
	    ContainerShape csh = ((Shape)pe).getContainer();
	    JavaPersistentType jpt = (JavaPersistentType)getBusinessObjectForPictogramElement(csh);
	    Properties props = JPADiagramPropertyPage.loadProperties(jpt.getJpaProject().getProject());

	    String specifiedEntityMappingName = JpaArtifactFactory.instance().getSpecifiedEntityName(jpt);
	    if(specifiedEntityMappingName == null){
	    	RenameEntityWithoutUIFeature ft = new RenameEntityWithoutUIFeature(getFeatureProvider(), value);
	    	ft.execute(jpt);
	    	return;
	    } else {
	    
	    JpaArtifactFactory.instance().renameEntity(jpt, value);
//	    Properties props = JPADiagramPropertyPage.loadProperties(jpt.getJpaProject().getProject());
			if ((JpaArtifactFactory.instance().hasEntityAnnotation(jpt) && JPADiagramPropertyPage.doesDirecteEditingAffectClassNameByDefault(jpt.getJpaProject().getProject(), props)) 
					|| JpaArtifactFactory.instance().hasMappedSuperclassAnnotation(jpt) 
					|| JpaArtifactFactory.instance().hasEmbeddableAnnotation(jpt)) {
				RenameEntityWithoutUIFeature ft = new RenameEntityWithoutUIFeature(getFeatureProvider(), value);
				ft.execute(jpt);
				return;
			}
		}
	    
	    final GraphicsAlgorithm alg = pe.getGraphicsAlgorithm().getGraphicsAlgorithmChildren().get(0);

		TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(alg);
		ted.getCommandStack().execute(new RecordingCommand(ted) {
			@Override
			protected void doExecute() {
				((Text) alg).setValue(value);
			}
		});	    
	    
	    Set<JavaPersistentAttribute> ats = JpaArtifactFactory.instance().getRelatedAttributes(jpt);
	    Iterator<JavaPersistentAttribute> it = ats.iterator();
	    while (it.hasNext()) {
	    	JavaPersistentAttribute at = it.next();
	    	PictogramElement pel = getFeatureProvider().getPictogramElementForBusinessObject(at);
	    	String newAtName = JPAEditorUtil.decapitalizeFirstLetter(value);
	    	if (JpaArtifactFactory.instance().isMethodAnnotated(at)) 
	    		newAtName = JPAEditorUtil.produceValidAttributeName(newAtName);
	    	try {
				newAtName = JpaArtifactFactory.instance().renameAttribute(jpt, at.getName(), newAtName, jpt.getName(), getFeatureProvider()).getName();
			} catch (InterruptedException e) {
				JPADiagramEditorPlugin.logError(e);
			}
			
	    	final GraphicsAlgorithm algo = pel.getGraphicsAlgorithm().getGraphicsAlgorithmChildren().get(0);
            final String attName = newAtName;
	  		TransactionalEditingDomain tedit = TransactionUtil.getEditingDomain(algo);
	  		tedit.getCommandStack().execute(new RecordingCommand(tedit) {
	  			@Override
				protected void doExecute() {
	  				((Text) algo).setValue(attName);
	  			}
	  		});	 
	    }
	}
	
	@Override
	public IJPAEditorFeatureProvider getFeatureProvider() {
		return  (IJPAEditorFeatureProvider)super.getFeatureProvider();
	}	
	
}

Back to the top