View Javadoc

1   package org.kuali.ole.sys.batch;
2   
3   import org.apache.commons.beanutils.PropertyUtils;
4   import org.kuali.ole.sys.businessobject.format.BatchDateFormatter;
5   import org.kuali.rice.core.web.format.Formatter;
6   import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
7   import org.springframework.util.StringUtils;
8   
9   
10  /**
11   * Base abstract class for configuration element which represents a substring of a given line
12   * to be formatted and set on a business object
13   */
14  public abstract class AbstractFlatFilePropertySpecificationBase implements FlatFilePropertySpecification {
15      protected String propertyName;
16      protected boolean rightTrim;
17      protected boolean leftTrim;
18      protected Class<? extends Formatter> formatterClass = Formatter.class;
19      protected String dateFormat;
20      protected boolean formatToTimestamp = false;
21  
22      /**
23       * @return the name of the property that should be set
24       */
25      @Override
26      public String getPropertyName() {
27          return propertyName;
28      }
29  
30      /**
31       * Sets the property on the business object
32       * @param value the substring of the parsed line to set
33       * @param businessObject the business object to set the parsed line on
34       * @param lineNumber the parsed line number
35       */
36  
37      @Override
38      public void setProperty(String value, Object businessObject, int lineNumber) {
39          if (leftTrim) {
40              value = StringUtils.trimLeadingWhitespace(value);
41          }
42  
43          if (rightTrim) {
44              value = StringUtils.trimTrailingWhitespace(value);
45          }
46          try {
47              PropertyUtils.setProperty(businessObject, propertyName, getFormattedObject(value, businessObject));
48          }
49          catch (Exception e) {
50              throw new RuntimeException("Exception occurred on line " + lineNumber + " while setting value " + value + " for property " + propertyName + "."  , e);
51          }
52      }
53  
54      /**
55       * Returns the formatter class to format the substring before it is set on the business object
56       * @param parsedObject the object that is being parsed into
57       * @return the class for the formatter
58       */
59      protected Class<?> getFormatterClass(Object parsedObject) {
60          if (Formatter.class.isAssignableFrom(formatterClass) ) {
61              Class<? extends Formatter> attributeFormatter = KRADServiceLocatorWeb.getDataDictionaryService().getAttributeFormatter(parsedObject.getClass(), this.propertyName);
62              if (attributeFormatter != null) {
63                  this.formatterClass = attributeFormatter;
64              }
65          }
66  
67          if (!Formatter.class.isAssignableFrom(this.formatterClass)) {
68              throw new RuntimeException("formatterClass is not a valid instance of " + Formatter.class.getName() + " instead was: " + formatterClass.getName());
69          }
70          return formatterClass;
71      }
72  
73      /**
74       * Builds a formatter to format the parsed substring before it is set as a property on the business object
75       * @param parsedObject the business object to parse into
76       * @return the formatter
77       */
78      protected Formatter getFormatter(Object parsedObject) {
79          Formatter formatter = null;
80          try {
81              formatter = (Formatter) getFormatterClass(parsedObject).newInstance();
82          }
83          catch (InstantiationException ie) {
84              throw new RuntimeException("Could not instantiate object of class " + formatterClass.getName(), ie);
85          }
86          catch (IllegalAccessException iae) {
87              throw new RuntimeException("Illegal access attempting to instantiate object of class " + formatterClass.getName(), iae);
88          }
89          return formatter;
90      }
91  
92      /**
93       * Sets the formatter class to use in this proprety specification
94       * @param formatterClass the class of the formatter to use
95       */
96      public void setFormatterClass(Class<? extends Formatter> formatterClass) {
97          if (!Formatter.class.isAssignableFrom(formatterClass)) {
98              throw new RuntimeException("formatterClass is not a valid instance of " + Formatter.class.getName() + " instead was: " + formatterClass.getName());
99          }
100         this.formatterClass = formatterClass;
101     }
102 
103     /**
104      * This method returns the formatted object for the given string. It uses the formatter class define for specification property
105      * if exists, otherwise look to the Data Dictionary of the parsedObject , otherwise use the default formatter class.
106      * @param subString the parsed subString
107      * @param the object to parse into
108      */
109     protected Object getFormattedObject(String subString, Object parsedObject) {
110         Formatter formatter = getFormatter(parsedObject);
111         if (formatter instanceof BatchDateFormatter) {
112             ((BatchDateFormatter) formatter).setDateFormat(dateFormat);
113             if (formatToTimestamp) {
114                 ((BatchDateFormatter) formatter).setFormatToTimestamp(true);
115             }
116         }
117         return formatter.convertFromPresentationFormat(subString);
118     }
119 
120     /**
121      * Sets the name of the property this property specification will target for filling in the business object to parse into
122      * @param propertyName the name of the target property
123      */
124     public void setPropertyName(String propertyName) {
125         this.propertyName = propertyName;
126     }
127 
128     /**
129      * Determines if the substring should have all whitespace on the right removed before setting
130      * @param rightTrim true if all whitespace on the right should be removed, false otherwise
131      */
132     public void setRightTrim(boolean rightTrim) {
133         this.rightTrim = rightTrim;
134     }
135 
136     /**
137      * Determines if the substring should have all whitepsace on the left removed before setting
138      * @param leftTrim true if all whitespace on the left should be removed, false otherwise
139      */
140     public void setLeftTrim(boolean leftTrim) {
141         this.leftTrim = leftTrim;
142     }
143 
144     /**
145      * If the substring represents a date, then this is the format used to parse that date; it should be
146      * compatible with java.text.SimpleDateFormat
147      * @param dateFormat the date format to utilized
148      */
149     public void setDateFormat(String dateFormat) {
150         this.dateFormat = dateFormat;
151     }
152 
153     /**
154      * If the formatter for this class is a BatchDateFormatter, then the formatted Date should be in the form of a timestamp
155      *
156      * @param formatToTimestamp true if we should format to timestamp, false (the default) if we should not
157      */
158     public void setFormatToTimestamp(boolean formatToTimestamp) {
159         this.formatToTimestamp = formatToTimestamp;
160     }
161 
162 }