]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/SharedOntologyExporter.java
eae7695747153fdc45ddeb66eda859f49775a7da
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / sharedontology / wizard / SharedOntologyExporter.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.sharedontology.wizard;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.lang.reflect.InvocationTargetException;
17 import java.nio.file.Path;
18 import java.nio.file.Paths;
19
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.SubMonitor;
22 import org.eclipse.jface.operation.IRunnableWithProgress;
23 import org.simantics.Simantics;
24 import org.simantics.databoard.binding.error.BindingException;
25 import org.simantics.databoard.serialization.SerializationException;
26 import org.simantics.db.ReadGraph;
27 import org.simantics.db.common.request.UniqueRead;
28 import org.simantics.db.common.utils.Logger;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.graph.refactoring.FixExportedOntology;
31 import org.simantics.modeling.ModelingUtils;
32 import org.simantics.modeling.ModelingUtils.LibraryInfo;
33 import org.simantics.modeling.utils.DumpOntologyStructure;
34 import org.simantics.utils.ui.dialogs.ShowMessage;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * @author Antti Villberg
39  */
40 public class SharedOntologyExporter implements IRunnableWithProgress {
41
42     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(SharedOntologyExporter.class);
43     ExportPlan exportModel;
44
45     public SharedOntologyExporter(ExportPlan exportModel) {
46         this.exportModel = exportModel;
47     }
48
49     @Override
50     public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
51         SubMonitor progress = SubMonitor.convert(monitor, 50);
52         try {
53             exportModel(progress.newChild(50, SubMonitor.SUPPRESS_NONE));
54         } catch (IOException e) {
55             throw new InvocationTargetException(e);
56         } catch (DatabaseException e) {
57             throw new InvocationTargetException(e);
58         } catch (BindingException e) {
59             throw new InvocationTargetException(e);
60         } finally {
61             monitor.done();
62         }
63     }
64
65     void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
66         try {
67             doExport(mon, exportModel.exportLocation, exportModel.model, exportModel.tgAndPgraph);
68
69         } catch (DatabaseException e) {
70             e.printStackTrace();
71             Logger.defaultLogError(e);
72             mon.setCanceled(true);
73             ShowMessage.showError("Export failed.", "Internal application error in export. See log for details.");
74         } finally {
75             mon.setWorkRemaining(0);
76         }
77     }
78     
79     public static void doExport(IProgressMonitor monitor, File location, final LibraryInfo info) throws DatabaseException, IOException {
80         doExport(monitor, location, info, false);
81     }
82     
83     public static void doExport(IProgressMonitor monitor, File location, final LibraryInfo info, boolean pgraphAndTg) throws DatabaseException, IOException {
84         ModelingUtils.exportSharedOntology(monitor, Simantics.getSession(), location,Constants.SHARED_LIBRARY_FORMAT, Constants.SHARED_LIBRARY_CURRENT_VERSION, info);
85         if (pgraphAndTg) {
86             try {
87                 Path input = Paths.get(location.toURI());
88                 FixExportedOntology.createTGAndPGraph(input);
89                 DumpOntologyStructure data = Simantics.sync(new UniqueRead<DumpOntologyStructure>() {
90
91                     @Override
92                     public DumpOntologyStructure perform(ReadGraph graph) throws DatabaseException {
93                         DumpOntologyStructure result = new DumpOntologyStructure();
94                         result.read(graph, info.library.getResource());
95                         return result;
96                     }
97                     
98                 });
99                 data.write(new File(new File(location.getParent(), location.getName() + ".dump"), info.library.getName()));
100             } catch (Exception e) {
101                 LOGGER.error("Could not generate TG and Pgraph", e);
102             }
103         }
104     }
105         
106 }