blob: 1428481b34a33d7fc90ed36538817b26a271ba3d [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2018, 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, Samina Kanwal and Muhammad Atif Javed
* Initial API and implementation and/or initial documentation
*******************************************************************************/
/**
*/
package org.eclipse.opencert.epf2OpenCert.transformation.popup.action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.opencert.epf2OpenCert.transformation.popup.action.ProcEvidGUI;
import org.eclipse.opencert.epf2OpenCert.transformation.popup.action.ProcEvidTransformation;
import org.eclipse.swt.widgets.Shell;
//import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
//import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
//import org.eclipse.opencert.sam.arg.arg.diagram.edit.parts.CaseEditPart;
//import org.eclipse.opencert.sam.arg.arg.diagram.edit.policies.CasePersistedCanonicalEditPolicy;
//import org.eclipse.opencert.sam.arg.arg.diagram.part.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collections;
//import java.util.LinkedList;
//import org.eclipse.core.commands.ExecutionException;
//import org.eclipse.core.commands.operations.OperationHistoryFactory;
//import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
//import org.eclipse.core.runtime.IAdaptable;
//import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
//import org.eclipse.core.runtime.NullProgressMonitor;
//import org.eclipse.core.runtime.Path;
import org.eclipse.emf.cdo.common.model.EMFUtil;
import org.eclipse.emf.cdo.dawn.preferences.PreferenceConstants;
import org.eclipse.emf.cdo.dawn.util.connection.CDOConnectionUtil;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.util.ConcurrentAccessException;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.common.util.URI;
//import org.eclipse.emf.common.util.WrappedException;
//import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.epf.uma.impl.ProcessComponentImpl;
public class ProcEvidTransformationHandler implements IObjectActionDelegate {
private Shell shell;
private IWorkbenchPage page;
private IWorkbenchWindow window;
private IProgressMonitor monitor2;
private String name;
/**
* Constructor for Action1.
*/
public ProcEvidTransformationHandler() {
super();
}
/**
* @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
*/
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
shell = targetPart.getSite().getShell();
page = targetPart.getSite().getPage();
window = targetPart.getSite().getWorkbenchWindow();
}
/**
* @see IActionDelegate#run(IAction)
*/
public void run(IAction action) {
MessageDialog dg = new MessageDialog(
shell,
"Select directory",
null,
"Please select the target assurance project from the CDO Repository",
MessageDialog.INFORMATION,
new String[]{
"Browse...",
IDialogConstants.CANCEL_LABEL},
-1//SWT.CLOSE
);
if(dg.open()==0){
//Connect to the CDO Repository
CDOConnectionUtil.instance.init(
PreferenceConstants.getRepositoryName(),
PreferenceConstants.getProtocol(),
PreferenceConstants.getServerName());
CDOSession session = CDOConnectionUtil.instance.getCurrentSession();
CDOView view = session.openView();
CDOTransaction transaction = session.openTransaction();
ProcEvidGUI gui = new ProcEvidGUI(shell,view);
gui.create();
gui.open();
String selection = gui.resourceName();
if(selection != null){
//Get the current delivery process
ISelection sel = page.getSelection();
TreeSelection tree = (TreeSelection) sel;
ProcessComponentImpl process = (ProcessComponentImpl) tree.getFirstElement();
name = process.getName();
//Invoke transformation
ProcEvidTransformation transformation = new ProcEvidTransformation(page);
ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
dialog.open();
IProgressMonitor monitor = dialog.getProgressMonitor();
monitor.beginTask("Generating the Process and Evidence models ... ", 15);
monitor.worked(1);
monitor.subTask("Performing the transformation...");
transformation.execute(monitor);
monitor.worked(5);
monitor.subTask("Creating CDO Resources...");
//Create the model CDO Resource
CDOResource procModel;
if (transaction.getOrCreateResource("/"+selection+"/PROCESSES/"+name+".process").isExisting()) {
try {
transaction.getResource("/"+selection+"/PROCESSES/"+name+".process").delete(Collections.EMPTY_MAP);
transaction.commit();
} catch (IOException | CommitException e) {
e.printStackTrace();
}
procModel = transaction.getOrCreateResource("/"+selection+"/PROCESSES/"+name+".process");
}else{
procModel = transaction.getOrCreateResource("/"+selection+"/PROCESSES/"+name+".process");
}
String procModelFile = ResourcesPlugin.getWorkspace().getRoot().getLocation().toString()+"/Process/"+name+".process";
URI procXmiUri = URI.createFileURI(procModelFile);
URI procCDOUri = procModel.getURI();
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
Resource procXmiResource1 = resourceSet.createResource(procXmiUri);
File newFile = new File(procModelFile);
monitor.worked(6);
monitor.subTask("Loading model to CDO Repository...");
//Load the model contents into the created resource
try {
FileInputStream procFileInStream = new FileInputStream(newFile);
procXmiResource1.load(procFileInStream,Collections.EMPTY_MAP);
procXmiResource1.setURI(procXmiUri);
} catch (IOException e) {
e.printStackTrace();
}
EMFUtil.safeResolveAll(resourceSet);
CDOResource procCDOResource = transaction.getOrCreateResource(procCDOUri.path());
procCDOResource.getContents().addAll(procXmiResource1.getContents());
EMFUtil.safeResolveAll(resourceSet);
monitor.worked(7);
try {
procCDOResource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
e.printStackTrace();
}
monitor.worked(8);
monitor.subTask("Creating Evidence Model...");
monitor.worked(9);
monitor.subTask("Creating CDO Resources...");
//Create the model CDO Resource
CDOResource eviModel;
if (transaction.getOrCreateResource("/"+selection+"/EVIDENCE/"+name+".evidence").isExisting()) {
try {
transaction.getResource("/"+selection+"/EVIDENCE/"+name+".evidence").delete(Collections.EMPTY_MAP);
transaction.commit();
} catch (IOException | CommitException e) {
e.printStackTrace();
}
eviModel = transaction.getOrCreateResource("/"+selection+"/EVIDENCE/"+name+".evidence");
}else{
eviModel = transaction.getOrCreateResource("/"+selection+"/EVIDENCE/"+name+".evidence");
}
String eviModelFile = ResourcesPlugin.getWorkspace().getRoot().getLocation().toString()+"/Evidence/"+name+".evidence";
URI eviXmiUri = URI.createFileURI(eviModelFile);
URI eviCDOUri = eviModel.getURI();
ResourceSet resourceSet1 = new ResourceSetImpl();
resourceSet1.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
Resource eviXmiResource1 = resourceSet1.createResource(eviXmiUri);
try {
FileInputStream eviFileInStream = new FileInputStream(newFile);
eviXmiResource1.load(eviFileInStream,Collections.EMPTY_MAP);
eviXmiResource1.setURI(eviXmiUri);
} catch (IOException e) {
e.printStackTrace();
}
EMFUtil.safeResolveAll(resourceSet1);
CDOResource eviCDOResource = transaction.getOrCreateResource(eviCDOUri.path());
eviCDOResource.getContents().addAll(eviXmiResource1.getContents());
EMFUtil.safeResolveAll(resourceSet1);
//monitor2.worked(4);
try {
eviCDOResource.save(Collections.EMPTY_MAP);
} catch (IOException e) {
e.printStackTrace();
}
monitor.worked(13);
//Commit the transaction
try {
transaction.commit();
} catch (ConcurrentAccessException e) {
e.printStackTrace();
} catch (CommitException e) {
e.printStackTrace();
}
monitor.worked(14);
monitor.done();
dialog.close();
//if(transfDiagramOk == true){
try{
MessageDialog.openInformation(shell, "Transformation completed",
"The CACM Process and Evidence Models are generated under:\n\n"
+" - PROCESSES AND EVIDENCE folders of the project "+selection+" in the CDO Repository\n"
+" - project in the current workspace");
}catch(Exception e){
System.out.println("Hola");
}
}
ProgressMonitorDialog dialog2 = new ProgressMonitorDialog(shell);
dialog2.open();
monitor2 = dialog2.getProgressMonitor();
monitor2.beginTask("Opening Evidence Model... ", 6);
monitor2.worked(1);
//Change the perspective
if (PlatformUI.getWorkbench() != null) {
IPerspectiveDescriptor descriptor = window.getWorkbench()
.getPerspectiveRegistry().findPerspectiveWithId("org.eclipse.ui.resourcePerspective");
PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().setPerspective(descriptor);
}
monitor2.worked(2);
monitor2.worked(5);
monitor2.done();
dialog2.close();
}else{
MessageDialog.openError(shell, "Selection error", "An assurance project must be selected in order to perform the transformation");
}
}
/**
* @see IActionDelegate#selectionChanged(IAction, ISelection)
*/
public void selectionChanged(IAction action, ISelection selection) {
}
}