1
2
3
4 package de.tivsource.page.helper.odf;
5
6
7 import java.text.SimpleDateFormat;
8 import java.util.Iterator;
9 import java.util.List;
10
11 import org.odftoolkit.odfdom.dom.element.style.StyleMasterPageElement;
12 import org.odftoolkit.odfdom.incubator.doc.style.OdfStylePageLayout;
13 import org.odftoolkit.simple.SpreadsheetDocument;
14 import org.odftoolkit.simple.style.Font;
15 import org.odftoolkit.simple.style.PageLayoutProperties;
16 import org.odftoolkit.simple.style.StyleTypeDefinitions.FontStyle;
17 import org.odftoolkit.simple.style.StyleTypeDefinitions.PrintOrientation;
18 import org.odftoolkit.simple.table.Cell;
19 import org.odftoolkit.simple.table.Table;
20
21 import de.tivsource.page.entity.event.Event;
22 import de.tivsource.page.entity.location.Location;
23 import de.tivsource.page.entity.reservation.Reservation;
24
25
26
27
28
29 public class CreateReservationODF {
30
31 private SpreadsheetDocument spreadsheetDocument;
32
33 private Table table;
34
35 private Cell cell;
36
37 public SpreadsheetDocument create(List<Reservation> reservations) throws Exception {
38
39
40 SimpleDateFormat formatDate = new SimpleDateFormat("dd.MM.yyyy");
41
42
43 spreadsheetDocument = SpreadsheetDocument.newSpreadsheetDocument();
44
45
46 generatePageLayout(spreadsheetDocument);
47
48
49 table = spreadsheetDocument.getTableList().get(0);
50
51
52 Event event = reservations.get(0).getEvent();
53
54 Location location = reservations.get(0).getEvent().getLocation();
55
56
57 StringBuilder stringBuilder = new StringBuilder();
58 stringBuilder.append("Reservierungen für das ");
59 stringBuilder.append(event.getName("de"));
60 stringBuilder.append(" am ");
61 stringBuilder.append(formatDate.format(event.getBeginning()));
62 stringBuilder.append(" im ");
63 stringBuilder.append(location.getName("de"));
64
65 table.setTableName(stringBuilder.toString());
66
67
68
69
70
71
72
73
74
75
76
77
78 generateHeader();
79
80
81 generateContent(reservations);
82
83 return spreadsheetDocument;
84 }
85
86 private void generatePageLayout(SpreadsheetDocument spreadsheetDocument) {
87 StyleMasterPageElement defaultPage = spreadsheetDocument.getOfficeMasterStyles().getMasterPage("Default");
88 String pageLayoutName = defaultPage.getStylePageLayoutNameAttribute();
89 OdfStylePageLayout pageLayoutStyle = defaultPage.getAutomaticStyles().getPageLayout(pageLayoutName);
90 PageLayoutProperties pageLayoutProperties = PageLayoutProperties.getOrCreatePageLayoutProperties(pageLayoutStyle);
91 pageLayoutProperties.setPageHeight(210);
92 pageLayoutProperties.setPageWidth(297);
93 pageLayoutProperties.setPrintOrientation(PrintOrientation.LANDSCAPE);
94 }
95
96 private void generateHeader() {
97
98 cell = table.getCellByPosition(0,0);
99 cell.setStringValue("Anrede");
100 cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
101
102
103 cell = table.getCellByPosition(1,0);
104 cell.setStringValue("Vorname");
105 cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
106
107 cell = table.getCellByPosition(2,0);
108 cell.setStringValue("Nachname");
109 cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
110
111 cell = table.getCellByPosition(3,0);
112 cell.setStringValue("Telefon");
113 cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
114
115 cell = table.getCellByPosition(4,0);
116 cell.setStringValue("Personen");
117 cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
118
119 cell = table.getCellByPosition(5,0);
120 cell.setStringValue("Uhrzeit");
121 cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
122
123 cell = table.getCellByPosition(6,0);
124 cell.setStringValue("Wünsche");
125 cell.setFont(new Font("Arial", FontStyle.BOLD, 10));
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 }
141
142 private void generateContent(List<Reservation> reservations) {
143 SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm");
144 SimpleDateFormat formatDate = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
145 Iterator<Reservation> iterator = reservations.iterator();
146 Integer row = 1;
147 while(iterator.hasNext()) {
148 Reservation next = iterator.next();
149
150
151 cell = table.getCellByPosition(0,row);
152 cell.setStringValue( next.getGender() ? "Frau" : "Herr" );
153
154
155 cell = table.getCellByPosition(1,row);
156 cell.setStringValue(next.getFirstname());
157
158 cell = table.getCellByPosition(2,row);
159 cell.setStringValue(next.getLastname());
160
161 cell = table.getCellByPosition(3,row);
162 cell.setStringValue(next.getTelephone());
163
164 cell = table.getCellByPosition(4,row);
165 cell.setDoubleValue(next.getQuantity().doubleValue());
166
167 cell = table.getCellByPosition(5,row);
168 cell.setStringValue(formatTime.format(next.getTime()));
169
170 cell = table.getCellByPosition(6,row);
171 cell.setStringValue(next.getWishes());
172
173
174
175
176
177
178
179
180
181
182
183
184 row++;
185 }
186 }
187
188 }