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.sys.document;
017    
018    import java.util.List;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.kuali.kfs.sys.KFSConstants;
022    import org.kuali.kfs.sys.businessobject.TaxRegion;
023    import org.kuali.rice.kns.document.MaintenanceDocument;
024    import org.kuali.rice.kns.maintenance.Maintainable;
025    import org.kuali.rice.kns.web.ui.Section;
026    
027    public class TaxRegionMaintainableImpl extends FinancialSystemMaintainable {
028    
029        
030        /**
031         * This method hides particular tax region sections based on tax region type code
032         * 
033         * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#getCoreSections(org.kuali.rice.kns.maintenance.Maintainable)
034         */
035        @Override
036        public List<Section> getCoreSections(MaintenanceDocument document, Maintainable oldMaintainable) {
037            List<Section> sections = super.getCoreSections(document, oldMaintainable);
038    
039            TaxRegion taxRegion = (TaxRegion) getBusinessObject();
040    
041            //have to check if type code is empty, because on the oldMaintainable on a NEW action, none of the old Maintainable BO's values are set
042            if( StringUtils.isNotEmpty( taxRegion.getTaxRegionTypeCode() ) ) {
043                String sectionIdToDisplay = getSectionIdToDisplay(taxRegion.getTaxRegionTypeCode());
044                for (Section section : sections) {
045                    if (!isMainOrRateSection(section.getSectionId()) && !sectionIdToDisplay.equals(section.getSectionId())) {
046                        section.setHidden(true);
047                    }
048                }            
049            }
050            
051            return sections;
052        }
053    
054        /**
055         * This method returns the appropriate section ID that should NOT be hidden based on a specific tax region type code
056         * 
057         * @param taxRegionTypeCode
058         * @return
059         */
060        protected String getSectionIdToDisplay(String taxRegionTypeCode) {
061            if (KFSConstants.TaxRegionConstants.TAX_REGION_TYPE_CODE_STATE.equals(taxRegionTypeCode)) {
062                return KFSConstants.TaxRegionConstants.TAX_REGION_STATES_SECTION_ID;
063            }
064            else if (KFSConstants.TaxRegionConstants.TAX_REGION_TYPE_CODE_COUNTY.equals(taxRegionTypeCode)) {
065                return KFSConstants.TaxRegionConstants.TAX_REGION_COUNTIES_SECTION_ID;
066            }
067            else if (KFSConstants.TaxRegionConstants.TAX_REGION_TYPE_CODE_POSTAL_CODE.equals(taxRegionTypeCode)) {
068                return KFSConstants.TaxRegionConstants.TAX_REGION_POSTAL_CODES_SECTION_ID;
069            }
070            else {
071                throw new RuntimeException("No section is set up for tax region type code " + taxRegionTypeCode);
072            }
073        }
074    
075        /**
076         * This method returns true if section is main or tax region rate section
077         * 
078         * @param sectionId
079         * @return
080         */
081        protected boolean isMainOrRateSection(String sectionId) {
082            return KFSConstants.TaxRegionConstants.TAX_REGION_RATES_SECTION_ID.equals(sectionId) || KFSConstants.TaxRegionConstants.TAX_REGION_CREATE_SECTION_ID.equals(sectionId);
083        }
084    }