package org.simantics.db.layer0.util;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
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))) {
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);
}
}
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;
+ }
+
}
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);
+ }
+ });
}
-
+
}
*******************************************************************************/
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;
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;
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();
}
});
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;
- }
-
}
--- /dev/null
+/*******************************************************************************
+ * 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;
+ }
+
+}