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.coa.document.validation.impl;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.kfs.coa.businessobject.Account;
020    import org.kuali.kfs.coa.businessobject.OrganizationReversionGlobal;
021    import org.kuali.kfs.coa.businessobject.OrganizationReversionGlobalDetail;
022    import org.kuali.kfs.coa.businessobject.OrganizationReversionGlobalOrganization;
023    import org.kuali.rice.kns.document.MaintenanceDocument;
024    import org.kuali.rice.kns.util.ObjectUtils;
025    
026    /**
027     * 
028     * PreRules checks for the {@link OrganizationReversionGlobal} that needs to occur while still in the Struts processing. This includes defaults
029     */
030    public class OrganizationReversionGlobalPreRules extends MaintenancePreRulesBase {
031        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationReversionGlobalPreRules.class);
032    
033        /**
034         * This is the hook method for the {@link MaintenancePreRulesBase} to call. It calls
035         * <ul>
036         * <li>{@link OrganizationReversionGlobalPreRules#checkForContinuationAccounts(OrganizationReversionGlobal)}</li>
037         * <li>{@link OrganizationReversionGlobalPreRules#copyKeyAttributesToCollections(OrganizationReversionGlobal)}</li>
038         * </ul>
039         * @see org.kuali.kfs.coa.document.validation.impl.MaintenancePreRulesBase#doCustomPreRules(org.kuali.rice.kns.document.MaintenanceDocument)
040         */
041        @Override
042        public boolean doCustomPreRules(MaintenanceDocument maintenanceDocument) {
043            OrganizationReversionGlobal globalOrgReversion = (OrganizationReversionGlobal) maintenanceDocument.getNewMaintainableObject().getBusinessObject();
044            checkForContinuationAccounts(globalOrgReversion);
045            copyKeyAttributesToCollections(globalOrgReversion);
046            return true;
047        }
048    
049        /**
050         * This method checks to see if the budget reversion or cash reversion accounts have continuation accounts.
051         * 
052         * @param globalOrgReversion Global Organization Reversion to check.
053         */
054        public void checkForContinuationAccounts(OrganizationReversionGlobal globalOrgReversion) {
055            if (!StringUtils.isBlank(globalOrgReversion.getBudgetReversionChartOfAccountsCode()) && !StringUtils.isBlank(globalOrgReversion.getBudgetReversionAccountNumber())) {
056                Account account = checkForContinuationAccount("Budget Reversion Account Number", globalOrgReversion.getBudgetReversionChartOfAccountsCode(), globalOrgReversion.getBudgetReversionAccountNumber(), "");
057                if (ObjectUtils.isNotNull(account)) {
058                    globalOrgReversion.setBudgetReversionChartOfAccountsCode(account.getChartOfAccountsCode());
059                    globalOrgReversion.setBudgetReversionAccountNumber(account.getAccountNumber());
060                }
061            }
062            if (!StringUtils.isBlank(globalOrgReversion.getCashReversionFinancialChartOfAccountsCode()) && !StringUtils.isBlank(globalOrgReversion.getBudgetReversionAccountNumber())) {
063                Account account = checkForContinuationAccount("Cash Reversion Account Number", globalOrgReversion.getCashReversionFinancialChartOfAccountsCode(), globalOrgReversion.getCashReversionAccountNumber(), "");
064                if (ObjectUtils.isNotNull(account)) {
065                    globalOrgReversion.setCashReversionFinancialChartOfAccountsCode(account.getChartOfAccountsCode());
066                    globalOrgReversion.setCashReversionAccountNumber(account.getAccountNumber());
067                }
068            }
069        }
070    
071        /**
072         * This method updates all children of a Global Organization Reversion so that they all are associated with the Global
073         * Organization Reversion document, by upudating their primary keys.
074         * 
075         * @param globalOrgRev the global organization reversion document to update.
076         */
077        public void copyKeyAttributesToCollections(OrganizationReversionGlobal globalOrgRev) {
078            for (OrganizationReversionGlobalDetail orgRevDetail : globalOrgRev.getOrganizationReversionGlobalDetails()) {
079                orgRevDetail.setDocumentNumber(globalOrgRev.getDocumentNumber());
080            }
081    
082            for (OrganizationReversionGlobalOrganization orgRevOrg : globalOrgRev.getOrganizationReversionGlobalOrganizations()) {
083                orgRevOrg.setDocumentNumber(globalOrgRev.getDocumentNumber());
084            }
085        }
086    
087    }