View Javadoc
1   /*
2    * Copyright 2008 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.ole.sys.document.service.impl;
17  
18  import java.util.Map;
19  
20  import org.kuali.ole.sys.businessobject.AccountingLine;
21  import org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation;
22  import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
23  import org.kuali.rice.kns.web.ui.Field;
24  import org.kuali.rice.krad.valuefinder.ValueFinder;
25  
26  /**
27   * A field transformer that populates a field with its default value
28   */
29  public class DefaultValuePopulationAccountingLineFieldRenderingTransformationImpl implements AccountingLineFieldRenderingTransformation {
30  
31      /**
32       * Using the data dictionary definition for the field, determines what the default value for this field would be should there be a default value defined;
33       * note that this value may be wiped out by the value from the business object during that transformation (which presumably happends after this one)
34       * @see org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation#transformField(org.kuali.ole.sys.document.web.AccountingLineViewField)
35       */
36      public void transformField(AccountingLine accountingLine, Field field, MaintainableFieldDefinition fieldDefinition, Map unconvertedValues) {
37          populateFieldWithDefault(field, fieldDefinition);
38      }
39  
40      /**
41       * Populates a maintenance field with its default value
42       * @param field the field to populate with a default value
43       * @param fieldDefinition the data dictionary definition of the field to transform
44       * 
45       * KRAD Conversion: Performs the customization of the field properties
46       */
47      protected void populateFieldWithDefault(Field field, MaintainableFieldDefinition fieldDefinition) {
48          try {
49              Class defaultValueFinderClass = fieldDefinition.getDefaultValueFinderClass();
50              if (defaultValueFinderClass != null) {
51                  field.setPropertyValue(((ValueFinder) defaultValueFinderClass.newInstance()).getValue());
52              }
53          }
54          catch (InstantiationException ie) {
55              throw new RuntimeException("Default Value Finder Class "+fieldDefinition.getDefaultValueFinderClass().getName()+" for property "+field.getPropertyName()+" could not be instantiated");
56          }
57          catch (IllegalAccessException e) {
58              throw new RuntimeException("Default Value Finder Class "+fieldDefinition.getDefaultValueFinderClass().getName()+" for property "+field.getPropertyName()+" was accessed illegally");
59          }
60      }
61  }