-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathFileBuilder.java
More file actions
215 lines (169 loc) · 5.45 KB
/
FileBuilder.java
File metadata and controls
215 lines (169 loc) · 5.45 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package org.vaadin.haijian.filegenerator;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import com.vaadin.data.Container;
import com.vaadin.data.Property;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.ui.Table;
public abstract class FileBuilder implements Serializable {
protected File file;
public Container container;
public Table table;
private Object[] visibleColumns;
private Map<Object, String> columnHeaderMap;
private String header;
private Locale locale = Locale.getDefault();;
private String dateFormatString = "MM/dd/yyyy hh:mm";
public FileBuilder() {
}
public FileBuilder(Container container) {
setContainer(container);
}
public FileBuilder(Table table) {
setTable(table);
}
public void setTable(Table table) {
this.table = table;
setContainer(table.getContainerDataSource());
}
public void setContainer(Container container) {
this.container = container;
columnHeaderMap = new HashMap<Object, String>();
for (Object propertyId : container.getContainerPropertyIds()) {
columnHeaderMap
.put(propertyId, propertyId.toString().toUpperCase());
}
if (visibleColumns == null) {
visibleColumns = container.getContainerPropertyIds().toArray();
}
}
public void setVisibleColumns(Object[] visibleColumns) {
this.visibleColumns = visibleColumns;
}
public File getFile() {
try {
initTempFile();
resetContent();
buildFileContent();
writeToFile();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
private void initTempFile() throws IOException {
if (file != null) {
file.delete();
}
file = createTempFile();
}
protected void buildFileContent() {
buildHeader();
buildColumnHeaders();
buildRows();
buildFooter();
}
protected void resetContent() {
}
protected void buildColumnHeaders() {
if (visibleColumns.length == 0) {
return;
}
onHeader();
for (Object propertyId : visibleColumns) {
String header = columnHeaderMap.get(propertyId);
onNewCell();
buildColumnHeaderCell(header);
}
}
protected void onHeader() {
onNewRow();
}
protected void buildColumnHeaderCell(String header) {
}
protected void buildHeader() {
// TODO Auto-generated method stub
}
private void buildRows() {
if (container == null || container.getItemIds().isEmpty()) {
return;
}
for (Object itemId : container.getItemIds()) {
onNewRow();
buildRow(itemId);
}
}
private void buildRow(Object itemId) {
if (visibleColumns.length == 0) {
return;
}
for (Object propertyId : visibleColumns) {
Property<?> property = container.getContainerProperty(itemId,
propertyId);
Object modelValue = property != null ? property.getValue() : null;
Object presentationValue = modelValue;
Converter<String, Object> converter = table != null ? table.getConverter(propertyId) : null;
if (converter != null && modelValue != null) {
presentationValue = converter.convertToPresentation(property.getValue(), locale);
}
onNewCell();
buildCell(modelValue, presentationValue);
}
}
protected void onNewRow() {
}
protected void onNewCell() {
}
/**
* Build the cell, with the provided value. If the implementation supports formatting specific
* classes, it should attempt to use modelValue. If modelValue has an unknown classes, it
* should rely on presentationValue, that will use Table's converters if available.
*
* @param modelValue The model value
* @param presentationValue The presentation value, using the table converter if available.
*/
protected abstract void buildCell(Object modelValue, Object presentationValue);
protected void buildFooter() {
// TODO Auto-generated method stub
}
protected abstract String getFileExtension();
protected String getFileName() {
return "tmp";
}
protected File createTempFile() throws IOException {
return File.createTempFile(getFileName(), getFileExtension());
}
protected abstract void writeToFile();
public void setColumnHeader(Object propertyId, String header) {
columnHeaderMap.put(propertyId, header);
}
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
protected int getNumberofColumns() {
return visibleColumns.length;
}
public void setLocale(Locale locale) {
this.locale = locale;
}
public void setDateFormat(String dateFormat) {
this.dateFormatString = dateFormat;
}
protected String getDateFormatString(){
return dateFormatString;
}
protected String formatDate(Date date){
SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatString, locale);
return dateFormat.format(date);
}
}