]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Still minor cleanup for generic model import/export code 53/1153/2
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 27 Oct 2017 23:26:31 +0000 (02:26 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 27 Oct 2017 23:57:49 +0000 (02:57 +0300)
refs #7572

Change-Id: I012071bcf94fd5e35485918c0ee6ca4096e47f1d

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelDependenciesBean.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExporter.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImportWizard.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImporter.java [new file with mode: 0644]

index 8b8e86b52adcbc2e24a932d9f7bd57fbf774490a..afeba2fb9d298b7c42183dd41cdcae7ed9ee40bd 100644 (file)
@@ -1,5 +1,6 @@
 package org.simantics.db.layer0.util;
 
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.LinkedList;
@@ -46,7 +47,7 @@ public class ModelDependenciesBean {
                collectDependencies(graph, resource, order, new HashSet<>());
                return order;
        }
-       
+
        private static void collectDependencies(ReadGraph graph, Resource resource, LinkedList<Resource> order, Set<Resource> visited) throws DatabaseException {
                Layer0 L0 = Layer0.getInstance(graph);
                for(Resource library : graph.syncRequest(new ObjectsWithType(resource, L0.IsLinkedTo, L0.SharedOntology))) {
@@ -57,12 +58,12 @@ public class ModelDependenciesBean {
                        order.addFirst(library);
                }
        }
-       
+
        private static List<ModelDependency> collectModelDependencies(ReadGraph graph, Resource resource) throws DatabaseException {
                List<Resource> order = collectDependencies(graph, resource);
                Collections.reverse(order);
 
-               List<ModelDependency> modelDependencies = new LinkedList<>();
+               List<ModelDependency> modelDependencies = new ArrayList<>(order.size());
 
                for (Resource library : order) {
                        String uri = graph.getPossibleURI(library);
@@ -78,27 +79,17 @@ public class ModelDependenciesBean {
                        }
                }
                return modelDependencies;
-
        }
 
        public static ModelDependenciesBean create(ReadGraph graph, Resource resource) throws DatabaseException {
-
                List<ModelDependency> dependencies = collectModelDependencies(graph, resource);
                return new ModelDependenciesBean(dependencies.toArray(new ModelDependency[dependencies.size()]));
-
        }
 
-       public static ModelDependenciesBean fromMigrationState(MigrationState state) throws DatabaseException {
+       public static ModelDependenciesBean fromMigrationState(MigrationState state) throws DatabaseException, AdaptException {
                Map<String,Variant> extensions = state.getProperty(MigrationStateKeys.TG_EXTENSIONS);
                final Variant variant = extensions.get(ModelDependenciesBean.EXTENSION_KEY);
-               if (variant != null) {
-                       try {
-                               return (ModelDependenciesBean) variant.getValue(ModelDependenciesBean.BINDING);
-                       } catch (AdaptException e) {
-                               e.printStackTrace();
-                       }
-               }
-               return null;
-       }       
-       
+               return variant != null ? (ModelDependenciesBean) variant.getValue(ModelDependenciesBean.BINDING) : null;
+       }
+
 }
index 7211c79035fe0ae91180231659f70ff60aa26150..8d3b124fd12b5058681e4104ca1df839990deccb 100644 (file)
@@ -71,21 +71,19 @@ public class ModelExporter implements IRunnableWithProgress {
             mon.setWorkRemaining(0);
         }
     }
-    
-    public static void doExport(IProgressMonitor monitor, final File location, final LibraryInfo info, boolean includeDependencies) throws DatabaseException, IOException {
-       Simantics.sync(new ReadRequest() {
 
-                       @Override
-                       public void run(ReadGraph graph) throws DatabaseException {
-                               TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, info.library.getResource(), true, false);
-                               if(includeDependencies) {
-                                       ModelDependenciesBean bean = ModelDependenciesBean.create(graph, info.library.getResource());
-                                       conf.baseExtensions.put(ModelDependenciesBean.EXTENSION_KEY, new Variant(ModelDependenciesBean.BINDING, bean));
-                               }
-                               ModelingUtils.exportModel(graph, conf, location.getAbsolutePath(), "", 1);
-                       }
-               
-       });
+    public static void doExport(IProgressMonitor monitor, final File location, final LibraryInfo info, boolean includeDependencies) throws DatabaseException, IOException {
+        Simantics.sync(new ReadRequest() {
+            @Override
+            public void run(ReadGraph graph) throws DatabaseException {
+                TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, info.library.getResource(), true, false);
+                if (includeDependencies) {
+                    ModelDependenciesBean bean = ModelDependenciesBean.create(graph, info.library.getResource());
+                    conf.baseExtensions.put(ModelDependenciesBean.EXTENSION_KEY, new Variant(ModelDependenciesBean.BINDING, bean));
+                }
+                ModelingUtils.exportModel(graph, conf, location.getAbsolutePath(), "", 1);
+            }
+        });
     }
