View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * A helper class to be used with the custom ExportView implementations for
45   * Display Tag.  Most of the logic for interfacing with the KNS export
46   * system is encapsulated in this helper class so it can be shared between
47   * the various Display Tag export implementations. 
48   * 
49   * @author Kuali Rice Team (rice.collab@kuali.org)
50   *
51   * @deprecated KNS Struts deprecated, use KRAD and the Spring MVC framework.
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     // KULRICE-12281: Turn off the ability to export results from the certain lookups
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 }