001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kns.lookup;
017
018import java.io.IOException;
019import java.io.OutputStream;
020import java.io.Writer;
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.lang.StringUtils;
027import org.directwebremoting.util.WriterOutputStream;
028import org.displaytag.model.Row;
029import org.displaytag.model.TableModel;
030import org.kuali.rice.kim.api.services.KimApiServiceLocator;
031import org.kuali.rice.kns.util.KNSGlobalVariables;
032import org.kuali.rice.kns.web.struts.form.KualiForm;
033import org.kuali.rice.kns.web.struts.form.LookupForm;
034import org.kuali.rice.kns.web.ui.ResultRow;
035import org.kuali.rice.krad.bo.BusinessObject;
036import org.kuali.rice.krad.bo.Exporter;
037import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
038import org.kuali.rice.krad.exception.AuthorizationException;
039import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
040import org.kuali.rice.krad.exception.ExportNotSupportedException;
041import org.kuali.rice.krad.util.GlobalVariables;
042
043/**
044 * A helper class to be used with the custom ExportView implementations for
045 * Display Tag.  Most of the logic for interfacing with the KNS export
046 * system is encapsulated in this helper class so it can be shared between
047 * the various Display Tag export implementations. 
048 * 
049 * @author Kuali Rice Team (rice.collab@kuali.org)
050 *
051 * @deprecated KNS Struts deprecated, use KRAD and the Spring MVC framework.
052 */
053@Deprecated
054public class ExportViewHelper {
055
056        private BusinessObjectEntry businessObjectEntry;
057        private List<BusinessObject> businessObjects;
058        
059        public ExportViewHelper(TableModel tableModel) {
060                this.businessObjectEntry = loadBusinessObjectEntry();
061                this.businessObjects = loadBusinessObjects(tableModel);
062        }
063        
064        protected BusinessObjectEntry loadBusinessObjectEntry() {
065                KualiForm kualiForm = KNSGlobalVariables.getKualiForm();
066                if (kualiForm instanceof LookupForm) {
067                        LookupForm lookupForm = (LookupForm) kualiForm;
068                        if (!StringUtils.isBlank(lookupForm.getBusinessObjectClassName())) {
069                                return KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(lookupForm.getBusinessObjectClassName());
070                        }
071                }
072                return null;
073        }
074        
075        protected List<BusinessObject> loadBusinessObjects(TableModel tableModel) {
076                List<BusinessObject> businessObjects = new ArrayList<BusinessObject>();
077                List<Row> rowList = tableModel.getRowListFull();
078                for (Row row : rowList) {
079                        ResultRow resultRow = (ResultRow)row.getObject();
080                        if (resultRow.getBusinessObject() != null) {
081                                businessObjects.add(resultRow.getBusinessObject());
082                        }
083                }
084                return businessObjects;
085        }
086        
087        public BusinessObjectEntry getBusinessObjectEntry() {
088                return businessObjectEntry;
089        }
090        
091        public List<BusinessObject> getBusinessObjects() {
092                return businessObjects;
093        }
094        
095        public boolean attemptCustomExport(OutputStream outputStream, String exportFormat) throws IOException {
096                if (getBusinessObjectEntry() != null && getBusinessObjectEntry().getExporterClass() != null) {
097                        final Exporter exporter;
098                        try {
099                                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}