X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.charts%2Fsrc%2Forg%2Fsimantics%2Fcharts%2Fui%2FCSVExporter.java;h=9798b57f2c63db2057656f233bca6ce9799924c4;hp=d4d48060187b27212d4eb14c7bdd85b9f29a057d;hb=bf39e5c2544ec6e60d4e68996a6a7d4a84b30950;hpb=d11fef0101853949671492de5b49ea94892ced78;ds=sidebyside diff --git a/bundles/org.simantics.charts/src/org/simantics/charts/ui/CSVExporter.java b/bundles/org.simantics.charts/src/org/simantics/charts/ui/CSVExporter.java index d4d480601..9798b57f2 100644 --- a/bundles/org.simantics.charts/src/org/simantics/charts/ui/CSVExporter.java +++ b/bundles/org.simantics.charts/src/org/simantics/charts/ui/CSVExporter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012 Association for Decentralized Information Management in + * Copyright (c) 2012,2017 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -21,7 +21,6 @@ import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; -import java.util.List; import java.util.Set; import org.eclipse.core.commands.ExecutionException; @@ -42,8 +41,6 @@ import org.simantics.databoard.binding.error.BindingException; import org.simantics.databoard.serialization.SerializationException; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.Session; -import org.simantics.db.common.NamedResource; import org.simantics.db.common.request.UniqueRead; import org.simantics.db.common.utils.Logger; import org.simantics.db.exception.DatabaseException; @@ -83,7 +80,6 @@ public class CSVExporter implements IRunnableWithProgress { } void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{ - try { doExport(mon, exportModel.exportLocation, exportModel); } catch (ExecutionException e) { @@ -94,16 +90,15 @@ public class CSVExporter implements IRunnableWithProgress { } finally { mon.setWorkRemaining(0); } - } - - private static Set resolveContainingModels(final Collection res) throws DatabaseException { + + private static Set resolveContainingModels(Collection res) throws DatabaseException { return Simantics.getSession().syncRequest(new UniqueRead>() { @Override public Set perform(ReadGraph graph) throws DatabaseException { - Set models = new HashSet(); - for (NamedResource r : res) { - Resource m = graph.syncRequest(new PossibleModel(r.getResource())); + Set models = new HashSet<>(); + for (Resource r : res) { + Resource m = graph.syncRequest(new PossibleModel(r)); if (m != null) models.add(m); } @@ -111,9 +106,8 @@ public class CSVExporter implements IRunnableWithProgress { } }); } - + public static void doExport(IProgressMonitor monitor, final File f, final CSVExportPlan plan) throws ExecutionException, IOException { - IScopeContext context = InstanceScope.INSTANCE; Preferences node = context.getNode(CSVPreferences.P_NODE); @@ -138,7 +132,7 @@ public class CSVExporter implements IRunnableWithProgress { Set models; try { - models = resolveContainingModels(plan.models); + models = resolveContainingModels(plan.items); } catch (DatabaseException e3) { throw new ExecutionException("Containing model resolution failed.", e3); } @@ -148,7 +142,7 @@ public class CSVExporter implements IRunnableWithProgress { throw new ExecutionException("Selected resources are part of several models, only subscriptions from a single model can be selected"); Resource model = models.iterator().next(); Key chartDataKey = ChartKeys.chartSourceKey(model); - + final ChartData data = Simantics.getProject().getHint(chartDataKey); if ( data == null ) { throw new ExecutionException("There is no "+chartDataKey); @@ -169,50 +163,38 @@ public class CSVExporter implements IRunnableWithProgress { csv.setFloatFormat( FormattingUtils.significantDigitFormat( plan.floatDigits ) ); csv.setNumberFormat( FormattingUtils.significantDigitFormat( plan.doubleDigits ) ); - try { - Session session = Simantics.getSession(); - List list = new ArrayList(); - for(NamedResource nr : plan.models) list.add(nr.getResource()); - session.sync( new CSVParamsQuery(data.history, csv, list) ); - csv.sort(); - } catch (DatabaseException e2) { - throw new ExecutionException(e2.getMessage(), e2); - } catch (HistoryException e) { - throw new ExecutionException(e.getMessage(), e); - } - - try { - // Ensure all views are built. - monitor.beginTask("Exporting Time Series as CSV...", IProgressMonitor.UNKNOWN); - try { - data.collector.flush(); - if ( !f.exists() ) { - f.createNewFile(); - } else { - RandomAccessFile raf = new RandomAccessFile(f, "rw"); - raf.setLength(0); - raf.close(); - } - - FileOutputStream fos = new FileOutputStream(f, true); - BufferedOutputStream bos = new BufferedOutputStream( fos ); - try { - PrintStream ps = new PrintStream( bos ); - csv.formulate2( new CSVProgressMonitor( monitor ), ps ); - bos.flush(); - } finally { - fos.close(); - } - } catch (HistoryException e) { - throw new ExecutionException(e.getMessage(), e); - } catch (IOException e1) { - throw new ExecutionException(e1.getMessage(), e1); - } - monitor.setTaskName("Done"); - } finally { - monitor.done(); - } - + try { + Simantics.getSession().syncRequest( + new CSVParamsQuery(data.history, csv, + new ArrayList<>(plan.items)) ); + csv.sort(); + + // Ensure all views are built. + monitor.beginTask("Exporting Time Series as CSV...", IProgressMonitor.UNKNOWN); + data.collector.flush(); + + // Truncate existing file it if happens to exist. + try (RandomAccessFile raf = new RandomAccessFile(f, "rw")) { + raf.setLength(0); + } + + // Write CSV + try (PrintStream ps = new PrintStream( + new BufferedOutputStream( + new FileOutputStream(f, true) ))) + { + csv.formulate2( new CSVProgressMonitor( monitor ), ps ); + ps.flush(); + } + + monitor.setTaskName("Done"); + } catch (DatabaseException e2) { + throw new ExecutionException(e2.getMessage(), e2); + } catch (HistoryException e) { + throw new ExecutionException(e.getMessage(), e); + } finally { + monitor.done(); + } } - + }