Commit fef3cc33 authored by Romain Bioteau's avatar Romain Bioteau
Browse files

BS-9737

Validate after import
parent 1f3f3b90
......@@ -33,6 +33,7 @@ import java.util.Properties;
import java.util.Set;
import org.bonitasoft.studio.common.Pair;
import org.bonitasoft.studio.common.ProcessesValidationAction;
import org.bonitasoft.studio.common.ProductVersion;
import org.bonitasoft.studio.common.jface.FileActionDialog;
import org.bonitasoft.studio.common.log.BonitaStudioLog;
......@@ -45,6 +46,8 @@ import org.bonitasoft.studio.common.repository.filestore.FileStoreChangeEvent.Ev
import org.bonitasoft.studio.common.repository.model.IRepository;
import org.bonitasoft.studio.common.repository.model.IRepositoryFileStore;
import org.bonitasoft.studio.common.repository.model.IRepositoryStore;
import org.bonitasoft.studio.model.process.AbstractProcess;
import org.bonitasoft.studio.model.process.MainProcess;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
......@@ -79,6 +82,8 @@ public class ImportBosArchiveOperation {
private IRepository currentRepository;
private final List<AbstractProcess> importedProcesses = new ArrayList<AbstractProcess>();
public IStatus run(final IProgressMonitor monitor) {
status = Status.OK_STATUS;
Assert.isNotNull(archiveFile);
......@@ -139,6 +144,7 @@ public class ImportBosArchiveOperation {
currentRepository.enableBuild();
currentRepository.refresh(Repository.NULL_PROGRESS_MONITOR);
currentRepository.notifyFileStoreEvent(new FileStoreChangeEvent(EventType.POST_IMPORT, null));
validateAllAfterImport();
} catch (final Exception e) {
BonitaStudioLog.error(e);
} finally {
......@@ -150,6 +156,19 @@ public class ImportBosArchiveOperation {
return status;
}
private void validateAllAfterImport() {
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
final ProcessesValidationAction validationAction = new ProcessesValidationAction(importedProcesses);
validationAction.activateValidationPart();
validationAction.performValidation();
}
});
}
public void setCurrentRepository(final IRepository currentRepository) {
this.currentRepository = currentRepository;
}
......@@ -202,6 +221,10 @@ public class ImportBosArchiveOperation {
final boolean openAfterImport = resourceToOpen != null && resourceToOpen.contains(filename)
|| resourceToOpen == null;
final IRepositoryFileStore fileStore = repository.importIResource(filename, child);
if (filename.endsWith(".proc")) {
importedProcesses.add((MainProcess) fileStore.getContent());
}
if (fileStore != null && openAfterImport) {
fileStoresToOpen.add(fileStore);
}
......
......@@ -28,33 +28,31 @@ import org.eclipse.ui.handlers.IHandlerService;
public class ProcessesValidationAction {
private List<AbstractProcess> listOfProcessesToValidate;
private final List<AbstractProcess> listOfProcessesToValidate;
private IStatus status;
public ProcessesValidationAction(List<AbstractProcess> executableProcesses){
this.listOfProcessesToValidate = executableProcesses;
}
public ProcessesValidationAction(final List<AbstractProcess> processes){
listOfProcessesToValidate = processes;
}
public void performValidation() {
final ICommandService cmdService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
Command cmd = cmdService.getCommand("org.bonitasoft.studio.validation.batchValidation");
final Command cmd = cmdService.getCommand("org.bonitasoft.studio.validation.batchValidation");
if (cmd.isEnabled()) {
final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
Set<String> procFiles = new HashSet<String>();
for (AbstractProcess p : listOfProcessesToValidate) {
Resource eResource = p.eResource();
final Set<String> procFiles = new HashSet<String>();
for (final AbstractProcess p : listOfProcessesToValidate) {
final Resource eResource = p.eResource();
if (eResource != null) {
procFiles.add(URI.decode(eResource.getURI().lastSegment()));
}
}
try {
Parameterization showReportParam = new Parameterization(cmd.getParameter("showReport"), Boolean.FALSE.toString());
Parameterization filesParam = new Parameterization(cmd.getParameter("diagrams"), procFiles.toString());
status = (IStatus) handlerService.executeCommand(new ParameterizedCommand(cmd, new Parameterization[] { showReportParam,
filesParam }), null);
} catch (Exception e) {
final Parameterization showReportParam = new Parameterization(cmd.getParameter("showReport"), Boolean.FALSE.toString());
final Parameterization filesParam = new Parameterization(cmd.getParameter("diagrams"), procFiles.toString());
status = (IStatus) handlerService.executeCommand(new ParameterizedCommand(cmd, new Parameterization[] { showReportParam,
filesParam }), null);
} catch (final Exception e) {
BonitaStudioLog.error(e);
}
}
......@@ -62,7 +60,7 @@ public class ProcessesValidationAction {
private boolean statusContainsError() {
if (status != null) {
for (IStatus s : status.getChildren()) {
for (final IStatus s : status.getChildren()) {
if (s.getSeverity() == IStatus.WARNING || s.getSeverity() == IStatus.ERROR) {
return true;
}
......@@ -74,10 +72,10 @@ public class ProcessesValidationAction {
public boolean displayConfirmationDialog(){
if (statusContainsError()) {
if (!FileActionDialog.getDisablePopup()) {
String errorMessage = Messages.errorValidationMessage
final String errorMessage = Messages.errorValidationMessage
+ PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getTitle()
+ Messages.errorValidationContinueAnywayMessage;
int result = new ValidationDialog(Display.getDefault().getActiveShell(), Messages.validationFailedTitle, errorMessage,
final int result = new ValidationDialog(Display.getDefault().getActiveShell(), Messages.validationFailedTitle, errorMessage,
ValidationDialog.YES_NO_SEEDETAILS).open();
if (result == ValidationDialog.NO) {
return false;
......@@ -91,12 +89,12 @@ public class ProcessesValidationAction {
}
return true;
}
public boolean displayOkSeeMoreDetailsDialog(){
if (statusContainsError()) {
String errorMessage = Messages.errorValidationMessage
final String errorMessage = Messages.errorValidationMessage
+ PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getTitle();
int result = new ValidationDialog(Display.getDefault().getActiveShell(), Messages.validationFailedTitle, errorMessage,
final int result = new ValidationDialog(Display.getDefault().getActiveShell(), Messages.validationFailedTitle, errorMessage,
ValidationDialog.OK_SEEDETAILS).open();
if (result == ValidationDialog.SEE_DETAILS) {
activateValidationPart();
......@@ -108,11 +106,11 @@ public class ProcessesValidationAction {
public void activateValidationPart(){
final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart part = activePage.getActiveEditor();
final IEditorPart part = activePage.getActiveEditor();
if (part != null && part instanceof DiagramEditor) {
MainProcess proc = ModelHelper.getMainProcess(((DiagramEditor) part).getDiagramEditPart().resolveSemanticElement());
String partName = proc.getName() + " (" + proc.getVersion() + ")";
for (IEditorReference ref : activePage.getEditorReferences()) {
final MainProcess proc = ModelHelper.getMainProcess(((DiagramEditor) part).getDiagramEditPart().resolveSemanticElement());
final String partName = proc.getName() + " (" + proc.getVersion() + ")";
for (final IEditorReference ref : activePage.getEditorReferences()) {
if (partName.equals(ref.getPartName())) {
activePage.activate(ref.getPart(true));
break;
......@@ -126,7 +124,7 @@ public class ProcessesValidationAction {
public void run() {
try {
activePage.showView("org.bonitasoft.studio.validation.view");
} catch (PartInitException e) {
} catch (final PartInitException e) {
BonitaStudioLog.error(e);
}
}
......
......@@ -17,9 +17,7 @@
package org.bonitasoft.studio.validation;
import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.bonitasoft.studio.common.emf.tools.ModelHelper;
import org.bonitasoft.studio.common.jface.ValidationDialog;
......@@ -28,17 +26,14 @@ import org.bonitasoft.studio.common.repository.RepositoryManager;
import org.bonitasoft.studio.diagram.custom.repository.DiagramFileStore;
import org.bonitasoft.studio.diagram.custom.repository.DiagramRepositoryStore;
import org.bonitasoft.studio.model.process.MainProcess;
import org.bonitasoft.studio.model.process.diagram.form.providers.ProcessMarkerNavigationProvider;
import org.bonitasoft.studio.validation.i18n.Messages;
import org.bonitasoft.studio.validation.ui.view.ValidationViewPart;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.swt.widgets.Display;
......@@ -56,156 +51,146 @@ import org.eclipse.ui.progress.IProgressService;
*/
public class BatchValidationHandler extends AbstractHandler {
/* (non-Javadoc)
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
if(PlatformUI.isWorkbenchRunning()){
Map<?,?> parameters = event.getParameters();
Set<Diagram> toValidate = new HashSet<Diagram>();
if(parameters != null && !parameters.isEmpty()){
String files = event.getParameter("diagrams");
if(files != null){
final DiagramRepositoryStore store = (DiagramRepositoryStore) RepositoryManager.getInstance().getRepositoryStore(DiagramRepositoryStore.class);
files = files.substring(1,files.length()-1);
String[] allFiles = files.split(",");
for(String fileName : allFiles){
DiagramFileStore fileStore = store.getChild(fileName.trim());
if(fileStore != null){
Resource r = fileStore.getEMFResource();
for(EObject eObject : r.getContents() ){
if(eObject instanceof Diagram){
toValidate.add((Diagram) eObject);
}
}
}else{
BonitaStudioLog.debug("Proc file : "+fileName.trim()+" not found in repository!", ValidationPlugin.PLUGIN_ID);
}
}
}
}
if(toValidate.isEmpty() && PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null
&& PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() != null){
IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor() ;
if(part instanceof DiagramEditor){
Resource resource = ((DiagramEditor) part).getDiagramEditPart().resolveSemanticElement().eResource();
if(resource != null){
for(EObject content : resource.getContents()){
if(content instanceof Diagram){
toValidate.add((Diagram) content);
}
}
}
}
}
if(!toValidate.isEmpty()){
Resource eResource = toValidate.iterator().next().eResource();
IFile target = eResource != null ? WorkspaceSynchronizer.getFile(eResource) : null;
if (target != null) {
ProcessMarkerNavigationProvider.deleteMarkers(target);
}
}
final IProgressService service = PlatformUI.getWorkbench().getProgressService() ;
final BatchValidationOperation validateOperation = new BatchValidationOperation();
validateOperation.setDiagramToValidate(toValidate);
Display.getDefault().syncExec(new Runnable() {
public void run() {
try {
service.run(true, false, validateOperation );
} catch (InvocationTargetException e) {
BonitaStudioLog.error(e);
} catch (InterruptedException e) {
BonitaStudioLog.error(e);
}
}
});
Object showReport = parameters.get("showReport");
if(showReport == null){
showReport = Boolean.TRUE;
}
if(showReport instanceof Boolean){
if(((Boolean)showReport).booleanValue()){
showReport(validateOperation.getResult());
}
}
if( PlatformUI.isWorkbenchRunning() && PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null){
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
if(activePage != null){
IViewPart part = activePage.findView("org.bonitasoft.studio.validation.view");
if(part instanceof ValidationViewPart){
((ValidationViewPart)part).refreshViewer();
}
}
}
return validateOperation.getResult();
} else {
return IStatus.OK;
}
}
private void showReport(IStatus status) {
if(statusContainsError(status)){
String errorMessage = Messages.validationErrorFoundMessage + " " + PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getTitle() ;
int result = new ValidationDialog(Display.getDefault().getActiveShell(), Messages.validationFailedTitle,errorMessage, ValidationDialog.OK_SEEDETAILS).open();
if(result == ValidationDialog.SEE_DETAILS){
final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart part = activePage.getActiveEditor();
if(part != null && part instanceof DiagramEditor){
MainProcess proc = ModelHelper.getMainProcess(((DiagramEditor)part).getDiagramEditPart().resolveSemanticElement());
String partName = proc.getName() +" ("+proc.getVersion()+")";
for(IEditorReference ref : activePage.getEditorReferences()){
if(partName.equals(ref.getPartName())){
activePage.activate(ref.getPart(true));
break;
}
}
}
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
try{
activePage.showView("org.bonitasoft.studio.validation.view");
} catch (PartInitException e) {
BonitaStudioLog.error(e);
}
}
});
}
}
}
private boolean statusContainsError(IStatus validationStatus) {
if(validationStatus != null){
for(IStatus s : validationStatus.getChildren()){
if(s.getSeverity() == IStatus.WARNING || s.getSeverity() == IStatus.ERROR){
return true;
}
}
}
return false;
}
@Override
public boolean isEnabled() {
if(PlatformUI.isWorkbenchRunning() && PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null){
IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor() ;
return part instanceof DiagramEditor;
}
return false;
}
/*
* (non-Javadoc)
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
*/
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
if (PlatformUI.isWorkbenchRunning()) {
final Map<?, ?> parameters = event.getParameters();
final BatchValidationOperation validateOperation = new BatchValidationOperation();
if (parameters != null && !parameters.isEmpty()) {
String files = event.getParameter("diagrams");
if (files != null) {
final DiagramRepositoryStore store = RepositoryManager.getInstance().getRepositoryStore(DiagramRepositoryStore.class);
files = files.substring(1, files.length() - 1);
final String[] allFiles = files.split(",");
for (final String fileName : allFiles) {
final DiagramFileStore fileStore = store.getChild(fileName.trim());
if (fileStore != null) {
final Resource r = fileStore.getEMFResource();
for (final EObject eObject : r.getContents()) {
if (eObject instanceof Diagram) {
validateOperation.addDiagram((Diagram) eObject);
}
}
} else {
BonitaStudioLog.debug("Proc file : " + fileName.trim() + " not found in repository!", ValidationPlugin.PLUGIN_ID);
}
}
}
} else if (PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null
&& PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() != null) {
final IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (part instanceof DiagramEditor) {
final Resource resource = ((DiagramEditor) part).getDiagramEditPart().resolveSemanticElement().eResource();
if (resource != null) {
for (final EObject content : resource.getContents()) {
if (content instanceof Diagram) {
validateOperation.addDiagram((Diagram) content);
}
}
}
}
}
// if (!toValidate.isEmpty()) {
// final Resource eResource = toValidate.iterator().next().eResource();
// final IFile target = eResource != null ? WorkspaceSynchronizer.getFile(eResource) : null;
// if (target != null) {
// ProcessMarkerNavigationProvider.deleteMarkers(target);
// }
// }
final IProgressService service = PlatformUI.getWorkbench().getProgressService();
try {
service.run(true, false, validateOperation);
} catch (final InvocationTargetException e) {
BonitaStudioLog.error(e);
} catch (final InterruptedException e) {
BonitaStudioLog.error(e);
}
Object showReport = parameters.get("showReport");
if (showReport == null) {
showReport = Boolean.TRUE;
}
if (showReport instanceof Boolean) {
if (((Boolean) showReport).booleanValue()) {
showReport(validateOperation.getResult());
}
}
if (PlatformUI.isWorkbenchRunning() && PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null) {
final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
if (activePage != null) {
final IViewPart part = activePage.findView("org.bonitasoft.studio.validation.view");
if (part instanceof ValidationViewPart) {
((ValidationViewPart) part).refreshViewer();
}
}
}
return validateOperation.getResult();
} else {
return IStatus.OK;
}
}
private void showReport(final IStatus status) {
if (statusContainsError(status)) {
final String errorMessage = Messages.validationErrorFoundMessage + " "
+ PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getTitle();
final int result = new ValidationDialog(Display.getDefault().getActiveShell(), Messages.validationFailedTitle, errorMessage,
ValidationDialog.OK_SEEDETAILS).open();
if (result == ValidationDialog.SEE_DETAILS) {
final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
final IEditorPart part = activePage.getActiveEditor();
if (part != null && part instanceof DiagramEditor) {
final MainProcess proc = ModelHelper.getMainProcess(((DiagramEditor) part).getDiagramEditPart().resolveSemanticElement());
final String partName = proc.getName() + " (" + proc.getVersion() + ")";
for (final IEditorReference ref : activePage.getEditorReferences()) {
if (partName.equals(ref.getPartName())) {
activePage.activate(ref.getPart(true));
break;
}
}
}
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
try {
activePage.showView("org.bonitasoft.studio.validation.view");
} catch (final PartInitException e) {
BonitaStudioLog.error(e);
}
}
});
}
}
}
private boolean statusContainsError(final IStatus validationStatus) {
if (validationStatus != null) {
for (final IStatus s : validationStatus.getChildren()) {
if (s.getSeverity() == IStatus.WARNING || s.getSeverity() == IStatus.ERROR) {
return true;
}
}
}
return false;
}
@Override
public boolean isEnabled() {
return PlatformUI.isWorkbenchRunning();
}
}
......@@ -16,9 +16,11 @@ package org.bonitasoft.studio.validation;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.Map.Entry;
import org.bonitasoft.studio.common.log.BonitaStudioLog;
import org.bonitasoft.studio.model.form.Form;
......@@ -47,6 +49,8 @@ import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
/**
......@@ -54,7 +58,7 @@ import org.eclipse.ui.PlatformUI;
*/
public class BatchValidationOperation implements IRunnableWithProgress {
private Set<Diagram> toValidate;
private final Map<Diagram, DiagramEditPart> diagramsToDiagramEditPart = new HashMap<Diagram, DiagramEditPart>();
private final List<Shell> toDispose = new ArrayList<Shell>();
private final List<IFile> fileProcessed = new ArrayList<IFile>(); //Avoid duplicate
......@@ -64,63 +68,71 @@ public class BatchValidationOperation implements IRunnableWithProgress {
*/
@Override
public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
Assert.isNotNull(toValidate);
Assert.isLegal(!diagramsToDiagramEditPart.isEmpty());
monitor.beginTask(Messages.validating, IProgressMonitor.UNKNOWN);
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
if (!toValidate.isEmpty()) {
final Iterator<Diagram> it = toValidate.iterator();
clearMarkers(it);
clearMarkers();
for (final Entry<Diagram, DiagramEditPart> entry : diagramsToDiagramEditPart.entrySet()) {
final DiagramEditPart diagramEp = entry.getValue();