Coverage Report - org.kuali.rice.kns.lookup.ExportViewHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
ExportViewHelper
0%
0/31
0%
0/14
2.571
 
 1  
 /*
 2  
  * Copyright 2007-2008 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.List;
 23  
 
 24  
 import org.apache.commons.io.output.WriterOutputStream;
 25  
 import org.apache.commons.lang.StringUtils;
 26  
 import org.displaytag.model.Row;
 27  
 import org.displaytag.model.TableModel;
 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.service.KNSServiceLocatorWeb;
 32  
 import org.kuali.rice.kns.exception.ExportNotSupportedException;
 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  
  * A helper class to be used with the custom ExportView implementations for
 40  
  * Display Tag.  Most of the logic for interfacing with the KNS export
 41  
  * system is encapsulated in this helper class so it can be shared between
 42  
  * the various Display Tag export implementations. 
 43  
  * 
 44  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 45  
  *
 46  
  */
 47  
 public class ExportViewHelper {
 48  
 
 49  
         private BusinessObjectEntry businessObjectEntry;
 50  
         private List<BusinessObject> businessObjects;
 51  
         
 52  0
         public ExportViewHelper(TableModel tableModel) {
 53  0
                 this.businessObjectEntry = loadBusinessObjectEntry();
 54  0
                 this.businessObjects = loadBusinessObjects(tableModel);
 55  0
         }
 56  
         
 57  
         protected BusinessObjectEntry loadBusinessObjectEntry() {
 58  0
                 KualiForm kualiForm = GlobalVariables.getKualiForm();
 59  0
                 if (kualiForm instanceof LookupForm) {
 60  0
                         LookupForm lookupForm = (LookupForm) kualiForm;
 61  0
                         if (!StringUtils.isBlank(lookupForm.getBusinessObjectClassName())) {
 62  0
                                 return KNSServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(lookupForm.getBusinessObjectClassName());
 63  
                         }
 64  
                 }
 65  0
                 return null;
 66  
         }
 67  
         
 68  
         protected List<BusinessObject> loadBusinessObjects(TableModel tableModel) {
 69  0
                 List<BusinessObject> businessObjects = new ArrayList<BusinessObject>();
 70  0
                 List<Row> rowList = tableModel.getRowListFull();
 71  0
                 for (Row row : rowList) {
 72  0
                         ResultRow resultRow = (ResultRow)row.getObject();
 73  0
                         if (resultRow.getBusinessObject() != null) {
 74  0
                                 businessObjects.add(resultRow.getBusinessObject());
 75  
                         }
 76  0
                 }
 77  0
                 return businessObjects;
 78  
         }
 79  
         
 80  
         public BusinessObjectEntry getBusinessObjectEntry() {
 81  0
                 return businessObjectEntry;
 82  
         }
 83  
         
 84  
         public List<BusinessObject> getBusinessObjects() {
 85  0
                 return businessObjects;
 86  
         }
 87  
         
 88  
         public boolean attemptCustomExport(OutputStream outputStream, String exportFormat) throws IOException {
 89  0
                 if (getBusinessObjectEntry() != null && getBusinessObjectEntry().getExporterClass() != null) {
 90  
                         final Exporter exporter;
 91  
                         try {
 92  0
                                 exporter = getBusinessObjectEntry().getExporterClass().newInstance();
 93  0
                         } catch (Exception e) {
 94  0
                                 throw new ExportNotSupportedException("Failed to load export class: " + businessObjectEntry.getExporterClass(),e);
 95  0
                         }
 96  0
                         List<String> supportedFormats = exporter.getSupportedFormats(businessObjectEntry.getBusinessObjectClass());
 97  0
                         if (supportedFormats.contains(exportFormat)) {
 98  0
                                 exporter.export(businessObjectEntry.getBusinessObjectClass(), getBusinessObjects(), exportFormat, outputStream);
 99  0
                                 return true;
 100  
                         }
 101  
                 }
 102  0
                 return false;
 103  
         }
 104  
         
 105  
         public boolean attemptCustomExport(Writer writer, String exportFormat) throws IOException {
 106  0
                 return attemptCustomExport(new WriterOutputStream(writer), exportFormat);
 107  
         }
 108  
         
 109  
 }