]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/SharedOntologyExporter.java
Improved shared library structure dump to take more types into account
[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
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.SubMonitor;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.simantics.Simantics;
23 import org.simantics.databoard.binding.error.BindingException;
24 import org.simantics.databoard.serialization.SerializationException;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.graph.refactoring.FixExportedOntology;
28 import org.simantics.modeling.ModelingUtils;
29 import org.simantics.modeling.ModelingUtils.LibraryInfo;
30 import org.simantics.modeling.utils.DumpOntologyStructure;
31 import org.simantics.utils.ui.dialogs.ShowMessage;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * @author Antti Villberg
36  */
37 public class SharedOntologyExporter implements IRunnableWithProgress {
38
39     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(SharedOntologyExporter.class);
40
41     ExportPlan exportModel;
42
43     public SharedOntologyExporter(ExportPlan exportModel) {
44         this.exportModel = exportModel;
45     }
46
47     @Override
48     public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
49         SubMonitor progress = SubMonitor.convert(monitor, 50);
50         try {
51             exportModel(progress.newChild(50, SubMonitor.SUPPRESS_NONE));
52         } catch (IOException e) {
53             throw new InvocationTargetException(e);
54         } catch (DatabaseException e) {
55             throw new InvocationTargetException(e);
56         } catch (BindingException e) {
57             throw new InvocationTargetException(e);
58         } finally {
59             monitor.done();
60         }
61     }
62
63     void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
64         try {
65             doExport(mon, exportModel.exportLocation, exportModel.model, exportModel.writeTransferableGraph, exportModel.dumpStructure);
66         } catch (DatabaseException e) {
67             LOGGER.error("Failed to export shared ontology", e);
68             mon.setCanceled(true);
69             ShowMessage.showError("Export failed.", "Internal application error in export. See log for details.");
70         } finally {
71             mon.setWorkRemaining(0);
72         }
73     }
74
75     public static void doExport(IProgressMonitor monitor, File location, LibraryInfo info) throws DatabaseException, IOException {
76         doExport(monitor, location, info, false, false);
77     }
78
79     public static void doExport(IProgressMonitor monitor, File location, LibraryInfo info, boolean writeTg, boolean dumpStructure) throws DatabaseException, IOException {
80         int work = 1 + (writeTg ? 1 : 0) + (dumpStructure ? 1 : 0);
81         SubMonitor mon = SubMonitor.convert(monitor, work);
82
83         ModelingUtils.exportSharedOntology(mon.split(1, SubMonitor.SUPPRESS_NONE), Simantics.getSession(), location, Constants.SHARED_LIBRARY_FORMAT, Constants.SHARED_LIBRARY_CURRENT_VERSION, info);
84
85         Path input = location.toPath();
86
87         if (writeTg) {
88             try {
89                 mon.subTask("Writing transferable graph");
90                 FixExportedOntology.createTGAndPGraph(input, false);
91                 mon.worked(1);
92             } catch (Exception e) {
93                 LOGGER.error("Could not generate Transferable Graph", e);
94             }
95         }
96         if (dumpStructure) {
97             try {
98                 monitor.subTask("Dumping library structure");
99                 DumpOntologyStructure data = Simantics.getSession().syncRequest((ReadGraph graph) -> {
100                     return new DumpOntologyStructure().read(graph, info.library.getResource());
101                 });
102                 data.write(new File(new File(location.getParent(), location.getName() + ".dump"), info.library.getName()));
103                 mon.worked(1);
104             } catch (Exception e) {
105                 LOGGER.error("Could not generate shared library structure dump", e);
106             }
107         }
108     }
109
110 }