1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 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 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.issues.ui.handler;
\r
14 import java.io.File;
\r
15 import java.io.PrintStream;
\r
16 import java.lang.reflect.InvocationTargetException;
\r
17 import java.util.Collection;
\r
19 import org.eclipse.core.commands.AbstractHandler;
\r
20 import org.eclipse.core.commands.ExecutionEvent;
\r
21 import org.eclipse.core.commands.ExecutionException;
\r
22 import org.eclipse.core.runtime.IProgressMonitor;
\r
23 import org.eclipse.core.runtime.SubMonitor;
\r
24 import org.eclipse.core.runtime.preferences.InstanceScope;
\r
25 import org.eclipse.jface.operation.IRunnableWithProgress;
\r
26 import org.eclipse.swt.SWT;
\r
27 import org.eclipse.swt.widgets.FileDialog;
\r
28 import org.eclipse.swt.widgets.Shell;
\r
29 import org.eclipse.ui.IWorkbenchWindow;
\r
30 import org.eclipse.ui.handlers.HandlerUtil;
\r
31 import org.osgi.service.prefs.BackingStoreException;
\r
32 import org.osgi.service.prefs.Preferences;
\r
33 import org.simantics.Simantics;
\r
34 import org.simantics.browsing.ui.common.ErrorLogger;
\r
35 import org.simantics.databoard.Bindings;
\r
36 import org.simantics.db.ReadGraph;
\r
37 import org.simantics.db.Resource;
\r
38 import org.simantics.db.common.request.ObjectsWithType;
\r
39 import org.simantics.db.common.request.ReadRequest;
\r
40 import org.simantics.db.common.utils.NameUtils;
\r
41 import org.simantics.db.exception.DatabaseException;
\r
42 import org.simantics.db.layer0.variable.Variable;
\r
43 import org.simantics.db.request.Read;
\r
44 import org.simantics.issues.common.AllVisibleIssues;
\r
45 import org.simantics.issues.ui.internal.Activator;
\r
46 import org.simantics.operation.Layer0X;
\r
47 import org.simantics.simulation.ontology.SimulationResource;
\r
48 import org.simantics.utils.DataContainer;
\r
49 import org.simantics.utils.FileUtils;
\r
50 import org.simantics.utils.strings.StringUtils;
\r
51 import org.simantics.utils.ui.ExceptionUtils;
\r
54 * @author Tuukka Lehtonen
\r
56 public class ExportIssuesAsCsv extends AbstractHandler {
\r
58 private static final String PROP_LAST_VALIDATION_REPORT_PATH= "validation.report.path";
\r
61 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
62 IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
\r
65 String fileName = generateFileName();
\r
66 validate(window, fileName);
\r
67 } catch (DatabaseException e) {
\r
68 ErrorLogger.defaultLogError(e);
\r
74 private String generateFileName() throws DatabaseException {
\r
75 String generatedName = Simantics.getSession().syncRequest(new Read<String>() {
\r
77 public String perform(ReadGraph graph) throws DatabaseException {
\r
78 Layer0X L0X = Layer0X.getInstance(graph);
\r
79 SimulationResource SIMU = SimulationResource.getInstance(graph);
\r
80 for (Resource model : graph.syncRequest(new ObjectsWithType(Simantics.getProjectResource(), L0X.Activates, SIMU.Model))) {
\r
81 return NameUtils.getSafeName(graph, model) + ".txt";
\r
83 return "issues.txt";
\r
87 if (!FileUtils.isValidFileName(generatedName))
\r
88 generatedName = (String) Bindings.STR_VARIANT.createUnchecked(Bindings.STRING, generatedName);
\r
90 return generatedName;
\r
93 public void validate(IWorkbenchWindow window, String fileName) {
\r
94 Preferences prefs = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
\r
95 String lastReportPath = prefs.get(PROP_LAST_VALIDATION_REPORT_PATH, null);
\r
97 // Query for output path
\r
98 Shell parentShell = null;
\r
100 parentShell = window.getShell();
\r
102 final DataContainer<PrintStream> externalOutput = new DataContainer<PrintStream>();
\r
104 FileDialog fd = new FileDialog(parentShell, SWT.SAVE);
\r
105 fd.setText("Select Validation Output");
\r
106 fd.setFilterExtensions(new String[] { "*.txt", "*.*" });
\r
107 fd.setFilterNames(new String[] { "Comma-Separated Values (*.txt)", "All Files (*.*)" });
\r
108 if (lastReportPath != null)
\r
109 fd.setFilterPath(lastReportPath);
\r
110 fd.setFileName(fileName);
\r
111 final String path = fd.open();
\r
112 if (path != null) {
\r
113 prefs.put(PROP_LAST_VALIDATION_REPORT_PATH, path);
\r
116 } catch (BackingStoreException e) {
\r
117 ExceptionUtils.logError(e);
\r
120 // boolean result = MessageDialog.openQuestion(parentShell, "Print to stdout?", "Would you like to perform model validation and print the results into stdout?");
\r
123 // externalOutput.set(System.out);
\r
128 window.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
\r
130 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
\r
132 if (externalOutput.get() == null)
\r
133 externalOutput.set(new PrintStream(new File(path)));
\r
135 final SubMonitor progress = SubMonitor.convert(monitor, "Export issues", IProgressMonitor.UNKNOWN);
\r
136 Simantics.getSession().syncRequest(new ReadRequest() {
\r
138 public void run(ReadGraph graph) throws DatabaseException {
\r
139 PrintStream out = externalOutput.get();
\r
141 Collection<Variable> activeIssues = graph.syncRequest(new AllVisibleIssues(Simantics.getProjectResource()));
\r
142 out.println("# Exported issues (" + activeIssues.size() + ")");
\r
143 for (Variable issue : activeIssues) {
\r
144 String description = StringUtils.safeString( (String) issue.getPossiblePropertyValue(graph, "HasDescription") );
\r
145 String severity = StringUtils.safeString( (String) issue.getPossiblePropertyValue(graph, "severity") );
\r
146 String resource = StringUtils.safeString( (String) issue.getPossiblePropertyValue(graph, "resource") );
\r
147 String path = StringUtils.safeString( (String) issue.getPossiblePropertyValue(graph, "path") );
\r
148 out.println(description + ";" + severity + ";" + resource + ";" + path);
\r
149 progress.worked(1);
\r
153 } catch (Exception e) {
\r
154 throw new InvocationTargetException(e);
\r
157 if (externalOutput.get() != System.out)
\r
158 FileUtils.uncheckedClose(externalOutput.get());
\r
162 } catch (InvocationTargetException e) {
\r
163 ExceptionUtils.logAndShowError(e.getTargetException());
\r
164 } catch (InterruptedException e) {
\r
165 // Operation cancelled, ignore.
\r