From: lempinen Date: Tue, 27 Nov 2012 06:41:35 +0000 (+0000) Subject: Migration for all imports: model, module and function library (fixes #3037) X-Git-Tag: simantics-1.10.1~103 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=930c129e1002c8fb550895e50c079dff55951964;p=simantics%2Fsysdyn.git Migration for all imports: model, module and function library (fixes #3037) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26453 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportFunctionLibrary.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportFunctionLibrary.java index 0784dd7c..5fe78d59 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportFunctionLibrary.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportFunctionLibrary.java @@ -39,7 +39,7 @@ import org.simantics.db.request.Read; import org.simantics.graph.representation.TransferableGraph1; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.ui.Activator; -import org.simantics.sysdyn.ui.handlers.imports.ImportFunctionLibrary; +import org.simantics.sysdyn.ui.utils.imports.ImportUtils; import org.simantics.ui.SimanticsUI; import org.simantics.ui.utils.ResourceAdaptionUtils; import org.simantics.utils.datastructures.Pair; @@ -85,7 +85,7 @@ public class ExportFunctionLibrary extends AbstractHandler { FileDialog fd = new FileDialog(shell, SWT.SAVE); fd.setText("Export Function Library"); fd.setFileName(name); - String path = Activator.getDefault().getPreferenceStore().getString(ImportFunctionLibrary.IMPORTFUNCTIONLIBRARYPATH); + String path = Activator.getDefault().getPreferenceStore().getString(ImportUtils.IMPORTFUNCTIONLIBRARYPATH); if(path.isEmpty() || !(new File(path).exists())) path = Platform.getLocation().toOSString(); fd.setFilterPath(path); @@ -95,7 +95,7 @@ public class ExportFunctionLibrary extends AbstractHandler { if(selected == null) return null; // Save location to preference store - Activator.getDefault().getPreferenceStore().setValue(ImportFunctionLibrary.IMPORTFUNCTIONLIBRARYPATH, (new File(selected)).getParent()); + Activator.getDefault().getPreferenceStore().setValue(ImportUtils.IMPORTFUNCTIONLIBRARYPATH, (new File(selected)).getParent()); // Asynchronously create the file using transferable graph SimanticsUI.getSession().asyncRequest(new ReadRequest() { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java index f67d8594..e0040a69 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModelHandler.java @@ -35,7 +35,7 @@ import org.simantics.graph.db.TransferableGraphSource; import org.simantics.graph.db.TransferableGraphs; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.ui.Activator; -import org.simantics.sysdyn.ui.handlers.imports.ImportModelHandler; +import org.simantics.sysdyn.ui.utils.imports.ImportUtils; import org.simantics.ui.SimanticsUI; import org.simantics.ui.utils.ResourceAdaptionUtils; @@ -81,7 +81,7 @@ public class ExportModelHandler extends AbstractHandler { FileDialog fd = new FileDialog(shell, SWT.SAVE); fd.setText("Export Model"); fd.setFileName(name); - String path = Activator.getDefault().getPreferenceStore().getString(ImportModelHandler.IMPORTMODELTPATH); + String path = Activator.getDefault().getPreferenceStore().getString(ImportUtils.IMPORTMODELTPATH); if(path.isEmpty() || !(new File(path).exists())) path = Platform.getLocation().toOSString(); fd.setFilterPath(path); @@ -91,7 +91,7 @@ public class ExportModelHandler extends AbstractHandler { if(selected == null) return null; // Save location to preference store - Activator.getDefault().getPreferenceStore().setValue(ImportModelHandler.IMPORTMODELTPATH, (new File(selected)).getParent()); + Activator.getDefault().getPreferenceStore().setValue(ImportUtils.IMPORTMODELTPATH, (new File(selected)).getParent()); // Asynchronously create the file using transferable graph SimanticsUI.getSession().asyncRequest(new ReadRequest() { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModuleHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModuleHandler.java index 34958a0e..84449b76 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModuleHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/exports/ExportModuleHandler.java @@ -48,7 +48,7 @@ import org.simantics.modeling.ModelingResources; import org.simantics.structural.stubs.StructuralResource2; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.Activator; -import org.simantics.sysdyn.ui.handlers.imports.ImportModuleHandler; +import org.simantics.sysdyn.ui.utils.imports.ImportUtils; import org.simantics.ui.SimanticsUI; import org.simantics.ui.utils.ResourceAdaptionUtils; import org.simantics.utils.datastructures.Pair; @@ -151,7 +151,7 @@ public class ExportModuleHandler extends AbstractHandler { FileDialog fd = new FileDialog(shell, SWT.SAVE); fd.setText("Export Module"); fd.setFileName(name); - String path = Activator.getDefault().getPreferenceStore().getString(ImportModuleHandler.IMPORTMODULETPATH); + String path = Activator.getDefault().getPreferenceStore().getString(ImportUtils.IMPORTMODULETPATH); if(path.isEmpty() || !(new File(path).exists())) path = Platform.getLocation().toOSString(); fd.setFilterPath(path); @@ -161,7 +161,7 @@ public class ExportModuleHandler extends AbstractHandler { if(selected == null) return null; // Save location to preference store - Activator.getDefault().getPreferenceStore().setValue(ImportModuleHandler.IMPORTMODULETPATH, (new File(selected)).getParent()); + Activator.getDefault().getPreferenceStore().setValue(ImportUtils.IMPORTMODULETPATH, (new File(selected)).getParent()); // Asynchronously create the file using transferable graph SimanticsUI.getSession().asyncRequest(new ReadRequest() { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportFunctionLibrary.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportFunctionLibrary.java index 00ad8c36..5eb21b0a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportFunctionLibrary.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportFunctionLibrary.java @@ -12,46 +12,26 @@ package org.simantics.sysdyn.ui.handlers.imports; import java.io.File; -import java.io.IOException; -import java.util.HashMap; 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.core.runtime.Platform; +import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; -import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.handlers.HandlerUtil; -import org.simantics.databoard.Bindings; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.container.DataContainer; -import org.simantics.databoard.container.DataContainers; -import org.simantics.databoard.container.DataFormatException; -import org.simantics.databoard.container.FormatHandler; -import org.simantics.db.ReadGraph; +import org.simantics.DatabaseJob; import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.WriteOnlyGraph; -import org.simantics.db.common.request.WriteRequest; -import org.simantics.db.common.utils.NameUtils; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.exception.ResourceNotFoundException; -import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; -import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; -import org.simantics.db.request.Read; -import org.simantics.graph.representation.Root; -import org.simantics.graph.representation.TransferableGraph1; -import org.simantics.layer0.Layer0; -import org.simantics.layer0.utils.direct.GraphUtils; -import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.Activator; import org.simantics.sysdyn.ui.browser.nodes.FunctionsFolder; -import org.simantics.ui.SimanticsUI; -import org.simantics.ui.utils.AdaptionUtils; +import org.simantics.sysdyn.ui.utils.imports.ImportUtils; import org.simantics.ui.utils.ResourceAdaptionUtils; +import org.simantics.utils.ui.AdaptionUtils; /** * Imports an exported function library (or shared function library) transferable graph. @@ -60,8 +40,6 @@ import org.simantics.ui.utils.ResourceAdaptionUtils; */ public class ImportFunctionLibrary extends AbstractHandler { - public static String IMPORTFUNCTIONLIBRARYPATH = "IMPORT_FUNCTION_LIBRARY_PATH"; - /** * Called from a functions folder node */ @@ -83,203 +61,28 @@ public class ImportFunctionLibrary extends AbstractHandler { FileDialog fd = new FileDialog(shell, SWT.OPEN); fd.setText("Import Function Library"); - String path = Activator.getDefault().getPreferenceStore().getString(IMPORTFUNCTIONLIBRARYPATH); + String path = Activator.getDefault().getPreferenceStore().getString(ImportUtils.IMPORTFUNCTIONLIBRARYPATH); if(path.isEmpty() || !(new File(path).exists())) - path = Platform.getLocation().toOSString(); + path = Platform.getLocation().toOSString(); fd.setFilterPath(path); String[] filterExt = {"*.tg"}; fd.setFilterExtensions(filterExt); - String selected = fd.open(); + final String selected = fd.open(); if(selected == null) return null; - Activator.getDefault().getPreferenceStore().setValue(IMPORTFUNCTIONLIBRARYPATH, (new File(selected)).getParent()); - - // Ensure that shared functions ontology exists - ensureSharedOntologies(); - - // Handler for importing function libraries - HashMap> handlers = new HashMap>(); - handlers.put("sysdynFunctionLibrary:1", new FormatHandler() { - @Override - public Binding getBinding() { - return TransferableGraph1.BINDING; - } - - @Override - public Object process(DataContainer container) throws DataFormatException { - SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(functionLibrary); - try { - DefaultPasteHandler.defaultExecute((TransferableGraph1)container.content.getValue(), functionLibrary, ia); - return ia.getRoot(); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - }); - - // Read function library file - Object result = null; - try { - File file = new File(selected); - if(!file.isFile()) { - displayErrorMessage(shell, "File " + selected + " not found"); - return null; - } else { - result = DataContainers.readFile(file, handlers); - } - } catch(DataFormatException e) { - e.printStackTrace(); - } catch(IOException e) { - e.printStackTrace(); - } catch(Exception e) { - if(e instanceof RuntimeException) - throw (RuntimeException)e; - else - throw new RuntimeException(e); - } - - if(result == null || !(result instanceof Resource)) { - displayErrorMessage(shell, "Function library could not be imported."); - } else { - final Resource FunctionLibraryRoot = (Resource) result; - // Link the imported library to the selected resource (functionLibrary) - SimanticsUI.getSession().asyncRequest(new WriteRequest() { - - /** - * Link the imported library to the selected resource (functionLibrary) - * The imported library can be either SysdynModelicaFunctionLibrary or SysdynFunctionOntology - */ - @Override - public void perform(WriteGraph graph) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - // Case: SharedFunctionOntology. Link to SharedOntologies - if(graph.isInstanceOf(FunctionLibraryRoot, SysdynResource.getInstance(graph).SharedFunctionOntology)) { - Resource library = graph.getResource("http://SharedOntologies"); - if(!graph.hasStatement(library, l0.ConsistsOf, FunctionLibraryRoot)) { - graph.claim(library, l0.ConsistsOf, FunctionLibraryRoot); - } + Job job = new DatabaseJob("Import function") { - // Link model to the shared library - SysdynResource sr = SysdynResource.getInstance(graph); - Resource model = functionLibrary; - while(!graph.isInstanceOf(model, sr.SysdynModel) && graph.isInstanceOf(model, l0.Ontology)) - model = graph.getSingleObject(model, l0.PartOf); - if(graph.isInstanceOf(model, sr.SysdynModel)) { - graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, FunctionLibraryRoot); - } + @Override + protected IStatus run(IProgressMonitor monitor) { + IStatus status = ImportUtils.importFunctionLibrary(functionLibrary, selected, shell, monitor); + return status; + } + }; - // Case: not SharedFunctionOntology or SysdynModelicaFunctionLibrary. - } else if(!graph.isInstanceOf(FunctionLibraryRoot, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary)) { - Resource instanceOf = graph.getPossibleObject(FunctionLibraryRoot,l0.InstanceOf); - String type = "..."; - if(instanceOf != null) - type = NameUtils.getSafeName(graph, instanceOf); - else { - Resource inheritedFrom = graph.getPossibleObject(FunctionLibraryRoot, l0.Inherits); - if(inheritedFrom != null) - type = NameUtils.getSafeName(graph, inheritedFrom); - } - final String ft = type; + job.setUser(true); + job.schedule(); - // Remove the functionLibrary ConsistsOf root relation - graph.deny(FunctionLibraryRoot, l0.PartOf); - - // Display error message - shell.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - displayErrorMessage(shell, "The imported file is not of type: Function Library (" + ft +")"); - } - }); - } - - } - }); - } return null; } - /** - * Import advisor for importing function libraries to SysDyn - * - * @author Teemu Lempinen - * - */ - private class SysdynFunctionLibraryImportAdvisor extends DefaultPasteImportAdvisor { - - public SysdynFunctionLibraryImportAdvisor(Resource library) { - super(library); - } - - @Override - public void analyzeType(ReadGraph graph, Root root) throws DatabaseException { - // Change the library to http://SharedOntologies, if the imported library is of type SharedFunctionOntology - if(root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) { - try { - library = graph.getResource("http://SharedOntologies"); - } catch (ResourceNotFoundException e) { - e.printStackTrace(); - } - } - } - - @Override - public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException { - Layer0 l0 = graph.getService(Layer0.class); - this.root = graph.newResource(); - graph.claim(library, l0.ConsistsOf, l0.PartOf, this.root); - String name = root.name; - String newName = nameMappings.get(name); - graph.addLiteral(this.root, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING); - return this.root; - - } - - } - - private void ensureSharedOntologies() { - // Make sure the "http://SharedOntologies resource exists - try { - Boolean hasSharedOntologies; - hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read() { - - @Override - public Boolean perform(ReadGraph graph) throws DatabaseException { - try { - graph.getResource("http://SharedOntologies"); - } catch (ResourceNotFoundException e) { - return false; - } - return true; - } - }); - - if(!hasSharedOntologies) { - SimanticsUI.getSession().syncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - Layer0 l0 = Layer0.getInstance(graph); - GraphUtils.create2(graph, l0.Library, - l0.HasName, "SharedOntologies", - l0.PartOf, graph.getResource("http:/")); - } - }); - - } - } catch (DatabaseException e) { - e.printStackTrace(); - } - - } - - private void displayErrorMessage(Shell shell, String message) { - MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); - mb.setText("Error"); - mb.setMessage("The imported file is not of type: Model"); - mb.open(); - } - } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModelHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModelHandler.java index 99cbbeaa..f08a7b76 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModelHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModelHandler.java @@ -12,13 +12,6 @@ package org.simantics.sysdyn.ui.handlers.imports; import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; @@ -26,50 +19,14 @@ import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; -import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.SubMonitor; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.handlers.HandlerUtil; import org.simantics.DatabaseJob; -import org.simantics.Simantics; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.container.DataContainer; -import org.simantics.databoard.container.DataContainers; -import org.simantics.databoard.container.DataFormatException; -import org.simantics.databoard.container.FormatHandler; -import org.simantics.db.Issue; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.Session; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.ObjectsWithType; -import org.simantics.db.common.request.WriteResultRequest; -import org.simantics.db.common.utils.NameUtils; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; -import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; -import org.simantics.db.layer0.migration.MigrationState; -import org.simantics.db.layer0.migration.MigrationStateKeys; -import org.simantics.db.layer0.migration.MigrationStep; -import org.simantics.db.layer0.migration.MigrationUtils; -import org.simantics.db.layer0.util.RemoverUtil; -import org.simantics.db.request.Read; -import org.simantics.graph.representation.TransferableGraph1; -import org.simantics.issues.common.AllBatchIssueSources; -import org.simantics.issues.common.BatchIssueSource; -import org.simantics.issues.ontology.IssueResource; -import org.simantics.layer0.Layer0; -import org.simantics.layer0.utils.direct.GraphUtils; -import org.simantics.modeling.utils.BatchValidations; -import org.simantics.operation.Layer0X; -import org.simantics.simulation.ontology.SimulationResource; -import org.simantics.structural.stubs.StructuralResource2; -import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.Activator; -import org.simantics.ui.SimanticsUI; +import org.simantics.sysdyn.ui.utils.imports.ImportUtils; /** * Imports models from exported transferable graph files. @@ -79,8 +36,6 @@ import org.simantics.ui.SimanticsUI; */ public class ImportModelHandler extends AbstractHandler { - public static String IMPORTMODELTPATH = "IMPORT_MODEL_PATH"; - @Override public Object execute(ExecutionEvent event) throws ExecutionException { @@ -89,7 +44,7 @@ public class ImportModelHandler extends AbstractHandler { FileDialog fd = new FileDialog(shell, SWT.OPEN); fd.setText("Import Model"); - String path = Activator.getDefault().getPreferenceStore().getString(IMPORTMODELTPATH); + String path = Activator.getDefault().getPreferenceStore().getString(ImportUtils.IMPORTMODELTPATH); if(path.isEmpty() || !(new File(path).exists())) path = Platform.getLocation().toOSString(); fd.setFilterPath(path); @@ -102,7 +57,7 @@ public class ImportModelHandler extends AbstractHandler { @Override protected IStatus run(IProgressMonitor monitor) { - IStatus status = importFile(selected, shell, monitor); + IStatus status = ImportUtils.importModelFile(selected, shell, monitor); return status; } }; @@ -112,289 +67,4 @@ public class ImportModelHandler extends AbstractHandler { return null; } - - private IStatus importFile(String path, final Shell shell, final IProgressMonitor monitor) { - final Resource project = SimanticsUI.getProject().get(); - if(project == null) return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model: project not found", null); - - monitor.beginTask("Import model", 7); - - Activator.getDefault().getPreferenceStore().setValue(IMPORTMODELTPATH, (new File(path)).getParent()); - - - - HashMap> handlers = new HashMap>(); - handlers.put("sysdynModel:1", new FormatHandler() { - @Override - public Binding getBinding() { - return TransferableGraph1.BINDING; - } - - @Override - public Object process(DataContainer container) throws DataFormatException { - DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(project); - - try { - monitor.subTask("Migrate"); - Session session = Simantics.getSession(); - TransferableGraph1 tg = (TransferableGraph1)container.content.getValue(); - - MigrationStep step = MigrationUtils.getStep(session, SysdynResource.URIs.from1$6to1$7); - MigrationState state = MigrationUtils.newState(); - state.setProperty(MigrationStateKeys.CURRENT_TG, tg); - step.applyTo(session, state); - tg = state.getProperty(MigrationStateKeys.CURRENT_TG); - - monitor.worked(1); - monitor.subTask("Import"); - DefaultPasteHandler.defaultExecute(tg, project, ia); - monitor.worked(1); - return ia.getRoot(); - - } catch (DatabaseException e) { - e.printStackTrace(); - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed", e); - } catch (Exception e) { - e.printStackTrace(); - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed", e); - } - } - }); - - Object result = null; - try { - File file = new File(path); - if(!file.isFile()) { -// displayErrorMessage(shell, "File " + path + " not found"); - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: File " + path + " not found", null); - } else { - result = DataContainers.readFile(file, handlers); - } - } catch(DataFormatException e) { - e.printStackTrace(); - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", e); - } catch(IOException e) { - e.printStackTrace(); - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", e); - } catch(Exception e) { - if(e instanceof RuntimeException) { - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", e); -// throw (RuntimeException)e; - } else { - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", e); -// throw new RuntimeException(e); - } - } - - if(result == null || !(result instanceof Resource)) { -// displayErrorMessage(shell, "Model could not be imported."); - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", null); - } else { - try { - final Resource ModelRoot = (Resource) result; - IStatus status = SimanticsUI.getSession().syncRequest(new WriteResultRequest() { - - @Override - public IStatus perform(WriteGraph graph) throws DatabaseException { - if(!graph.isInstanceOf(ModelRoot, SysdynResource.getInstance(graph).SysdynModel)) { - // Imported model was not a SysdynModel, display error message. - Resource instanceOf = graph.getPossibleObject(ModelRoot, Layer0.getInstance(graph).InstanceOf); - String type = "..."; - if(instanceOf != null) - type = NameUtils.getSafeName(graph, instanceOf); - else { - Resource inheritedFrom = graph.getPossibleObject(ModelRoot, Layer0.getInstance(graph).Inherits); - if(inheritedFrom != null) - type = NameUtils.getSafeName(graph, inheritedFrom); - } - final String ft = type; - graph.deny(ModelRoot, Layer0.getInstance(graph).PartOf); - -// shell.getDisplay().asyncExec(new Runnable() { -// -// @Override -// public void run() { -// displayErrorMessage(shell, "The imported file is not of type: System Dynamics Model (" + ft +")"); - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "The imported file is not of type: System Dynamics Model (" + ft +")", null); -// } -// }); - - } else { - monitor.subTask("Add required dependencies"); - addDependencies(graph, ModelRoot); - monitor.worked(1); - monitor.subTask("Remove unnecessary issue sources"); - removeIssueSourcesFromModules(graph, ModelRoot); - monitor.worked(1); - monitor.subTask("Add enumeration issue source"); - addEnumerationIssueSource(graph, ModelRoot); - monitor.worked(1); - monitor.subTask("Activate model"); - activateModel(graph, ModelRoot); - monitor.worked(1); - } - return null; - - } - - }); - - if(status != null) - return status; - - monitor.subTask("Validate model"); - Collection confs = SimanticsUI.getSession().syncRequest(new Read>() { - - @Override - public Collection perform(ReadGraph graph) throws DatabaseException { - return findAllConfigurations(graph, ModelRoot); - } - - }); - - - // Batch validate all issue sources after import - try { - for(Resource conf : confs) { - /* - * This section is copied from BatchValidations.runAll. runAll did - * not find module types to validate. - */ -// BatchValidations.runAll(null, mod); - - final Session session = Simantics.getSession(); - final Collection validations = session.sync( new AllBatchIssueSources(ModelRoot) ); - SubMonitor progress = SubMonitor.convert(monitor, "Validate Model", 100); - Collection contexts = Collections.singletonList(conf); - for(BatchIssueSource bis : validations) { - Map> is = BatchValidations.validate(progress.newChild(90, SubMonitor.SUPPRESS_NONE), bis, contexts); - BatchValidations.store(progress.newChild(10, SubMonitor.SUPPRESS_NONE), bis.getResource(), is); - } - } - } catch(Throwable t) { - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", t); - } - - monitor.worked(1); - - } catch (DatabaseException e) { - e.printStackTrace(); - return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", e); - } - - return Status.OK_STATUS; - } - } - - protected void removeIssueSourcesFromModules(WriteGraph graph, Resource modelRoot) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - IssueResource ISSUE = IssueResource.getInstance(graph); - - for(Resource m : findAllModelsAndModules(graph, modelRoot)) { - if(m.equals(modelRoot)) - continue; - - // Module may contain issue sources due to legacy reasons. Remove these. - for(Resource issueSource : graph.syncRequest(new ObjectsWithType(m, L0.ConsistsOf, ISSUE.IssueSource))) { - // Issues are created to virtual graphs, so they should not need to be removed. Just in case they are created, remove them first - for(Resource issue : graph.getObjects(issueSource, ISSUE.IssueSource_Manages)) { - RemoverUtil.remove(graph, issue); - } - - // finally remove the issue source - RemoverUtil.remove(graph, issueSource); - } - } - - } - - protected void activateModel(WriteGraph graph, Resource modelRoot) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - Layer0X L0X = Layer0X.getInstance(graph); - if(!graph.hasStatement(modelRoot, L0X.IsActivatedBy)) { - Resource project = graph.getPossibleObject(modelRoot, L0.PartOf); - if(project != null) { - graph.claim(modelRoot, L0X.IsActivatedBy, project); - } - } - } - - protected void addDependencies(WriteGraph graph, Resource modelRoot) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - - ArrayList links = new ArrayList(); - for(Resource link : graph.getObjects(modelRoot, L0.IsLinkedTo)) - links.add(graph.getURI(link)); - - - if(!links.contains("http://www.simantics.org/Documentation-1.1")) { - Resource documentation = graph.getPossibleResource("http://www.simantics.org/Documentation-1.1"); - if(documentation != null) - graph.claim(modelRoot, L0.IsLinkedTo, documentation); - } - - if(!links.contains("http://www.simantics.org/SelectionView-1.2")) { - Resource selectionView = graph.getPossibleResource("http://www.simantics.org/SelectionView-1.2"); - if(selectionView != null) - graph.claim(modelRoot, L0.IsLinkedTo, selectionView); - } - - if(!links.contains("http://www.simantics.org/DocumentWorkbench-1.0")) { - Resource documentWb = graph.getPossibleResource("http://www.simantics.org/DocumentWorkbench-1.0"); - if(documentWb != null) - graph.claim(modelRoot, L0.IsLinkedTo, documentWb); - } - } - - protected void addEnumerationIssueSource(WriteGraph graph, Resource modelRoot) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - Layer0X L0X = Layer0X.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - Collection enumerationIssueSources = graph.syncRequest(new ObjectsWithType(modelRoot, L0.ConsistsOf, sr.Validations_Enumerations_EnumerationIssueSource)); - if(enumerationIssueSources.isEmpty()) { - GraphUtils.create2(graph, - sr.Validations_Enumerations_EnumerationIssueSource, - L0X.IsActivatedBy, modelRoot, - L0.PartOf, modelRoot - ); - } } - - private Collection findAllConfigurations(ReadGraph graph, Resource modelRoot) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - StructuralResource2 SR2 = StructuralResource2.getInstance(graph); - Collection modulesAndModels = findAllModelsAndModules(graph, modelRoot); - Collection configurations = new ArrayList(); - - for(Resource r : modulesAndModels) { - if(graph.isInheritedFrom(r, sr.Module)) { - configurations.add(graph.getPossibleObject(r, SR2.IsDefinedBy)); - } else if(graph.isInstanceOf(r, sr.SysdynModel)) { - configurations.add(graph.getPossibleObject(modelRoot, SimulationResource.getInstance(graph).HasConfiguration)); - } - } - - return configurations; - } - - private Collection findAllModelsAndModules(ReadGraph graph, Resource modelRoot) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - Collection modelsAndModules = new ArrayList(); - modelsAndModules.add(modelRoot); - - for(Resource r : graph.getObjects(modelRoot, L0.ConsistsOf)) { - if(graph.isInheritedFrom(r, sr.Module)) { - modelsAndModules.add(r); - } - } - - return modelsAndModules; - } - -// private void displayErrorMessage(Shell shell, String message) { -// MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); -// mb.setText("Error"); -// mb.setMessage("The imported file is not of type: Model"); -// mb.open(); -// } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModuleHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModuleHandler.java index cf422425..9b7f5877 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModuleHandler.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/imports/ImportModuleHandler.java @@ -12,38 +12,25 @@ package org.simantics.sysdyn.ui.handlers.imports; import java.io.File; -import java.io.IOException; -import java.util.HashMap; 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.core.runtime.Platform; +import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; -import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.DatabaseJob; import org.simantics.browsing.ui.common.node.AbstractNode; -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.container.DataContainer; -import org.simantics.databoard.container.DataContainers; -import org.simantics.databoard.container.DataFormatException; -import org.simantics.databoard.container.FormatHandler; import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.WriteRequest; -import org.simantics.db.common.utils.NameUtils; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; -import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; -import org.simantics.graph.representation.TransferableGraph1; -import org.simantics.layer0.Layer0; -import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.Activator; -import org.simantics.ui.SimanticsUI; -import org.simantics.ui.utils.AdaptionUtils; +import org.simantics.sysdyn.ui.utils.imports.ImportUtils; +import org.simantics.utils.ui.AdaptionUtils; /** * Imports modules from exported transferable graph files. @@ -53,7 +40,6 @@ import org.simantics.ui.utils.AdaptionUtils; */ public class ImportModuleHandler extends AbstractHandler { - public static String IMPORTMODULETPATH = "IMPORT_MODULE_PATH"; @Override public Object execute(ExecutionEvent event) throws ExecutionException { @@ -71,104 +57,27 @@ public class ImportModuleHandler extends AbstractHandler { FileDialog fd = new FileDialog(shell, SWT.OPEN); fd.setText("Import Module"); - String path = Activator.getDefault().getPreferenceStore().getString(IMPORTMODULETPATH); + String path = Activator.getDefault().getPreferenceStore().getString(ImportUtils.IMPORTMODULETPATH); if(path.isEmpty() || !(new File(path).exists())) path = Platform.getLocation().toOSString(); fd.setFilterPath(path); String[] filterExt = {"*.tg"}; fd.setFilterExtensions(filterExt); - String selected = fd.open(); + final String selected = fd.open(); if(selected == null) return null; + + Job job = new DatabaseJob("Import model") { + + @Override + protected IStatus run(IProgressMonitor monitor) { + IStatus status = ImportUtils.importModuleFile(model, selected, shell, monitor); + return status; + } + }; + job.setUser(true); + job.schedule(); - Activator.getDefault().getPreferenceStore().setValue(IMPORTMODULETPATH, (new File(selected)).getParent()); - - // Handler for importing modules - HashMap> handlers = new HashMap>(); - handlers.put("sysdynModule:1", new FormatHandler() { - @Override - public Binding getBinding() { - return TransferableGraph1.BINDING; - } - - @Override - public Object process(DataContainer container) throws DataFormatException { - DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(model); - try { - DefaultPasteHandler.defaultExecute((TransferableGraph1)container.content.getValue(), model, ia); - return ia.getRoot(); - - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - }); - - // Read module file - Object result = null; - try { - File file = new File(selected); - if(!file.isFile()) { - displayErrorMessage(shell, "File " + selected + " not found"); - return null; - } else { - result = DataContainers.readFile(file, handlers); - } - } catch(DataFormatException e) { - e.printStackTrace(); - } catch(IOException e) { - e.printStackTrace(); - } catch(Exception e) { - if(e instanceof RuntimeException) - throw (RuntimeException)e; - else - throw new RuntimeException(e); - } - - - if(result == null || !(result instanceof Resource)) { - displayErrorMessage(shell, "Module could not be imported."); - } else { - final Resource ModuleRoot = (Resource) result; - // Check that the imported file actually was a module. Display error message otherwise. - SimanticsUI.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - if(!graph.isInheritedFrom(ModuleRoot, SysdynResource.getInstance(graph).Module)) { - Resource instanceOf = graph.getPossibleObject(ModuleRoot, Layer0.getInstance(graph).InstanceOf); - String type = "..."; - if(instanceOf != null) - type = NameUtils.getSafeName(graph, instanceOf); - else { - Resource inheritedFrom = graph.getPossibleObject(ModuleRoot, Layer0.getInstance(graph).Inherits); - if(inheritedFrom != null) - type = NameUtils.getSafeName(graph, inheritedFrom); - } - final String ft = type; - graph.deny(ModuleRoot, Layer0.getInstance(graph).PartOf); - - shell.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - displayErrorMessage(shell, "The imported file is not of type: System Dynamics Module (" + ft +")"); - } - }); - } - } - }); - - } return null; } - - - private void displayErrorMessage(Shell shell, String message) { - MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ERROR); - mb.setText("Error"); - mb.setMessage("The imported file is not of type: Model"); - mb.open(); - } - + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/imports/ImportUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/imports/ImportUtils.java new file mode 100644 index 00000000..82416ea8 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/imports/ImportUtils.java @@ -0,0 +1,529 @@ +/******************************************************************************* + * Copyright (c) 2012 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.utils.imports; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.SubMonitor; +import org.eclipse.swt.widgets.Shell; +import org.simantics.Simantics; +import org.simantics.databoard.container.DataContainers; +import org.simantics.databoard.container.DataFormatException; +import org.simantics.databoard.container.FormatHandler; +import org.simantics.db.Issue; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.request.WriteResultRequest; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ResourceNotFoundException; +import org.simantics.db.layer0.util.RemoverUtil; +import org.simantics.db.request.Read; +import org.simantics.issues.common.AllBatchIssueSources; +import org.simantics.issues.common.BatchIssueSource; +import org.simantics.issues.ontology.IssueResource; +import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.modeling.utils.BatchValidations; +import org.simantics.operation.Layer0X; +import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.Activator; +import org.simantics.ui.SimanticsUI; + +/** + * Utilities for importing tg files: Models, Modules and Function libraries + * @author Teemu Lempinen + * + */ +public class ImportUtils { + + public static String IMPORTMODELTPATH = "IMPORT_MODEL_PATH"; + public static String IMPORTMODULETPATH = "IMPORT_MODULE_PATH"; + public static String IMPORTFUNCTIONLIBRARYPATH = "IMPORT_FUNCTION_LIBRARY_PATH"; + + /** + * Null-checked begin task for monitor + * @param monitor + * @param taskName + * @param totalWork + */ + private static void beginTask(IProgressMonitor monitor, String taskName, int totalWork) { + if(monitor != null) + monitor.beginTask(taskName, totalWork); + } + + /** + * Null-checked sub task for monitor + * @param monitor + * @param taskName + */ + private static void subTask(IProgressMonitor monitor, String taskName) { + if(monitor != null) + monitor.subTask(taskName); + } + + /** + * Null-checked worked command for monitor + * @param monitor + * @param work + */ + private static void worked(IProgressMonitor monitor, int work) { + if(monitor != null) + monitor.worked(work); + } + + + /** + * Read a file from hard drive using handlers + * @param path + * @param handlers + * @return IStatus if import failed, otherwise the result is determined by handlers + */ + public static Object readFile(String path, HashMap> handlers) { + Object result = null; + try { + File file = new File(path); + if(!file.isFile()) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import failed: File " + path + " not found", null); + } else { + result = DataContainers.readFile(file, handlers); + } + } catch(DataFormatException e) { + e.printStackTrace(); + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import failed", e); + } catch(IOException e) { + e.printStackTrace(); + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import failed", e); + } catch(Exception e) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import failed", e); + } + return result; + } + + + + /* ********************************* */ + /* ******* IMPORT MODEL ********* */ + /* ********************************* */ + public static IStatus importModelFile(String path, final Shell shell, final IProgressMonitor monitor) { + final Resource project = SimanticsUI.getProject().get(); + if(project == null) return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model: project not found", null); + + beginTask(monitor, "Import model", 7); + + Activator.getDefault().getPreferenceStore().setValue(IMPORTMODELTPATH, (new File(path)).getParent()); + + + + HashMap> handlers = new HashMap>(); + handlers.put("sysdynModel:1", new SysdynImportFormatHandler(project, SysdynResource.URIs.from1$6to1$7, monitor)); + + Object result = ImportUtils.readFile(path, handlers); + + if(result == null || !(result instanceof Resource)) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", null); + } else { + try { + final Resource ModelRoot = (Resource) result; + IStatus status = SimanticsUI.getSession().syncRequest(new WriteResultRequest() { + + @Override + public IStatus perform(WriteGraph graph) throws DatabaseException { + if(!graph.isInstanceOf(ModelRoot, SysdynResource.getInstance(graph).SysdynModel)) { + // Imported model was not a SysdynModel, display error message. + Resource instanceOf = graph.getPossibleObject(ModelRoot, Layer0.getInstance(graph).InstanceOf); + String type = "..."; + if(instanceOf != null) + type = NameUtils.getSafeName(graph, instanceOf); + else { + Resource inheritedFrom = graph.getPossibleObject(ModelRoot, Layer0.getInstance(graph).Inherits); + if(inheritedFrom != null) + type = NameUtils.getSafeName(graph, inheritedFrom); + } + final String ft = type; + graph.deny(ModelRoot, Layer0.getInstance(graph).PartOf); + + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "The imported file is not of type: System Dynamics Model (" + ft +")", null); + } else { + subTask(monitor, "Add required dependencies"); + addDependencies(graph, ModelRoot); + worked(monitor, 1); + subTask(monitor, "Remove unnecessary issue sources"); + removeIssueSourcesFromModules(graph, ModelRoot); + worked(monitor, 1); + subTask(monitor, "Add enumeration issue source"); + addEnumerationIssueSource(graph, ModelRoot); + worked(monitor, 1); + subTask(monitor, "Activate model"); + activateModel(graph, ModelRoot); + worked(monitor, 1); + } + return null; + + } + + }); + + if(status != null) + return status; + + subTask(monitor, "Validate model"); + Collection confs = SimanticsUI.getSession().syncRequest(new Read>() { + + @Override + public Collection perform(ReadGraph graph) throws DatabaseException { + return findAllConfigurations(graph, ModelRoot); + } + + }); + + + // Batch validate all issue sources after import + try { + for(Resource conf : confs) { + /* + * This section is copied from BatchValidations.runAll. runAll did + * not find module types to validate. + */ +// BatchValidations.runAll(null, mod); + + final Session session = Simantics.getSession(); + final Collection validations = session.sync( new AllBatchIssueSources(ModelRoot) ); + SubMonitor progress = null; + if(monitor != null) + progress = SubMonitor.convert(monitor, "Validate Model", 100); + Collection contexts = Collections.singletonList(conf); + for(BatchIssueSource bis : validations) { + Map> is = BatchValidations.validate(monitor != null ? progress.newChild(90, SubMonitor.SUPPRESS_NONE) : null, bis, contexts); + BatchValidations.store(monitor != null ? progress.newChild(10, SubMonitor.SUPPRESS_NONE) : null, bis.getResource(), is); + } + } + } catch(Throwable t) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", t); + } + + worked(monitor, 1); + + } catch (DatabaseException e) { + e.printStackTrace(); + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import model failed: Model could not be imported.", e); + } + + return Status.OK_STATUS; + } + } + + private static void removeIssueSourcesFromModules(WriteGraph graph, Resource modelRoot) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + IssueResource ISSUE = IssueResource.getInstance(graph); + + for(Resource m : findAllModelsAndModules(graph, modelRoot)) { + if(m.equals(modelRoot)) + continue; + + // Module may contain issue sources due to legacy reasons. Remove these. + for(Resource issueSource : graph.syncRequest(new ObjectsWithType(m, L0.ConsistsOf, ISSUE.IssueSource))) { + // Issues are created to virtual graphs, so they should not need to be removed. Just in case they are created, remove them first + for(Resource issue : graph.getObjects(issueSource, ISSUE.IssueSource_Manages)) { + RemoverUtil.remove(graph, issue); + } + + // finally remove the issue source + RemoverUtil.remove(graph, issueSource); + } + } + + } + + private static void activateModel(WriteGraph graph, Resource modelRoot) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + Layer0X L0X = Layer0X.getInstance(graph); + if(!graph.hasStatement(modelRoot, L0X.IsActivatedBy)) { + Resource project = graph.getPossibleObject(modelRoot, L0.PartOf); + if(project != null) { + graph.claim(modelRoot, L0X.IsActivatedBy, project); + } + } + } + + private static void addDependencies(WriteGraph graph, Resource modelRoot) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + + ArrayList links = new ArrayList(); + for(Resource link : graph.getObjects(modelRoot, L0.IsLinkedTo)) + links.add(graph.getURI(link)); + + + if(!links.contains("http://www.simantics.org/Documentation-1.1")) { + Resource documentation = graph.getPossibleResource("http://www.simantics.org/Documentation-1.1"); + if(documentation != null) + graph.claim(modelRoot, L0.IsLinkedTo, documentation); + } + + if(!links.contains("http://www.simantics.org/SelectionView-1.2")) { + Resource selectionView = graph.getPossibleResource("http://www.simantics.org/SelectionView-1.2"); + if(selectionView != null) + graph.claim(modelRoot, L0.IsLinkedTo, selectionView); + } + + if(!links.contains("http://www.simantics.org/DocumentWorkbench-1.0")) { + Resource documentWb = graph.getPossibleResource("http://www.simantics.org/DocumentWorkbench-1.0"); + if(documentWb != null) + graph.claim(modelRoot, L0.IsLinkedTo, documentWb); + } + } + + private static void addEnumerationIssueSource(WriteGraph graph, Resource modelRoot) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + Layer0X L0X = Layer0X.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + Collection enumerationIssueSources = graph.syncRequest(new ObjectsWithType(modelRoot, L0.ConsistsOf, sr.Validations_Enumerations_EnumerationIssueSource)); + if(enumerationIssueSources.isEmpty()) { + GraphUtils.create2(graph, + sr.Validations_Enumerations_EnumerationIssueSource, + L0X.IsActivatedBy, modelRoot, + L0.PartOf, modelRoot + ); + } } + + private static Collection findAllConfigurations(ReadGraph graph, Resource modelRoot) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + StructuralResource2 SR2 = StructuralResource2.getInstance(graph); + Collection modulesAndModels = findAllModelsAndModules(graph, modelRoot); + Collection configurations = new ArrayList(); + + for(Resource r : modulesAndModels) { + if(graph.isInheritedFrom(r, sr.Module)) { + configurations.add(graph.getPossibleObject(r, SR2.IsDefinedBy)); + } else if(graph.isInstanceOf(r, sr.SysdynModel)) { + configurations.add(graph.getPossibleObject(modelRoot, SimulationResource.getInstance(graph).HasConfiguration)); + } + } + + return configurations; + } + + private static Collection findAllModelsAndModules(ReadGraph graph, Resource modelRoot) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + Collection modelsAndModules = new ArrayList(); + modelsAndModules.add(modelRoot); + + for(Resource r : graph.getObjects(modelRoot, L0.ConsistsOf)) { + if(graph.isInheritedFrom(r, sr.Module)) { + modelsAndModules.add(r); + } + } + + return modelsAndModules; + } + + /* ********************************* */ + /* ******* IMPORT MODULE ********* */ + /* ********************************* */ + public static IStatus importModuleFile(final Resource model, String path, final Shell shell, final IProgressMonitor monitor) { + beginTask(monitor, "Import Module", 3); + + + Activator.getDefault().getPreferenceStore().setValue(IMPORTMODULETPATH, (new File(path)).getParent()); + + // Handler for importing modules + HashMap> handlers = new HashMap>(); + handlers.put("sysdynModule:1", new SysdynImportFormatHandler(model, SysdynResource.URIs.from1$6to1$7, monitor)); + + // Read module file + Object result = ImportUtils.readFile(path, handlers); + + + if(result == null || !(result instanceof Resource)) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import module failed: Module could not be imported.", null); + } else { + final Resource ModuleRoot = (Resource) result; + // Check that the imported file actually was a module. Display error message otherwise. + try { + subTask(monitor, "Validate model"); + IStatus status = SimanticsUI.getSession().syncRequest(new WriteResultRequest() { + + @Override + public IStatus perform(WriteGraph graph) throws DatabaseException { + if(!graph.isInheritedFrom(ModuleRoot, SysdynResource.getInstance(graph).Module)) { + Resource instanceOf = graph.getPossibleObject(ModuleRoot, Layer0.getInstance(graph).InstanceOf); + String type = "..."; + if(instanceOf != null) + type = NameUtils.getSafeName(graph, instanceOf); + else { + Resource inheritedFrom = graph.getPossibleObject(ModuleRoot, Layer0.getInstance(graph).Inherits); + if(inheritedFrom != null) + type = NameUtils.getSafeName(graph, inheritedFrom); + } + final String ft = type; + graph.deny(ModuleRoot, Layer0.getInstance(graph).PartOf); + + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "The imported file is not of type: System Dynamics Module (" + ft +")", null); + } + return null; + } + }); + + if(status != null) + return status; + worked(monitor, 1); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + } + + return Status.OK_STATUS; + } + + + + /* ********************************* */ + /* *** IMPORT FUNCTION LIBRARY *** */ + /* ********************************* */ + + public static IStatus importFunctionLibrary(final Resource functionLibrary, final String path, final Shell shell, final IProgressMonitor monitor) { + beginTask(monitor, "Import Module", 3); + + Activator.getDefault().getPreferenceStore().setValue(IMPORTFUNCTIONLIBRARYPATH, (new File(path)).getParent()); + + // Ensure that shared functions ontology exists + ensureSharedOntologies(); + + // Handler for importing function libraries + HashMap> handlers = new HashMap>(); + handlers.put("sysdynFunctionLibrary:1", new SysdynImportFormatHandler(functionLibrary, SysdynResource.URIs.from1$6to1$7, monitor, new SysdynFunctionLibraryImportAdvisor(functionLibrary))); + + // Read function library file + Object result = ImportUtils.readFile(path, handlers); + + if(result == null || !(result instanceof Resource)) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import Function library failed: Function library not be imported.", null); + } else { + final Resource FunctionLibraryRoot = (Resource) result; + // Link the imported library to the selected resource (functionLibrary) + try { + subTask(monitor, "Validate model"); + IStatus status = SimanticsUI.getSession().syncRequest(new WriteResultRequest() { + + /** + * Link the imported library to the selected resource (functionLibrary) + * The imported library can be either SysdynModelicaFunctionLibrary or SysdynFunctionOntology + */ + @Override + public IStatus perform(WriteGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + // Case: SharedFunctionOntology. Link to SharedOntologies + if(graph.isInstanceOf(FunctionLibraryRoot, SysdynResource.getInstance(graph).SharedFunctionOntology)) { + Resource library = graph.getResource("http://SharedOntologies"); + if(!graph.hasStatement(library, l0.ConsistsOf, FunctionLibraryRoot)) { + graph.claim(library, l0.ConsistsOf, FunctionLibraryRoot); + } + + // Link model to the shared library + SysdynResource sr = SysdynResource.getInstance(graph); + Resource model = functionLibrary; + while(!graph.isInstanceOf(model, sr.SysdynModel) && graph.isInstanceOf(model, l0.Ontology)) + model = graph.getSingleObject(model, l0.PartOf); + if(graph.isInstanceOf(model, sr.SysdynModel)) { + graph.claim(model, l0.IsLinkedTo, l0.IsLinkedTo_Inverse, FunctionLibraryRoot); + } + + // Case: not SharedFunctionOntology or SysdynModelicaFunctionLibrary. + } else if(!graph.isInstanceOf(FunctionLibraryRoot, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary)) { + Resource instanceOf = graph.getPossibleObject(FunctionLibraryRoot,l0.InstanceOf); + String type = "..."; + if(instanceOf != null) + type = NameUtils.getSafeName(graph, instanceOf); + else { + Resource inheritedFrom = graph.getPossibleObject(FunctionLibraryRoot, l0.Inherits); + if(inheritedFrom != null) + type = NameUtils.getSafeName(graph, inheritedFrom); + } + final String ft = type; + + // Remove the functionLibrary ConsistsOf root relation + graph.deny(FunctionLibraryRoot, l0.PartOf); + + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "The imported file is not of type: Function Library (" + ft +")", null); + } + return null; + + + } + }); + + if(status != null) + return status; + worked(monitor, 1); + + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + + return Status.OK_STATUS; + } + + private static void ensureSharedOntologies() { + // Make sure the "http://SharedOntologies resource exists + try { + Boolean hasSharedOntologies; + hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public Boolean perform(ReadGraph graph) throws DatabaseException { + try { + graph.getResource("http://SharedOntologies"); + } catch (ResourceNotFoundException e) { + return false; + } + return true; + } + }); + + if(!hasSharedOntologies) { + SimanticsUI.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + GraphUtils.create2(graph, l0.Library, + l0.HasName, "SharedOntologies", + l0.PartOf, graph.getResource("http:/")); + } + }); + + } + } catch (DatabaseException e) { + e.printStackTrace(); + } + + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/imports/SysdynFunctionLibraryImportAdvisor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/imports/SysdynFunctionLibraryImportAdvisor.java new file mode 100644 index 00000000..b23385af --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/imports/SysdynFunctionLibraryImportAdvisor.java @@ -0,0 +1,61 @@ +/******************************************************************************* + * Copyright (c) 2012 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.utils.imports; + +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteOnlyGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ResourceNotFoundException; +import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; +import org.simantics.graph.representation.Root; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.SysdynResource; + +/** + * Import advisor for importing function libraries to SysDyn + * + * @author Teemu Lempinen + * + */ +public class SysdynFunctionLibraryImportAdvisor extends DefaultPasteImportAdvisor { + + public SysdynFunctionLibraryImportAdvisor(Resource library) { + super(library); + } + + @Override + public void analyzeType(ReadGraph graph, Root root) throws DatabaseException { + // Change the library to http://SharedOntologies, if the imported library is of type SharedFunctionOntology + if(root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) { + try { + library = graph.getResource("http://SharedOntologies"); + } catch (ResourceNotFoundException e) { + e.printStackTrace(); + } + } + } + + @Override + public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException { + Layer0 l0 = graph.getService(Layer0.class); + this.root = graph.newResource(); + graph.claim(library, l0.ConsistsOf, l0.PartOf, this.root); + String name = root.name; + String newName = nameMappings.get(name); + graph.addLiteral(this.root, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING); + return this.root; + + } + +} \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/imports/SysdynImportFormatHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/imports/SysdynImportFormatHandler.java new file mode 100644 index 00000000..05d02714 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/imports/SysdynImportFormatHandler.java @@ -0,0 +1,107 @@ +/******************************************************************************* + * Copyright (c) 2012 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.utils.imports; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.simantics.Simantics; +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.container.DataContainer; +import org.simantics.databoard.container.DataFormatException; +import org.simantics.databoard.container.FormatHandler; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; +import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; +import org.simantics.db.layer0.migration.MigrationState; +import org.simantics.db.layer0.migration.MigrationStateKeys; +import org.simantics.db.layer0.migration.MigrationStep; +import org.simantics.db.layer0.migration.MigrationUtils; +import org.simantics.graph.representation.TransferableGraph1; +import org.simantics.sysdyn.ui.Activator; + +/** + * Handler for migrating a tg and importing it to database + * @author Teemu Lempinen + * + */ +public class SysdynImportFormatHandler implements FormatHandler { + + private Resource parent; + private IProgressMonitor monitor; + private String migrationURI; + private DefaultPasteImportAdvisor importAdvisor; + + /** + * Works 2 (2 * 1) in the monitor + * @param parent + * @param migrationURI + * @param monitor + */ + public SysdynImportFormatHandler(Resource parent, String migrationURI, IProgressMonitor monitor) { + this(parent, migrationURI, monitor, null); + } + + public SysdynImportFormatHandler(Resource parent, String migrationURI, IProgressMonitor monitor, + DefaultPasteImportAdvisor importAdvisor) { + assert(parent != null); + assert(migrationURI != null); + this.parent = parent; + this.monitor = monitor; + this.migrationURI = migrationURI; + this.importAdvisor = importAdvisor; + } + + private void subTask(String taskName) { + if(monitor != null) + monitor.subTask(taskName); + } + + private void worked(int work) { + if(monitor != null) + monitor.worked(work); + } + + @Override + public Binding getBinding() { + return TransferableGraph1.BINDING; + } + + @Override + public Object process(DataContainer container) throws DataFormatException { + DefaultPasteImportAdvisor ia = importAdvisor != null ? importAdvisor : new DefaultPasteImportAdvisor(parent); + try { + subTask("Migrate"); + Session session = Simantics.getSession(); + TransferableGraph1 tg = (TransferableGraph1)container.content.getValue(); + + MigrationStep step = MigrationUtils.getStep(session, migrationURI); + MigrationState state = MigrationUtils.newState(); + state.setProperty(MigrationStateKeys.CURRENT_TG, tg); + step.applyTo(session, state); + tg = state.getProperty(MigrationStateKeys.CURRENT_TG); + + worked(1); + subTask("Import"); + + DefaultPasteHandler.defaultExecute(tg, parent, ia); + worked(1); + + return ia.getRoot(); + + } catch (Exception e) { + return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Import module failed", e); + } + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/WizardFunctionsImportPage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/WizardFunctionsImportPage.java index ee679124..ee210702 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/WizardFunctionsImportPage.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/functions/WizardFunctionsImportPage.java @@ -1,9 +1,10 @@ package org.simantics.sysdyn.ui.wizards.functions; import java.io.File; -import java.io.IOException; +import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Status; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.PixelConverter; import org.eclipse.jface.viewers.ISelection; @@ -27,25 +28,12 @@ import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.Tree; import org.simantics.browsing.ui.swt.AdaptableHintContext; import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite; -import org.simantics.databoard.Bindings; -import org.simantics.databoard.Files; -import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.WriteOnlyGraph; -import org.simantics.db.common.request.WriteRequest; -import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.exception.ResourceNotFoundException; -import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; -import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; import org.simantics.db.request.Read; -import org.simantics.graph.representation.Root; -import org.simantics.graph.representation.TransferableGraph1; -import org.simantics.layer0.Layer0; -import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.utils.imports.ImportUtils; import org.simantics.ui.SimanticsUI; import org.simantics.utils.datastructures.ArrayMap; @@ -68,8 +56,6 @@ public class WizardFunctionsImportPage extends WizardPage{ GraphExplorerComposite functionLibraryExplorer; - private String error = ""; - private boolean selectionMade = false; /** @@ -294,7 +280,10 @@ public class WizardFunctionsImportPage extends WizardPage{ setErrorMessage("No file selected"); return false; } + + IStatus status = ImportUtils.importFunctionLibrary(selectedModel, selected, shell, null); + /* TransferableGraph1 tg = null; try { tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class)); @@ -396,9 +385,15 @@ public class WizardFunctionsImportPage extends WizardPage{ setErrorMessage("The imported file is not of type: Function Library (" + error +")"); error = ""; return false; + } + */ + if(status == null || !status.equals(Status.OK_STATUS)) { + setErrorMessage(status.getMessage()); + return false; } return true; } + /* private class SysdynFunctionLibraryImportAdvisor extends DefaultPasteImportAdvisor { @@ -430,6 +425,8 @@ public class WizardFunctionsImportPage extends WizardPage{ } } + */ + void validatePage() { if (previouslyBrowsedFile.isEmpty() || selectionMade == false){ diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/WizardModelsImportPage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/WizardModelsImportPage.java index 6de98d7a..3f041ee5 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/WizardModelsImportPage.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/models/WizardModelsImportPage.java @@ -1,11 +1,10 @@ package org.simantics.sysdyn.ui.wizards.models; import java.io.File; -import java.io.IOException; -import java.util.HashSet; -import java.util.UUID; +import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Status; import org.eclipse.jface.layout.PixelConverter; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.WizardPage; @@ -22,29 +21,10 @@ import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; -import org.simantics.databoard.Bindings; -import org.simantics.databoard.Files; -import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.ObjectsWithType; -import org.simantics.db.common.request.WriteRequest; -import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; -import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; -import org.simantics.graph.representation.TransferableGraph1; -import org.simantics.layer0.Layer0; -import org.simantics.layer0.utils.direct.GraphUtils; -import org.simantics.modeling.ModelingResources; -import org.simantics.operation.Layer0X; -import org.simantics.simulation.ontology.SimulationResource; -import org.simantics.spreadsheet.resource.SpreadsheetResource; -import org.simantics.structural.stubs.StructuralResource2; -import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.Activator; -import org.simantics.sysdyn.ui.handlers.imports.ImportModelHandler; -import org.simantics.sysdyn.ui.utils.OldTransferableGraph1; +import org.simantics.sysdyn.ui.utils.imports.ImportUtils; import org.simantics.ui.SimanticsUI; public class WizardModelsImportPage extends WizardPage{ @@ -59,8 +39,6 @@ public class WizardModelsImportPage extends WizardPage{ private Shell shell; - private String error = ""; - /** * Creates a new project creation wizard page. * @@ -182,7 +160,7 @@ public class WizardModelsImportPage extends WizardPage{ dialog.setText("Import Model"); - String path = Activator.getDefault().getPreferenceStore().getString(ImportModelHandler.IMPORTMODELTPATH); + String path = Activator.getDefault().getPreferenceStore().getString(ImportUtils.IMPORTMODELTPATH); if(path.isEmpty() || !(new File(path).exists())) path = Platform.getLocation().toOSString(); dialog.setFilterPath(path); @@ -204,12 +182,15 @@ public class WizardModelsImportPage extends WizardPage{ return false; } - String selected = previouslyBrowsedFile; + final String selected = previouslyBrowsedFile; if(selected == null){ setErrorMessage("No file selected"); return false; } + IStatus status = ImportUtils.importModelFile(selected, shell, null); + + /* TransferableGraph1 tg = null; try { tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class)); @@ -269,12 +250,17 @@ public class WizardModelsImportPage extends WizardPage{ } catch (Exception e) { e.printStackTrace(); } - + if (!error.isEmpty()){ setErrorMessage("The imported file is not of type: System Dynamics Model (" + error +")"); error = ""; return false; } + */ + if(status == null || !status.equals(Status.OK_STATUS)) { + setErrorMessage(status.getMessage()); + return false; + } return true; } @@ -283,6 +269,7 @@ public class WizardModelsImportPage extends WizardPage{ * @param graph WriteGraph * @param model Imported model */ + /* private static void updateOldConfigurationToBaseRealization(WriteGraph graph, Resource model) { Layer0X L0X = Layer0X.getInstance(graph); try { @@ -294,6 +281,7 @@ public class WizardModelsImportPage extends WizardPage{ } } + */ /** * Links should be exported and imported automatically. If it has failed, the @@ -302,7 +290,7 @@ public class WizardModelsImportPage extends WizardPage{ * @param graph WriteGraph * @param model Imported model */ - + /* private static void addDefaultOntologyLinks(WriteGraph graph, Resource model) { try { Layer0 l0 = Layer0.getInstance(graph); @@ -353,7 +341,7 @@ public class WizardModelsImportPage extends WizardPage{ e.printStackTrace(); } } - +*/ /** * Add a missing spreadsheet book to the model @@ -361,6 +349,7 @@ public class WizardModelsImportPage extends WizardPage{ * @param graph * @param model */ + /* private static void addSpreadSheetBook(WriteGraph graph, Resource model) { try { Layer0 l0 = Layer0.getInstance(graph); @@ -379,7 +368,7 @@ public class WizardModelsImportPage extends WizardPage{ e.printStackTrace(); } } - + */ /** * Create a sheet (Copied from SysdynProject) * @@ -391,6 +380,7 @@ public class WizardModelsImportPage extends WizardPage{ * @return * @throws DatabaseException */ + /* private static Resource createSheet(WriteGraph graph, Resource book, String name, String[] colNames, int[] colWidths) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); @@ -443,7 +433,7 @@ public class WizardModelsImportPage extends WizardPage{ return result; } - + */ void validatePage(){ if (previouslyBrowsedFile.isEmpty()){ diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/WizardModulesImportPage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/WizardModulesImportPage.java index 9ec3377d..9c08c02e 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/WizardModulesImportPage.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/wizards/modules/WizardModulesImportPage.java @@ -1,9 +1,10 @@ package org.simantics.sysdyn.ui.wizards.modules; import java.io.File; -import java.io.IOException; +import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Status; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.PixelConverter; import org.eclipse.jface.viewers.ISelection; @@ -27,22 +28,12 @@ import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.Tree; import org.simantics.browsing.ui.swt.AdaptableHintContext; import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite; -import org.simantics.databoard.Bindings; -import org.simantics.databoard.Files; -import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.WriteRequest; -import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler; -import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; import org.simantics.db.request.Read; -import org.simantics.graph.db.MissingDependencyException; -import org.simantics.graph.representation.TransferableGraph1; -import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.utils.imports.ImportUtils; import org.simantics.ui.SimanticsUI; import org.simantics.utils.datastructures.ArrayMap; @@ -71,8 +62,6 @@ public class WizardModulesImportPage extends WizardPage{ private boolean selectionMade = false; - private String error = ""; - /** * Creates a new project creation wizard page. * @@ -294,6 +283,9 @@ public class WizardModulesImportPage extends WizardPage{ return false; } + IStatus status = ImportUtils.importModuleFile(selectedModel, selected, shell, null); + + /* TransferableGraph1 tg = null; try { tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class)); @@ -349,6 +341,12 @@ public class WizardModulesImportPage extends WizardPage{ error = ""; return false; } + */ + if(status == null || !status.equals(Status.OK_STATUS)) { + setErrorMessage(status.getMessage()); + return false; + } + return true; }