/*
 * Copyright 2006 The Kuali Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.kuali.module.financial.bo;

import static org.kuali.KeyConstants.AccountingLineParser.ERROR_INVALID_PROPERTY_VALUE;
import static org.kuali.KeyConstants.AccountingLineParser.ERROR_TOO_MANY_AMOUNTS;
import static org.kuali.PropertyConstants.ACCOUNT_NUMBER;
import static org.kuali.PropertyConstants.CHART_OF_ACCOUNTS_CODE;
import static org.kuali.PropertyConstants.CREDIT;
import static org.kuali.PropertyConstants.DEBIT;
import static org.kuali.PropertyConstants.FINANCIAL_OBJECT_CODE;
import static org.kuali.PropertyConstants.FINANCIAL_SUB_OBJECT_CODE;
import static org.kuali.PropertyConstants.ORGANIZATION_REFERENCE_ID;
import static org.kuali.PropertyConstants.OVERRIDE_CODE;
import static org.kuali.PropertyConstants.PROJECT_CODE;
import static org.kuali.PropertyConstants.SUB_ACCOUNT_NUMBER;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.kuali.Constants;
import org.kuali.core.bo.AccountingLineParserBase;
import org.kuali.core.bo.SourceAccountingLine;
import org.kuali.core.exceptions.AccountingLineParserException;
import org.kuali.core.util.KualiDecimal;

/**
 * <code>AuxiliaryVocherDocument</code> accounting line parser
 */
public class AuxiliaryVoucherAccountingLineParser extends AccountingLineParserBase {
    private 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 };

    /**
     * Constructs a AuxiliaryVoucherAccountingLineParser.java.
     */
    public AuxiliaryVoucherAccountingLineParser() {
        super();
    }

    /**
     * @see org.kuali.core.bo.AccountingLineParserBase#performCustomSourceAccountingLinePopulation(java.util.Map,
     *      org.kuali.core.bo.SourceAccountingLine, java.lang.String)
     */
    @Override
    protected void performCustomSourceAccountingLinePopulation(Map<String, String> attributeValueMap, SourceAccountingLine sourceAccountingLine, String accountingLineAsString) {
        super.performCustomSourceAccountingLinePopulation(attributeValueMap, sourceAccountingLine, accountingLineAsString);
        // chose debit/credit
        String debitValue = attributeValueMap.remove(DEBIT);
        String creditValue = attributeValueMap.remove(CREDIT);
        KualiDecimal amount = null;
        String debitCreditCode = null;

        if (StringUtils.isNotBlank(debitValue)) {
            try {
                amount = new KualiDecimal(debitValue);
                if (amount.isNonZero()) {
                    debitCreditCode = Constants.GL_DEBIT_CODE;
                }
            }
            catch (NumberFormatException e) {
                String[] errorParameters = { debitValue, retrieveAttributeLabel(sourceAccountingLine.getClass(), DEBIT), accountingLineAsString };
                throw new AccountingLineParserException("invalid (NaN) '" + DEBIT + "=" + debitValue + " for " + accountingLineAsString, ERROR_INVALID_PROPERTY_VALUE, errorParameters);
            }
        }

        if (StringUtils.isNotBlank(creditValue)) {
            try {
                amount = new KualiDecimal(creditValue);
                if (amount.isNonZero()) {
                    debitCreditCode = Constants.GL_CREDIT_CODE;
                }
            }
            catch (NumberFormatException e) {
                String[] errorParameters = { creditValue, retrieveAttributeLabel(sourceAccountingLine.getClass(), CREDIT), accountingLineAsString };
                throw new AccountingLineParserException("invalid (NaN) '" + CREDIT + "=" + creditValue + " for " + accountingLineAsString, ERROR_INVALID_PROPERTY_VALUE, errorParameters);
            }
        }
        
        sourceAccountingLine.setAmount(amount);
        sourceAccountingLine.setDebitCreditCode(debitCreditCode);
    }

    /**
     * @see org.kuali.core.bo.AccountingLineParserBase#getSourceAccountingLineFormat()
     */
    @Override
    public String[] getSourceAccountingLineFormat() {
        return AV_FORMAT;
    }
}

@@ -2769,16 +2769,17 @@
 tring);%0A
+%0A
         
@@ -2945,17 +2945,22 @@
 Decimal 
-a
+debitA
 mount = 
@@ -2977,40 +2977,18 @@
     
-S
 tr
-ing debitCreditCode = null;%0A%0A
+y %7B%0A    
     
@@ -3045,39 +3045,26 @@
         
-try %7B%0A                a
+    debitA
 mount = 
@@ -3109,123 +3109,10 @@
     
-    if (amount.isNonZero()) %7B%0A                    debitCreditCode = Constants.GL_DEBIT_CODE;%0A                %7D%0A    
+%7D%0A
     
@@ -3109,36 +3109,32 @@
     %7D%0A        %7D%0A
-    
         catch (N
@@ -3163,36 +3163,32 @@
 ) %7B%0A            
-   
- 
 String%5B%5D errorPa
@@ -3294,36 +3294,32 @@
 LineAsString %7D;%0A
-    
             thro
@@ -3495,25 +3495,70 @@
     
+%7D%0A
     
-%7D%0A        %7D%0A%0A
+    KualiDecimal creditAmount = null;%0A        try %7B%0A    
     
@@ -3620,31 +3620,19 @@
     
-try %7B%0A                a
+    creditA
 moun
@@ -3682,124 +3682,10 @@
     
-    if (amount.isNonZero()) %7B%0A                    debitCreditCode = Constants.GL_CREDIT_CODE;%0A                %7D%0A    
+%7D%0A
     
@@ -3686,28 +3686,24 @@
 %7D%0A        %7D%0A
-    
         catc
@@ -3724,36 +3724,32 @@
 tException e) %7B%0A
-    
             Stri
@@ -3881,36 +3881,32 @@
  %7D;%0A            
-   
- 
 throw new Accoun
@@ -4060,37 +4060,426 @@
 eters);%0A        
-    %7D
+%7D%0A%0A        KualiDecimal amount = null;%0A        String debitCreditCode = null;%0A        if (debitAmount != null && debitAmount.isNonZero()) %7B%0A            amount = debitAmount;%0A            debitCreditCode = Constants.GL_DEBIT_CODE;%0A        %7D%0A%0A        if (creditAmount != null && creditAmount.isNonZero()) %7B%0A            amount = creditAmount;%0A            debitCreditCode = Constants.GL_CREDIT_CODE;
 %0A        %7D%0A     
@@ -4473,24 +4473,16 @@
       %7D%0A
-        
 %0A       

fix for jv import bug done the other day