Coverage Report - org.kuali.rice.shareddata.impl.country.CountryServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
CountryServiceImpl
100%
33/33
91%
11/12
2.833
 
 1  
 /*
 2  
  * Copyright 2006-2011 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  
 
 17  
 package org.kuali.rice.shareddata.impl.country;
 18  
 
 19  
 
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
 22  
 import org.kuali.rice.core.api.exception.RiceIllegalStateException;
 23  
 import org.kuali.rice.kns.service.BusinessObjectService;
 24  
 import org.kuali.rice.kns.util.KNSPropertyConstants;
 25  
 import org.kuali.rice.shareddata.api.country.Country;
 26  
 import org.kuali.rice.shareddata.api.country.CountryService;
 27  
 
 28  
 import java.util.ArrayList;
 29  
 import java.util.Collection;
 30  
 import java.util.Collections;
 31  
 import java.util.HashMap;
 32  
 import java.util.List;
 33  
 import java.util.Map;
 34  
 
 35  11
 public final class CountryServiceImpl implements CountryService {
 36  
 
 37  
     private BusinessObjectService businessObjectService;
 38  
 
 39  
     @Override
 40  
     public Country getCountry(final String code) {
 41  3
         if (StringUtils.isBlank(code)) {
 42  2
             throw new RiceIllegalArgumentException("code is blank");
 43  
         }
 44  
 
 45  1
         CountryBo countryBo = businessObjectService.findByPrimaryKey(CountryBo.class, Collections.singletonMap(KNSPropertyConstants.POSTAL_COUNTRY_CODE, code));
 46  
 
 47  1
         return CountryBo.to(countryBo);
 48  
     }
 49  
 
 50  
     @Override
 51  
     public Country getCountryByAlternateCode(final String alternateCode) {
 52  5
         if (StringUtils.isBlank(alternateCode)) {
 53  2
             throw new RiceIllegalArgumentException("alt code is blank");
 54  
         }
 55  
 
 56  3
         Collection<CountryBo> countryList = businessObjectService.findMatching(CountryBo.class, Collections.singletonMap(KNSPropertyConstants.ALTERNATE_POSTAL_COUNTRY_CODE, alternateCode));
 57  3
         if (countryList == null || countryList.isEmpty()) {
 58  1
             return null;
 59  2
         } else if (countryList.size() == 1) {
 60  1
             return CountryBo.to(countryList.iterator().next());
 61  1
         } else throw new RiceIllegalStateException("Multiple countries found with same alternateCode");
 62  
     }
 63  
 
 64  
     @Override
 65  
     public List<Country> findAllCountriesNotRestricted() {
 66  2
         List<String> criteriaValues = new ArrayList<String>();
 67  2
         criteriaValues.add(null);
 68  2
         criteriaValues.add("N");
 69  
 
 70  2
         final Map<String, Object> map = new HashMap<String, Object>();
 71  2
         map.put(KNSPropertyConstants.POSTAL_COUNTRY_RESTRICTED_INDICATOR, criteriaValues);
 72  2
         map.put("active", Boolean.TRUE);
 73  
 
 74  2
         Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map));
 75  
 
 76  2
         return convertListOfBosToImmutables(countryBos);
 77  
     }
 78  
 
 79  
     @Override
 80  
     public List<Country> findAllCountries() {
 81  1
         final Map<String, Object> map = new HashMap<String, Object>();
 82  1
         map.put("active", Boolean.TRUE);
 83  
 
 84  1
         Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map));
 85  1
         return convertListOfBosToImmutables(countryBos);
 86  
     }
 87  
 
 88  
     /**
 89  
      * Sets the businessObjectServiceMockFor attribute value.
 90  
      *
 91  
      * @param businessObjectService The businessObjectServiceMockFor to set.
 92  
      */
 93  
     public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
 94  7
         this.businessObjectService = businessObjectService;
 95  7
     }
 96  
 
 97  
     /**
 98  
      * Converts a List<CountryBo> to an Unmodifiable List<Country>
 99  
      *
 100  
      * @param countryBos a mutable List<CountryBo> to made completely immutable.
 101  
      * @return An unmodifiable List<Country>
 102  
      */
 103  
     List<Country> convertListOfBosToImmutables(final Collection<CountryBo> countryBos) {
 104  3
         ArrayList<Country> countries = new ArrayList<Country>();
 105  3
         for (CountryBo bo : countryBos) {
 106  7
             Country country = CountryBo.to(bo);
 107  7
             countries.add(country);
 108  7
         }
 109  3
         return Collections.unmodifiableList(countries);
 110  
     }
 111  
 }