]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.event/src/org/simantics/event/util/EventExporter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / util / EventExporter.java
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
new file mode 100644 (file)
index 0000000..7c563fe
--- /dev/null
@@ -0,0 +1,103 @@
+/*******************************************************************************\r
+ * Copyright (c) 2014 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.event.util;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.io.PrintStream;\r
+import java.util.Collection;\r
+import java.util.Map;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.SubMonitor;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.event.view.contribution.EventLabelRule;\r
+import org.simantics.event.view.contribution.ProjectEventsRule;\r
+import org.simantics.utils.FileUtils;\r
+import org.simantics.utils.strings.EString;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class EventExporter {\r
+\r
+       public EventExporter() {\r
+       }\r
+\r
+       /**\r
+        * @param monitor\r
+        *            the progress monitor to use for reporting progress to the\r
+        *            user. It is the caller's responsibility to call done() on the\r
+        *            given monitor. Accepts <code>null</code>, indicating that no\r
+        *            progress should be reported and that the operation cannot be\r
+        *            cancelled.</pre>\r
+        * @param file\r
+        * @param columnSeparator\r
+        * @throws DatabaseException\r
+        * @throws IOException\r
+        */\r
+       public void exportCsv(final IProgressMonitor monitor, File file, final String columnSeparator) throws DatabaseException, IOException {\r
+               final PrintStream out = new PrintStream(file);\r
+               try {\r
+                       Simantics.getSession().syncRequest(new ReadRequest() {\r
+                               @Override\r
+                               public void run(ReadGraph graph) throws DatabaseException {\r
+                                       exportCsv(graph, monitor, out, columnSeparator);\r
+                               }\r
+                       });\r
+               } finally {\r
+                       FileUtils.uncheckedClose(out);\r
+               }\r
+       }\r
+\r
+       /**\r
+        * @param graph\r
+        * @param monitor\r
+        *            the progress monitor to use for reporting progress to the\r
+        *            user. It is the caller's responsibility to call done() on the\r
+        *            given monitor. Accepts <code>null</code>, indicating that no\r
+        *            progress should be reported and that the operation cannot be\r
+        *            cancelled.</pre>\r
+        * @param out\r
+        * @param columnSeparator\r
+        * @throws DatabaseException\r
+        */\r
+       public void exportCsv(ReadGraph graph, IProgressMonitor monitor, PrintStream out, String columnSeparator) throws DatabaseException {\r
+               Collection<?> events = ProjectEventsRule.INSTANCE.getChildren(graph, Simantics.getProjectResource());\r
+               if (events.isEmpty()) {\r
+                       out.println("No visible events recorded.");\r
+                       return;\r
+               }\r
+\r
+               final SubMonitor mon = SubMonitor.convert(monitor, "Export events", events.size());\r
+\r
+               boolean first = true;\r
+               for (Object obj : events) {\r
+                       if (mon.isCanceled())\r
+                               return;\r
+\r
+                       Resource event = (Resource) obj;\r
+                       Map<String, String> labels = EventLabelRule.INSTANCE.getLabel(graph, event, true);\r
+                       if (first) {\r
+                               out.println(EString.implode(labels.keySet(), columnSeparator));\r
+                               first = false;\r
+                       }\r
+                       out.println(EString.implode(labels.values(), columnSeparator));\r
+                       mon.worked(1);\r
+               }\r
+       }\r
+\r
+}\r