]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.charts/src/org/simantics/charts/ui/CSVExporter.java
Add SCL support for exporting subscription data as CSV
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / CSVExporter.java
index d4d48060187b27212d4eb14c7bdd85b9f29a057d..9798b57f2c63db2057656f233bca6ce9799924c4 100644 (file)
@@ -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
  * 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.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
-import java.util.List;
 import java.util.Set;
 
 import org.eclipse.core.commands.ExecutionException;
 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.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;
 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{
     }
 
     void exportModel(SubMonitor mon) throws IOException, DatabaseException, SerializationException, BindingException{
-       
         try {
             doExport(mon, exportModel.exportLocation, exportModel);
         } catch (ExecutionException e) {
         try {
             doExport(mon, exportModel.exportLocation, exportModel);
         } catch (ExecutionException e) {
@@ -94,16 +90,15 @@ public class CSVExporter implements IRunnableWithProgress {
         } finally {
             mon.setWorkRemaining(0);
         }
         } finally {
             mon.setWorkRemaining(0);
         }
-        
     }
     }
-    
-    private static Set<Resource> resolveContainingModels(final Collection<NamedResource> res) throws DatabaseException {
+
+    private static Set<Resource> resolveContainingModels(Collection<Resource> res) throws DatabaseException {
         return Simantics.getSession().syncRequest(new UniqueRead<Set<Resource>>() {
             @Override
             public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
         return Simantics.getSession().syncRequest(new UniqueRead<Set<Resource>>() {
             @Override
             public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
-                Set<Resource> models = new HashSet<Resource>();
-                for (NamedResource r : res) {
-                    Resource m = graph.syncRequest(new PossibleModel(r.getResource()));
+                Set<Resource> models = new HashSet<>();
+                for (Resource r : res) {
+                    Resource m = graph.syncRequest(new PossibleModel(r));
                     if (m != null)
                         models.add(m);
                 }
                     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 {
     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);
 
         IScopeContext context = InstanceScope.INSTANCE;
         Preferences node = context.getNode(CSVPreferences.P_NODE);
 
@@ -138,7 +132,7 @@ public class CSVExporter implements IRunnableWithProgress {
 
         Set<Resource> models;
         try {
 
         Set<Resource> models;
         try {
-            models = resolveContainingModels(plan.models);
+            models = resolveContainingModels(plan.items);
         } catch (DatabaseException e3) {
             throw new ExecutionException("Containing model resolution failed.", e3);
         }
         } 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);
             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);
         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 ) );
 
         csv.setFloatFormat( FormattingUtils.significantDigitFormat( plan.floatDigits ) );
         csv.setNumberFormat( FormattingUtils.significantDigitFormat( plan.doubleDigits ) );
 
-               try {
-                       Session session = Simantics.getSession();
-                       List<Resource> list = new ArrayList<Resource>();
-                       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();
+        }
     }
     }
-    
+
 }
 }