001: /*
002: This file is part of the PolePosition database benchmark
003: http://www.polepos.org
004:
005: This program is free software; you can redistribute it and/or
006: modify it under the terms of the GNU General Public License
007: as published by the Free Software Foundation; either version 2
008: of the License, or (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public
016: License along with this program; if not, write to the Free
017: Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
018: MA 02111-1307, USA. */
019:
020: package org.polepos.reporters;
021:
022: import java.awt.*;
023: import java.awt.Font;
024: import java.io.*;
025: import java.util.*;
026: import java.util.List;
027:
028: import org.jfree.chart.*;
029: import org.polepos.framework.*;
030:
031: import com.lowagie.text.*;
032: import com.lowagie.text.pdf.*;
033:
034: public class PDFReporter extends GraphReporter {
035: private Circuit mCircuit;
036: private Document mDocument;
037: private PdfWriter mWriter;
038:
039: private com.lowagie.text.Font h1Font = FontFactory.getFont(
040: FontFactory.HELVETICA, 15, Font.BOLD);
041: private com.lowagie.text.Font h2Font = FontFactory.getFont(
042: FontFactory.HELVETICA, 12, Font.BOLD);
043:
044: private com.lowagie.text.Font bigFont = FontFactory.getFont(
045: FontFactory.HELVETICA, 10, Font.BOLD);
046: private com.lowagie.text.Font smallFont = FontFactory.getFont(
047: FontFactory.HELVETICA, 9, Font.PLAIN);
048:
049: protected void report(Graph graph) {
050:
051: if (mDocument == null) {
052: setupDocument(PATH);
053: try {
054: renderFirstPage(graph);
055: } catch (DocumentException e) {
056: e.printStackTrace();
057: }
058: }
059: if (mDocument == null) {
060: return;
061: }
062:
063: Circuit circuit = graph.circuit();
064: if (!circuit.equals(mCircuit)) {
065: mCircuit = circuit;
066: }
067:
068: JFreeChart chart = new ChartBuilder().createChart(graph);
069:
070: chart.setBackgroundPaint(null);
071: try {
072: renderTable(graph);
073: renderChart(chart);
074: mDocument.newPage();
075: } catch (Exception e) {
076: e.printStackTrace();
077: }
078: }
079:
080: private void renderFirstPage(Graph graph) throws DocumentException {
081: if (mDocument == null) {
082: return;
083: }
084:
085: Paragraph para = new Paragraph();
086: para.add(new Chunk("PolePosition\n", h1Font));
087: para.add(new Chunk("the open source database benchmark\n",
088: smallFont));
089: para.add(linked(new Chunk(WEBSITE + "\n\n\n", smallFont),
090: WEBSITE));
091: para.add(new Chunk("Participating teams\n\n", h2Font));
092:
093: mDocument.add(para);
094:
095: List printed = new ArrayList();
096:
097: for (TeamCar teamCar : graph.teamCars()) {
098: Team team = teamCar.getTeam();
099: String webSite = team.website();
100: if (webSite != null) {
101: if (!printed.contains(team)) {
102: printed.add(team);
103: renderTeam(team.name(), team.description(), webSite);
104: }
105: } else {
106: Car car = teamCar.getCar();
107: webSite = car.getWebsite();
108: if (webSite != null) {
109: if (!printed.contains(car)) {
110: printed.add(car);
111: renderTeam(car.name(), car.description(),
112: webSite);
113: }
114: }
115: }
116: }
117: mDocument.newPage();
118: }
119:
120: private void renderTeam(String name, String description,
121: String website) throws DocumentException {
122: Paragraph para = new Paragraph();
123: para.add(linked(new Chunk(name + "\n", bigFont), website));
124: if (description != null) {
125: para.add(linked(new Chunk(description + "\n", smallFont),
126: website));
127: }
128: if (website != null) {
129: para.add(linked(new Chunk(website + "\n", smallFont),
130: website));
131: }
132: para.add(new Chunk("\n", smallFont));
133: mDocument.add(para);
134: }
135:
136: private Element linked(Chunk chunk, String link) {
137: if (link == null) {
138: return chunk;
139: }
140: Anchor anchor = new Anchor(chunk);
141: anchor.setReference(link);
142: return anchor;
143: }
144:
145: private void setupDocument(String path) {
146: String fileName = path + "/" + "PolePosition.pdf";
147: File file = new File(fileName);
148: file.delete();
149: mDocument = new Document();
150: try {
151: mWriter = PdfWriter.getInstance(mDocument,
152: new FileOutputStream(file));
153: mDocument.open();
154: } catch (Exception exc) {
155: exc.printStackTrace();
156: mDocument = null;
157: }
158: }
159:
160: @Override
161: public void endSeason() {
162: super .endSeason();
163: if (mDocument != null) {
164: mDocument.close();
165: }
166: if (mWriter != null) {
167: mWriter.close();
168: }
169: }
170:
171: private void renderTable(Graph graph) throws DocumentException {
172: Paragraph para = new Paragraph();
173: // para.setAlignment(Element.ALIGN_CENTER);
174:
175: Circuit circuit = graph.circuit();
176: Lap lap = graph.lap();
177:
178: para
179: .add(new Chunk("Circuit: " + circuit.name() + "\n",
180: bigFont));
181: para.add(new Chunk(circuit.description() + "\n", smallFont));
182: para.add(new Chunk("Lap: " + lap.name() + "\n\n", bigFont));
183:
184: List<TeamCar> teamCars = graph.teamCars();
185: List<TurnSetup> setups = graph.setups();
186: Table table = setupTable(graph);
187: int idx = 1;
188: addTableCell(table, 0, 0, "t [time in ms]", null, false, true);
189: for (TurnSetup setup : setups) {
190: StringBuffer header = new StringBuffer();
191: boolean first = true;
192: for (SetupProperty sp : setup.properties()) {
193: if (!first) {
194: header.append("\n");
195: }
196: String name = sp.name();
197: if (!name.equals("commitinterval")) {
198: header.append(name);
199: header.append(":");
200: header.append(sp.value());
201: first = false;
202: }
203: }
204: addTableCell(table, idx, 0, header.toString(), null, true,
205: true);
206: idx++;
207: }
208: table.endHeaders();
209: int vidx = 1;
210: for (TeamCar teamCar : teamCars) {
211: addTableCell(table, 0, vidx, teamCar.toString(), teamCar
212: .website(), true, false);
213: int hidx = 1;
214: for (TurnSetup setup : setups) {
215: String text = String.valueOf(graph.timeFor(teamCar,
216: setup));
217: addTableCell(table, hidx, vidx, text, null, false,
218: false);
219: hidx++;
220: }
221: vidx++;
222: }
223: para.add(table);
224: para.add(new Chunk("\n", bigFont));
225: mDocument.add(para);
226: }
227:
228: private void addTableCell(Table table, int hidx, int vidx,
229: String text, String link, boolean bold, boolean header)
230: throws BadElementException {
231: Chunk chunk = new Chunk(text, FontFactory.getFont(
232: FontFactory.HELVETICA, 9, (bold ? Font.BOLD
233: : Font.PLAIN)));
234: chunk.setTextRise(3);
235: Cell cell = new Cell(linked(chunk, link));
236: cell.setHeader(header);
237: if (!header) {
238: cell.setNoWrap(true);
239: }
240: cell.setVerticalAlignment(Element.ALIGN_CENTER);
241: table.addCell(cell, new Point(vidx, hidx));
242: }
243:
244: private void renderChart(JFreeChart chart)
245: throws DocumentException, BadElementException {
246: int width = 500;
247: int height = 300;
248: PdfContentByte cb = mWriter.getDirectContent();
249: PdfTemplate tp = cb.createTemplate(width, height);
250: Graphics2D graphics = tp.createGraphics(width, height,
251: new DefaultFontMapper());
252: java.awt.Rectangle area = new java.awt.Rectangle(0, 0, width,
253: height);
254: chart.draw(graphics, area);
255: graphics.dispose();
256: ImgTemplate imgtmpl = new ImgTemplate(tp);
257: //imgtmpl.setAlignment(Element.ALIGN_CENTER);
258: mDocument.add(imgtmpl);
259: }
260:
261: private Table setupTable(Graph graph) throws BadElementException {
262: Table table = new Table(graph.setups().size() + 1);
263: table.setAutoFillEmptyCells(true);
264: table.setSpaceInsideCell(2);
265:
266: table.setBorderWidth(0);
267: table.setDefaultCellBorder(1);
268: table.setTableFitsPage(true);
269: return table;
270: }
271:
272: protected void finish() {
273: }
274: }
|