-       
+
 }
index fd8aaff3cf882023d630130d01d4b6549e38f3c0..bb0d6f68a7958832f4221bf1738a3523cd34df1b 100644 (file)
  *******************************************************************************/
 package org.simantics.modeling.ui.sharedontology.wizard;
 
-import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
-import java.util.Collection;
 import java.util.Deque;
-import java.util.HashMap;
-import java.util.Map;
 
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.SubMonitor;
 import org.eclipse.core.runtime.preferences.InstanceScope;
-import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.preference.IPersistentPreferenceStore;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.viewers.IStructuredSelection;
@@ -33,25 +26,11 @@ import org.eclipse.ui.IImportWizard;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.preferences.ScopedPreferenceStore;
 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.FormatHandler;
 import org.simantics.databoard.util.URIStringUtils;
 import org.simantics.db.Resource;
-import org.simantics.db.Session;
-import org.simantics.db.common.primitiverequest.PossibleResource;
-import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
 import org.simantics.db.layer0.migration.MigratedImportResult;
-import org.simantics.db.layer0.migration.MigrationState;
-import org.simantics.db.layer0.migration.MigrationStateKeys;
-import org.simantics.db.layer0.migration.MigrationUtils;
-import org.simantics.db.layer0.util.ModelDependenciesBean;
-import org.simantics.db.layer0.util.ModelDependency;
 import org.simantics.db.management.ISessionContext;
-import org.simantics.graph.db.ImportResult;
 import org.simantics.graph.db.MissingDependencyException;
-import org.simantics.graph.representation.TransferableGraph1;
 import org.simantics.modeling.ui.Activator;
 import org.simantics.modeling.ui.utils.NoProjectPage;
 import org.simantics.project.IProject;
