Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / sharedontology / wizard / SharedOntologyExporter.java
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 \r
18 import org.eclipse.core.runtime.IProgressMonitor;\r
19 import org.eclipse.core.runtime.SubMonitor;\r
20 import org.eclipse.jface.operation.IRunnableWithProgress;\r
21 import org.simantics.Simantics;\r
22 import org.simantics.databoard.binding.error.BindingException;\r
23 import org.simantics.databoard.serialization.SerializationException;\r
24 import org.simantics.db.common.utils.Logger;\r
25 import org.simantics.db.exception.DatabaseException;\r
26 import org.simantics.modeling.ModelingUtils;\r
27 import org.simantics.modeling.ModelingUtils.LibraryInfo;\r
28 import org.simantics.utils.ui.dialogs.ShowMessage;\r
29 \r
30 /**\r
31  * @author Antti Villberg\r
32  */\r
33 public class SharedOntologyExporter implements IRunnableWithProgress {\r
34 \r
35     ExportPlan exportModel;\r
36 \r
37     public SharedOntologyExporter(ExportPlan exportModel) {\r
38         this.exportModel = exportModel;\r
39     }\r
40 \r
41     @Override\r
42     public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
43         SubMonitor progress = SubMonitor.convert(monitor, 50);\r
44         try {\r
45             exportModel(progress.newChild(50, SubMonitor.SUPPRESS_NONE));\r
46         } catch (IOException e) {\r
47             throw new InvocationTargetException(e);\r
48         } catch (DatabaseException e) {\r
49             throw new InvocationTargetException(e);\r
50         } catch (BindingException e) {\r
51             throw new InvocationTargetException(e);\r
52         } finally {\r
53             monitor.done();\r
54         }\r
55     }\r
56 \r
57     void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{\r
58         try {\r
59             doExport(mon, exportModel.exportLocation, exportModel.model);\r
60 \r
61         } catch (DatabaseException e) {\r
62             e.printStackTrace();\r
63             Logger.defaultLogError(e);\r
64             mon.setCanceled(true);\r
65             ShowMessage.showError("Export failed.", "Internal application error in export. See log for details.");\r
66         } finally {\r
67             mon.setWorkRemaining(0);\r
68         }\r
69     }\r
70     \r
71     public static void doExport(IProgressMonitor monitor, File location, final LibraryInfo info) throws DatabaseException, IOException {\r
72         ModelingUtils.exportSharedOntology(monitor, Simantics.getSession(), location,Constants.SHARED_LIBRARY_FORMAT, Constants.SHARED_LIBRARY_CURRENT_VERSION, info);\r
73     }\r
74         \r
75 }\r