1
18 package net.bull.javamelody.internal.web.pdf;
19
20 import java.text.DecimalFormat;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24
25 import com.lowagie.text.Anchor;
26 import com.lowagie.text.Document;
27 import com.lowagie.text.DocumentException;
28 import com.lowagie.text.Element;
29 import com.lowagie.text.Font;
30 import com.lowagie.text.Paragraph;
31 import com.lowagie.text.pdf.PdfPCell;
32
33 import net.bull.javamelody.internal.common.I18N;
34 import net.bull.javamelody.internal.model.CacheInformations;
35 import net.bull.javamelody.internal.web.html.HtmlCacheInformationsReport;
36
37
41 class PdfCacheInformationsReport extends PdfAbstractTableReport {
42 private final List<CacheInformations> cacheInformationsList;
43 private final DecimalFormat integerFormat = I18N.createIntegerFormat();
44 private final Font cellFont = PdfFonts.TABLE_CELL.getFont();
45 private final boolean hitsRatioEnabled;
46 private final boolean configurationEnabled;
47
48 PdfCacheInformationsReport(List<CacheInformations> cacheInformationsList, Document document) {
49 super(document);
50 assert cacheInformationsList != null;
51
52 this.cacheInformationsList = cacheInformationsList;
53 this.hitsRatioEnabled = HtmlCacheInformationsReport
54 .isHitsRatioEnabled(cacheInformationsList);
55 this.configurationEnabled = HtmlCacheInformationsReport
56 .isConfigurationEnabled(cacheInformationsList);
57 }
58
59 @Override
60 void toPdf() throws DocumentException {
61 writeHeader();
62
63 for (final CacheInformations cacheInformations : cacheInformationsList) {
64 nextRow();
65 writeCacheInformations(cacheInformations);
66 }
67 addTableToDocument();
68 if (!hitsRatioEnabled) {
69 final Paragraph statisticsEnabledParagraph = new Paragraph(
70 getString("caches_statistics_enable"), cellFont);
71 statisticsEnabledParagraph.setAlignment(Element.ALIGN_RIGHT);
72 addToDocument(statisticsEnabledParagraph);
73 }
74 addConfigurationReference();
75 }
76
77 private void addConfigurationReference() throws DocumentException {
78 final Anchor ehcacheAnchor = new Anchor("Configuration reference", PdfFonts.BLUE.getFont());
79 ehcacheAnchor.setName("Ehcache configuration reference");
80 ehcacheAnchor.setReference(
81 "http:);
82 ehcacheAnchor.setFont(PdfFonts.BLUE.getFont());
83 final Paragraph ehcacheParagraph = new Paragraph();
84 ehcacheParagraph.add(ehcacheAnchor);
85 ehcacheParagraph.setAlignment(Element.ALIGN_RIGHT);
86 addToDocument(ehcacheParagraph);
87 }
88
89 private void writeHeader() throws DocumentException {
90 final List<String> headers = createHeaders();
91 final int[] relativeWidths = new int[headers.size()];
92 Arrays.fill(relativeWidths, 0, headers.size(), 1);
93 if (configurationEnabled) {
94 relativeWidths[headers.size() - 1] = 4;
95 }
96
97 initTable(headers, relativeWidths);
98 }
99
100 private List<String> createHeaders() {
101 final List<String> headers = new ArrayList<>();
102 headers.add(getString("Cache"));
103 if (configurationEnabled) {
104 headers.add(getString("Pourcentage_memoire_utilise"));
105 }
106 headers.add(getString("Nb_objets_en_memoire"));
107 headers.add(getString("Nb_objets_sur_disque"));
108 if (hitsRatioEnabled) {
109 headers.add(getString("Efficacite_cache_memoire"));
110 headers.add(getString("Efficacite_cache"));
111 }
112 if (configurationEnabled) {
113 headers.add(getString("Configuration"));
114 }
115 return headers;
116 }
117
118 private void writeCacheInformations(CacheInformations cacheInformations) {
119 final PdfPCell defaultCell = getDefaultCell();
120 defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
121 addCell(cacheInformations.getName());
122 defaultCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
123 if (configurationEnabled) {
124 addCell(integerFormat.format(cacheInformations.getInMemoryPercentUsed()));
125 }
126 addCell(integerFormat.format(cacheInformations.getInMemoryObjectCount()));
127 addCell(integerFormat.format(cacheInformations.getOnDiskObjectCount()));
128 if (hitsRatioEnabled) {
129 addCell(integerFormat.format(cacheInformations.getInMemoryHitsRatio()));
130 addCell(integerFormat.format(cacheInformations.getHitsRatio()));
131 }
132 if (configurationEnabled) {
133 defaultCell.setHorizontalAlignment(Element.ALIGN_LEFT);
134 addCell(cacheInformations.getConfiguration());
135 }
136 }
137 }
138