View Javadoc

1   package org.kuali.student.common.ui.server.screenreport.jasper;
2   
3   import java.text.DateFormat;
4   import java.text.SimpleDateFormat;
5   import java.util.Date;
6   import java.util.Iterator;
7   import java.util.NoSuchElementException;
8   
9   import net.sf.jasperreports.engine.JRDataSource;
10  import net.sf.jasperreports.engine.JRException;
11  import net.sf.jasperreports.engine.JRField;
12  
13  import org.kuali.student.common.assembly.data.Data;
14  import org.kuali.student.common.assembly.data.Data.Property;
15  
16  /**
17   * This is a custom data source class to convert a datamodel to a Jasper report data source object.
18   * 
19   * @author Kuali Rice Team (kuali-rice@googlegroups.com)
20   */
21  public class KSCustomDataSource implements JRDataSource {
22  
23      private Iterator<Property> iterator;
24  
25      private Property property;
26  
27      private DateFormat format;
28  
29      /**
30  	 *
31  	 */
32      public KSCustomDataSource(final Iterator<Property> source) {
33          this.iterator = source;
34          this.format = new SimpleDateFormat("yyyy-MM-dd");
35      }
36  
37      /**
38  	 *
39  	 */
40      public boolean next() throws JRException {
41          try {
42              property = (Property) iterator.next();
43              if (("metaInfo".equals(property.getKey().toString())) || ("id".equals(property.getKey().toString())) || ("_runtimeData".equals(property.getKey().toString()))) {
44                  return this.next();
45              }
46          } catch (NoSuchElementException e) {
47              return false;
48          }
49          return true;
50      }
51  
52      /**
53  	 *
54  	 */
55      public Object getFieldValue(JRField field) throws JRException {
56          Object value = null;
57  
58          String fieldName = field.getName();
59          if ("key".equals(fieldName)) {
60              value = property.getKey().toString();
61          } else {
62              if (property.getValueType().equals(Data.class)) {
63                  if ("sub".equals(fieldName)) {
64                      value = new Boolean(true);
65                  } else if ("value".equals(fieldName)) {
66                      value = null;
67                  } else if ("subset".equals(fieldName)) {
68                      value = new KSCustomDataSource(((Data) property.getValue()).iterator());
69                  }
70              } else {
71                  if ("sub".equals(fieldName)) {
72                      value = new Boolean(false);
73                  } else if ("value".equals(fieldName)) {
74                      if (property.getValue() instanceof Date) {
75                          value = format.format((Date) property.getValue());
76                      } else {
77                          if (property.getValue() != null) {
78                              value = property.getValue().toString();
79                          }
80                      }
81                  } else if ("subset".equals(fieldName)) {
82                      value = null;
83                  }
84              }
85          }
86          return value;
87      }
88  
89  }