1 /*******************************************************************************
2 * Copyright (c) 2014 Association for Decentralized Information Management
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.event.util;
15 import java.io.IOException;
16 import java.io.PrintStream;
17 import java.util.Collection;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.SubMonitor;
22 import org.simantics.Simantics;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.common.request.ReadRequest;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.event.view.contribution.EventLabelRule;
28 import org.simantics.event.view.contribution.ProjectEventsRule;
29 import org.simantics.utils.strings.EString;
32 * @author Tuukka Lehtonen
34 public class EventExporter {
40 * @throws DatabaseException
43 public static void exportCurrentEvents(String columnSeparator, String outputFile) throws DatabaseException, IOException {
44 new EventExporter().exportCsv(null, new File(outputFile), columnSeparator);
51 * @throws DatabaseException
54 public static void exportCurrentEventsG(ReadGraph graph, String columnSeparator, String outputFile) throws DatabaseException, IOException {
55 new EventExporter().exportCsv(graph, null, new File(outputFile), columnSeparator);
58 public EventExporter() {
63 * the progress monitor to use for reporting progress to the
64 * user. It is the caller's responsibility to call done() on the
65 * given monitor. Accepts <code>null</code>, indicating that no
66 * progress should be reported and that the operation cannot be
69 * @param columnSeparator
70 * @throws DatabaseException
73 public void exportCsv(IProgressMonitor monitor, File file, String columnSeparator) throws DatabaseException, IOException {
74 try (PrintStream out = new PrintStream(file)) {
75 Simantics.getSession().syncRequest(new ReadRequest() {
77 public void run(ReadGraph graph) throws DatabaseException {
78 exportCsv(graph, monitor, out, columnSeparator);
87 * the progress monitor to use for reporting progress to the
88 * user. It is the caller's responsibility to call done() on the
89 * given monitor. Accepts <code>null</code>, indicating that no
90 * progress should be reported and that the operation cannot be
93 * @param columnSeparator
94 * @throws DatabaseException
97 public void exportCsv(ReadGraph graph, IProgressMonitor monitor, File file, String columnSeparator) throws DatabaseException, IOException {
98 try (PrintStream out = new PrintStream(file)) {
99 exportCsv(graph, monitor, out, columnSeparator);
106 * the progress monitor to use for reporting progress to the
107 * user. It is the caller's responsibility to call done() on the
108 * given monitor. Accepts <code>null</code>, indicating that no
109 * progress should be reported and that the operation cannot be
112 * @param columnSeparator
113 * @throws DatabaseException
115 public void exportCsv(ReadGraph graph, IProgressMonitor monitor, PrintStream out, String columnSeparator) throws DatabaseException {
116 Collection<?> events = ProjectEventsRule.INSTANCE.getChildren(graph, Simantics.getProjectResource());
117 if (events.isEmpty()) {
118 out.println("No visible events recorded.");
122 final SubMonitor mon = SubMonitor.convert(monitor, "Export events", events.size());
124 boolean first = true;
125 for (Object obj : events) {
126 if (mon.isCanceled())
129 Resource event = (Resource) obj;
130 Map<String, String> labels = EventLabelRule.INSTANCE.getLabel(graph, event, true);
132 out.println(EString.implode(labels.keySet(), columnSeparator));
135 out.println(EString.implode(labels.values(), columnSeparator));