@@ -135,18 +114,15 @@ public class ModelImportWizard extends Wizard implements IImportWizard {
 
         try {
             MigratedImportResult[] result = { null };
-            getContainer().run(true, true, new IRunnableWithProgress() {
-                @Override
-                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
-                    try {
-                        Resource target = Simantics.getProjectResource();
-                        importModel.sessionContext.getSession().markUndoPoint();
-                        result[0] = doImport(monitor, importModel.importLocation, importModel.sessionContext.getSession(), target, importModel.includeDependencies);
-                    } catch (Exception e) {
-                        throw new InvocationTargetException(e);
-                    } finally {
-                        monitor.done();
-                    }
+            getContainer().run(true, true, monitor -> {
+                try {
+                    Resource target = Simantics.getProjectResource();
+                    importModel.sessionContext.getSession().markUndoPoint();
+                    result[0] = ModelImporter.doImport(monitor, importModel.importLocation, importModel.sessionContext.getSession(), target, importModel.includeDependencies);
+                } catch (Exception e) {
+                    throw new InvocationTargetException(e);
+                } finally {
+                    monitor.done();
                 }
             });
 
@@ -180,61 +156,4 @@ public class ModelImportWizard extends Wizard implements IImportWizard {
         return true;
     }
 
-    public static MigratedImportResult doImport(IProgressMonitor monitor, File modelFile, Session session, Resource target, boolean includeDependencies)
-            throws Exception
-    {
-        SubMonitor mon = SubMonitor.convert(monitor);
-        mon.beginTask("Loading model from disk", 1000);
-
-        FormatHandler<MigratedImportResult> handler1 = new FormatHandler<MigratedImportResult>() {
-            @Override
-            public Binding getBinding() {
-                return TransferableGraph1.BINDING;
-            }
-
-            @Override
-            public MigratedImportResult process(DataContainer container) throws Exception {
-                mon.worked(100);
-                mon.setTaskName("Importing model into database");
-
-                MigrationState state = MigrationUtils.newState();
-                state.setProperty(MigrationStateKeys.UPDATE_DEPENDENCIES, false);
-                state.setProperty(MigrationStateKeys.MODEL_FILE, modelFile);
-                state.setProperty(MigrationStateKeys.SESSION, session);
-                state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor);
-                
-                if(includeDependencies) {
-                       final ModelDependenciesBean libraryDependenciesBean = ModelDependenciesBean.fromMigrationState(state);
-                       if(libraryDependenciesBean != null) {
-                           for(ModelDependency dependency : libraryDependenciesBean.dependencies) {
-                               Resource existing = session.sync(new PossibleResource(dependency.uri));
-                               if(existing == null) {
-                                   MigrationUtils.importSharedOntology(session, dependency.tg, false);
-                               }
-                           }
-                       }
-                }
-                
-                MigrationUtils.importMigrated(monitor, session, modelFile, state, new DefaultPasteImportAdvisor(target), target);
-
-                Collection<Resource> resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
-                ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT);
-                return new MigratedImportResult(resultRoots, result);
-                
-            }
-        };
-
-        Map<String, FormatHandler<MigratedImportResult>> handlers = new HashMap<>();
-        handlers.put(":1", handler1);
-        handlers.put(Constants.MODEL_FORMAT_V1, handler1);
-
-        MigratedImportResult result = DataContainers.readFile(modelFile, handlers);
-
-        mon.setTaskName("Postprocessing");
-        mon.subTask("");
-        mon.newChild(50).done();
-
-        return result;
-    }
-
 }
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImporter.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImporter.java
new file mode 100644 (file)
index 0000000..6a8d671
--- /dev/null
@@ -0,0 +1,108 @@
+/*******************************************************************************
+ * Copyright (c) 2017 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:
+ *     Semantum Oy - initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling.ui.sharedontology.wizard;
+
+import java.io.File;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+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.simantics.databoard.adapter.AdaptException;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.container.DataContainer;
+import org.simantics.databoard.container.DataContainers;
+import org.simantics.databoard.container.FormatHandler;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.common.primitiverequest.PossibleResource;
+import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
+import org.simantics.db.layer0.migration.MigratedImportResult;
+import org.simantics.db.layer0.migration.MigrationState;
+import org.simantics.db.layer0.migration.MigrationStateKeys;
+import org.simantics.db.layer0.migration.MigrationUtils;
+import org.simantics.db.layer0.util.ModelDependenciesBean;
+import org.simantics.db.layer0.util.ModelDependency;
+import org.simantics.graph.db.ImportResult;
+import org.simantics.graph.representation.TransferableGraph1;
+import org.simantics.modeling.ui.Activator;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 1.31.0
+ */
+public class ModelImporter {
+
+    public static MigratedImportResult doImport(IProgressMonitor monitor, File modelFile, Session session, Resource target, boolean includeDependencies)
+            throws Exception
+    {
+        SubMonitor mon = SubMonitor.convert(monitor);
+        mon.beginTask("Loading model from disk", 1000);
+
+        FormatHandler<MigratedImportResult> handler1 = new FormatHandler<MigratedImportResult>() {
+            @Override
+            public Binding getBinding() {
+                return TransferableGraph1.BINDING;
+            }
+
+            @Override
+            public MigratedImportResult process(DataContainer container) throws Exception {
+                mon.worked(100);
+                mon.setTaskName("Importing model into database");
+
+                MigrationState state = MigrationUtils.newState();
+                state.setProperty(MigrationStateKeys.UPDATE_DEPENDENCIES, false);
+                state.setProperty(MigrationStateKeys.MODEL_FILE, modelFile);
+                state.setProperty(MigrationStateKeys.SESSION, session);
+                state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor);
+
+                if (includeDependencies) {
+                    try {
+                        ModelDependenciesBean libraryDependenciesBean = ModelDependenciesBean.fromMigrationState(state);
+                        if (libraryDependenciesBean != null) {
+                            for (ModelDependency dependency : libraryDependenciesBean.dependencies) {
+                                Resource existing = session.sync(new PossibleResource(dependency.uri));
+                                if (existing == null) {
+                                    MigrationUtils.importSharedOntology(session, dependency.tg, false);
+                                }
+                            }
+                        }
+                    } catch (AdaptException e) {
+                        Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not read model dependencies bean.", e));
+                    }
+                }
+
+                MigrationUtils.importMigrated(monitor, session, modelFile, state, new DefaultPasteImportAdvisor(target), target);
+
+                Collection<Resource> resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
+                ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT);
+                return new MigratedImportResult(resultRoots, result);
+            }
+        };
+
+        Map<String, FormatHandler<MigratedImportResult>> handlers = new HashMap<>();
+        handlers.put(":1", handler1);
+        handlers.put(Constants.MODEL_FORMAT_V1, handler1);
+
+        MigratedImportResult result = DataContainers.readFile(modelFile, handlers);
+
+        mon.setTaskName("Postprocessing");
+        mon.subTask("");
+        mon.newChild(50).done();
+
+        return result;
+    }
+
+}