]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
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
8  *\r
9  * Contributors:\r
10  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.event.util;\r
13 \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
19 \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
31 \r
32 /**\r
33  * @author Tuukka Lehtonen\r
34  */\r
35 public class EventExporter {\r
36 \r
37         public EventExporter() {\r
38         }\r
39 \r
40         /**\r
41          * @param monitor\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
46          *            cancelled.</pre>\r
47          * @param file\r
48          * @param columnSeparator\r
49          * @throws DatabaseException\r
50          * @throws IOException\r
51          */\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
54                 try {\r
55                         Simantics.getSession().syncRequest(new ReadRequest() {\r
56                                 @Override\r
57                                 public void run(ReadGraph graph) throws DatabaseException {\r
58                                         exportCsv(graph, monitor, out, columnSeparator);\r
59                                 }\r
60                         });\r
61                 } finally {\r
62                         FileUtils.uncheckedClose(out);\r
63                 }\r
64         }\r
65 \r
66         /**\r
67          * @param graph\r
68          * @param monitor\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
73          *            cancelled.</pre>\r
74          * @param out\r
75          * @param columnSeparator\r
76          * @throws DatabaseException\r
77          */\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
82                         return;\r
83                 }\r
84 \r
85                 final SubMonitor mon = SubMonitor.convert(monitor, "Export events", events.size());\r
86 \r
87                 boolean first = true;\r
88                 for (Object obj : events) {\r
89                         if (mon.isCanceled())\r
90                                 return;\r
91 \r
92                         Resource event = (Resource) obj;\r
93                         Map<String, String> labels = EventLabelRule.INSTANCE.getLabel(graph, event, true);\r
94                         if (first) {\r
95                                 out.println(EString.implode(labels.keySet(), columnSeparator));\r
96                                 first = false;\r
97                         }\r
98                         out.println(EString.implode(labels.values(), columnSeparator));\r
99                         mon.worked(1);\r
100                 }\r
101         }\r
102 \r
103 }\r