1 package org.simantics.document.linking.report;
5 import java.util.HashMap;
9 import org.eclipse.core.runtime.IProgressMonitor;
10 import org.eclipse.core.runtime.IStatus;
11 import org.eclipse.core.runtime.NullProgressMonitor;
12 import org.eclipse.core.runtime.Status;
13 import org.eclipse.core.runtime.jobs.Job;
14 import org.simantics.Simantics;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.Session;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.request.Read;
20 import org.simantics.document.linking.Activator;
21 import org.simantics.document.linking.report.html.HTMLDocument;
22 import org.simantics.document.linking.report.pdf.PDFDocument;
23 import org.simantics.document.linking.report.templates.CompleteStructureWriter;
24 import org.simantics.document.linking.report.templates.DocumentStructureWriter;
25 import org.simantics.document.linking.report.templates.ReportWriter;
28 public class ExportToPDF {
29 public static final int DOCUMENT_BROWSE_STRCTURE = 0;
30 public static final int DOCUMENT_STRUCTURE = 1;
31 public static final int DIAGRAM_STRUCTURE = 2;
32 public static final int COMPLETE_STRUCTURE = 3;
34 private Session session;
35 private Resource model;
37 public ExportToPDF(Session session,Resource model) {
39 this.session = session;
42 public void exportPDF(String filename, int report) {
43 ReportWriter<?> reportWriter = null;
45 case DOCUMENT_BROWSE_STRCTURE:
48 case DOCUMENT_STRUCTURE:
49 reportWriter = new DocumentStructureWriter();
51 case DIAGRAM_STRUCTURE:
54 case COMPLETE_STRUCTURE:
55 reportWriter = new CompleteStructureWriter();
60 ExportJob job = new ExportJob(filename, reportWriter);
66 private class ExportJob extends Job {
69 ReportWriter<?> reportWriter;
71 public ExportJob(String filename, ReportWriter<?> reportWriter) {
72 super(reportWriter.getName());
73 this.filename = filename;
74 this.reportWriter = reportWriter;
78 protected IStatus run(IProgressMonitor monitor) {
79 return export(filename, reportWriter, monitor);
83 public <T> IStatus export(String filename, ReportWriter<T> reportWriter, IProgressMonitor monitor) {
85 Document lineWriter = null;
87 if (filename.toLowerCase().endsWith(".pdf"))
88 lineWriter = new PDFDocument(filename, new Font("Arial", Font.PLAIN, 10),new Font("Arial", Font.BOLD, 10));
89 else if (filename.toLowerCase().endsWith(".html") ||
90 filename.toLowerCase().endsWith(".htm"))
91 lineWriter = new HTMLDocument(new File(filename));
93 throw new Exception("File has unknow extension " + filename);
95 export(lineWriter, reportWriter, monitor);
97 } catch (Exception e) {
98 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Could not generate report", e);
100 if (lineWriter != null) {
103 } catch (Exception e) {
109 String status = "Report generated.";
110 // if (lineWriter instanceof PDFTableWriter) {
111 // status += " Report size " + ((PDFTableWriter)lineWriter).getCurrentPageIndex() + " pages.";
114 return new Status(IStatus.OK, Activator.PLUGIN_ID, status);
120 private <T> void export(final Document document, final ReportWriter<T> reportWriter, final IProgressMonitor monitor) throws DatabaseException{
121 session.syncRequest(new Read<Object>() {
123 public Object perform(ReadGraph graph) throws DatabaseException {
126 Map<Object, Object> context = new HashMap<Object, Object>();
127 context.put("model", model);
128 context.put(Document.class, document);
129 context.put(ReportWriter.class, reportWriter);
130 context.put("DocumentName", reportWriter.getName());
132 reportWriter.start(graph, model, document, context);
134 List<T> list = reportWriter.getReportItems(graph);
136 monitor.beginTask("Write Report", list.size());
141 for (int i = 0; i < list.size(); i++) {
142 current = list.get(i);
144 previous = list.get(i-1);
147 if (i < list.size()-1)
148 next = list.get(i+1);
151 reportWriter.write(previous, current, next, document);
154 } catch (Exception e) {
155 throw new DatabaseException(e);
164 public static void exportDocumentToPDF(Resource model, String filename) {
165 ExportToPDF exportPdf = new ExportToPDF(Simantics.getSession(), model);
166 exportPdf.export(filename, new DocumentStructureWriter(), new NullProgressMonitor());