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.datadictionary; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.kuali.kfs.sys.businessobject.AccountingLine; 020 import org.kuali.kfs.sys.context.SpringContext; 021 import org.kuali.kfs.sys.document.service.AccountingLineRenderingService; 022 import org.kuali.kfs.sys.document.web.AccountingLineViewCurrentBaseAmount; 023 import org.kuali.kfs.sys.document.web.TableJoining; 024 import org.kuali.rice.kns.datadictionary.BusinessObjectEntry; 025 import org.kuali.rice.kns.datadictionary.DataDictionary; 026 import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition; 027 import org.kuali.rice.kns.datadictionary.exception.AttributeValidationException; 028 import org.kuali.rice.kns.service.DataDictionaryService; 029 import org.kuali.rice.kns.util.FieldUtils; 030 import org.kuali.rice.kns.web.ui.Field; 031 import org.kuali.rice.kns.web.ui.FieldBridge; 032 033 /** 034 * The definition for an amount field which reports both current and base amounts 035 */ 036 public class AccountingLineViewCurrentBaseAmountFieldDefinition extends MaintainableFieldDefinition implements AccountingLineViewRenderableElementDefinition { 037 private String currentAmountPropertyName; 038 private String baseAmountPropertyName; 039 private boolean useShortLabels = false; 040 041 /** 042 * Creates a property initiated AccountingLineViewCurrentBaseAmount element 043 * @see org.kuali.kfs.sys.document.datadictionary.AccountingLineViewRenderableElementDefinition#createLayoutElement(java.lang.Class) 044 */ 045 public TableJoining createLayoutElement(Class<? extends AccountingLine> accountingLineClass) { 046 AccountingLineViewCurrentBaseAmount layoutElement = new AccountingLineViewCurrentBaseAmount(); 047 048 layoutElement.setBaseAmountField(createFieldForPropertyName(baseAmountPropertyName, accountingLineClass)); 049 layoutElement.setBaseAmountFieldDefinition(createFieldDefinitionForProperty(baseAmountPropertyName)); 050 051 layoutElement.setCurrentAmountField(createFieldForPropertyName(currentAmountPropertyName, accountingLineClass)); 052 layoutElement.setCurrentAmountFieldDefinition(createFieldDefinitionForProperty(currentAmountPropertyName)); 053 054 layoutElement.setDefinition(this); 055 056 return layoutElement; 057 } 058 059 /** 060 * Creates a field for the given AccountingLine class and property name 061 * @param propertyName the name of the property to create a Field for 062 * @param accountingLineClass the Class of the AccountingLine we're planning on rendering 063 * @return an appropriately created Field 064 */ 065 protected Field createFieldForPropertyName(String propertyName, Class<? extends AccountingLine> accountingLineClass) { 066 Field realField = FieldUtils.getPropertyField(accountingLineClass, propertyName, false); 067 FieldBridge.setupField(realField, this, null); 068 if (useShortLabels) { 069 BusinessObjectEntry boEntry = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getBusinessObjectEntry(accountingLineClass.getName()); 070 realField.setFieldLabel(boEntry.getAttributeDefinition(propertyName).getShortLabel()); 071 } 072 return realField; 073 } 074 075 /** 076 * Creates an AccountingLineViewFieldDefinition for the given property name 077 * @param propertyName the name of the field property that we're creating a definition for 078 * @return an appropriately created AccountingLineViewFieldDefinition 079 */ 080 protected AccountingLineViewFieldDefinition createFieldDefinitionForProperty(String propertyName) { 081 AccountingLineViewFieldDefinition fieldDefinition = SpringContext.getBean(AccountingLineRenderingService.class).createGenericAccountingLineViewFieldDefinition(this); 082 fieldDefinition.setName(propertyName); 083 return fieldDefinition; 084 } 085 086 /** 087 * @see org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition#completeValidation(java.lang.Class, java.lang.Class) 088 */ 089 @Override 090 public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) { 091 if (StringUtils.isBlank(currentAmountPropertyName)) { 092 throw new AttributeValidationException("The currentAmountPropertyName property must be specified on the AccountingLineView-CurentBaseAmountField definition for "+rootBusinessObjectClass.getName()); 093 } 094 095 if (StringUtils.isBlank(baseAmountPropertyName)) { 096 throw new AttributeValidationException("The baseAmountPropertyName property must be specified on the AccountingLineView-CurentBaseAmountField definition for "+rootBusinessObjectClass.getName()); 097 } 098 099 if (!StringUtils.isBlank(getName())) { 100 throw new AttributeValidationException("please do not specify name on the AccountingLineView-CurentBaseAmountField definition for "+rootBusinessObjectClass.getName()); 101 } 102 103 if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, getCurrentAmountPropertyName())) { 104 throw new AttributeValidationException("unable to find attribute or collection named '" + getCurrentAmountPropertyName() + "' in rootBusinessObjectClass '" + rootBusinessObjectClass.getName() + "' (" + "" + ")"); 105 } 106 107 if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, getBaseAmountPropertyName())) { 108 throw new AttributeValidationException("unable to find attribute or collection named '" + getBaseAmountPropertyName() + "' in rootBusinessObjectClass '" + rootBusinessObjectClass.getName() + "' (" + "" + ")"); 109 } 110 111 if (defaultValueFinderClass != null && defaultValue != null) { 112 throw new AttributeValidationException("Both defaultValue and defaultValueFinderClass can not be specified on attribute " + getName() + " in rootBusinessObjectClass " + rootBusinessObjectClass.getName()); 113 } 114 } 115 116 /** 117 * Gets the baseAmountPropertyName attribute. 118 * @return Returns the baseAmountPropertyName. 119 */ 120 public String getBaseAmountPropertyName() { 121 return baseAmountPropertyName; 122 } 123 124 /** 125 * Sets the baseAmountPropertyName attribute value. 126 * @param baseAmountPropertyName The baseAmountPropertyName to set. 127 */ 128 public void setBaseAmountPropertyName(String baseAmountPropertyName) { 129 this.baseAmountPropertyName = baseAmountPropertyName; 130 } 131 132 /** 133 * Gets the currentAmountPropertyName attribute. 134 * @return Returns the currentAmountPropertyName. 135 */ 136 public String getCurrentAmountPropertyName() { 137 return currentAmountPropertyName; 138 } 139 140 /** 141 * Sets the currentAmountPropertyName attribute value. 142 * @param currentAmountPropertyName The currentAmountPropertyName to set. 143 */ 144 public void setCurrentAmountPropertyName(String currentAmountPropertyName) { 145 this.currentAmountPropertyName = currentAmountPropertyName; 146 } 147 148 /** 149 * Gets the useShortLabels attribute. 150 * @return Returns the useShortLabels. 151 */ 152 public boolean isUseShortLabels() { 153 return useShortLabels; 154 } 155 156 /** 157 * Sets the useShortLabels attribute value. 158 * @param useShortLabels The useShortLabels to set. 159 */ 160 public void setUseShortLabels(boolean useShortLabels) { 161 this.useShortLabels = useShortLabels; 162 } 163 164 }