Coverage Report - org.kuali.student.common.ui.server.screenreport.jasper.KSCollectionDataSource
 
Classes in this File Line Coverage Branch Coverage Complexity
KSCollectionDataSource
79%
23/29
72%
16/22
2.875
 
 1  
 package org.kuali.student.common.ui.server.screenreport.jasper;
 2  
 
 3  
 import java.util.Collection;
 4  
 import java.util.Iterator;
 5  
 
 6  
 import net.sf.jasperreports.engine.JRException;
 7  
 import net.sf.jasperreports.engine.JRField;
 8  
 import net.sf.jasperreports.engine.data.JRAbstractBeanDataSource;
 9  
 import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
 10  
 
 11  
 import org.kuali.student.common.ui.client.util.ExportElement;
 12  
 
 13  
 /**
 14  
  * This class overwrites the common JRBeanCollectionDataSource in order to return a JRDataSource to be used on the subreport
 15  
  * when the field is called "subset".
 16  
  * 
 17  
  * @author Kuali Rice Team (kuali-rice@googlegroups.com)
 18  
  */
 19  
 public class KSCollectionDataSource extends JRAbstractBeanDataSource {
 20  
 
 21  
     /**
 22  
     *
 23  
     */
 24  
     private Collection<ExportElement> data;
 25  
     private Iterator<ExportElement> iterator;
 26  
     private ExportElement currentBean;
 27  
     private ExportElement parent;
 28  
 
 29  
     public KSCollectionDataSource(Collection<ExportElement> elements, ExportElement parent) {
 30  71
         super(true);
 31  
 
 32  71
         this.parent = parent;
 33  71
         this.data = elements;
 34  
 
 35  71
         if (this.data != null) {
 36  20
             this.iterator = this.data.iterator();
 37  
         }
 38  71
     }
 39  
 
 40  
     /**
 41  
     *
 42  
     */
 43  
     public boolean next() {
 44  104
         boolean hasNext = false;
 45  
 
 46  104
         if (this.iterator != null) {
 47  104
             hasNext = this.iterator.hasNext();
 48  
 
 49  104
             if (hasNext) {
 50  78
                 this.currentBean = this.iterator.next();
 51  
                 
 52  
                 // Skip the empty elements in the collection.
 53  78
                 if (this.currentBean.isEmpty()){
 54  7
                     return next();
 55  
                 }
 56  
             }
 57  
         }
 58  
 
 59  97
         return hasNext;
 60  
     }
 61  
 
 62  
     @Override
 63  
     public Object getFieldValue(JRField field) throws JRException {
 64  577
         return getFieldValue(currentBean, field);
 65  
     }
 66  
     
 67  
     /**
 68  
     *
 69  
     */
 70  
     protected Object getFieldValue(ExportElement bean, JRField field) throws JRException {
 71  577
         Object object = getBeanProperty(bean, getPropertyName(field));
 72  
 
 73  
         // Return a new datasource object with subset data for subreport.
 74  577
         if ("subset".equals(field.getName())) {
 75  64
             return new KSCollectionDataSource((Collection<ExportElement>) object, bean);
 76  
             
 77  
         // Do not repeat section name for subsets.
 78  513
         } else if ("sectionName".equals(field.getName())) {
 79  64
             if (parent != null && parent.getSectionName() != null && parent.getSectionName().equals(object)) {
 80  6
                 return null;
 81  
             } else {
 82  58
                 return object;
 83  
             }
 84  
         }
 85  449
         return object;
 86  
     }
 87  
 
 88  
     /**
 89  
     *
 90  
     */
 91  
     public void moveFirst() {
 92  0
         if (this.data != null) {
 93  0
             this.iterator = this.data.iterator();
 94  
         }
 95  0
     }
 96  
 
 97  
     /**
 98  
      * Returns the underlying bean collection used by this data source.
 99  
      * 
 100  
      * @return the underlying bean collection
 101  
      */
 102  
     public Collection getData() {
 103  0
         return data;
 104  
     }
 105  
 
 106  
     /**
 107  
      * Returns the total number of records/beans that this data source contains.
 108  
      * 
 109  
      * @return the total number of records of this data source
 110  
      */
 111  
     public int getRecordCount() {
 112  0
         return data == null ? 0 : data.size();
 113  
     }
 114  
 
 115  
     /**
 116  
      * Clones this data source by creating a new instance that reuses the same underlying bean collection.
 117  
      * 
 118  
      * @return a clone of this data source
 119  
      */
 120  
     public JRBeanCollectionDataSource cloneDataSource() {
 121  0
         return new JRBeanCollectionDataSource(data);
 122  
     }
 123  
 
 124  
    
 125  
 
 126  
 }