1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.lookup;
17
18 import java.io.IOException;
19 import java.io.OutputStream;
20 import java.io.Writer;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.displaytag.model.Row;
26 import org.displaytag.model.TableModel;
27 import org.kuali.rice.core.util.WriterOutputStream;
28 import org.kuali.rice.kns.bo.BusinessObject;
29 import org.kuali.rice.kns.bo.Exporter;
30 import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
31 import org.kuali.rice.kns.exception.KualiException;
32 import org.kuali.rice.kns.service.KNSServiceLocator;
33 import org.kuali.rice.kns.util.GlobalVariables;
34 import org.kuali.rice.kns.web.struts.form.KualiForm;
35 import org.kuali.rice.kns.web.struts.form.LookupForm;
36 import org.kuali.rice.kns.web.ui.ResultRow;
37
38
39
40
41
42
43
44
45
46
47 public class ExportViewHelper {
48
49 private BusinessObjectEntry businessObjectEntry;
50 private List<BusinessObject> businessObjects;
51
52 public ExportViewHelper(TableModel tableModel) {
53 this.businessObjectEntry = loadBusinessObjectEntry();
54 this.businessObjects = loadBusinessObjects(tableModel);
55 }
56
57 protected BusinessObjectEntry loadBusinessObjectEntry() {
58 KualiForm kualiForm = GlobalVariables.getKualiForm();
59 if (kualiForm instanceof LookupForm) {
60 LookupForm lookupForm = (LookupForm) kualiForm;
61 if (!StringUtils.isBlank(lookupForm.getBusinessObjectClassName())) {
62 return KNSServiceLocator.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(lookupForm.getBusinessObjectClassName());
63 }
64 }
65 return null;
66 }
67
68 protected List<BusinessObject> loadBusinessObjects(TableModel tableModel) {
69 List<BusinessObject> businessObjects = new ArrayList<BusinessObject>();
70 List<Row> rowList = tableModel.getRowListFull();
71 for (Row row : rowList) {
72 ResultRow resultRow = (ResultRow)row.getObject();
73 if (resultRow.getBusinessObject() != null) {
74 businessObjects.add(resultRow.getBusinessObject());
75 }
76 }
77 return businessObjects;
78 }
79
80 public BusinessObjectEntry getBusinessObjectEntry() {
81 return businessObjectEntry;
82 }
83
84 public List<BusinessObject> getBusinessObjects() {
85 return businessObjects;
86 }
87
88 public boolean attemptCustomExport(OutputStream outputStream, String exportFormat) throws IOException {
89 if (getBusinessObjectEntry() != null && getBusinessObjectEntry().getExporterClass() != null) {
90 Exporter exporter = null;
91 try {
92 exporter = getBusinessObjectEntry().getExporterClass().newInstance();
93 } catch (Exception e) {
94 throw new KualiException("Failed to load export class: " + businessObjectEntry.getExporterClass());
95 }
96 List<String> supportedFormats = exporter.getSupportedFormats(businessObjectEntry.getBusinessObjectClass());
97 if (supportedFormats.contains(exportFormat)) {
98 exporter.export(businessObjectEntry.getBusinessObjectClass(), getBusinessObjects(), exportFormat, outputStream);
99 return true;
100 }
101 }
102 return false;
103 }
104
105 public boolean attemptCustomExport(Writer writer, String exportFormat) throws IOException {
106 return attemptCustomExport(new WriterOutputStream(writer), exportFormat);
107 }
108
109 }