1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.sharedontology.wizard;
\r
14 import java.io.File;
\r
15 import java.io.IOException;
\r
16 import java.lang.reflect.InvocationTargetException;
\r
17 import java.util.Deque;
\r
18 import java.util.HashMap;
\r
20 import org.eclipse.core.runtime.IProgressMonitor;
\r
21 import org.eclipse.core.runtime.preferences.InstanceScope;
\r
22 import org.eclipse.jface.operation.IRunnableWithProgress;
\r
23 import org.eclipse.jface.preference.IPersistentPreferenceStore;
\r
24 import org.eclipse.jface.preference.IPreferenceStore;
\r
25 import org.eclipse.jface.viewers.IStructuredSelection;
\r
26 import org.eclipse.jface.wizard.Wizard;
\r
27 import org.eclipse.jface.wizard.WizardPage;
\r
28 import org.eclipse.ui.IImportWizard;
\r
29 import org.eclipse.ui.IWorkbench;
\r
30 import org.eclipse.ui.preferences.ScopedPreferenceStore;
\r
31 import org.simantics.Simantics;
\r
32 import org.simantics.databoard.binding.Binding;
\r
33 import org.simantics.databoard.binding.mutable.Variant;
\r
34 import org.simantics.databoard.container.DataContainer;
\r
35 import org.simantics.databoard.container.DataContainers;
\r
36 import org.simantics.databoard.container.DataFormatException;
\r
37 import org.simantics.databoard.container.FormatHandler;
\r
38 import org.simantics.databoard.serialization.SerializationException;
\r
39 import org.simantics.db.Resource;
\r
40 import org.simantics.db.Session;
\r
41 import org.simantics.db.exception.DatabaseException;
\r
42 import org.simantics.db.layer0.migration.MigrationUtils;
\r
43 import org.simantics.db.layer0.util.DraftStatusBean;
\r
44 import org.simantics.db.management.ISessionContext;
\r
45 import org.simantics.graph.db.MissingDependencyException;
\r
46 import org.simantics.graph.representation.TransferableGraph1;
\r
47 import org.simantics.modeling.ui.Activator;
\r
48 import org.simantics.modeling.ui.utils.NoProjectPage;
\r
49 import org.simantics.project.IProject;
\r
50 import org.simantics.project.ProjectKeys;
\r
51 import org.simantics.ui.SimanticsUI;
\r
52 import org.simantics.ui.utils.ResourceAdaptionUtils;
\r
53 import org.simantics.utils.ui.ErrorLogger;
\r
54 import org.simantics.utils.ui.ExceptionUtils;
\r
57 * @author Tuukka Lehtonen
\r
59 public class SharedOntologyImportWizard extends Wizard implements IImportWizard {
\r
61 private static final int MAX_RECENT_IMPORT_PATHS = 10;
\r
63 ImportPlan importModel;
\r
65 private boolean readPreferences(IStructuredSelection selection) {
\r
66 IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
\r
68 String recentPathsPref = store.getString(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS);
\r
69 Deque<String> recentImportPaths = Preferences.decodePaths(recentPathsPref);
\r
71 ISessionContext ctx = SimanticsUI.getSessionContext();
\r
74 IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
\r
75 if (project == null)
\r
78 importModel = new ImportPlan(ctx, recentImportPaths);
\r
79 importModel.project = project;
\r
80 importModel.selection = selection.getFirstElement();
\r
85 private void writePreferences() throws IOException {
\r
86 IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
\r
88 store.putValue(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS, Preferences.encodePaths(importModel.recentLocations));
\r
90 if (store.needsSaving())
\r
94 public SharedOntologyImportWizard() {
\r
95 setWindowTitle("Import Shared Library");
\r
96 setNeedsProgressMonitor(true);
\r
100 public void init(IWorkbench workbench, IStructuredSelection selection) {
\r
101 readPreferences(selection);
\r
105 public void addPages() {
\r
107 if (importModel != null) {
\r
108 addPage(new SharedOntologyImportPage(importModel));
\r
110 addPage(new NoProjectPage("Import Shared Library"));
\r
115 public boolean performFinish() {
\r
117 importModel.recentLocations.addFirst(importModel.importLocation.getAbsolutePath());
\r
118 Preferences.removeDuplicates(importModel.recentLocations);
\r
119 if (importModel.recentLocations.size() > MAX_RECENT_IMPORT_PATHS)
\r
120 importModel.recentLocations.pollLast();
\r
122 writePreferences();
\r
123 } catch (IOException e) {
\r
124 ErrorLogger.defaultLogError("Failed to write preferences", e);
\r
128 getContainer().run(true, true, new IRunnableWithProgress() {
\r
130 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
\r
132 Resource target = ResourceAdaptionUtils.toSingleResource(importModel.selection);
\r
133 importModel.sessionContext.getSession().markUndoPoint();
\r
134 doImport(monitor, importModel.importLocation, importModel.sessionContext.getSession(), target);
\r
135 } catch (Exception e) {
\r
136 throw new InvocationTargetException(e);
\r
140 } catch (InvocationTargetException e) {
\r
141 Throwable t = e.getTargetException();
\r
142 WizardPage cp = (WizardPage) getContainer().getCurrentPage();
\r
143 if (t instanceof IOException) {
\r
144 cp.setErrorMessage("An I/O problem occurred while importing shared library.\n\nMessage: " + e.getMessage());
\r
146 ErrorLogger.defaultLogError(t);
\r
148 } catch (InterruptedException e) {
\r
149 ExceptionUtils.logAndShowError(e);
\r
156 public static void doImport(final IProgressMonitor monitor, File modelFile, final Session session, final Resource target)
\r
157 throws IOException, SerializationException, DatabaseException {
\r
161 monitor.beginTask("Loading shared library from disk", 1000);
\r
163 FormatHandler<Object> handler1 = new FormatHandler<Object>() {
\r
165 public Binding getBinding() {
\r
166 return TransferableGraph1.BINDING;
\r
170 public Object process(DataContainer container) throws Exception {
\r
171 monitor.worked(100);
\r
172 monitor.setTaskName("Importing shared library into database");
\r
173 Variant draftStatus = container.metadata.get(DraftStatusBean.EXTENSION_KEY);
\r
174 TransferableGraph1 tg = (TransferableGraph1)container.content.getValue();
\r
175 MigrationUtils.importSharedOntology(session, tg, draftStatus == null);
\r
176 monitor.worked(850);
\r
182 HashMap<String, FormatHandler<Object>> handlers = new HashMap<String, FormatHandler<Object>>();
\r
183 handlers.put(Constants.SHARED_LIBRARY_FORMAT_V1, handler1);
\r
186 DataContainers.readFile(modelFile, handlers);
\r
187 } catch(DataFormatException e) {
\r
188 throw new IOException(e);
\r
189 } catch(IOException e) {
\r
191 } catch (MissingDependencyException e) {
\r
192 ExceptionUtils.logAndShowError(e.getShortExplanation(), e.getMessage() + "\n\n", e);
\r
193 ErrorLogger.defaultLogError("Shared Library import failed, see exception for details", e);
\r
194 } catch(Exception e) {
\r
195 if(e instanceof RuntimeException)
\r
196 throw (RuntimeException)e;
\r
198 throw new RuntimeException(e);
\r
200 } catch(IOException e) {
\r
203 monitor.setTaskName("Postprocessing");
\r
204 monitor.worked(50);
\r
206 } catch (Throwable t) {
\r
207 t.printStackTrace();
\r