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