001/*
002 * Copyright 2008 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 */
016package org.kuali.ole.sys.document.service.impl;
017
018import java.util.Map;
019
020import org.kuali.ole.sys.businessobject.AccountingLine;
021import org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation;
022import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
023import org.kuali.rice.kns.web.ui.Field;
024import org.kuali.rice.krad.valuefinder.ValueFinder;
025
026/**
027 * A field transformer that populates a field with its default value
028 */
029public class DefaultValuePopulationAccountingLineFieldRenderingTransformationImpl implements AccountingLineFieldRenderingTransformation {
030
031    /**
032     * 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;
033     * note that this value may be wiped out by the value from the business object during that transformation (which presumably happends after this one)
034     * @see org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation#transformField(org.kuali.ole.sys.document.web.AccountingLineViewField)
035     */
036    public void transformField(AccountingLine accountingLine, Field field, MaintainableFieldDefinition fieldDefinition, Map unconvertedValues) {
037        populateFieldWithDefault(field, fieldDefinition);
038    }
039
040    /**
041     * Populates a maintenance field with its default value
042     * @param field the field to populate with a default value
043     * @param fieldDefinition the data dictionary definition of the field to transform
044     * 
045     * KRAD Conversion: Performs the customization of the field properties
046     */
047    protected void populateFieldWithDefault(Field field, MaintainableFieldDefinition fieldDefinition) {
048        try {
049            Class defaultValueFinderClass = fieldDefinition.getDefaultValueFinderClass();
050            if (defaultValueFinderClass != null) {
051                field.setPropertyValue(((ValueFinder) defaultValueFinderClass.newInstance()).getValue());
052            }
053        }
054        catch (InstantiationException ie) {
055            throw new RuntimeException("Default Value Finder Class "+fieldDefinition.getDefaultValueFinderClass().getName()+" for property "+field.getPropertyName()+" could not be instantiated");
056        }
057        catch (IllegalAccessException e) {
058            throw new RuntimeException("Default Value Finder Class "+fieldDefinition.getDefaultValueFinderClass().getName()+" for property "+field.getPropertyName()+" was accessed illegally");
059        }
060    }
061}