This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
DataListDownloadWebScript.java
407 lines (368 loc) · 13.8 KB
/
DataListDownloadWebScript.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
/*
* Copyright 2005 - 2020 Alfresco Software Limited.
*
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of the paid license agreement will prevail.
* Otherwise, the software is provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.web.scripts.datalist;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import org.alfresco.model.ContentModel;
import org.alfresco.model.DataListModel;
import org.alfresco.repo.web.scripts.DeclarativeSpreadsheetWebScript;
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.service.cmr.dictionary.TypeDefinition;
import org.alfresco.service.cmr.repository.AssociationRef;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.service.namespace.InvalidQNameException;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.Pair;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
* Data List Download
*
* Exports the contents of a Data List as an Excel file
*
* @author Nick Burch
*/
public class DataListDownloadWebScript extends DeclarativeSpreadsheetWebScript
implements InitializingBean
{
// Logger
private static final Log logger = LogFactory.getLog(DataListDownloadWebScript.class);
private static final QName DATA_LIST_ITEM_TYPE = DataListModel.PROP_DATALIST_ITEM_TYPE;
private NodeService nodeService;
private SiteService siteService;
private NamespaceService namespaceService;
private Map<QName,List<QName>> modelOrder;
private Map<String,String> rawModelOrder;
public DataListDownloadWebScript()
{
this.filenameBase = "DataListExport";
}
/**
* @param nodeService NodeService
*/
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
/**
* @param siteService SiteService
*/
public void setSiteService(SiteService siteService)
{
this.siteService = siteService;
}
/**
* @param namespaceService NamespaceService
*/
public void setNamespaceService(NamespaceService namespaceService)
{
this.namespaceService = namespaceService;
}
public void setModelOrder(Map<String,String> rawModelOrder)
{
this.rawModelOrder = rawModelOrder;
}
@Override
public void afterPropertiesSet() throws Exception {
modelOrder = new HashMap<QName, List<QName>>();
for(String key : rawModelOrder.keySet())
{
QName model;
List<QName> order = new ArrayList<QName>();
try
{
model= QName.createQName(key, namespaceService);
}
catch(InvalidQNameException e)
{
logger.warn("Skipping invalid model type " + key);
continue;
}
StringTokenizer st = new StringTokenizer(rawModelOrder.get(key), ",");
while(st.hasMoreTokens())
{
order.add( QName.createQName(st.nextToken(), namespaceService) );
}
modelOrder.put(model, order);
}
}
/**
* Identify the datalist
*/
@Override
protected Object identifyResource(String format, WebScriptRequest req) {
// Try to find the datalist they requested
NodeRef list;
Map<String,String> args = req.getServiceMatch().getTemplateVars();
if(args.get("store_type") != null)
{
list = new NodeRef(
args.get("store_type"),
args.get("store_id"),
args.get("id")
);
}
else
{
// Get the site
SiteInfo site = siteService.getSite(args.get("site"));
if(site == null)
{
throw new WebScriptException(Status.STATUS_NOT_FOUND, "Site not found with supplied name");
}
// Now find the data list container with in
NodeRef container = nodeService.getChildByName(
site.getNodeRef(),
ContentModel.ASSOC_CONTAINS,
args.get("container")
);
if(container == null)
{
throw new WebScriptException(Status.STATUS_NOT_FOUND, "Container not found within site");
}
// Now get the data list itself
list = nodeService.getChildByName(
container,
ContentModel.ASSOC_CONTAINS,
args.get("list")
);
}
if(list == null || !nodeService.exists(list))
{
throw new WebScriptException(Status.STATUS_NOT_FOUND, "The Data List could not be found");
}
return list;
}
/**
* We don't have a HTML version
*/
@Override
protected boolean allowHtmlFallback() {
return false;
}
/**
* Fetch the properties, in the requested order, from
* the data list definition
*/
@Override
protected List<Pair<QName, Boolean>> buildPropertiesForHeader(
Object resource, String format, WebScriptRequest req) {
NodeRef list = (NodeRef)resource;
QName type = buildType(list);
// Has the user given us rules for what to do
// with this type?
List<QName> props;
if(modelOrder.containsKey(type))
{
props = modelOrder.get(type);
}
else
{
// We'll have to try to guess it for them
// For now, just use DataList properties for the type
TypeDefinition typeDef = dictionaryService.getType(type);
Map<QName, PropertyDefinition> allProps = typeDef.getProperties();
props = new ArrayList<QName>();
for(QName prop : allProps.keySet())
{
if(NamespaceService.DATALIST_MODEL_1_0_URI.equals(prop.getNamespaceURI()))
{
props.add(prop);
}
}
}
// Everything is required
List<Pair<QName, Boolean>> properties = new ArrayList<Pair<QName,Boolean>>();
for(QName qname : props)
{
properties.add(new Pair<QName, Boolean>(qname, true));
}
return properties;
}
private QName buildType(NodeRef list)
{
String typeS = (String)nodeService.getProperty(list, DATA_LIST_ITEM_TYPE);
if(! typeS.startsWith(NamespaceService.DATALIST_MODEL_PREFIX + ":"))
{
throw new WebScriptException(Status.STATUS_NOT_IMPLEMENTED, "Unexpected list type " + typeS);
}
QName type = QName.createQName(NamespaceService.DATALIST_MODEL_1_0_URI, typeS.substring(typeS.indexOf(':')+1));
return type;
}
private List<NodeRef> getItems(NodeRef list)
{
Set<QName> typeSet = new HashSet<QName>(Arrays.asList(new QName[] { buildType(list) }));
List<NodeRef> items = new ArrayList<NodeRef>();
for(ChildAssociationRef ca : nodeService.getChildAssocs(list, typeSet))
{
items.add(ca.getChildRef());
}
return items;
}
@Override
protected void populateBody(Object resource, CSVPrinter csv,
List<QName> properties) throws IOException {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "CSV not currently supported");
}
@Override
protected void populateBody(Object resource, Workbook workbook,
Sheet sheet, List<QName> properties) throws IOException {
NodeRef list = (NodeRef)resource;
List<NodeRef> items = getItems(list);
// Our various formats
DataFormat formatter = workbook.createDataFormat();
CellStyle styleInt = workbook.createCellStyle();
styleInt.setDataFormat( formatter.getFormat("0") );
CellStyle styleDate = workbook.createCellStyle();
styleDate.setDataFormat( formatter.getFormat("yyyy-mm-dd") );
CellStyle styleDouble = workbook.createCellStyle();
styleDouble.setDataFormat( formatter.getFormat("General") );
CellStyle styleNewLines = workbook.createCellStyle();
styleNewLines.setWrapText(true);
// Export the items
int rowNum = 1, colNum = 0;
for(NodeRef item : items)
{
Row r = sheet.createRow(rowNum);
colNum = 0;
for(QName prop : properties)
{
Cell c = r.createCell(colNum);
Serializable val = nodeService.getProperty(item, prop);
if(val == null)
{
// Is it an association, or just missing?
List<AssociationRef> assocs = nodeService.getTargetAssocs(item, prop);
if(assocs.size() > 0)
{
StringBuffer text = new StringBuffer();
int lines = 1;
for(AssociationRef ref : assocs)
{
NodeRef child = ref.getTargetRef();
QName type = nodeService.getType(child);
if(ContentModel.TYPE_PERSON.equals(type))
{
if(text.length() > 0) {
text.append('\n');
lines++;
}
text.append(nodeService.getProperty(
child, ContentModel.PROP_USERNAME
));
}
else if(ContentModel.TYPE_CONTENT.equals(type))
{
// TODO Link to the content
if(text.length() > 0) {
text.append('\n');
lines++;
}
text.append(nodeService.getProperty(
child, ContentModel.PROP_TITLE
));
}
else
{
System.err.println("TODO: handle " + type + " for " + child);
}
}
String v = text.toString();
c.setCellValue( v );
if(lines > 1)
{
c.setCellStyle(styleNewLines);
r.setHeightInPoints( lines*sheet.getDefaultRowHeightInPoints() );
}
}
else
{
// This property isn't set
c.setCellType(CellType.BLANK);
}
}
else
{
// Regular property, set
if(val instanceof String)
{
c.setCellValue((String)val);
}
else if(val instanceof Date)
{
c.setCellValue((Date)val);
c.setCellStyle(styleDate);
}
else if(val instanceof Integer || val instanceof Long)
{
double v = 0.0;
if(val instanceof Long) v = (double)(Long)val;
if(val instanceof Integer) v = (double)(Integer)val;
c.setCellValue(v);
c.setCellStyle(styleInt);
}
else if(val instanceof Float || val instanceof Double)
{
double v = 0.0;
if(val instanceof Float) v = (double)(Float)val;
if(val instanceof Double) v = (double)(Double)val;
c.setCellValue(v);
c.setCellStyle(styleDouble);
}
else
{
// TODO
System.err.println("TODO: handle " + val.getClass().getName() + " - " + val);
}
}
colNum++;
}
rowNum++;
}
// Sensible column widths please!
colNum = 0;
for(QName prop : properties)
{
sheet.autoSizeColumn(colNum);
colNum++;
}
}
}