]> gerrit.simantics Code Review - simantics/platform.git/blob
6a8d6718d7c5d140a6a077637ea2802cf7fbd7bf
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2017 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.sharedontology.wizard;
13
14 import java.io.File;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.Map;
18
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;
41
42 /**
43  * @author Tuukka Lehtonen
44  * @since 1.31.0
45  */
46 public class ModelImporter {
47
48     public static MigratedImportResult doImport(IProgressMonitor monitor, File modelFile, Session session, Resource target, boolean includeDependencies)
49             throws Exception
50     {
51         SubMonitor mon = SubMonitor.convert(monitor);
52         mon.beginTask("Loading model from disk", 1000);
53
54         FormatHandler<MigratedImportResult> handler1 = new FormatHandler<MigratedImportResult>() {
55             @Override
56             public Binding getBinding() {
57                 return TransferableGraph1.BINDING;
58             }
59
60             @Override
61             public MigratedImportResult process(DataContainer container) throws Exception {
62                 mon.worked(100);
63                 mon.setTaskName("Importing model into database");
64
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);
70
71                 if (includeDependencies) {
72                     try {
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);
79                                 }
80                             }
81                         }
82                     } catch (AdaptException e) {
83                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not read model dependencies bean.", e));
84                     }
85                 }
86
87                 MigrationUtils.importMigrated(monitor, session, modelFile, state, new DefaultPasteImportAdvisor(target), target);
88
89                 Collection<Resource> resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
90                 ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT);
91                 return new MigratedImportResult(resultRoots, result);
92             }
93         };
94
95         Map<String, FormatHandler<MigratedImportResult>> handlers = new HashMap<>();
96         handlers.put(":1", handler1);
97         handlers.put(Constants.MODEL_FORMAT_V1, handler1);
98
99         MigratedImportResult result = DataContainers.readFile(modelFile, handlers);
100
101         mon.setTaskName("Postprocessing");
102         mon.subTask("");
103         mon.newChild(50).done();
104
105         return result;
106     }
107
108 }