Commit cfe85ba7 authored by Romain Bioteau's avatar Romain Bioteau

BS-14163 Do not re-import provided connector definition

* If a provided connector definition is imported, skip it and add a
warning
parent a74e62a5
......@@ -53,7 +53,7 @@ public class ImportActorFilterArchiveOperation extends ImportConnectorArchiveOpe
@Override
protected SourceRepositoryStore<?> getSourceStore() {
return (SourceRepositoryStore<?>) RepositoryManager.getInstance().getRepositoryStore(ActorFilterSourceRepositoryStore.class);
return RepositoryManager.getInstance().getRepositoryStore(ActorFilterSourceRepositoryStore.class);
}
}
/**
* Copyright (C) 2009 BonitaSoft S.A.
* BonitaSoft, 31 rue Gustave Eiffel - 38000 Grenoble
*
* Copyright (C) 2009-2015 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.actors.ui.handler;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import org.bonitasoft.studio.actors.i18n.Messages;
import org.bonitasoft.studio.actors.repository.ImportActorFilterArchiveOperation;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;
import org.bonitasoft.studio.connectors.handler.ImportConnectorHandler;
import org.bonitasoft.studio.connectors.repository.ImportConnectorArchiveOperation;
/**
* @author Mickael Istria
*
*/
public class ImportActorFilterHandler extends AbstractHandler {
public class ImportActorFilterHandler extends ImportConnectorHandler {
/* (non-Javadoc)
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
/*
* (non-Javadoc)
* @see org.bonitasoft.studio.connectors.handler.ImportConnectorHandler#newImportOperation()
*/
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
try {
FileDialog fileDialog = new FileDialog(Display.getDefault().getActiveShell(), SWT.OPEN);
fileDialog.setFilterExtensions(new String[] {"*.zip"});
fileDialog.setText(Messages.importFilterArchive);
final String fileName = fileDialog.open();
IProgressService service = PlatformUI.getWorkbench().getProgressService() ;
service.run(true, false,new IRunnableWithProgress() {
protected ImportConnectorArchiveOperation newImportOperation() {
return new ImportActorFilterArchiveOperation();
}
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException,
InterruptedException {
try{
if (fileName != null) {
monitor.beginTask(Messages.importingFilterArchive,IProgressMonitor.UNKNOWN) ;
File zipFile = new File(fileName);
ImportActorFilterArchiveOperation importOp = new ImportActorFilterArchiveOperation() ;
importOp.setFile(zipFile) ;
final IStatus status = importOp.run(monitor) ;
if(IStatus.OK == status.getSeverity()){
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openInformation(Display.getDefault().getActiveShell(), Messages.importSuccessfulTitle, Messages.importSuccessfulMsg) ;
}
}) ;
}else{
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openError(Display.getDefault().getActiveShell(), Messages.importFailedTitle,Messages.bind(Messages.importFailedMsg,status.getMessage())) ;
}
}) ;
}
}
}catch (Exception e) {
throw new InvocationTargetException(e);
}
/*
* (non-Javadoc)
* @see org.bonitasoft.studio.connectors.handler.ImportConnectorHandler#getDialogTitle()
*/
@Override
protected String getDialogTitle() {
return Messages.importFilterArchive;
}
}
});
/*
* (non-Javadoc)
* @see org.bonitasoft.studio.connectors.handler.ImportConnectorHandler#getFailedImportTitle()
*/
@Override
protected String getFailedImportTitle() {
return Messages.importFailedTitle;
}
return null;
} catch (Exception ex) {
throw new ExecutionException(ex.getMessage(), ex);
}
/*
* (non-Javadoc)
* @see org.bonitasoft.studio.connectors.handler.ImportConnectorHandler#getFailedTitleMessage()
*/
@Override
protected String getFailedImportMessage() {
return Messages.importFailedMsg;
}
/*
* (non-Javadoc)
* @see org.bonitasoft.studio.connectors.handler.ImportConnectorHandler#getSuccessDialogMessage()
*/
@Override
protected String getImportSuccessMessage() {
return Messages.importSuccessfulMsg;
}
/*
* (non-Javadoc)
* @see org.bonitasoft.studio.connectors.handler.ImportConnectorHandler#getSuccessDialogTitle()
*/
@Override
protected String getImportSuccessTitle() {
return Messages.importSuccessfulTitle;
}
}
......@@ -47,6 +47,7 @@ Export-Package: org.bonitasoft.studio.connectors,
org.bonitasoft.studio.connectors.configuration,
org.bonitasoft.studio.connectors.expression,
org.bonitasoft.studio.connectors.extension,
org.bonitasoft.studio.connectors.handler,
org.bonitasoft.studio.connectors.i18n;x-friends:="org.bonitasoft.studio.tests.ex,org.bonitasoft.studio.connectors.test",
org.bonitasoft.studio.connectors.operation,
org.bonitasoft.studio.connectors.repository,
......
......@@ -141,4 +141,5 @@ copy=Copy
move=Move
connectorInChoice={0} (Connectors in)
connectorOutChoice={0} (Connectors out)
connectorEventLabel=Connector execution event
\ No newline at end of file
connectorEventLabel=Connector execution event
providedDefinitionAlreadyExists={0} definition is already provided and has not been imported.
\ No newline at end of file
/**
* Copyright (C) 2009 BonitaSoft S.A.
* BonitaSoft, 31 rue Gustave Eiffel - 38000 Grenoble
*
* Copyright (C) 2009-2015 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.studio.connectors.handler;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import org.bonitasoft.studio.connectors.i18n.Messages;
import org.bonitasoft.studio.connectors.repository.ImportConnectorArchiveOperation;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;
/**
* @author Mickael Istria
*
*/
public class ImportConnectorHandler extends AbstractHandler {
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
*/
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
try {
FileDialog fileDialog = new FileDialog(Display.getDefault().getActiveShell(), SWT.OPEN);
fileDialog.setFilterExtensions(new String[] {"*.zip"});
fileDialog.setText(Messages.importConnectorArchive);
final FileDialog fileDialog = new FileDialog(Display.getDefault().getActiveShell(), SWT.OPEN);
fileDialog.setFilterExtensions(new String[] { "*.zip" });
fileDialog.setText(getDialogTitle());
final String fileName = fileDialog.open();
IProgressService service = PlatformUI.getWorkbench().getProgressService() ;
service.run(true, false,new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException,
InterruptedException {
try{
if (fileName != null) {
monitor.beginTask(Messages.importingConnectorArchive,IProgressMonitor.UNKNOWN) ;
File zipFile = new File(fileName);
ImportConnectorArchiveOperation importOp = new ImportConnectorArchiveOperation() ;
importOp.setFile(zipFile) ;
final IStatus status = importOp.run(monitor) ;
if(IStatus.OK == status.getSeverity()){
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openInformation(Display.getDefault().getActiveShell(), Messages.importSuccessfulTitle, Messages.importSuccessfulMsg) ;
}
}) ;
}else{
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openError(Display.getDefault().getActiveShell(), Messages.importFailedTitle,Messages.bind(Messages.importFailedMsg,status.getMessage())) ;
}
}) ;
}
}
}catch (Exception e) {
throw new InvocationTargetException(e);
}
if (fileName != null) {
final IProgressService service = PlatformUI.getWorkbench().getProgressService();
final ImportConnectorArchiveOperation importOp = newImportOperation();
importOp.setFile(new File(fileName));
service.run(true, false, importOp);
final IStatus status = importOp.getStatus();
switch (status.getSeverity()) {
case IStatus.OK:
MessageDialog.openInformation(Display.getDefault().getActiveShell(), getImportSuccessTitle(), getImportSuccessMessage());
break;
case IStatus.WARNING:
MessageDialog.openWarning(Display.getDefault().getActiveShell(), getImportSuccessTitle(), status.getMessage());
break;
case IStatus.ERROR:
MessageDialog.openError(Display.getDefault().getActiveShell(), getFailedImportTitle(),
Messages.bind(getFailedImportMessage(), status.getMessage()));
break;
default:
break;
}
});
}
return null;
} catch (Exception ex) {
} catch (final Exception ex) {
throw new ExecutionException(ex.getMessage(), ex);
}
}
protected ImportConnectorArchiveOperation newImportOperation() {
return new ImportConnectorArchiveOperation();
}
protected String getFailedImportMessage() {
return Messages.importFailedMsg;
}
protected String getFailedImportTitle() {
return Messages.importFailedTitle;
}
protected String getImportSuccessMessage() {
return Messages.importSuccessfulMsg;
}
protected String getImportSuccessTitle() {
return Messages.importSuccessfulTitle;
}
protected String getDialogTitle() {
return Messages.importConnectorArchive;
}
}
......@@ -181,6 +181,8 @@ public class Messages extends NLS {
public static String selectMoveOrCopyAction;
public static String copy;
public static String providedDefinitionAlreadyExists;
/**
* @param event
* @return
......
......@@ -18,8 +18,11 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import org.bonitasoft.studio.common.NamingUtils;
......@@ -34,7 +37,10 @@ import org.bonitasoft.studio.common.repository.model.IRepositoryStore;
import org.bonitasoft.studio.common.repository.model.ReadFileStoreException;
import org.bonitasoft.studio.common.repository.store.SourceRepositoryStore;
import org.bonitasoft.studio.connector.model.definition.ConnectorDefinition;
import org.bonitasoft.studio.connector.model.definition.DocumentRoot;
import org.bonitasoft.studio.connector.model.definition.util.ConnectorDefinitionResourceFactoryImpl;
import org.bonitasoft.studio.connector.model.implementation.ConnectorImplementation;
import org.bonitasoft.studio.connectors.i18n.Messages;
import org.bonitasoft.studio.dependencies.repository.DependencyRepositoryStore;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.resources.IFile;
......@@ -42,11 +48,16 @@ import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jface.operation.IRunnableWithProgress;
/**
* @author Romain Bioteau
*/
public class ImportConnectorArchiveOperation {
public class ImportConnectorArchiveOperation implements IRunnableWithProgress {
private File zipFile;
......@@ -85,13 +96,18 @@ public class ImportConnectorArchiveOperation {
}
};
private IStatus status = Status.OK_STATUS;
public void setFile(final File file) {
zipFile = file;
}
public IStatus run(final IProgressMonitor monitor) {
@Override
public void run(final IProgressMonitor monitor) {
monitor.beginTask(Messages.importingConnectorArchive, IProgressMonitor.UNKNOWN);
if (zipFile == null) {
return ValidationStatus.error("input file not set");
status = ValidationStatus.error("input file not set");
return;
}
final File tmp = new File(ProjectUtil.getBonitaStudioWorkFolder(), "tmpImportConnectorDir");
tmp.delete();
......@@ -102,10 +118,10 @@ public class ImportConnectorArchiveOperation {
BonitaStudioLog.error(e);
}
final IStatus status = readManifest(tmp);
status = readManifest(tmp);
if (status.getSeverity() != IStatus.OK) {
PlatformUtil.delete(tmp, monitor);
return status;
return;
}
try {
......@@ -116,7 +132,10 @@ public class ImportConnectorArchiveOperation {
} finally {
FileActionDialog.deactivateYesNoToAll();
}
return Status.OK_STATUS;
}
public IStatus getStatus() {
return status;
}
private IStatus readManifest(final File tmp) {
......@@ -198,25 +217,58 @@ public class ImportConnectorArchiveOperation {
final File defFile = files[0];
final IRepositoryStore defStore = getDefinitionStore();
IRepositoryFileStore fileStore = null;
try {
final FileInputStream fis = new FileInputStream(defFile);
fileStore = defStore.importInputStream(defFile.getName(), fis);
fis.close();
try (final FileInputStream fis = new FileInputStream(defFile);) {
final ConnectorDefinition connectorDefinition = toConnectorDefinition(defFile);
if (connectorDefinition == null) {
fileStore = defStore.importInputStream(defFile.getName(), fis);
} else {
final IRepositoryFileStore existingDef = defStore.getChild(defFile.getName());
if (existingDef == null || !existingDef.isReadOnly()
|| !defintionHasIdAndVersion(existingDef, connectorDefinition.getId(), connectorDefinition.getVersion())) {
fileStore = defStore.importInputStream(defFile.getName(), fis);
} else {
status = ValidationStatus.warning(Messages.bind(Messages.providedDefinitionAlreadyExists, existingDef.getDisplayName()));
}
}
} catch (final Exception e) {
BonitaStudioLog.error(e);
}
importIcons(tmpDir);
importMessages(tmpDir);
if (fileStore != null) {
try {
importDefinitionDependencies(tmpDir, (ConnectorDefinition) fileStore.getContent());
} catch (final ReadFileStoreException e) {
BonitaStudioLog.error("Failed to read connector implementation", e);
importIcons(tmpDir);
importMessages(tmpDir);
if (fileStore != null) {
try {
importDefinitionDependencies(tmpDir, (ConnectorDefinition) fileStore.getContent());
} catch (final ReadFileStoreException e) {
BonitaStudioLog.error("Failed to read connector implementation", e);
}
}
}
}
}
protected ConnectorDefinition toConnectorDefinition(final File defFile) {
final Resource r = new ConnectorDefinitionResourceFactoryImpl().createResource(URI.createFileURI(defFile.getAbsolutePath()));
try {
r.load(Collections.emptyMap());
} catch (final IOException e) {
BonitaStudioLog.error(e);
}
final EList<EObject> contents = r.getContents();
if (!contents.isEmpty()) {
final DocumentRoot documentRoot = (DocumentRoot) contents.get(0);
if (documentRoot != null) {
return documentRoot.getConnectorDefinition();
}
}
return null;
}
private boolean defintionHasIdAndVersion(IRepositoryFileStore definition, String id, String version) throws ReadFileStoreException {
final ConnectorDefinition content = (ConnectorDefinition) definition.getContent();
return Objects.equals(id, content.getId()) && Objects.equals(version, content.getVersion());
}
private void importMessages(final File tmpDir) {
final File[] files = tmpDir.listFiles(messageFileFilter);
if (files != null) {
......
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