]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelImporter.java
Improved usability of shared library export wizard
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / sharedontology / wizard / ModelImporter.java
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.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;
42
43 /**
44  * @author Tuukka Lehtonen
45  * @since 1.31.0
46  */
47 public class ModelImporter {
48
49     public static MigratedImportResult doImport(IProgressMonitor monitor, File modelFile, Session session, Resource target, boolean includeDependencies)
50             throws Exception
51     {
52         SubMonitor mon = SubMonitor.convert(monitor);
53         mon.beginTask("Loading model from disk", 1000);
54
55         FormatHandler<MigratedImportResult> handler1 = new FormatHandler<MigratedImportResult>() {
56             @Override
57             public Binding getBinding() {
58                 return TransferableGraph1.BINDING;
59             }
60
61             @Override
62             public MigratedImportResult process(DataContainer container) throws Exception {
63                 mon.worked(100);
64                 mon.setTaskName("Importing model into database");
65
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);
71
72                 if (includeDependencies) {
73                     try {
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);
81                                 }
82                             }
83                         }
84                     } catch (AdaptException e) {
85                         Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not read model dependencies bean.", e));
86                     }
87                 }
88
89                 MigrationUtils.importMigrated(monitor, session, modelFile, state, new DefaultPasteImportAdvisor(target), target);
90
91                 Collection<Resource> resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
92                 ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT);
93                 return new MigratedImportResult(resultRoots, result);
94             }
95         };
96
97         Map<String, FormatHandler<MigratedImportResult>> handlers = new HashMap<>();
98         handlers.put(":1", handler1);
99         handlers.put(Constants.MODEL_FORMAT_V1, handler1);
100
101         MigratedImportResult result = DataContainers.readFile(modelFile, handlers);
102
103         mon.setTaskName("Postprocessing");
104         mon.subTask("");
105         mon.newChild(50).done();
106
107         return result;
108     }
109
110 }