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.PossibleResource;
31 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
32 import org.simantics.db.layer0.migration.MigratedImportResult;
33 import org.simantics.db.layer0.migration.MigrationState;
34 import org.simantics.db.layer0.migration.MigrationStateKeys;
35 import org.simantics.db.layer0.migration.MigrationUtils;
36 import org.simantics.db.layer0.util.ModelDependenciesBean;
37 import org.simantics.db.layer0.util.ModelDependency;
38 import org.simantics.graph.db.ImportResult;
39 import org.simantics.graph.representation.TransferableGraph1;
40 import org.simantics.modeling.ui.Activator;
43 * @author Tuukka Lehtonen
46 public class ModelImporter {
48 public static MigratedImportResult doImport(IProgressMonitor monitor, File modelFile, Session session, Resource target, boolean includeDependencies)
51 SubMonitor mon = SubMonitor.convert(monitor);
52 mon.beginTask("Loading model from disk", 1000);
54 FormatHandler<MigratedImportResult> handler1 = new FormatHandler<MigratedImportResult>() {
56 public Binding getBinding() {
57 return TransferableGraph1.BINDING;
61 public MigratedImportResult process(DataContainer container) throws Exception {
63 mon.setTaskName("Importing model into database");
65 MigrationState state = MigrationUtils.newState();
66 state.setProperty(MigrationStateKeys.UPDATE_DEPENDENCIES, false);
67 state.setProperty(MigrationStateKeys.MODEL_FILE, modelFile);
68 state.setProperty(MigrationStateKeys.SESSION, session);
69 state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor);
71 if (includeDependencies) {
73 ModelDependenciesBean libraryDependenciesBean = ModelDependenciesBean.fromMigrationState(state);
74 if (libraryDependenciesBean != null) {
75 for (ModelDependency dependency : libraryDependenciesBean.dependencies) {
76 Resource existing = session.sync(new PossibleResource(dependency.uri));
77 if (existing == null) {
78 MigrationUtils.importSharedOntology(session, dependency.tg, false);
82 } catch (AdaptException e) {
83 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not read model dependencies bean.", e));
87 MigrationUtils.importMigrated(monitor, session, modelFile, state, new DefaultPasteImportAdvisor(target), target);
89 Collection<Resource> resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
90 ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT);
91 return new MigratedImportResult(resultRoots, result);
95 Map<String, FormatHandler<MigratedImportResult>> handlers = new HashMap<>();
96 handlers.put(":1", handler1);
97 handlers.put(Constants.MODEL_FORMAT_V1, handler1);
99 MigratedImportResult result = DataContainers.readFile(modelFile, handlers);
101 mon.setTaskName("Postprocessing");
103 mon.newChild(50).done();