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 org.apache.commons.lang.StringUtils;
019import org.kuali.ole.sys.businessobject.AccountingLine;
020import org.kuali.ole.sys.document.service.DynamicNameLabelGenerator;
021import org.kuali.rice.krad.util.ObjectUtils;
022
023public 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.ole.sys.document.service.DynamicNameLabelGenerator#getDynamicNameLabelFieldName(org.kuali.ole.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.ole.sys.document.service.DynamicNameLabelGenerator#getDynamicNameLabelOnBlur(org.kuali.ole.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.ole.sys.document.service.DynamicNameLabelGenerator#getDynamicNameLabelName(org.kuali.ole.sys.businessobject.AccountingLine, java.lang.String)
059     */
060    public String getDynamicNameLabelFieldName(AccountingLine line, String accountingLineProperty) {
061        return accountingLineProperty+".objectCode.financialObjectCodeName";
062    }
063
064}