1 /*******************************************************************************
2 * Copyright (c) 2012 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 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.sharedontology.wizard;
15 import java.io.IOException;
16 import java.lang.reflect.InvocationTargetException;
17 import java.util.Collection;
18 import java.util.Deque;
19 import java.util.HashMap;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.SubMonitor;
24 import org.eclipse.core.runtime.preferences.InstanceScope;
25 import org.eclipse.jface.operation.IRunnableWithProgress;
26 import org.eclipse.jface.preference.IPersistentPreferenceStore;
27 import org.eclipse.jface.preference.IPreferenceStore;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.wizard.Wizard;
30 import org.eclipse.jface.wizard.WizardPage;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.ui.IImportWizard;
33 import org.eclipse.ui.IWorkbench;
34 import org.eclipse.ui.preferences.ScopedPreferenceStore;
35 import org.simantics.Simantics;
36 import org.simantics.databoard.binding.Binding;
37 import org.simantics.databoard.container.DataContainer;
38 import org.simantics.databoard.container.DataContainers;
39 import org.simantics.databoard.container.FormatHandler;
40 import org.simantics.databoard.util.URIStringUtils;
41 import org.simantics.db.Resource;
42 import org.simantics.db.Session;
43 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
44 import org.simantics.db.layer0.migration.MigratedImportResult;
45 import org.simantics.db.layer0.migration.MigrationState;
46 import org.simantics.db.layer0.migration.MigrationStateKeys;
47 import org.simantics.db.layer0.migration.MigrationUtils;
48 import org.simantics.db.management.ISessionContext;
49 import org.simantics.graph.db.ImportResult;
50 import org.simantics.graph.db.MissingDependencyException;
51 import org.simantics.graph.representation.TransferableGraph1;
52 import org.simantics.modeling.ui.Activator;
53 import org.simantics.modeling.ui.utils.NoProjectPage;
54 import org.simantics.project.IProject;
55 import org.simantics.project.ProjectKeys;
56 import org.simantics.ui.SimanticsUI;
57 import org.simantics.utils.strings.EString;
58 import org.simantics.utils.ui.ErrorLogger;
59 import org.simantics.utils.ui.ExceptionUtils;
60 import org.simantics.utils.ui.dialogs.InfoDialog;
63 * @author Tuukka Lehtonen
65 public class ModelImportWizard extends Wizard implements IImportWizard {
67 private static final int MAX_RECENT_IMPORT_PATHS = 10;
69 ImportPlan importModel;
71 private boolean readPreferences(IStructuredSelection selection) {
72 IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
74 String recentPathsPref = store.getString(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS);
75 Deque<String> recentImportPaths = Preferences.decodePaths(recentPathsPref);
77 ISessionContext ctx = SimanticsUI.getSessionContext();
80 IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
84 importModel = new ImportPlan(ctx, recentImportPaths);
85 importModel.project = project;
86 importModel.selection = selection.getFirstElement();
91 private void writePreferences() throws IOException {
92 IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
94 store.putValue(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS, Preferences.encodePaths(importModel.recentLocations));
96 if (store.needsSaving())
100 public ModelImportWizard() {
101 setWindowTitle("Import Model");
102 setNeedsProgressMonitor(true);
106 public void init(IWorkbench workbench, IStructuredSelection selection) {
107 readPreferences(selection);
111 public void addPages() {
113 if (importModel != null) {
114 addPage(new ModelImportPage(importModel));
116 addPage(new NoProjectPage("Import Model"));
121 public boolean performFinish() {
123 importModel.recentLocations.addFirst(importModel.importLocation.getAbsolutePath());
124 Preferences.removeDuplicates(importModel.recentLocations);
125 if (importModel.recentLocations.size() > MAX_RECENT_IMPORT_PATHS)
126 importModel.recentLocations.pollLast();
129 } catch (IOException e) {
130 ErrorLogger.defaultLogError("Failed to write preferences", e);
134 MigratedImportResult[] result = { null };
135 getContainer().run(true, true, new IRunnableWithProgress() {
137 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
139 Resource target = Simantics.getProjectResource();
140 importModel.sessionContext.getSession().markUndoPoint();
141 result[0] = doImport(monitor, importModel.importLocation, importModel.sessionContext.getSession(), target);
142 } catch (Exception e) {
143 throw new InvocationTargetException(e);
150 if (result[0].hasMissingExternals()) {
151 InfoDialog.open(getShell(), "Missing Externals Created",
152 "The system was unable to find some of the external entities referenced by the imported material. Place-holders have been created for the missing entities.\nThe missing entities are:\n"
153 + URIStringUtils.unescape(EString.implode(result[0].tgResult.missingExternals)),
156 } catch (InvocationTargetException e) {
157 Throwable cause = e.getCause();
158 WizardPage cp = (WizardPage) getContainer().getCurrentPage();
159 if (cause instanceof MissingDependencyException) {
160 cp.setErrorMessage("Failed to import model due to missing dependencies.\n" + cause.getMessage());
161 ErrorLogger.defaultLogError("Model " + importModel.importLocation + " import failed due to missing database dependencies. See exception for details.", cause);
162 ExceptionUtils.showError("Failed to import model due to missing dependencies.\n\n" + cause.getMessage(), null);
164 cp.setErrorMessage("Unexpected problem importing model.\nMessage: " + cause.getMessage());
165 ErrorLogger.defaultLogError("Model " + importModel.importLocation + " import failed unexpectedly. See exception for details.", cause);
166 ExceptionUtils.showError("Unexpected problem importing model.\n\n" + cause.getMessage(), cause);
169 } catch (InterruptedException e) {
170 WizardPage cp = (WizardPage) getContainer().getCurrentPage();
171 cp.setErrorMessage("Import interrupted.\nMessage: " + e.getMessage());
172 ErrorLogger.defaultLogError("Model " + importModel.importLocation + " import interrupted.", e);
173 ExceptionUtils.showError("Model import was interrupted.", e);
180 public static MigratedImportResult doImport(IProgressMonitor monitor, File modelFile, Session session, Resource target)
183 SubMonitor mon = SubMonitor.convert(monitor);
184 mon.beginTask("Loading model from disk", 1000);
186 FormatHandler<MigratedImportResult> handler1 = new FormatHandler<MigratedImportResult>() {
188 public Binding getBinding() {
189 return TransferableGraph1.BINDING;
193 public MigratedImportResult process(DataContainer container) throws Exception {
195 mon.setTaskName("Importing model into database");
197 MigrationState state = MigrationUtils.newState();
198 state.setProperty(MigrationStateKeys.UPDATE_DEPENDENCIES, false);
199 state.setProperty(MigrationStateKeys.MODEL_FILE, modelFile);
200 state.setProperty(MigrationStateKeys.SESSION, session);
201 state.setProperty(MigrationStateKeys.PROGRESS_MONITOR, monitor);
203 MigrationUtils.importMigrated(monitor, session, modelFile, state, new DefaultPasteImportAdvisor(target), target);
205 Collection<Resource> resultRoots = state.getProperty(MigrationStateKeys.CURRENT_ROOT_RESOURCES);
206 ImportResult result = state.getProperty(MigrationStateKeys.IMPORT_RESULT);
207 return new MigratedImportResult(resultRoots, result);
212 Map<String, FormatHandler<MigratedImportResult>> handlers = new HashMap<>();
213 handlers.put(":1", handler1);
214 handlers.put(Constants.MODEL_FORMAT_V1, handler1);
216 MigratedImportResult result = DataContainers.readFile(modelFile, handlers);
218 mon.setTaskName("Postprocessing");
220 mon.newChild(50).done();