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.Deque;
18 import java.util.HashMap;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.SubMonitor;
23 import org.eclipse.core.runtime.preferences.InstanceScope;
24 import org.eclipse.jface.operation.IRunnableWithProgress;
25 import org.eclipse.jface.preference.IPersistentPreferenceStore;
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.wizard.Wizard;
29 import org.eclipse.jface.wizard.WizardPage;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.ui.IImportWizard;
32 import org.eclipse.ui.IWorkbench;
33 import org.eclipse.ui.preferences.ScopedPreferenceStore;
34 import org.simantics.databoard.binding.Binding;
35 import org.simantics.databoard.binding.mutable.Variant;
36 import org.simantics.databoard.container.DataContainer;
37 import org.simantics.databoard.container.DataContainers;
38 import org.simantics.databoard.container.FormatHandler;
39 import org.simantics.db.Resource;
40 import org.simantics.db.Session;
41 import org.simantics.db.layer0.migration.MigratedImportResult;
42 import org.simantics.db.layer0.migration.MigrationUtils;
43 import org.simantics.db.layer0.util.DraftStatusBean;
44 import org.simantics.db.management.ISessionContext;
45 import org.simantics.graph.db.MissingDependencyException;
46 import org.simantics.graph.representation.TransferableGraph1;
47 import org.simantics.modeling.ui.Activator;
48 import org.simantics.modeling.ui.utils.NoProjectPage;
49 import org.simantics.project.IProject;
50 import org.simantics.project.ProjectKeys;
51 import org.simantics.ui.SimanticsUI;
52 import org.simantics.ui.utils.ResourceAdaptionUtils;
53 import org.simantics.utils.strings.EString;
54 import org.simantics.utils.ui.ErrorLogger;
55 import org.simantics.utils.ui.ExceptionUtils;
56 import org.simantics.utils.ui.dialogs.InfoDialog;
59 * @author Tuukka Lehtonen
61 public class SharedOntologyImportWizard extends Wizard implements IImportWizard {
63 private static final int MAX_RECENT_IMPORT_PATHS = 10;
65 ImportPlan importModel;
67 private boolean readPreferences(IStructuredSelection selection) {
68 IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
70 String recentPathsPref = store.getString(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS);
71 Deque<String> recentImportPaths = Preferences.decodePaths(recentPathsPref);
73 ISessionContext ctx = SimanticsUI.getSessionContext();
76 IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
80 importModel = new ImportPlan(ctx, recentImportPaths);
81 importModel.project = project;
82 importModel.selection = selection.getFirstElement();
87 private void writePreferences() throws IOException {
88 IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
90 store.putValue(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS, Preferences.encodePaths(importModel.recentLocations));
92 if (store.needsSaving())
96 public SharedOntologyImportWizard() {
97 setWindowTitle("Import Shared Library");
98 setNeedsProgressMonitor(true);
102 public void init(IWorkbench workbench, IStructuredSelection selection) {
103 readPreferences(selection);
107 public void addPages() {
109 if (importModel != null) {
110 addPage(new SharedOntologyImportPage(importModel));
112 addPage(new NoProjectPage("Import Shared Library"));
117 public boolean performFinish() {
119 importModel.recentLocations.addFirst(importModel.importLocation.getAbsolutePath());
120 Preferences.removeDuplicates(importModel.recentLocations);
121 if (importModel.recentLocations.size() > MAX_RECENT_IMPORT_PATHS)
122 importModel.recentLocations.pollLast();
125 } catch (IOException e) {
126 ErrorLogger.defaultLogError("Failed to write preferences", e);
130 MigratedImportResult[] result = { null };
131 getContainer().run(true, true, new IRunnableWithProgress() {
133 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
135 Resource target = ResourceAdaptionUtils.toSingleResource(importModel.selection);
136 importModel.sessionContext.getSession().markUndoPoint();
137 result[0] = doImport(monitor, importModel.importLocation, importModel.sessionContext.getSession(), target);
138 } catch (Exception e) {
139 throw new InvocationTargetException(e);
146 if (result[0].hasMissingExternals()) {
147 InfoDialog.open(getShell(), "Missing Externals Created",
148 "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"
149 + EString.implode(result[0].tgResult.missingExternals),
152 } catch (InvocationTargetException e) {
153 Throwable cause = e.getCause();
154 WizardPage cp = (WizardPage) getContainer().getCurrentPage();
155 if (cause instanceof MissingDependencyException) {
156 cp.setErrorMessage("Failed to import shared library due to missing dependencies.\n" + cause.getMessage());
157 ErrorLogger.defaultLogError("Shared Library " + importModel.importLocation + " import failed due to missing database dependencies. See exception for details.", cause);
158 ExceptionUtils.showError("Failed to import shared library due to missing dependencies.\n\n" + cause.getMessage(), null);
160 cp.setErrorMessage("Unexpected problem importing shared library.\nMessage: " + cause.getMessage());
161 ErrorLogger.defaultLogError("Shared Library " + importModel.importLocation + " import failed unexpectedly. See exception for details.", cause);
162 ExceptionUtils.showError("Unexpected problem importing shared library.\n\n" + cause.getMessage(), cause);
165 } catch (InterruptedException e) {
166 WizardPage cp = (WizardPage) getContainer().getCurrentPage();
167 cp.setErrorMessage("Import interrupted.\nMessage: " + e.getMessage());
168 ErrorLogger.defaultLogError("Shared Library " + importModel.importLocation + " import interrupted.", e);
169 ExceptionUtils.showError("Shared library import was interrupted.", e);
176 public static MigratedImportResult doImport(IProgressMonitor monitor, File modelFile, Session session, Resource target)
179 SubMonitor mon = SubMonitor.convert(monitor);
180 mon.beginTask("Loading shared library from disk", 1000);
182 FormatHandler<MigratedImportResult> handler1 = new FormatHandler<MigratedImportResult>() {
184 public Binding getBinding() {
185 return TransferableGraph1.BINDING;
189 public MigratedImportResult process(DataContainer container) throws Exception {
191 mon.setTaskName("Importing shared library into database");
192 Variant draftStatus = container.metadata.get(DraftStatusBean.EXTENSION_KEY);
193 TransferableGraph1 tg = (TransferableGraph1) container.content.getValue();
194 return MigrationUtils.importSharedOntology(mon.newChild(850, SubMonitor.SUPPRESS_NONE), session, tg, draftStatus == null);
198 Map<String, FormatHandler<MigratedImportResult>> handlers = new HashMap<>();
199 handlers.put(Constants.SHARED_LIBRARY_FORMAT_V1, handler1);
201 MigratedImportResult result = DataContainers.readFile(modelFile, handlers);
203 mon.setTaskName("Postprocessing");
205 mon.newChild(50).done();