From: Tuukka Lehtonen Date: Fri, 11 May 2018 08:57:05 +0000 (+0300) Subject: Added SCL functions for exporting event log contents to CSV X-Git-Tag: v1.43.0~136^2~485 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=refs%2Fchanges%2F69%2F1769%2F4 Added SCL functions for exporting event log contents to CSV gitlab #2 Change-Id: I14423646d035e02f0ec6149a87d170ebb3c01bae --- diff --git a/bundles/org.simantics.event/scl/Simantics/Event.scl b/bundles/org.simantics.event/scl/Simantics/Event.scl index 01a520d52..6839c964c 100644 --- a/bundles/org.simantics.event/scl/Simantics/Event.scl +++ b/bundles/org.simantics.event/scl/Simantics/Event.scl @@ -20,4 +20,25 @@ findEventSlices log = do @private findEventsFromSlice :: Resource -> [Resource] findEventsFromSlice slice = do - collectionToList (objects_ slice L0.ConsistsOf) \ No newline at end of file + collectionToList (objects_ slice L0.ConsistsOf) + +importJava "org.simantics.event.util.EventExporter" where + """ + exportCurrentEvents "\t" "X:/events.txt" + + exports the currently visible events into the specified + text file using the specified column separator string. + This function should be used when invoking outside of + a database transaction. + """ + exportCurrentEvents :: String -> String -> () + + """ + exportCurrentEventsG "\t" "X:/events.txt" + + exports the currently visible events into the specified + text file using the specified column separator string. + This variant must be used when invoking the method with + DB transaction. + """ + exportCurrentEventsG :: String -> String -> () diff --git a/bundles/org.simantics.event/src/org/simantics/event/util/EventExporter.java b/bundles/org.simantics.event/src/org/simantics/event/util/EventExporter.java index 10d2dfe7f..0e34770c5 100644 --- a/bundles/org.simantics.event/src/org/simantics/event/util/EventExporter.java +++ b/bundles/org.simantics.event/src/org/simantics/event/util/EventExporter.java @@ -26,7 +26,6 @@ import org.simantics.db.common.request.ReadRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.event.view.contribution.EventLabelRule; import org.simantics.event.view.contribution.ProjectEventsRule; -import org.simantics.utils.FileUtils; import org.simantics.utils.strings.EString; /** @@ -34,6 +33,28 @@ import org.simantics.utils.strings.EString; */ public class EventExporter { + /** + * SCL API. + * + * @param outputFile + * @throws DatabaseException + * @throws IOException + */ + public static void exportCurrentEvents(String columnSeparator, String outputFile) throws DatabaseException, IOException { + new EventExporter().exportCsv(null, new File(outputFile), columnSeparator); + } + + /** + * SCL API. + * + * @param outputFile + * @throws DatabaseException + * @throws IOException + */ + public static void exportCurrentEventsG(ReadGraph graph, String columnSeparator, String outputFile) throws DatabaseException, IOException { + new EventExporter().exportCsv(graph, null, new File(outputFile), columnSeparator); + } + public EventExporter() { } @@ -49,17 +70,33 @@ public class EventExporter { * @throws DatabaseException * @throws IOException */ - public void exportCsv(final IProgressMonitor monitor, File file, final String columnSeparator) throws DatabaseException, IOException { - final PrintStream out = new PrintStream(file); - try { + public void exportCsv(IProgressMonitor monitor, File file, String columnSeparator) throws DatabaseException, IOException { + try (PrintStream out = new PrintStream(file)) { Simantics.getSession().syncRequest(new ReadRequest() { @Override public void run(ReadGraph graph) throws DatabaseException { exportCsv(graph, monitor, out, columnSeparator); } }); - } finally { - FileUtils.uncheckedClose(out); + } + } + + /** + * @param graph + * @param monitor + * the progress monitor to use for reporting progress to the + * user. It is the caller's responsibility to call done() on the + * given monitor. Accepts null, indicating that no + * progress should be reported and that the operation cannot be + * cancelled. + * @param file + * @param columnSeparator + * @throws DatabaseException + * @throws IOException + */ + public void exportCsv(ReadGraph graph, IProgressMonitor monitor, File file, String columnSeparator) throws DatabaseException, IOException { + try (PrintStream out = new PrintStream(file)) { + exportCsv(graph, monitor, out, columnSeparator); } }