1 /*******************************************************************************
\r
2 * Copyright (c) 2014 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * Semantum Oy - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.event.util;
\r
14 import java.io.File;
\r
15 import java.io.IOException;
\r
16 import java.io.PrintStream;
\r
17 import java.util.Collection;
\r
18 import java.util.Map;
\r
20 import org.eclipse.core.runtime.IProgressMonitor;
\r
21 import org.eclipse.core.runtime.SubMonitor;
\r
22 import org.simantics.Simantics;
\r
23 import org.simantics.db.ReadGraph;
\r
24 import org.simantics.db.Resource;
\r
25 import org.simantics.db.common.request.ReadRequest;
\r
26 import org.simantics.db.exception.DatabaseException;
\r
27 import org.simantics.event.view.contribution.EventLabelRule;
\r
28 import org.simantics.event.view.contribution.ProjectEventsRule;
\r
29 import org.simantics.utils.FileUtils;
\r
30 import org.simantics.utils.strings.EString;
\r
33 * @author Tuukka Lehtonen
\r
35 public class EventExporter {
\r
37 public EventExporter() {
\r
42 * the progress monitor to use for reporting progress to the
\r
43 * user. It is the caller's responsibility to call done() on the
\r
44 * given monitor. Accepts <code>null</code>, indicating that no
\r
45 * progress should be reported and that the operation cannot be
\r
48 * @param columnSeparator
\r
49 * @throws DatabaseException
\r
50 * @throws IOException
\r
52 public void exportCsv(final IProgressMonitor monitor, File file, final String columnSeparator) throws DatabaseException, IOException {
\r
53 final PrintStream out = new PrintStream(file);
\r
55 Simantics.getSession().syncRequest(new ReadRequest() {
\r
57 public void run(ReadGraph graph) throws DatabaseException {
\r
58 exportCsv(graph, monitor, out, columnSeparator);
\r
62 FileUtils.uncheckedClose(out);
\r
69 * the progress monitor to use for reporting progress to the
\r
70 * user. It is the caller's responsibility to call done() on the
\r
71 * given monitor. Accepts <code>null</code>, indicating that no
\r
72 * progress should be reported and that the operation cannot be
\r
75 * @param columnSeparator
\r
76 * @throws DatabaseException
\r
78 public void exportCsv(ReadGraph graph, IProgressMonitor monitor, PrintStream out, String columnSeparator) throws DatabaseException {
\r
79 Collection<?> events = ProjectEventsRule.INSTANCE.getChildren(graph, Simantics.getProjectResource());
\r
80 if (events.isEmpty()) {
\r
81 out.println("No visible events recorded.");
\r
85 final SubMonitor mon = SubMonitor.convert(monitor, "Export events", events.size());
\r
87 boolean first = true;
\r
88 for (Object obj : events) {
\r
89 if (mon.isCanceled())
\r
92 Resource event = (Resource) obj;
\r
93 Map<String, String> labels = EventLabelRule.INSTANCE.getLabel(graph, event, true);
\r
95 out.println(EString.implode(labels.keySet(), columnSeparator));
\r
98 out.println(EString.implode(labels.values(), columnSeparator));
\r