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.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.directwebremoting.util.WriterOutputStream;
28 import org.displaytag.model.Row;
29 import org.displaytag.model.TableModel;
30 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
31 import org.kuali.rice.kns.util.KNSGlobalVariables;
32 import org.kuali.rice.kns.web.struts.form.KualiForm;
33 import org.kuali.rice.kns.web.struts.form.LookupForm;
34 import org.kuali.rice.kns.web.ui.ResultRow;
35 import org.kuali.rice.krad.bo.BusinessObject;
36 import org.kuali.rice.krad.bo.Exporter;
37 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
38 import org.kuali.rice.krad.exception.AuthorizationException;
39 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
40 import org.kuali.rice.krad.exception.ExportNotSupportedException;
41 import org.kuali.rice.krad.util.GlobalVariables;
42
43
44
45
46
47
48
49
50
51
52
53 @Deprecated
54 public class ExportViewHelper {
55
56 private BusinessObjectEntry businessObjectEntry;
57 private List<BusinessObject> businessObjects;
58
59 public ExportViewHelper(TableModel tableModel) {
60 this.businessObjectEntry = loadBusinessObjectEntry();
61 this.businessObjects = loadBusinessObjects(tableModel);
62 }
63
64 protected BusinessObjectEntry loadBusinessObjectEntry() {
65 KualiForm kualiForm = KNSGlobalVariables.getKualiForm();
66 if (kualiForm instanceof LookupForm) {
67 LookupForm lookupForm = (LookupForm) kualiForm;
68 if (!StringUtils.isBlank(lookupForm.getBusinessObjectClassName())) {
69 return KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(lookupForm.getBusinessObjectClassName());
70 }
71 }
72 return null;
73 }
74
75 protected List<BusinessObject> loadBusinessObjects(TableModel tableModel) {
76 List<BusinessObject> businessObjects = new ArrayList<BusinessObject>();
77 List<Row> rowList = tableModel.getRowListFull();
78 for (Row row : rowList) {
79 ResultRow resultRow = (ResultRow)row.getObject();
80 if (resultRow.getBusinessObject() != null) {
81 businessObjects.add(resultRow.getBusinessObject());
82 }
83 }
84 return businessObjects;
85 }
86
87 public BusinessObjectEntry getBusinessObjectEntry() {
88 return businessObjectEntry;
89 }
90
91 public List<BusinessObject> getBusinessObjects() {
92 return businessObjects;
93 }
94
95 public boolean attemptCustomExport(OutputStream outputStream, String exportFormat) throws IOException {
96 if (getBusinessObjectEntry() != null && getBusinessObjectEntry().getExporterClass() != null) {
97 final Exporter exporter;
98 try {
99 exporter = getBusinessObjectEntry().getExporterClass().newInstance();
100 } catch (Exception e) {
101 throw new ExportNotSupportedException("Failed to load export class: " + businessObjectEntry.getExporterClass(),e);
102 }
103 List<String> supportedFormats = exporter.getSupportedFormats(businessObjectEntry.getBusinessObjectClass());
104 if (supportedFormats.contains(exportFormat)) {
105 exporter.export(businessObjectEntry.getBusinessObjectClass(), getBusinessObjects(), exportFormat, outputStream);
106 return true;
107 }
108 }
109 return false;
110 }
111
112 public boolean attemptCustomExport(Writer writer, String exportFormat) throws IOException {
113 return attemptCustomExport(new WriterOutputStream(writer), exportFormat);
114 }
115
116
117 public void checkPermission() throws AuthorizationException {
118 boolean isAuthorized = false;
119 String componentName = businessObjectEntry.getBusinessObjectClass().getName();
120 String nameSpaceCode = "KR-NS";
121 String templateName = "Export Records";
122 String principalId = GlobalVariables.getUserSession().getPrincipalId();
123 String principalUserName = GlobalVariables.getUserSession().getPrincipalName();
124 Map<String, String> permissionDetails = new HashMap<String,String>();
125 permissionDetails.put("componentName", componentName);
126 isAuthorized = KimApiServiceLocator.getPermissionService().isAuthorizedByTemplate(principalId,nameSpaceCode,templateName,permissionDetails,new HashMap<String,String>());
127 if(!isAuthorized){
128 throw new AuthorizationException(principalUserName, "Exporting the LookUp Results", componentName);
129 }
130 }
131
132 }