1 /*******************************************************************************
2 * Copyright (c) 2017 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.sharedontology.wizard;
15 import java.util.Collection;
16 import java.util.HashMap;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.core.runtime.SubMonitor;
23 import org.simantics.databoard.adapter.AdaptException;
24 import org.simantics.databoard.binding.Binding;
25 import org.simantics.databoard.container.DataContainer;
26 import org.simantics.databoard.container.DataContainers;
27 import org.simantics.databoard.container.FormatHandler;
28 import org.simantics.db.Resource;
29 import org.simantics.db.Session;
30 import org.simantics.db.common.primitiverequest.IsExternalEntity;
31 import org.simantics.db.common.primitiverequest.PossibleResource;
32 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
33 import org.simantics.db.layer0.migration.MigratedImportResult;
34 import org.simantics.db.layer0.migration.MigrationState;
35 import org.simantics.db.layer0.migration.MigrationStateKeys;
36 import org.simantics.db.layer0.migration.MigrationUtils;
37 import org.simantics.db.layer0.util.ModelDependenciesBean;
38 import org.simantics.db.layer0.util.ModelDependency;
39 import org.simantics.graph.db.ImportResult;
40 import org.simantics.graph.representation.TransferableGraph1;
41 import org.simantics.modeling.ui.Activator;
44 * @author Tuukka Lehtonen
47 public class ModelImporter {
49 public static MigratedImportResult doImport(IProgressMonitor monitor, File modelFile, Session session, Resource target, boolean includeDependencies)
52 SubMonitor mon = SubMonitor.convert(monitor);
53 mon.beginTask("Loading model from disk", 1000);
55 FormatHandler<MigratedImportResult> handler1 = new FormatHandler<MigratedImportResult>() {
57 public Binding getBinding() {
58 return TransferableGraph1.BINDING;
62 public MigratedImportResult process(DataContainer container) throws Exception {
64 mon.setTaskName("Importing model into database");
66 MigrationState state = MigrationUtils.newState();
67 state.setProperty(MigrationStateKeys.UPDATE_DEPENDENCIES, false);
68 state.setProperty(MigrationStateKeys.MODEL_FILE, modelFile);
69 state.setProperty(MigrationStateKeys.SESSION, session);
70 state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor);
72 if (includeDependencies) {
74 ModelDependenciesBean libraryDependenciesBean = ModelDependenciesBean.fromMigrationState(state);
75 if (libraryDependenciesBean != null) {
76 for (ModelDependency dependency : libraryDependenciesBean.dependencies) {
77 Resource existing = session.sync(new PossibleResource(dependency.uri));
78 boolean isExternalEntity = existing != null && session.syncRequest(new IsExternalEntity(existing));
79 if (existing == null || isExternalEntity) {
80 MigrationUtils.importSharedOntology(session, dependency.tg, false);
84 } catch (AdaptException e) {
85 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not read model dependencies bean.", e));
89 MigrationUtils.importMigrated(monitor, session, modelFile, state, new DefaultPasteImportAdvisor(target), target);
91 Collection<Resource> resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
92 ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT);
93 return new MigratedImportResult(resultRoots, result);
97 Map<String, FormatHandler<MigratedImportResult>> handlers = new HashMap<>();
98 handlers.put(":1", handler1);
99 handlers.put(Constants.MODEL_FORMAT_V1, handler1);
101 MigratedImportResult result = DataContainers.readFile(modelFile, handlers);
103 mon.setTaskName("Postprocessing");
105 mon.newChild(50).done();