]> gerrit.simantics Code Review - simantics/platform.git/blob
a0ca44f64c2a73e9a910eef46f28eaa8e1f7692f
[simantics/platform.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.sharedontology.wizard;\r
13 \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
19 \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
55 \r
56 /**\r
57  * @author Tuukka Lehtonen\r
58  */\r
59 public class SharedOntologyImportWizard extends Wizard implements IImportWizard {\r
60 \r
61     private static final int MAX_RECENT_IMPORT_PATHS = 10;\r
62 \r
63     ImportPlan        importModel;\r
64 \r
65     private boolean readPreferences(IStructuredSelection selection) {\r
66         IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
67 \r
68         String recentPathsPref = store.getString(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS);\r
69         Deque<String> recentImportPaths = Preferences.decodePaths(recentPathsPref);\r
70 \r
71         ISessionContext ctx = SimanticsUI.getSessionContext();\r
72         if (ctx == null)\r
73             return false;\r
74         IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);\r
75         if (project == null)\r
76             return false;\r
77 \r
78         importModel = new ImportPlan(ctx, recentImportPaths);\r
79         importModel.project = project;\r
80         importModel.selection = selection.getFirstElement();\r
81 \r
82         return true;\r
83     }\r
84 \r
85     private void writePreferences() throws IOException {\r
86         IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
87 \r
88         store.putValue(Preferences.RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS, Preferences.encodePaths(importModel.recentLocations));\r
89 \r
90         if (store.needsSaving())\r
91             store.save();\r
92     }\r
93 \r
94     public SharedOntologyImportWizard() {\r
95         setWindowTitle("Import Shared Library");\r
96         setNeedsProgressMonitor(true);\r
97     }\r
98 \r
99     @Override\r
100     public void init(IWorkbench workbench, IStructuredSelection selection) {\r
101         readPreferences(selection);\r
102     }\r
103 \r
104     @Override\r
105     public void addPages() {\r
106         super.addPages();\r
107         if (importModel != null) {\r
108             addPage(new SharedOntologyImportPage(importModel));\r
109         } else {\r
110             addPage(new NoProjectPage("Import Shared Library"));\r
111         }\r
112     }\r
113 \r
114     @Override\r
115     public boolean performFinish() {\r
116         try {\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
121 \r
122             writePreferences();\r
123         } catch (IOException e) {\r
124             ErrorLogger.defaultLogError("Failed to write preferences", e);\r
125         }\r
126 \r
127         try {\r
128             getContainer().run(true, true, new IRunnableWithProgress() {\r
129                 @Override\r
130                 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
131                     try {\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
137                     }\r
138                 }\r
139             });\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
145             }\r
146             ErrorLogger.defaultLogError(t);\r
147             return false;\r
148         } catch (InterruptedException e) {\r
149             ExceptionUtils.logAndShowError(e);\r
150             return false;\r
151         }\r
152 \r
153         return true;\r
154     }\r
155 \r
156     public static void doImport(final IProgressMonitor monitor, File modelFile, final Session session, final Resource target)\r
157     throws IOException, SerializationException, DatabaseException {\r
158         \r
159         try {\r
160                 \r
161             monitor.beginTask("Loading shared library from disk", 1000);\r
162             try {\r
163                 FormatHandler<Object> handler1 = new FormatHandler<Object>() {\r
164                     @Override\r
165                     public Binding getBinding() {\r
166                         return TransferableGraph1.BINDING;\r
167                     }\r
168 \r
169                     @Override\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
177                         return null;\r
178                     }\r
179                     \r
180                 };\r
181 \r
182                 HashMap<String, FormatHandler<Object>> handlers = new HashMap<String, FormatHandler<Object>>();\r
183                 handlers.put(Constants.SHARED_LIBRARY_FORMAT_V1, handler1);\r
184 \r
185                 try {\r
186                     DataContainers.readFile(modelFile, handlers);\r
187                 } catch(DataFormatException e) {\r
188                     throw new IOException(e);\r
189                 } catch(IOException e) {\r
190                     throw 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
197                     else\r
198                         throw new RuntimeException(e);\r
199                 }\r
200             } catch(IOException e) {\r
201             }\r
202 \r
203             monitor.setTaskName("Postprocessing");\r
204             monitor.worked(50);\r
205 \r
206         } catch (Throwable t) {\r
207             t.printStackTrace();\r
208         } finally {\r
209             monitor.done();\r
210         }\r
211     }\r
212 \r
213 }\r