1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.sharedontology.wizard;
15 import java.io.IOException;
16 import java.lang.reflect.InvocationTargetException;
17 import java.nio.file.Path;
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;
35 * @author Antti Villberg
37 public class SharedOntologyExporter implements IRunnableWithProgress {
39 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(SharedOntologyExporter.class);
41 ExportPlan exportModel;
43 public SharedOntologyExporter(ExportPlan exportModel) {
44 this.exportModel = exportModel;
48 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
49 SubMonitor progress = SubMonitor.convert(monitor, 50);
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);
63 void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
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.");
71 mon.setWorkRemaining(0);
75 public static void doExport(IProgressMonitor monitor, File location, LibraryInfo info) throws DatabaseException, IOException {
76 doExport(monitor, location, info, false, false);
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);
83 ModelingUtils.exportSharedOntology(mon.split(1, SubMonitor.SUPPRESS_NONE), Simantics.getSession(), location, Constants.SHARED_LIBRARY_FORMAT, Constants.SHARED_LIBRARY_CURRENT_VERSION, info);
85 Path input = location.toPath();
89 mon.subTask("Writing transferable graph");
90 FixExportedOntology.createTGAndPGraph(input, false);
92 } catch (Exception e) {
93 LOGGER.error("Could not generate Transferable Graph", e);
98 monitor.subTask("Dumping library structure");
99 DumpOntologyStructure data = Simantics.getSession().syncRequest((ReadGraph graph) -> {
100 return new DumpOntologyStructure().read(graph, info.library.getResource());
102 data.write(new File(new File(location.getParent(), location.getName() + ".dump"), info.library.getName()));
104 } catch (Exception e) {
105 LOGGER.error("Could not generate shared library structure dump", e);