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;
017    
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    import org.kuali.kfs.coa.businessobject.OrganizationReversionCategory;
022    import org.kuali.kfs.coa.service.OrganizationReversionDetailTrickleDownInactivationService;
023    import org.kuali.kfs.sys.context.SpringContext;
024    import org.kuali.kfs.sys.document.FinancialSystemMaintainable;
025    import org.kuali.rice.kns.service.BusinessObjectService;
026    import org.kuali.rice.kns.util.KNSConstants;
027    import org.kuali.rice.kns.util.ObjectUtils;
028    
029    /**
030     * A Maintainable for the Organization Reversion Category maintenance document
031     */
032    public class OrganizationReversionCategoryMaintainableImpl extends FinancialSystemMaintainable {
033    
034        /**
035         * Determines if this maint doc is inactivating an organization reversion category
036         * @return true if the document is inactivating an active organization reversioncategory, false otherwise
037         */
038        protected boolean isInactivatingOrganizationReversionCategory() {
039            // the account has to be closed on the new side when editing in order for it to be possible that we are closing the account
040            if (KNSConstants.MAINTENANCE_EDIT_ACTION.equals(getMaintenanceAction()) && !((OrganizationReversionCategory) getBusinessObject()).isActive()) {
041                OrganizationReversionCategory existingOrganizationReversionCategoryFromDB = retrieveExistingOrganizationReversionCategory();
042                if (ObjectUtils.isNotNull(existingOrganizationReversionCategoryFromDB)) {
043                    // now see if the original account was not closed, in which case, we are closing the account
044                    if (existingOrganizationReversionCategoryFromDB.isActive()) {
045                        return true;
046                    }
047                }
048            }
049            return false;
050        }
051        
052        /**
053         * Determines if this maint doc is activating an organization reversion category
054         * @return true if the document is activating an inactive organization reversion category, false otherwise
055         */
056        protected boolean isActivatingOrganizationReversionCategory() {
057            // the account has to be closed on the new side when editing in order for it to be possible that we are closing the account
058            if (KNSConstants.MAINTENANCE_EDIT_ACTION.equals(getMaintenanceAction()) && ((OrganizationReversionCategory) getBusinessObject()).isActive()) {
059                OrganizationReversionCategory existingOrganizationReversionCategoryFromDB = retrieveExistingOrganizationReversionCategory();
060                if (ObjectUtils.isNotNull(existingOrganizationReversionCategoryFromDB)) {
061                    // now see if the original account was not closed, in which case, we are closing the account
062                    if (!existingOrganizationReversionCategoryFromDB.isActive()) {
063                        return true;
064                    }
065                }
066            }
067            return false;
068        }
069        
070        /**
071         * Grabs the old version of this org reversion category from the database
072         * @return the old version of this organization reversion category
073         */
074        protected OrganizationReversionCategory retrieveExistingOrganizationReversionCategory() {
075            final OrganizationReversionCategory orgRevCategory = (OrganizationReversionCategory)getBusinessObject();
076            Map<String, Object> pkMap = new HashMap<String, Object>();
077            pkMap.put("organizationReversionCategoryCode", ((OrganizationReversionCategory)getBusinessObject()).getOrganizationReversionCategoryCode());
078            final OrganizationReversionCategory oldOrgRevCategory = (OrganizationReversionCategory)SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(OrganizationReversionCategory.class, pkMap);
079            return oldOrgRevCategory;
080        }
081    
082        /**
083         * Overridden to trickle down inactivation or activation to details
084         * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#saveBusinessObject()
085         */
086        @Override
087        public void saveBusinessObject() {
088            final boolean isActivatingOrgReversionCategory = isActivatingOrganizationReversionCategory();
089            final boolean isInactivatingOrgReversionCategory = isInactivatingOrganizationReversionCategory();
090            
091            super.saveBusinessObject();
092            
093            if (isActivatingOrgReversionCategory) {
094                SpringContext.getBean(OrganizationReversionDetailTrickleDownInactivationService.class).trickleDownActiveOrganizationReversionDetails((OrganizationReversionCategory)getBusinessObject(), documentNumber);
095            } else if (isInactivatingOrgReversionCategory) {
096                SpringContext.getBean(OrganizationReversionDetailTrickleDownInactivationService.class).trickleDownInactiveOrganizationReversionDetails((OrganizationReversionCategory)getBusinessObject(), documentNumber);
097            }
098        }
099    
100    }