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.FileUtils;
30 import org.simantics.utils.strings.EString;
33 * @author Tuukka Lehtonen
35 public class EventExporter {
37 public EventExporter() {
42 * the progress monitor to use for reporting progress to the
43 * user. It is the caller's responsibility to call done() on the
44 * given monitor. Accepts <code>null</code>, indicating that no
45 * progress should be reported and that the operation cannot be
48 * @param columnSeparator
49 * @throws DatabaseException
52 public void exportCsv(final IProgressMonitor monitor, File file, final String columnSeparator) throws DatabaseException, IOException {
53 final PrintStream out = new PrintStream(file);
55 Simantics.getSession().syncRequest(new ReadRequest() {
57 public void run(ReadGraph graph) throws DatabaseException {
58 exportCsv(graph, monitor, out, columnSeparator);
62 FileUtils.uncheckedClose(out);
69 * the progress monitor to use for reporting progress to the
70 * user. It is the caller's responsibility to call done() on the
71 * given monitor. Accepts <code>null</code>, indicating that no
72 * progress should be reported and that the operation cannot be
75 * @param columnSeparator
76 * @throws DatabaseException
78 public void exportCsv(ReadGraph graph, IProgressMonitor monitor, PrintStream out, String columnSeparator) throws DatabaseException {
79 Collection<?> events = ProjectEventsRule.INSTANCE.getChildren(graph, Simantics.getProjectResource());
80 if (events.isEmpty()) {
81 out.println("No visible events recorded.");
85 final SubMonitor mon = SubMonitor.convert(monitor, "Export events", events.size());
88 for (Object obj : events) {
92 Resource event = (Resource) obj;
93 Map<String, String> labels = EventLabelRule.INSTANCE.getLabel(graph, event, true);
95 out.println(EString.implode(labels.keySet(), columnSeparator));
98 out.println(EString.implode(labels.values(), columnSeparator));