-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXLSXResponseWriter.java
407 lines (346 loc) · 11.4 KB
/
XLSXResponseWriter.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package org.stig.solr.response;
import org.apache.lucene.index.IndexableField;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.DateUtil;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.response.RawResponseWriter;
import org.apache.solr.response.TextResponseWriter;
import org.apache.solr.response.QueryResponseWriter;
import org.apache.solr.response.ResultContext;
import org.apache.solr.schema.FieldType;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.schema.StrField;
import org.apache.solr.search.DocList;
import org.apache.solr.search.ReturnFields;
import org.apache.solr.core.SolrCore;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ContentStreamBase;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.Writer;
import java.io.CharArrayWriter;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.awt.Color;
import java.util.*;
/**
*
*/
public class XLSXResponseWriter extends RawResponseWriter {
@Override
public void init(NamedList n) {
}
@Override
public void write(OutputStream out, SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {
//throwaway arraywriter just to satisfy super requirements; we're grabbing
//all writes before they go to it anyway
XLSXWriter w = new XLSXWriter(new CharArrayWriter(), req, rsp);
try {
w.writeResponse(out);
} finally {
w.close();
}
}
@Override
public String getContentType(SolrQueryRequest request, SolrQueryResponse response) {
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
}
class XLSXWriter extends TextResponseWriter {
SolrQueryRequest req;
SolrQueryResponse rsp;
Logger log = LoggerFactory.getLogger(SolrCore.class);
Calendar cal;
class SerialWriteWorkbook {
SXSSFWorkbook swb;
Sheet sh;
XSSFCellStyle headerStyle;
int rowIndex;
Row curRow;
int cellIndex;
public SerialWriteWorkbook() {
this.swb = new SXSSFWorkbook(100);
this.sh = this.swb.createSheet();
this.rowIndex = 0;
this.headerStyle = (XSSFCellStyle)swb.createCellStyle();
this.headerStyle.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
//solid fill
this.headerStyle.setFillPattern((short)1);
Font headerFont = swb.createFont();
headerFont.setFontHeightInPoints((short)14);
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
headerFont.setColor(IndexedColors.WHITE.getIndex());
this.headerStyle.setFont(headerFont);
}
public void addRow() {
curRow = sh.createRow(rowIndex++);
cellIndex = 0;
}
public void setHeaderRow() {
curRow.setHeightInPoints((short)21);
}
//sets last created cell to have header style
public void setHeaderCell() {
curRow.getCell(cellIndex - 1).setCellStyle(this.headerStyle);
}
//set the width of the most recently created column
public void setColWidth(int charWidth) {
//width is set in units of 1/256th of a character width for some reason
this.sh.setColumnWidth(cellIndex - 1, 256*charWidth);
}
public void writeCell(String value) {
Cell cell = curRow.createCell(cellIndex++);
cell.setCellValue(value);
}
public void flush(OutputStream out) {
try {
swb.write(out);
log.info("Flush complete");
} catch (IOException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String stacktrace = sw.toString();
log.info("Failed to export to XLSX - "+stacktrace);
}finally {
//clean temp files created by poi
swb.dispose();
}
}
}
SerialWriteWorkbook wb = new SerialWriteWorkbook();
static class Field {
String name;
SchemaField sf;
// used to collect values
List<IndexableField> values = new ArrayList<IndexableField>(1); // low starting amount in case there are many fields
int tmp;
}
Map<String,Field> xlFields = new LinkedHashMap<String,Field>();
public XLSXWriter(Writer writer, SolrQueryRequest req, SolrQueryResponse rsp){
super(writer, req, rsp);
this.req = req;
this.rsp = rsp;
}
public void writeResponse(OutputStream out) throws IOException {
SolrParams params = req.getParams();
log.info("Beginning export");
Collection<String> fields = returnFields.getRequestedFieldNames();
Object responseObj = rsp.getValues().get("response");
boolean returnOnlyStored = false;
if (fields==null||returnFields.hasPatternMatching()) {
if (responseObj instanceof SolrDocumentList) {
// get the list of fields from the SolrDocumentList
if(fields==null) {
fields = new LinkedHashSet<String>();
}
for (SolrDocument sdoc: (SolrDocumentList)responseObj) {
fields.addAll(sdoc.getFieldNames());
}
} else {
// get the list of fields from the index
Collection<String> all = req.getSearcher().getFieldNames();
if(fields==null) {
fields = all;
}
else {
fields.addAll(all);
}
}
if (returnFields.wantsScore()) {
fields.add("score");
} else {
fields.remove("score");
}
returnOnlyStored = true;
}
for (String field : fields) {
if (!returnFields.wantsField(field)) {
continue;
}
if (field.equals("score")) {
Field xlField = new Field();
xlField.name = "score";
xlFields.put("score", xlField);
continue;
}
SchemaField sf = schema.getFieldOrNull(field);
if (sf == null) {
FieldType ft = new StrField();
sf = new SchemaField(field, ft);
}
// Return only stored fields, unless an explicit field list is specified
if (returnOnlyStored && sf != null && !sf.stored()) {
continue;
}
Field xlField = new Field();
xlField.name = field;
xlField.sf = sf;
xlFields.put(field, xlField);
}
NiceMetadataNames niceMap = new NiceMetadataNames();
wb.addRow();
//write header
for (Field xlField : xlFields.values()) {
String printName = xlField.name;
int colWidth = 14;
NameAndWidth nextField = niceMap.get(xlField.name);
if (nextField != null) {
printName = nextField.getName();;
colWidth = nextField.getWidth();
}
writeStr(xlField.name, printName, false);
wb.setColWidth(colWidth);
wb.setHeaderCell();
}
wb.setHeaderRow();
wb.addRow();
//write rows
if (responseObj instanceof ResultContext ) {
writeDocuments(null, (ResultContext)responseObj, returnFields );
}
else if (responseObj instanceof DocList) {
ResultContext ctx = new ResultContext();
ctx.docs = (DocList)responseObj;
writeDocuments(null, ctx, returnFields );
} else if (responseObj instanceof SolrDocumentList) {
writeSolrDocumentList(null, (SolrDocumentList)responseObj, returnFields );
}
log.info("Export complete; flushing document");
//flush to outputstream
wb.flush(out);
wb = null;
}
@Override
public void close() throws IOException {
super.close();
}
@Override
public void writeNamedList(String name, NamedList val) throws IOException {
}
@Override
public void writeStartDocumentList(String name,
long start, int size, long numFound, Float maxScore) throws IOException
{
// nothing
}
@Override
public void writeEndDocumentList() throws IOException
{
// nothing
}
//NOTE: a document cannot currently contain another document
List tmpList;
@Override
public void writeSolrDocument(String name, SolrDocument doc, ReturnFields returnFields, int idx ) throws IOException {
if (tmpList == null) {
tmpList = new ArrayList(1);
tmpList.add(null);
}
for (Field xlField : xlFields.values()) {
Object val = doc.getFieldValue(xlField.name);
int nVals = val instanceof Collection ? ((Collection)val).size() : (val==null ? 0 : 1);
if (nVals == 0) {
writeNull(xlField.name);
continue;
}
if ((xlField.sf != null && xlField.sf.multiValued()) || nVals > 1) {
Collection values;
// normalize to a collection
if (val instanceof Collection) {
values = (Collection)val;
} else {
tmpList.set(0, val);
values = tmpList;
}
writeArray(xlField.name, values.iterator());
} else {
// normalize to first value
if (val instanceof Collection) {
Collection values = (Collection)val;
val = values.iterator().next();
}
writeVal(xlField.name, val);
}
}
wb.addRow();
}
@Override
public void writeStr(String name, String val, boolean needsEscaping) throws IOException {
wb.writeCell(val);
}
@Override
public void writeMap(String name, Map val, boolean excludeOuter, boolean isFirstVal) throws IOException {
}
@Override
public void writeArray(String name, Iterator val) throws IOException {
StringBuffer output = new StringBuffer();
while (val.hasNext()) {
Object v = val.next();
if (v instanceof IndexableField) {
IndexableField f = (IndexableField)v;
output.append(f.stringValue() + "; ");
} else {
output.append(v.toString() + "; ");
}
}
if (output.length() > 0) {
output.deleteCharAt(output.length()-1);
output.deleteCharAt(output.length()-1);
}
writeStr(name, output.toString(), false);
}
@Override
public void writeNull(String name) throws IOException {
wb.writeCell("");
}
@Override
public void writeInt(String name, String val) throws IOException {
wb.writeCell(val);
}
@Override
public void writeLong(String name, String val) throws IOException {
wb.writeCell(val);
}
@Override
public void writeBool(String name, String val) throws IOException {
wb.writeCell(val);
}
@Override
public void writeFloat(String name, String val) throws IOException {
wb.writeCell(val);
}
@Override
public void writeDouble(String name, String val) throws IOException {
wb.writeCell(val);
}
@Override
public void writeDate(String name, Date val) throws IOException {
StringBuilder sb = new StringBuilder(25);
cal = DateUtil.formatDate(val, cal, sb);
String outputDate = sb.substring(0,10);
writeDate(name, outputDate);
}
@Override
public void writeDate(String name, String val) throws IOException {
wb.writeCell(val);
}
}