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    
017    package org.kuali.kfs.fp.businessobject;
018    
019    import static org.kuali.kfs.sys.KFSKeyConstants.AccountingLineParser.ERROR_INVALID_PROPERTY_VALUE;
020    import static org.kuali.kfs.sys.KFSPropertyConstants.ACCOUNT_NUMBER;
021    import static org.kuali.kfs.sys.KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE;
022    import static org.kuali.kfs.sys.KFSPropertyConstants.CREDIT;
023    import static org.kuali.kfs.sys.KFSPropertyConstants.DEBIT;
024    import static org.kuali.kfs.sys.KFSPropertyConstants.FINANCIAL_OBJECT_CODE;
025    import static org.kuali.kfs.sys.KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE;
026    import static org.kuali.kfs.sys.KFSPropertyConstants.ORGANIZATION_REFERENCE_ID;
027    import static org.kuali.kfs.sys.KFSPropertyConstants.PROJECT_CODE;
028    import static org.kuali.kfs.sys.KFSPropertyConstants.SUB_ACCOUNT_NUMBER;
029    
030    import java.util.Map;
031    
032    import org.apache.commons.lang.StringUtils;
033    import org.kuali.kfs.sys.KFSConstants;
034    import org.kuali.kfs.sys.businessobject.AccountingLineParserBase;
035    import org.kuali.kfs.sys.businessobject.SourceAccountingLine;
036    import org.kuali.kfs.sys.exception.AccountingLineParserException;
037    import org.kuali.rice.kns.util.KualiDecimal;
038    
039    /**
040     * This class is used to parse an <code>AuxiliaryVocherDocument</code> accounting line.
041     */
042    public class AuxiliaryVoucherAccountingLineParser extends AccountingLineParserBase {
043        protected static final String[] AV_FORMAT = { CHART_OF_ACCOUNTS_CODE, ACCOUNT_NUMBER, SUB_ACCOUNT_NUMBER, FINANCIAL_OBJECT_CODE, FINANCIAL_SUB_OBJECT_CODE, PROJECT_CODE, ORGANIZATION_REFERENCE_ID, DEBIT, CREDIT };
044    
045        /**
046         * Constructs a AuxiliaryVoucherAccountingLineParser.java.
047         */
048        public AuxiliaryVoucherAccountingLineParser() {
049            super();
050        }
051    
052        /**
053         * Populates source accounting lines and sets debit and credit amounts and codes if they exist
054         * 
055         * @see org.kuali.kfs.sys.businessobject.AccountingLineParserBase#performCustomSourceAccountingLinePopulation(java.util.Map, org.kuali.kfs.sys.businessobject.SourceAccountingLine, java.lang.String)
056         */
057        @Override
058        protected void performCustomSourceAccountingLinePopulation(Map<String, String> attributeValueMap, SourceAccountingLine sourceAccountingLine, String accountingLineAsString) {
059            super.performCustomSourceAccountingLinePopulation(attributeValueMap, sourceAccountingLine, accountingLineAsString);
060    
061            // chose debit/credit
062            String debitValue = attributeValueMap.remove(DEBIT);
063            String creditValue = attributeValueMap.remove(CREDIT);
064            KualiDecimal debitAmount = null;
065            try {
066                if (StringUtils.isNotBlank(debitValue)) {
067                    debitAmount = new KualiDecimal(debitValue);
068                }
069            }
070            catch (NumberFormatException e) {
071                String[] errorParameters = { debitValue, retrieveAttributeLabel(sourceAccountingLine.getClass(), DEBIT), accountingLineAsString };
072                throw new AccountingLineParserException("invalid (NaN) '" + DEBIT + "=" + debitValue + " for " + accountingLineAsString, ERROR_INVALID_PROPERTY_VALUE, errorParameters);
073            }
074            KualiDecimal creditAmount = null;
075            try {
076                if (StringUtils.isNotBlank(creditValue)) {
077                    creditAmount = new KualiDecimal(creditValue);
078                }
079            }
080            catch (NumberFormatException e) {
081                String[] errorParameters = { creditValue, retrieveAttributeLabel(sourceAccountingLine.getClass(), CREDIT), accountingLineAsString };
082                throw new AccountingLineParserException("invalid (NaN) '" + CREDIT + "=" + creditValue + " for " + accountingLineAsString, ERROR_INVALID_PROPERTY_VALUE, errorParameters);
083            }
084    
085            KualiDecimal amount = null;
086            String debitCreditCode = null;
087            if (debitAmount != null && debitAmount.isNonZero()) {
088                amount = debitAmount;
089                debitCreditCode = KFSConstants.GL_DEBIT_CODE;
090            }
091    
092            if (creditAmount != null && creditAmount.isNonZero()) {
093                amount = creditAmount;
094                debitCreditCode = KFSConstants.GL_CREDIT_CODE;
095            }
096    
097            sourceAccountingLine.setAmount(amount);
098            sourceAccountingLine.setDebitCreditCode(debitCreditCode);
099        }
100    
101        /**
102         * @see org.kuali.rice.kns.bo.AccountingLineParserBase#getSourceAccountingLineFormat()
103         */
104        @Override
105        public String[] getSourceAccountingLineFormat() {
106            return removeChartFromFormatIfNeeded(AV_FORMAT);
107        }
108    }