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