001/*
002 * Copyright 2008 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.sys.document;
017
018import java.util.List;
019
020import org.apache.commons.lang.StringUtils;
021import org.kuali.ole.sys.OLEConstants;
022import org.kuali.ole.sys.businessobject.TaxRegion;
023import org.kuali.rice.kns.document.MaintenanceDocument;
024import org.kuali.rice.kns.maintenance.Maintainable;
025import org.kuali.rice.kns.web.ui.Section;
026
027public 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     * KRAD Conversion: Maintainable customizes the hiding/showing of the sections
036     * No Use of data dictionary
037     */
038    @Override
039    public List<Section> getCoreSections(MaintenanceDocument document, Maintainable oldMaintainable) {
040        List<Section> sections = super.getCoreSections(document, oldMaintainable);
041
042        TaxRegion taxRegion = (TaxRegion) getBusinessObject();
043
044        //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
045        if( StringUtils.isNotEmpty( taxRegion.getTaxRegionTypeCode() ) ) {
046            String sectionIdToDisplay = getSectionIdToDisplay(taxRegion.getTaxRegionTypeCode());
047            for (Section section : sections) {
048                if (!isMainOrRateSection(section.getSectionId()) && !sectionIdToDisplay.equals(section.getSectionId())) {
049                    section.setHidden(true);
050                }
051            }            
052        }
053        
054        return sections;
055    }
056
057    /**
058     * This method returns the appropriate section ID that should NOT be hidden based on a specific tax region type code
059     * 
060     * @param taxRegionTypeCode
061     * @return
062     */
063    protected String getSectionIdToDisplay(String taxRegionTypeCode) {
064        if (OLEConstants.TaxRegionConstants.TAX_REGION_TYPE_CODE_STATE.equals(taxRegionTypeCode)) {
065            return OLEConstants.TaxRegionConstants.TAX_REGION_STATES_SECTION_ID;
066        }
067        else if (OLEConstants.TaxRegionConstants.TAX_REGION_TYPE_CODE_COUNTY.equals(taxRegionTypeCode)) {
068            return OLEConstants.TaxRegionConstants.TAX_REGION_COUNTIES_SECTION_ID;
069        }
070        else if (OLEConstants.TaxRegionConstants.TAX_REGION_TYPE_CODE_POSTAL_CODE.equals(taxRegionTypeCode)) {
071            return OLEConstants.TaxRegionConstants.TAX_REGION_POSTAL_CODES_SECTION_ID;
072        }
073        else {
074            throw new RuntimeException("No section is set up for tax region type code " + taxRegionTypeCode);
075        }
076    }
077
078    /**
079     * This method returns true if section is main or tax region rate section
080     * 
081     * @param sectionId
082     * @return
083     */
084    protected boolean isMainOrRateSection(String sectionId) {
085        return OLEConstants.TaxRegionConstants.TAX_REGION_RATES_SECTION_ID.equals(sectionId) || OLEConstants.TaxRegionConstants.TAX_REGION_CREATE_SECTION_ID.equals(sectionId);
086    }
087}