001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.sys.document.service.impl;
017    
018    import java.util.Map;
019    import java.util.Set;
020    
021    import org.kuali.kfs.sys.businessobject.AccountingLine;
022    import org.kuali.kfs.sys.document.service.AccountingLineFieldRenderingTransformation;
023    import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
024    import org.kuali.rice.kns.lookup.valueFinder.ValueFinder;
025    import org.kuali.rice.kns.web.ui.Field;
026    
027    /**
028     * A field transformer that populates a field with its default value
029     */
030    public class DefaultValuePopulationAccountingLineFieldRenderingTransformationImpl implements AccountingLineFieldRenderingTransformation {
031    
032        /**
033         * 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;
034         * note that this value may be wiped out by the value from the business object during that transformation (which presumably happends after this one)
035         * @see org.kuali.kfs.sys.document.service.AccountingLineFieldRenderingTransformation#transformField(org.kuali.kfs.sys.document.web.AccountingLineViewField)
036         */
037        public void transformField(AccountingLine accountingLine, Field field, MaintainableFieldDefinition fieldDefinition, Map unconvertedValues) {
038            populateFieldWithDefault(field, fieldDefinition);
039        }
040    
041        /**
042         * Populates a maintenance field with its default value
043         * @param field the field to populate with a default value
044         * @param fieldDefinition the data dictionary definition of the field to transform
045         */
046        protected void populateFieldWithDefault(Field field, MaintainableFieldDefinition fieldDefinition) {
047            try {
048                Class defaultValueFinderClass = fieldDefinition.getDefaultValueFinderClass();
049                if (defaultValueFinderClass != null) {
050                    field.setPropertyValue(((ValueFinder) defaultValueFinderClass.newInstance()).getValue());
051                }
052            }
053            catch (InstantiationException ie) {
054                throw new RuntimeException("Default Value Finder Class "+fieldDefinition.getDefaultValueFinderClass().getName()+" for property "+field.getPropertyName()+" could not be instantiated");
055            }
056            catch (IllegalAccessException e) {
057                throw new RuntimeException("Default Value Finder Class "+fieldDefinition.getDefaultValueFinderClass().getName()+" for property "+field.getPropertyName()+" was accessed illegally");
058            }
059        }
060    }