001/*
002 * Copyright 2008-2009 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.businessobject;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.commons.lang.StringUtils;
022import org.kuali.ole.sys.context.SpringContext;
023import org.kuali.rice.core.api.mo.common.active.MutableInactivatable;
024import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
025import org.kuali.rice.krad.service.KualiModuleService;
026import org.kuali.rice.krad.service.ModuleService;
027import org.kuali.rice.location.api.LocationConstants;
028import org.kuali.rice.location.framework.country.CountryEbo;
029import org.kuali.rice.location.framework.county.CountyEbo;
030
031public class TaxRegionCounty extends PersistableBusinessObjectBase implements MutableInactivatable {
032
033    protected String postalCountryCode;
034        protected String countyCode;
035        protected String stateCode;
036        protected String taxRegionCode;
037        protected boolean active;
038
039        protected CountryEbo country;
040        protected CountyEbo county;
041        protected TaxRegion taxRegion;
042        
043        public String getCountyCode() {
044                return countyCode;
045        }
046        public void setCountyCode(String countyCode) {
047                this.countyCode = countyCode;
048        }
049        @Override
050        public boolean isActive() {
051                return active;
052        }
053        @Override
054        public void setActive(boolean active) {
055                this.active = active;
056        }
057        public TaxRegion getTaxRegion() {
058        return taxRegion;
059    }
060    public void setTaxRegion(TaxRegion taxRegion) {
061        this.taxRegion = taxRegion;
062    }
063    public String getStateCode() {
064                return stateCode;
065        }
066        public void setStateCode(String stateCode) {
067                this.stateCode = stateCode;
068        }
069        public String getTaxRegionCode() {
070                return taxRegionCode;
071        }
072        public void setTaxRegionCode(String taxRegionCode) {
073                this.taxRegionCode = taxRegionCode;
074        }
075        
076        public CountyEbo getCounty() {
077        if ( StringUtils.isBlank(postalCountryCode) || StringUtils.isBlank(stateCode) || StringUtils.isBlank(countyCode) ) {
078            county = null;
079        } else {
080            if ( county == null
081                    || !StringUtils.equals( county.getCode(),countyCode)
082                    || !StringUtils.equals( county.getStateCode(), stateCode )
083                    || !StringUtils.equals( county.getCountryCode(), postalCountryCode )) {
084                ModuleService moduleService = SpringContext.getBean(KualiModuleService.class).getResponsibleModuleService(CountyEbo.class);
085                if ( moduleService != null ) {
086                    Map<String,Object> keys = new HashMap<String, Object>(3);
087                    keys.put(LocationConstants.PrimaryKeyConstants.COUNTRY_CODE, postalCountryCode);
088                    keys.put(LocationConstants.PrimaryKeyConstants.STATE_CODE, stateCode);
089                    keys.put(LocationConstants.PrimaryKeyConstants.CODE, countyCode);
090                    county = moduleService.getExternalizableBusinessObject(CountyEbo.class, keys);
091                } else {
092                    throw new RuntimeException( "CONFIGURATION ERROR: No responsible module found for EBO class.  Unable to proceed." );
093                }
094            }
095    }
096                return county;
097        }
098        public void setCounty(CountyEbo county) {
099                this.county = county;
100        }
101    /**
102     * Gets the postalCountryCode attribute. 
103     * @return Returns the postalCountryCode.
104     */
105    public String getPostalCountryCode() {
106        return postalCountryCode;
107    }
108    /**
109     * Sets the postalCountryCode attribute value.
110     * @param postalCountryCode The postalCountryCode to set.
111     */
112    public void setPostalCountryCode(String postalCountryCode) {
113        this.postalCountryCode = postalCountryCode;
114    }
115    /**
116     * Gets the country attribute. 
117     * @return Returns the country.
118     */
119    public CountryEbo getCountry() {
120        if ( StringUtils.isBlank(postalCountryCode) ) {
121            country = null;
122        } else {
123            if ( country == null || !StringUtils.equals( country.getCode(),postalCountryCode) ) {
124                ModuleService moduleService = SpringContext.getBean(KualiModuleService.class).getResponsibleModuleService(CountryEbo.class);
125                if ( moduleService != null ) {
126                    Map<String,Object> keys = new HashMap<String, Object>(1);
127                    keys.put(LocationConstants.PrimaryKeyConstants.CODE, postalCountryCode);
128                    country = moduleService.getExternalizableBusinessObject(CountryEbo.class, keys);
129                } else {
130                    throw new RuntimeException( "CONFIGURATION ERROR: No responsible module found for EBO class.  Unable to proceed." );
131                }
132            }
133        }
134        return country;
135    }
136    /**
137     * Sets the country attribute value.
138     * @param country The country to set.
139     */
140    public void setCountry(CountryEbo country) {
141        this.country = country;
142    }
143}