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 org.apache.commons.lang.StringUtils;
019    import org.kuali.kfs.sys.businessobject.AccountingLine;
020    import org.kuali.kfs.sys.document.service.DynamicNameLabelGenerator;
021    import org.kuali.rice.kns.util.ObjectUtils;
022    
023    public class ObjectCodeDynamicNameLabelGeneratorImpl implements DynamicNameLabelGenerator {
024    
025        /**
026         * If the objectCode reference on the line is refreshable, returns the object code's name; otherwise returns null 
027         * @see org.kuali.kfs.sys.document.service.DynamicNameLabelGenerator#getDynamicNameLabelFieldName(org.kuali.kfs.sys.businessobject.AccountingLine, java.lang.String)
028         */
029        public String getDynamicNameLabelValue(AccountingLine line, String accountingLineProperty) {
030            if (line.getPostingYear() != null && !StringUtils.isBlank(line.getChartOfAccountsCode()) && !StringUtils.isBlank(line.getFinancialObjectCode())) {
031                line.refreshReferenceObject("objectCode");
032                if (!ObjectUtils.isNull(line.getObjectCode())) return line.getObjectCode().getFinancialObjectCodeName();
033            }
034            return null;
035        }
036    
037        /**
038         * Builds the proper call to loadObjectInfo
039         * @see org.kuali.kfs.sys.document.service.DynamicNameLabelGenerator#getDynamicNameLabelOnBlur(org.kuali.kfs.sys.businessobject.AccountingLine, java.lang.String)
040         */
041        public String getDynamicNameLabelOnBlur(AccountingLine line, String accountingLineProperty) {
042            StringBuilder onBlur = new StringBuilder();
043            onBlur.append("loadObjectInfo( '");
044            onBlur.append(line.getPostingYear());
045            onBlur.append("', '");
046            onBlur.append(accountingLineProperty+".objectType.name");
047            onBlur.append("', '");
048            onBlur.append(accountingLineProperty+".objectTypeCode");
049            onBlur.append("', ");
050            onBlur.append("this.name, '");
051            onBlur.append(getDynamicNameLabelFieldName(line, accountingLineProperty));
052            onBlur.append("' );");
053            return onBlur.toString();
054        }
055    
056        /**
057         * Always returns accountingLineProperty+".objectCode.financialObjectCodeName"
058         * @see org.kuali.kfs.sys.document.service.DynamicNameLabelGenerator#getDynamicNameLabelName(org.kuali.kfs.sys.businessobject.AccountingLine, java.lang.String)
059         */
060        public String getDynamicNameLabelFieldName(AccountingLine line, String accountingLineProperty) {
061            return accountingLineProperty+".objectCode.financialObjectCodeName";
062        }
063    
064    }