001/*
002 * Copyright 2006 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.validation.impl;
017
018import java.util.List;
019
020import org.apache.commons.lang.StringUtils;
021import org.kuali.ole.coa.businessobject.OrganizationReversion;
022import org.kuali.ole.coa.businessobject.OrganizationReversionDetail;
023import org.kuali.ole.coa.service.OrganizationReversionService;
024import org.kuali.ole.sys.context.SpringContext;
025import org.kuali.rice.kns.document.MaintenanceDocument;
026import org.kuali.rice.krad.util.ObjectUtils;
027
028/**
029 * PreRules checks for the {@link OrganizationReversion} that needs to occur while still in the Struts processing. This includes defaults
030 */
031public class OrganizationReversionPreRules extends MaintenancePreRulesBase {
032
033    private transient OrganizationReversionService organizationReversionService;
034    public OrganizationReversionPreRules() {
035
036    }
037
038    /**
039     * This calls the {@link OrganizationReversionPreRules#copyKeyAttributesToDetail(OrganizationReversion)}
040     * @see org.kuali.ole.coa.document.validation.impl.MaintenancePreRulesBase#doCustomPreRules(org.kuali.rice.kns.document.MaintenanceDocument)
041     */
042    @Override
043    protected boolean doCustomPreRules(MaintenanceDocument document) {
044
045        OrganizationReversion orgRev = (OrganizationReversion) document.getNewMaintainableObject().getBusinessObject();
046        // copy year and chart to detail records
047        copyKeyAttributesToDetail(orgRev);
048        copyDefaultObjectcodeIfNoCarryForwardByObjectCode(orgRev);
049        return true;
050    }
051
052    /**
053     * 
054     * This copies the chart of accounts, and the fiscal year from the parent {@link OrganizationReversion} to the 
055     * {@link OrganizationReversionDetail} objects and refreshes the reference object on them if the values have 
056     * been filled out
057     * @param orgRev
058     */
059    protected void copyKeyAttributesToDetail(OrganizationReversion orgRev) {
060        if (orgRev.getUniversityFiscalYear() != null && orgRev.getUniversityFiscalYear().intValue() != 0 && StringUtils.isNotBlank(orgRev.getChartOfAccountsCode())) {
061            // loop over detail records, copying their details
062            for (OrganizationReversionDetail dtl : orgRev.getOrganizationReversionDetail()) {
063                dtl.setChartOfAccountsCode(orgRev.getChartOfAccountsCode());
064                dtl.setUniversityFiscalYear(orgRev.getUniversityFiscalYear());
065                // load the object, if possible
066                if (StringUtils.isNotBlank(dtl.getOrganizationReversionObjectCode())) {
067                    dtl.refreshReferenceObject("organizationReversionObject");
068                }
069            }
070        }
071
072    }
073    
074    /**
075     * 
076     * This copies the object code from system parameter to the Organization Reversion object code field if the 
077     * "Carry Forward by Object Code" indicator is set to true
078     * @param orgRev
079     */
080    protected void copyDefaultObjectcodeIfNoCarryForwardByObjectCode(OrganizationReversion orgRev) {
081        if (orgRev.isCarryForwardByObjectCodeIndicator() == true) return; 
082        
083        if (organizationReversionService == null) {
084            organizationReversionService = SpringContext.getBean(OrganizationReversionService.class);
085        }
086        String objectCode = organizationReversionService.getOrganizationReversionDetaiFromSystemParameters();
087        
088        List<OrganizationReversionDetail> details = orgRev.getOrganizationReversionDetail();
089        for (OrganizationReversionDetail dtl : details) {
090            if (ObjectUtils.isNull(dtl.getOrganizationReversionObjectCode()))  {
091                dtl.setOrganizationReversionObjectCode(objectCode);
092            }
093        }
094    }       
095}