Commit 67fe9b88 authored by Romain Bioteau's avatar Romain Bioteau Committed by GitHub

fix(rename) move resource instead of delete/create a duplicate (#1436)

Closes BS-19033
parent 7f38ff0c
......@@ -20,6 +20,7 @@ import java.util.Optional;
import org.bonitasoft.studio.common.diagram.Identifier;
import org.bonitasoft.studio.common.diagram.dialog.OpenNameAndVersionForDiagramDialog;
import org.bonitasoft.studio.common.emf.tools.ModelHelper;
import org.bonitasoft.studio.common.repository.Repository;
import org.bonitasoft.studio.common.repository.RepositoryManager;
import org.bonitasoft.studio.common.repository.filestore.FileStoreFinder;
import org.bonitasoft.studio.diagram.custom.repository.DiagramFileStore;
......@@ -37,9 +38,7 @@ import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;
/**
* @author Romain Bioteau
*/
public class RenameDiagramCommandHandler extends AbstractHandler {
private FileStoreFinder fileStoreFinder;
......@@ -60,9 +59,9 @@ public class RenameDiagramCommandHandler extends AbstractHandler {
Display.getDefault().getActiveShell(), diagram,
diagramStore);
if (nameDialog.open() == Dialog.OK) {
Optional<ProcessDiagramEditor> editor = getDiagramEditor();
getDiagramEditor().filter(IEditorPart::isDirty)
.ifPresent(editor -> editor.doSave(Repository.NULL_PROGRESS_MONITOR));
RenameDiagramOperation renameDiagramOperation = new RenameDiagramOperation();
editor.ifPresent(renameDiagramOperation::setEditor);
renameDiagramOperation.setDiagramToDuplicate(diagram);
Identifier identifier = nameDialog.getIdentifier();
renameDiagramOperation.setNewDiagramName(identifier.getName());
......@@ -95,7 +94,8 @@ public class RenameDiagramCommandHandler extends AbstractHandler {
if (PlatformUI.isWorkbenchRunning()) {
Optional<ProcessDiagramEditor> diagramEditor = getDiagramEditor();
if (diagramEditor.isPresent()) {
final EObject mainElement = diagramEditor.get().getDiagramEditPart().resolveSemanticElement();
ProcessDiagramEditor processDiagramEditor = diagramEditor.get();
final EObject mainElement = processDiagramEditor.getDiagramEditPart().resolveSemanticElement();
final MainProcess diagram = ModelHelper.getMainProcess(mainElement);
return diagram;
}
......
......@@ -111,7 +111,7 @@ public class SaveCommandHandler extends SaveHandler {
final MainProcess proc = findProc(editorPart);
DiagramFileStore oldArtifact = null;
final List<DiagramDocumentEditor> editorsWithSameResourceSet = new ArrayList<DiagramDocumentEditor>();
if (nameOrVersionChanged(proc)) {
if (nameOrVersionChanged(proc, editorPart)) {
IEditorReference[] editorReferences;
editorReferences = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
final IEditorInput editorInput = editorPart.getEditorInput();
......@@ -187,9 +187,10 @@ public class SaveCommandHandler extends SaveHandler {
/**
* @param proc
* @param editorPart
* @return
*/
protected boolean nameOrVersionChanged(final MainProcess proc) {
protected boolean nameOrVersionChanged(final MainProcess proc, DiagramEditor editorPart) {
final MainProcess originalProcess = getOldProcess(proc);
if (originalProcess.getAuthor() == null
&& BonitaStudioPreferencesPlugin.getDefault().getPreferenceStore().getBoolean(BonitaPreferenceConstants.ASK_RENAME_ON_FIRST_SAVE)) {
......@@ -227,8 +228,8 @@ public class SaveCommandHandler extends SaveHandler {
}
};
if (nameDialog.open() == Dialog.OK) {
editorPart.doSave(Repository.NULL_PROGRESS_MONITOR);
final RenameDiagramOperation renameDiagramOperation = new RenameDiagramOperation();
renameDiagramOperation.setEditor((DiagramEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor());
renameDiagramOperation.setDiagramToDuplicate(proc);
final Identifier identifier = nameDialog.getIdentifier();
renameDiagramOperation.setNewDiagramName(identifier.getName());
......
......@@ -17,38 +17,37 @@ package org.bonitasoft.studio.properties.operation;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.bonitasoft.studio.common.NamingUtils;
import org.bonitasoft.studio.common.diagram.dialog.ProcessesNameVersion;
import org.bonitasoft.studio.common.emf.tools.ModelHelper;
import org.bonitasoft.studio.common.repository.Repository;
import org.bonitasoft.studio.common.repository.RepositoryManager;
import org.bonitasoft.studio.diagram.custom.operation.DuplicateDiagramOperation;
import org.bonitasoft.studio.diagram.custom.repository.DiagramFileStore;
import org.bonitasoft.studio.diagram.custom.repository.DiagramRepositoryStore;
import org.bonitasoft.studio.diagram.custom.repository.ProcessConfigurationFileStore;
import org.bonitasoft.studio.diagram.custom.repository.ProcessConfigurationRepositoryStore;
import org.bonitasoft.studio.model.process.AbstractProcess;
import org.bonitasoft.studio.model.process.Element;
import org.bonitasoft.studio.model.process.MainProcess;
import org.bonitasoft.studio.model.process.Pool;
import org.bonitasoft.studio.model.process.ProcessPackage;
import org.bonitasoft.studio.properties.i18n.Messages;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.IWorkbenchPart;
/**
* @author Romain Bioteau
*/
public class RenameDiagramOperation implements IRunnableWithProgress {
private MainProcess diagram;
private String diagramVersion;
private String diagramName;
private List<ProcessesNameVersion> pools = new ArrayList<>();
private DiagramEditor editor;
@Override
public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
......@@ -60,49 +59,58 @@ public class RenameDiagramOperation implements IRunnableWithProgress {
final String oldName = diagram.getName();
final String oldVersion = diagram.getVersion();
Optional<String> partName = Optional.ofNullable(editor).map(DiagramEditor::getPartName);
final DiagramRepositoryStore diagramStore = RepositoryManager.getInstance()
.getRepositoryStore(DiagramRepositoryStore.class);
final DuplicateDiagramOperation operation = new DuplicateDiagramOperation();
operation.setDiagramToDuplicate(diagram);
operation.setNewDiagramName(diagramName);
operation.setNewDiagramVersion(diagramVersion);
operation.setPoolsRenamed(pools);
operation.run(Repository.NULL_PROGRESS_MONITOR);
if (!(oldName.equals(diagramName) && oldVersion.equals(diagramVersion))) {
final DiagramFileStore diagramFileStore = diagramStore.getDiagram(oldName, oldVersion);
DiagramEditor openedEditor = diagramFileStore.getOpenedEditor();
if (openedEditor != null) {
openedEditor.doSave(Repository.NULL_PROGRESS_MONITOR);
DiagramFileStore diagramFileStore = diagramStore.getDiagram(oldName, oldVersion);
IFile resource = diagramFileStore.getResource();
try {
resource.move(Path.fromOSString(NamingUtils.toDiagramFilename(diagramName, diagramVersion)), true, monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
}
try {
resource.getParent().refreshLocal(IResource.DEPTH_ONE, monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
}
diagramFileStore = diagramStore.getChild(NamingUtils.toDiagramFilename(diagramName, diagramVersion));
diagram = diagramFileStore.getContent();
TransactionalEditingDomain editingDomain = TransactionUtil.getEditingDomain(diagram);
CompoundCommand compoundCommand = new CompoundCommand();
changeProcessNameAndVersion(diagram, compoundCommand, diagramName, diagramVersion, editingDomain);
for (final ProcessesNameVersion pnv : pools) {
final AbstractProcess fromPool = pnv.getAbstractProcess();
final String fromPoolName = fromPool.getName();
final String fromPoolVersion = fromPool.getVersion();
/* Find corresponding element in the duplicated model */
for (final Element element : diagram.getElements()) {
if (element instanceof AbstractProcess) {
if (element.getName().equals(fromPoolName)
&& ((AbstractProcess) element).getVersion().equals(fromPoolVersion)) {
if (!pnv.getNewName().equals(fromPoolName) || !pnv.getNewVersion().equals(fromPoolVersion)) {
changeProcessNameAndVersion((AbstractProcess) element, compoundCommand, pnv.getNewName(),
pnv.getNewVersion(), editingDomain);
break;
}
}
}
}
cleanOldFileStores(diagramFileStore);
partName.ifPresent(pName -> reopenEditors(pName, diagramStore));
}
editingDomain.getCommandStack().execute(compoundCommand);
diagramFileStore.save(null);
}
protected void reopenEditors(final String partName, final DiagramRepositoryStore diagramStore) {
final DiagramFileStore fStore = diagramStore.getChild(NamingUtils.toDiagramFilename(diagramName, diagramVersion));
fStore.save(null);
IWorkbenchPart partToActivate = fStore.open();
partToActivate.getSite().getPage().activate(partToActivate);
private void changeProcessNameAndVersion(final AbstractProcess process, final CompoundCommand cc,
final String newProcessLabel,
final String newProcessVersion, EditingDomain editingDomain) {
cc.append(SetCommand.create(editingDomain, process, ProcessPackage.Literals.ELEMENT__NAME, newProcessLabel));
cc.append(SetCommand.create(editingDomain, process, ProcessPackage.Literals.ABSTRACT_PROCESS__VERSION,
newProcessVersion));
}
protected void cleanOldFileStores(final DiagramFileStore diagramFileStore) {
final List<Pool> allPools = ModelHelper.getAllItemsOfType(diagram, ProcessPackage.Literals.POOL);
final ProcessConfigurationRepositoryStore confStore = RepositoryManager.getInstance()
.getRepositoryStore(ProcessConfigurationRepositoryStore.class);
for (final Pool p : allPools) {
ProcessConfigurationFileStore fileStore = confStore
.getChild(ModelHelper.getEObjectID(p) + "." + ProcessConfigurationRepositoryStore.CONF_EXT);
if (fileStore != null) {
fileStore.delete();
}
}
diagramFileStore.close();
diagramFileStore.delete();
}
public void setDiagramToDuplicate(final MainProcess diagram) {
this.diagram = diagram;
......@@ -120,8 +128,4 @@ public class RenameDiagramOperation implements IRunnableWithProgress {
this.diagramVersion = diagramVersion;
}
public void setEditor(final DiagramEditor editor) {
this.editor = editor;
}
}
......@@ -334,6 +334,7 @@ public class ProcessElementNameContribution extends AbstractNamePropertySectionC
if (new Identifier(oldName, oldVersion).equals(nameDialog.getIdentifier())) {
renamePoolsOnly(nameDialog, editor, newProcess);
} else {
editor.doSave(Repository.NULL_PROGRESS_MONITOR);
renameDiagramAndPool(nameDialog, editor, newProcess);
}
}
......@@ -347,7 +348,6 @@ public class ProcessElementNameContribution extends AbstractNamePropertySectionC
renameDiagramOperation.setNewDiagramName(identifier.getName());
renameDiagramOperation.setNewDiagramVersion(identifier.getVersion());
renameDiagramOperation.setPoolsRenamed(nameDialog.getPools());
renameDiagramOperation.setEditor(editor);
final IProgressService service = PlatformUI.getWorkbench().getProgressService();
try {
service.run(false, false, renameDiagramOperation);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment