blob: d2bdbfc552f6b8834e74dac2601cead14bc457f7 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2017, MDH
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Faiz Ul Muram <faiz.ul.muram@mdh.se>
* Initial API and implementation and/or initial documentation
*******************************************************************************/
package org.eclipse.opencert.process2assurproj.assuranceproject.utils;
import java.util.HashMap;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.cdo.dawn.preferences.PreferenceConstants;
import org.eclipse.emf.cdo.dawn.ui.views.DawnExplorer;
import org.eclipse.emf.cdo.dawn.util.connection.CDOConnectionUtil;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.eresource.CDOResourceFolder;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
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.xmi.XMLResource;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.ui.util.EditUIUtil;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.opencert.apm.assurproj.assuranceproject.AssetsPackage;
import org.eclipse.opencert.apm.assurproj.assuranceproject.AssuranceProject;
import org.eclipse.opencert.apm.assurproj.assuranceproject.AssuranceprojectFactory;
import org.eclipse.opencert.apm.assurproj.assuranceproject.AssuranceprojectPackage;
import org.eclipse.opencert.process2assurproj.assuranceproject.utils.DawnArgDiagramUtil;
import org.eclipse.opencert.process2assurproj.assuranceproject.etl.EPF2ARGTransformHandler;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.PlatformUI;
//import org.eclipse.opencert.evm.evidspec.evidence.ArtefactModel;
//import org.eclipse.opencert.pam.procspec.process.ProcessModel;
import org.eclipse.opencert.sam.arg.arg.Case;
public class EPF2ARGUI extends Dialog {
private static final String EXTENSION_DIAGRAM = "_diagram";
private static final String ARGUMENTATION_Extension = ".arg";
private static final String ARGUMENTATION_Folder = "ARGUMENTATION";
private static final String IMPORT = "Generate";
private static final String CLOSE = "Close";
protected AssuranceprojectPackage dprojectPackage = AssuranceprojectPackage.eINSTANCE;
protected AssuranceprojectFactory dprojectFactory = dprojectPackage.getAssuranceprojectFactory();
CDOResourceFolder assuranceprojectFolder;
CDOResourceFolder argFolder;
private IEditorPart editor;
private URI resourceURI;
private static URI diagramURI;
private CDOSession sessionCDO = null;
private CDOTransaction transaction;
HashMap<Object, Object> options = new HashMap<Object, Object>();
private static final String UTF_8 = "UTF-8";
private Label methodLabel;
private Text methodText;
private Button methodButton;
private String methodPath;
private String methodDirectory;
private Label argLabel;
private Text argNameText;
private Label importedArgModelPath;
public EPF2ARGUI(Shell parentShell) {
super(parentShell);
options.put(XMLResource.OPTION_ENCODING, UTF_8);
editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().getActiveEditor();
resourceURI = EditUIUtil.getURI(editor.getEditorInput());
CDOConnectionUtil.instance.init(
PreferenceConstants.getRepositoryName(),
PreferenceConstants.getProtocol(),
PreferenceConstants.getServerName());
sessionCDO = CDOConnectionUtil.instance.getCurrentSession();
transaction = sessionCDO.openTransaction();
}
public EPF2ARGUI(IShellProvider parentShell) {
super(parentShell);
}
@Override
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
Button ok = getButton(IDialogConstants.OK_ID);
ok.setText(IMPORT);
setButtonLayoutData(ok);
Button cancel = getButton(IDialogConstants.CANCEL_ID);
cancel.setText(CLOSE);
setButtonLayoutData(cancel);
}
@Override
protected Control createDialogArea(final Composite parent) {
ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(
ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
final Composite contents = (Composite) super.createDialogArea(parent);
GridData contentsGridData = (GridData) contents.getLayoutData();
contentsGridData.horizontalAlignment = SWT.FILL;
contentsGridData.verticalAlignment = SWT.FILL;
createSourceDialogArea(contents, adapterFactory);
createTargetDialogArea(contents, adapterFactory);
return contents;
}
protected void createSourceDialogArea(final Composite contents,
ComposedAdapterFactory adapterFactory) {
final Composite sourceComposite = new Composite(contents, SWT.NONE);
{
GridData data = new GridData(SWT.FILL, SWT.FILL, false, true);
data.horizontalAlignment = SWT.END;
data.horizontalAlignment = SWT.FILL;
sourceComposite.setLayoutData(data);
GridLayout layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 5;
sourceComposite.setLayout(layout);
}
Group groupSource = new Group(sourceComposite, SWT.NULL);
groupSource.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
groupSource.setText("EPF Source Files");
GridLayout layoutNew = new GridLayout();
layoutNew.marginHeight = 10;
layoutNew.marginWidth = 10;
layoutNew.numColumns = 3;
groupSource.setLayout(layoutNew);
// Label Method
methodLabel = new Label(groupSource, SWT.NONE);
methodLabel.setText("EPF Delivery Process:");
GridData methodLabelGridData = new GridData();
methodLabelGridData.horizontalAlignment = SWT.FILL;
methodLabelGridData.verticalAlignment = SWT.FILL;
methodLabel.setLayoutData(methodLabelGridData);
// Text Method
methodText = new Text(groupSource, SWT.BORDER);
GridData methodTextGridData = new GridData();
methodTextGridData.grabExcessHorizontalSpace = true;
methodTextGridData.minimumWidth = 500;
methodTextGridData.horizontalAlignment = SWT.FILL;
methodTextGridData.verticalAlignment = SWT.FILL;
methodText.setLayoutData(methodTextGridData);
methodText.setEditable(true);
// Button Method
methodButton = new Button(groupSource, SWT.NONE);
methodButton.setText("Browse...");
methodButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
FileDialog dlg = new FileDialog(methodButton.getShell(), SWT.None);
// Set the initial filter path according
// to anything they've selected or typed in
dlg.setFilterPath(methodDirectory);
// Change the title bar text
dlg.setText("Select a EPF Delivery Process"); //Select a EPF Method File
String[] methodFilterExt = {"*.xml"};
dlg.setFilterExtensions(methodFilterExt);
// Calling open() will open and run the dialog.
// It will return the selected directory, or
// null if user cancels
// I hide below three lines and subsequent two are added
methodPath = dlg.open();
methodDirectory = dlg.getFilterPath();
String methodFileName = dlg.getFileName();
//////
if (methodPath != null) {
// Set the text box to the new selection
methodText.setText(methodPath);
setDefaultTargetModelNames(methodFileName);
}
}
});
}
protected void setDefaultTargetModelNames (final String fileName){
String defaultArgName="";
defaultArgName=fileName.substring(0, fileName.lastIndexOf('.')) + "_Arg";
argNameText.setText(defaultArgName);
}
protected void createTargetDialogArea(final Composite contents,
ComposedAdapterFactory adapterFactory) {
final Composite targetComposite = new Composite(contents, SWT.NONE);
{
GridData data = new GridData(SWT.FILL, SWT.FILL, false, true);
data.horizontalAlignment = SWT.END;
data.horizontalAlignment = SWT.FILL;
targetComposite.setLayoutData(data);
GridLayout layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 5;
targetComposite.setLayout(layout);
}
Group groupTarget = new Group(targetComposite, SWT.NULL);
groupTarget.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
groupTarget.setText("Imported Model");
GridLayout layoutNew = new GridLayout();
layoutNew.marginHeight = 10;
layoutNew.marginWidth = 10;
layoutNew.numColumns = 3;
groupTarget.setLayout(layoutNew);
Label modelLabel = new Label(groupTarget, SWT.NONE);
modelLabel.setText("");
GridData modelLabelGridData = new GridData();
modelLabelGridData.horizontalAlignment = SWT.FILL;
modelLabelGridData.verticalAlignment = SWT.FILL;
modelLabel.setLayoutData(modelLabelGridData);
Label nameLabel = new Label(groupTarget, SWT.NONE);
nameLabel.setText("Import using Name:");
GridData nameLabelGridData = new GridData();
nameLabelGridData.horizontalAlignment = SWT.FILL;
nameLabelGridData.verticalAlignment = SWT.FILL;
nameLabel.setLayoutData(nameLabelGridData);
Label importedLabel = new Label(groupTarget, SWT.NONE);
String projectPath = resourceURI.path().substring(0, resourceURI.path().lastIndexOf("/"));
projectPath=projectPath.substring(0, projectPath.lastIndexOf("/"));
importedLabel.setText("Generating Result" + projectPath);
GridData importedLabelGridData = new GridData();
importedLabelGridData.horizontalAlignment = SWT.FILL;
importedLabelGridData.verticalAlignment = SWT.FILL;
importedLabel.setLayoutData(importedLabelGridData);
argLabel = new Label(groupTarget, SWT.NONE);
argLabel.setText("ARGUMENTATION model:");
GridData argLabelGridData = new GridData();
argLabelGridData.horizontalAlignment = SWT.FILL;
argLabelGridData.verticalAlignment = SWT.FILL;
argLabel.setLayoutData(argLabelGridData);
// Argument Name Text
argNameText = new Text(groupTarget, SWT.BORDER);
GridData argNameTextGridData = new GridData();
argNameTextGridData.grabExcessHorizontalSpace = true;
argNameTextGridData.minimumWidth = 200;
argNameText.setLayoutData(argNameTextGridData);
argNameText.setEditable(true);
// Path of imported Argumentation model
importedArgModelPath = new Label(groupTarget, SWT.BORDER);
GridData importedArgModelPathGridData = new GridData();
importedArgModelPathGridData.grabExcessHorizontalSpace = true;
importedArgModelPathGridData.minimumWidth = 350;
importedArgModelPathGridData.horizontalAlignment = SWT.FILL;
importedArgModelPathGridData.verticalAlignment = SWT.FILL;
importedArgModelPath.setLayoutData(importedArgModelPathGridData);
}
// protected void setImportBottom(){
//if((configurationText.getText().length()==0) || (methodText.getText().length()==0)){
//}
//}
protected void setImportBottom(){
if((methodText.getText().length()==0)){
}
}
@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText("EPF Composer to Argumentation");
}
@Override
protected void okPressed() {
boolean allOK=true;
if(methodText.getText().length()==0){
MessageDialog.openError(getShell(), "EPF Process File",
"You must select the EPF process file.");
return;
}
ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
dialog.open();
IProgressMonitor monitor = dialog.getProgressMonitor();
monitor.beginTask("Generating the process-based arguments ... ", 5);
monitor.worked(1);
try {
runImportFromEPF(monitor, resourceURI);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
monitor.worked(5);
monitor.done();
dialog.close();
if(allOK){
MessageDialog.openInformation(getShell(), "Transformation completed", "The model and diagram are generated under the argumentation folder"); //Selected EPF files imported
getButton(IDialogConstants.OK_ID).setEnabled(false);
}
}
@Override
public boolean close() {
//Refresh the Repository explorer view
DawnExplorer repoView =null;
IViewReference viewReferences[] = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
for (int i = 0; i < viewReferences.length; i++) {
if ("org.eclipse.emf.cdo.dawn.ui.views.DawnExplorer".equals(viewReferences[i].getId())) {
repoView = (DawnExplorer)viewReferences[i].getView(false);
repoView.refreshViewer(false);
break;
}
}
if (!transaction.isClosed()) transaction.close();
return super.close();
}
private void runImportFromEPF(
final IProgressMonitor monitor, final URI projectModel) throws ExecutionException {
CDOResource assuranceprojectResource=null;
String path = projectModel.path();
path=path.substring(0, path.lastIndexOf("/"));
assuranceprojectFolder= transaction.getResourceFolder(path);
String argFolderPath = assuranceprojectFolder.getPath().substring(0, assuranceprojectFolder.getPath().lastIndexOf("/"));
argFolderPath=argFolderPath + "/" + ARGUMENTATION_Folder;
argFolder= transaction.getResourceFolder(argFolderPath);
//CDOResource targetResource = transaction.createResource(targetPath);
//String sourceDiagram= sourceResource.getPath() + "_diagram";*/
URI argModelURI=null;
URI diagramURI=null;
CDOResource argModelResource=null;
CDOResource argDiagramResource=null;
argModelResource = transaction.getOrCreateResource(argFolder.getPath() + "/" + argNameText.getText() + ARGUMENTATION_Extension); //.evidence
argModelURI = argModelResource.getURI();
argDiagramResource = transaction.getOrCreateResource(argFolder.getPath() + "/" + argNameText.getText() + ARGUMENTATION_Extension + EXTENSION_DIAGRAM);
diagramURI = argDiagramResource.getURI();
// end here
try {
synchronized (transaction)
{
EPF2ARGTransformHandler transfo = new EPF2ARGTransformHandler(assuranceprojectFolder, methodText.getText(), argModelResource, argDiagramResource, transaction); //URI diagram,
transfo.execute();
transaction.commit();
String argSubPath=argModelURI.path().substring(argModelURI.path().indexOf("/")+1);
argSubPath=argSubPath.substring(argSubPath.indexOf("/"));
importedArgModelPath.setText(argSubPath);
monitor.worked(4);
String argSubPath1=diagramURI.path().substring(diagramURI.path().indexOf("/")+1);
argSubPath1=argSubPath1.substring(argSubPath1.indexOf("/"));
importedArgModelPath.setText(argSubPath1);
}
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
String msgError="Problems occur while importing the process model:";
importedArgModelPath.setText(msgError + e.getMessage());
monitor.done();
}
try {
assuranceprojectResource = transaction.getResource(projectModel.path());
EList<EObject> contents = assuranceprojectResource.getContents();
EObject rootObject =contents.get(0);
AssuranceProject assurproj = (AssuranceProject)rootObject;
AssetsPackage assPackageconfig = null;
for (AssetsPackage assetPackage: assurproj.getAssetsPackage()) {
if(assetPackage.isIsActive()){
assPackageconfig=assetPackage;
}
}
CDOResource argResource = transaction.getResource(argModelURI.path());
EList<EObject> argContents = argResource.getContents();
EObject argRootObject =argContents.get(0);
Case argCase = (Case)argRootObject;
//assPackageconfig.getArgumentationModel().add(argCase);
// Save the contents of the resource to the file system.
assuranceprojectResource.save(options);
transaction.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//MessageDialog.openWarning(getShell(), "Models not Linked", "The generated models cannot be linked to the Assurance Project");
transaction.rollback();
monitor.done();
}
}
}