001/*
002 * Copyright 2009 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.coa.document;
017
018import java.util.Map;
019
020import org.apache.commons.lang.StringUtils;
021import org.apache.log4j.Logger;
022import org.kuali.ole.coa.businessobject.A21SubAccount;
023import org.kuali.ole.coa.businessobject.Account;
024import org.kuali.ole.coa.businessobject.SubAccount;
025import org.kuali.ole.coa.service.A21SubAccountService;
026import org.kuali.ole.coa.service.AccountService;
027import org.kuali.ole.sys.OLEConstants;
028import org.kuali.ole.sys.OLEPropertyConstants;
029import org.kuali.ole.sys.context.SpringContext;
030import org.kuali.ole.sys.document.FinancialSystemMaintainable;
031import org.kuali.rice.kim.api.identity.Person;
032import org.kuali.rice.kns.document.MaintenanceDocument;
033import org.kuali.rice.kns.document.authorization.MaintenanceDocumentRestrictions;
034import org.kuali.rice.krad.bo.PersistableBusinessObject;
035import org.kuali.rice.krad.util.GlobalVariables;
036import org.kuali.rice.krad.util.ObjectUtils;
037
038/**
039 * This class...
040 */
041public class SubAccountMaintainableImpl extends FinancialSystemMaintainable {
042    private static final Logger LOG = Logger.getLogger(SubAccountMaintainableImpl.class);
043
044    // account fields that are PKs of nested reference accounts but don't exist in the Sub-Account BO as FKs.
045    public static final String[] COA_CODE_NAMES = {        
046        OLEPropertyConstants.A21_SUB_ACCOUNT + "." + OLEPropertyConstants.COST_SHARE_SOURCE_CHART_OF_ACCOUNTS_CODE, 
047    };
048    public static final String[] ACCOUNT_NUMBER_NAMES = {        
049        OLEPropertyConstants.A21_SUB_ACCOUNT + "." + OLEPropertyConstants.COST_SHARE_SOURCE_ACCOUNT_NUMBER, 
050    };
051
052    /**
053     * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#refresh(java.lang.String, java.util.Map,
054     *      org.kuali.rice.kns.document.MaintenanceDocument)
055     */
056    @Override
057    public void refresh(String refreshCaller, Map fieldValues, MaintenanceDocument document) {
058        super.refresh(refreshCaller, fieldValues, document);
059
060        Person person = GlobalVariables.getUserSession().getPerson();
061        MaintenanceDocumentRestrictions restrictions = getBusinessObjectAuthorizationService().getMaintenanceDocumentRestrictions(document, person);
062        boolean canEdit = !restrictions.isHiddenSectionId(OLEConstants.SUB_ACCOUNT_EDIT_CG_ICR_SECTION_ID) && !restrictions.isReadOnlySectionId(OLEConstants.SUB_ACCOUNT_EDIT_CG_ICR_SECTION_ID);
063        
064        // after account lookup, refresh the CG ICR account fields
065        if (refreshCaller != null && (refreshCaller.toUpperCase().endsWith(OLEConstants.LOOKUPABLE_SUFFIX.toUpperCase())) && fieldValues.containsKey("document.newMaintainableObject.accountNumber") && canEdit) {
066            SubAccount subAccount = (SubAccount) this.getBusinessObject();
067            this.populateCGIcrFields(subAccount);
068        }
069    }
070
071    // populate the CG ICR fields if any
072    private void populateCGIcrFields(SubAccount subAccount) {
073        A21SubAccount a21SubAccount = subAccount.getA21SubAccount();
074        String chartOfAccountsCode = subAccount.getChartOfAccountsCode();
075        String accountNumber = subAccount.getAccountNumber();
076        
077        //populate ICR fields if subAccount COA or Acct does not match a21SubAccount COA/Acct
078        if (ObjectUtils.isNotNull(a21SubAccount) && (!StringUtils.equals(chartOfAccountsCode, a21SubAccount.getChartOfAccountsCode()) || !StringUtils.equals(accountNumber, a21SubAccount.getAccountNumber()))) {                  
079            A21SubAccountService a21SubAccountService = SpringContext.getBean(A21SubAccountService.class);
080            a21SubAccountService.populateCgIcrAccount(a21SubAccount, chartOfAccountsCode, accountNumber);
081        }
082    }
083    
084    /**
085     * @see org.kuali.ole.sys.document.FinancialSystemMaintainable#populateChartOfAccountsCodeFields()
086     * 
087     * Special treatment is needed to populate chart code fields from account number fields 
088     * in nested reference accounts a21SubAccount.costShareAccount 
089     * when accounts can't cross charts.
090     */
091    @Override
092    protected void populateChartOfAccountsCodeFields() {
093        super.populateChartOfAccountsCodeFields();
094        PersistableBusinessObject bo = getBusinessObject();        
095        AccountService acctService = SpringContext.getBean(AccountService.class);
096
097        for (int i=0; i<COA_CODE_NAMES.length; i++) {
098            String coaCodeName = COA_CODE_NAMES[i];            
099            String acctNumName = ACCOUNT_NUMBER_NAMES[i];        
100            String accountNumber = (String)ObjectUtils.getPropertyValue(bo, acctNumName);
101            String coaCode = null;
102            Account account = acctService.getUniqueAccountForAccountNumber(accountNumber);            
103            if (ObjectUtils.isNotNull(account)) {
104                coaCode = account.getChartOfAccountsCode();
105            }
106            try {
107                ObjectUtils.setObjectProperty(bo, coaCodeName, coaCode); 
108            }
109            catch (Exception e) {
110                LOG.error("Error in setting property value for " + coaCodeName);
111            } 
112        }
113    }
114    
115}