Minor fix and optimization for exporting TG and Pgraph with sharedlib
[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.common.utils.Logger;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.graph.refactoring.FixExportedOntology;
29 import org.simantics.modeling.ModelingUtils;
30 import org.simantics.modeling.ModelingUtils.LibraryInfo;
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     ExportPlan exportModel;
41
42     public SharedOntologyExporter(ExportPlan exportModel) {
43         this.exportModel = exportModel;
44     }
45
46     @Override
47     public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
48         SubMonitor progress = SubMonitor.convert(monitor, 50);
49         try {
50             exportModel(progress.newChild(50, SubMonitor.SUPPRESS_NONE));
51         } catch (IOException e) {
52             throw new InvocationTargetException(e);
53         } catch (DatabaseException e) {
54             throw new InvocationTargetException(e);
55         } catch (BindingException e) {
56             throw new InvocationTargetException(e);
57         } finally {
58             monitor.done();
59         }
60     }
61
62     void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
63         try {
64             doExport(mon, exportModel.exportLocation, exportModel.model, exportModel.tgAndPgraph);
65
66         } catch (DatabaseException e) {
67             e.printStackTrace();
68             Logger.defaultLogError(e);
69             mon.setCanceled(true);
70             ShowMessage.showError("Export failed.", "Internal application error in export. See log for details.");
71         } finally {
72             mon.setWorkRemaining(0);
73         }
74     }
75     
76     public static void doExport(IProgressMonitor monitor, File location, final LibraryInfo info) throws DatabaseException, IOException {
77         doExport(monitor, location, info, false);
78     }
79     
80     public static void doExport(IProgressMonitor monitor, File location, final LibraryInfo info, boolean pgraphAndTg) throws DatabaseException, IOException {
81         ModelingUtils.exportSharedOntology(monitor, Simantics.getSession(), location,Constants.SHARED_LIBRARY_FORMAT, Constants.SHARED_LIBRARY_CURRENT_VERSION, info);
82         if (pgraphAndTg) {
83             try {
84                 Path input = Paths.get(location.toURI());
85                 FixExportedOntology.createTGAndPGraph(input);
86             } catch (Exception e) {
87                 LOGGER.error("Could not generate TG and Pgraph", e);
88             }
89         }
90     }
91         
92 }