001/**
002 * Copyright 2005-2016 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.List;
023
024import org.apache.commons.lang.StringUtils;
025import org.directwebremoting.util.WriterOutputStream;
026import org.displaytag.model.Row;
027import org.displaytag.model.TableModel;
028import org.kuali.rice.kns.util.KNSGlobalVariables;
029import org.kuali.rice.kns.web.struts.form.KualiForm;
030import org.kuali.rice.kns.web.struts.form.LookupForm;
031import org.kuali.rice.kns.web.ui.ResultRow;
032import org.kuali.rice.krad.bo.BusinessObject;
033import org.kuali.rice.krad.bo.Exporter;
034import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
035import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
036import org.kuali.rice.krad.exception.ExportNotSupportedException;
037import org.kuali.rice.krad.util.GlobalVariables;
038
039/**
040 * A helper class to be used with the custom ExportView implementations for
041 * Display Tag.  Most of the logic for interfacing with the KNS export
042 * system is encapsulated in this helper class so it can be shared between
043 * the various Display Tag export implementations. 
044 * 
045 * @author Kuali Rice Team (rice.collab@kuali.org)
046 *
047 */
048public class ExportViewHelper {
049
050        private BusinessObjectEntry businessObjectEntry;
051        private List<BusinessObject> businessObjects;
052        
053        public ExportViewHelper(TableModel tableModel) {
054                this.businessObjectEntry = loadBusinessObjectEntry();
055                this.businessObjects = loadBusinessObjects(tableModel);
056        }
057        
058        protected BusinessObjectEntry loadBusinessObjectEntry() {
059                KualiForm kualiForm = KNSGlobalVariables.getKualiForm();
060                if (kualiForm instanceof LookupForm) {
061                        LookupForm lookupForm = (LookupForm) kualiForm;
062                        if (!StringUtils.isBlank(lookupForm.getBusinessObjectClassName())) {
063                                return KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(lookupForm.getBusinessObjectClassName());
064                        }
065                }
066                return null;
067        }
068        
069        protected List<BusinessObject> loadBusinessObjects(TableModel tableModel) {
070                List<BusinessObject> businessObjects = new ArrayList<BusinessObject>();
071                List<Row> rowList = tableModel.getRowListFull();
072                for (Row row : rowList) {
073                        ResultRow resultRow = (ResultRow)row.getObject();
074                        if (resultRow.getBusinessObject() != null) {
075                                businessObjects.add(resultRow.getBusinessObject());
076                        }
077                }
078                return businessObjects;
079        }
080        
081        public BusinessObjectEntry getBusinessObjectEntry() {
082                return businessObjectEntry;
083        }
084        
085        public List<BusinessObject> getBusinessObjects() {
086                return businessObjects;
087        }
088        
089        public boolean attemptCustomExport(OutputStream outputStream, String exportFormat) throws IOException {
090                if (getBusinessObjectEntry() != null && getBusinessObjectEntry().getExporterClass() != null) {
091                        final Exporter exporter;
092                        try {
093                                exporter = getBusinessObjectEntry().getExporterClass().newInstance();
094                        } catch (Exception e) {
095                                throw new ExportNotSupportedException("Failed to load export class: " + businessObjectEntry.getExporterClass(),e);
096                        }
097                        List<String> supportedFormats = exporter.getSupportedFormats(businessObjectEntry.getBusinessObjectClass());
098                        if (supportedFormats.contains(exportFormat)) {
099                                exporter.export(businessObjectEntry.getBusinessObjectClass(), getBusinessObjects(), exportFormat, outputStream);
100                                return true;
101                        }
102                }
103                return false;
104        }
105        
106        public boolean attemptCustomExport(Writer writer, String exportFormat) throws IOException {
107                return attemptCustomExport(new WriterOutputStream(writer), exportFormat);
108        }
109        
110}