View Javadoc

1   /*
2    * Copyright 2008-2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.service.impl;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.log4j.Logger;
25  import org.kuali.rice.kns.bo.Country;
26  import org.kuali.rice.kns.service.CountryService;
27  import org.kuali.rice.kns.service.KNSServiceLocator;
28  import org.kuali.rice.kns.service.KualiModuleService;
29  import org.kuali.rice.kns.util.KNSConstants;
30  import org.kuali.rice.kns.util.KNSPropertyConstants;
31  
32  public class CountryServiceImpl implements CountryService {
33      private static Logger LOG = Logger.getLogger(CountryServiceImpl.class);
34  
35      private KualiModuleService kualiModuleService;
36  
37      /**
38       * @see org.kuali.kfs.sys.service.CountryService#getByPrimaryId(java.lang.String)
39       */
40      public Country getByPrimaryId(String postalCountryCode) {
41          if (StringUtils.isBlank(postalCountryCode)) {
42              LOG.debug("The postalCountryCode cannot be empty String.");
43              return null;
44          }
45  
46          Map<String, Object> postalCountryMap = new HashMap<String, Object>();
47          postalCountryMap.put(KNSPropertyConstants.POSTAL_COUNTRY_CODE, postalCountryCode);
48  
49          return kualiModuleService.getResponsibleModuleService(Country.class).getExternalizableBusinessObject(Country.class, postalCountryMap);
50      }
51  
52      public Country getByPrimaryIdIfNecessary(String postalCountryCode, Country existingCountry) {
53          if (existingCountry != null) {
54              if (StringUtils.equals(postalCountryCode, existingCountry.getPostalCountryCode())) {
55                  return existingCountry;
56              }
57          }
58  
59          return this.getByPrimaryId(postalCountryCode);
60      }
61  
62  	/**
63  	 * @see org.kuali.rice.kns.service.CountryService#getByAlternatePostalCountryCode(java.lang.String)
64  	 */
65  	public Country getByAlternatePostalCountryCode(String alternatePostalCountryCode) {
66          if (StringUtils.isBlank(alternatePostalCountryCode)) {
67              LOG.debug("The alternatePostalCountryCode cannot be empty String.");
68              return null;
69          }
70  
71          Map<String, Object> postalCountryMap = new HashMap<String, Object>();
72          postalCountryMap.put(KNSPropertyConstants.ALTERNATE_POSTAL_COUNTRY_CODE, alternatePostalCountryCode);
73  
74          List<Country> countryList = kualiModuleService.getResponsibleModuleService(Country.class).getExternalizableBusinessObjectsList(Country.class, postalCountryMap);
75          if (countryList == null || countryList.isEmpty()) {
76          	return null;
77          }
78          else if (countryList.size() == 1) {
79          	return countryList.get(0);
80          }
81          else throw new IllegalStateException("Multiple countries found with same alternatePostalCountryCode");
82  	}
83  
84  	/**
85  	 * @see org.kuali.rice.kns.service.CountryService#getByAlternatePostalCountryCodeIfNecessary(java.lang.String, org.kuali.rice.kns.bo.Country)
86  	 */
87  	public Country getByAlternatePostalCountryCodeIfNecessary(String alternatePostalCountryCode, Country existingCountry) {
88  		if (existingCountry != null) {
89  			if (StringUtils.equals(alternatePostalCountryCode, existingCountry.getAlternatePostalCountryCode())) {
90  				return existingCountry;
91  			}
92  		}
93  		
94  		return this.getByAlternatePostalCountryCode(alternatePostalCountryCode);
95  	}
96      
97      /**
98       * @see org.kuali.kfs.sys.service.CountryService#getDefaultCountry()
99       */
100     public Country getDefaultCountry() {
101         String postalCountryCode = KNSServiceLocator.getParameterService().getParameterValue(KNSConstants.KNS_NAMESPACE,
102 	        	KNSConstants.DetailTypes.ALL_DETAIL_TYPE, KNSConstants.SystemGroupParameterNames.DEFAULT_COUNTRY);
103         return this.getByPrimaryId(postalCountryCode);
104     }
105     
106     /**
107      * @see org.kuali.kfs.sys.service.CountryService#findAllCountriesNotRestricted()
108      */
109     public List<Country> findAllCountriesNotRestricted() {
110         List<String> criteriaValues = new ArrayList<String>();
111         criteriaValues.add(null);
112         criteriaValues.add("N");
113         
114         Map<String, Object> postalCountryMap = new HashMap<String, Object>();
115         postalCountryMap.put(KNSPropertyConstants.POSTAL_COUNTRY_RESTRICTED_INDICATOR, criteriaValues);
116         
117         return kualiModuleService.getResponsibleModuleService(Country.class).getExternalizableBusinessObjectsList(Country.class, postalCountryMap);
118     }
119 
120     /**
121      * @see org.kuali.kfs.sys.service.CountryService#findAllCountries()
122      */
123     public List<Country> findAllCountries() {
124         Map<String, Object> postalCountryMap = new HashMap<String, Object>();
125         return kualiModuleService.getResponsibleModuleService(Country.class).getExternalizableBusinessObjectsList(Country.class, postalCountryMap);
126     }
127 
128     /**
129      * Sets the kualiModuleService attribute value.
130      * 
131      * @param kualiModuleService The kualiModuleService to set.
132      */
133     public void setKualiModuleService(KualiModuleService kualiModuleService) {
134         this.kualiModuleService = kualiModuleService;
135     }
136 
137 }