-
Notifications
You must be signed in to change notification settings - Fork 0
/
Report.java
126 lines (110 loc) · 5.14 KB
/
Report.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* @author Yannis Exidaridis <[email protected]>
* @brief pdf output.
* Uses iText library (https://itextpdf.com)
*/
package albums;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.List;
import com.itextpdf.layout.element.ListItem;
import com.itextpdf.layout.element.Text;
import com.itextpdf.io.font.PdfEncodings;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Report {
private final Document document;
private final PdfDocument pdf;
// pdf filename
private static final String FILENAME = "albums.pdf";
// font size
private static final float ARTIST_FONTSIZE = 10f;
private static final float ALBUM_FONTSIZE = 9f;
private static final String TITLE = "Albums List";
public Report() throws FileNotFoundException {
File file = new File(FILENAME);
FileOutputStream fos = new FileOutputStream(file);
PdfWriter writer = new PdfWriter(fos);
pdf = new PdfDocument(writer);
document = new Document(pdf);
}
/**
* @brief create report in PDF format
* @throws IOException
*/
public void CreateReport() throws IOException {
try (document) {
String ListEntry, text, pdf_font = null;
Config cfg = new Config();
if (cfg.ReadConfig()) { // read font file
pdf_font = cfg.get_pdf_font();
}
PdfFont font = PdfFontFactory.createFont(pdf_font, PdfEncodings.IDENTITY_H);
document.add(new Paragraph(TITLE).setFont(font).setFontSize(14f));
try {
Database db = new Database();
ResultSet rs = db.FetchArtist();
while (rs.next()) {
Text ArtistName = new Text(rs.getString(db.get_fld_artist())); // artist name
ArtistName.setUnderline(1, -3);
document.add(new Paragraph(ArtistName)).setFont(font).setFontSize(ARTIST_FONTSIZE);
ResultSet res = db.SearchAlbum(rs.getString(db.get_fld_artist()));
while (res.next()) {
List list = new List();
list.setFont(font);
list.setFontSize(ALBUM_FONTSIZE);
list.setSymbolIndent(8f);
ListEntry = res.getString(db.get_fld_title()); // artist album
ListEntry += " (" + res.getString(db.get_fld_format()); // album format
if (res.getString(db.get_fld_year()) != null && !res.getString(db.get_fld_year()).isEmpty()) {
ListEntry += " - " + res.getString(db.get_fld_year()); // year
if (res.getString(db.get_fld_label()) != null && !res.getString(db.get_fld_label()).isEmpty()) {
ListEntry += " @ " + res.getString(db.get_fld_label()); // label
}
}
ListEntry += ")";
String AlbumComment = res.getString(db.get_fld_comment()); // album comment
if (AlbumComment != null && !AlbumComment.isEmpty()) {
// TO DO: display compatible unicode character !!
ListEntry += " " + Character.toString((char)0x1F449) + AlbumComment;
}
list.setSymbolIndent(20f);
list.add(new ListItem(ListEntry));
document.add(list);
}
}
// statistics
ResultSet data = db.CountAlbum();
while (data.next()) {
if (data.getString(db.get_fld_format()) != null) {
text = data.getString(db.get_fld_format());
} else {
text = "Total";
}
text += ": ";
text += data.getString("cnt");
document.add(new Paragraph(new Text(text)));
}
// date stamp
String TimeStamp = ZonedDateTime.now().format(DateTimeFormatter.ofPattern("d MMM uuuu"));
text = "(updated at: " + TimeStamp + ")";
document.add(new Paragraph(new Text(text)));
} catch (SQLException ex) {
Logger.getLogger(DisplayArtistAlbumsForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}