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.krad.service.BusinessObjectService;
 24  
 import org.kuali.rice.krad.util.KRADPropertyConstants;
 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  12
 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(
 46  
                 KRADPropertyConstants.POSTAL_COUNTRY_CODE, code));
 47  
 
 48  1
         return CountryBo.to(countryBo);
 49  
     }
 50  
 
 51  
     @Override
 52  
     public Country getCountryByAlternateCode(final String alternateCode) {
 53  5
         if (StringUtils.isBlank(alternateCode)) {
 54  2
             throw new RiceIllegalArgumentException("alt code is blank");
 55  
         }
 56  
 
 57  3
         Collection<CountryBo> countryList = businessObjectService.findMatching(CountryBo.class, Collections.singletonMap(
 58  
                 KRADPropertyConstants.ALTERNATE_POSTAL_COUNTRY_CODE, alternateCode));
 59  3
         if (countryList == null || countryList.isEmpty()) {
 60  1
             return null;
 61  2
         } else if (countryList.size() == 1) {
 62  1
             return CountryBo.to(countryList.iterator().next());
 63  1
         } else throw new RiceIllegalStateException("Multiple countries found with same alternateCode");
 64  
     }
 65  
 
 66  
     @Override
 67  
     public List<Country> findAllCountriesNotRestricted() {
 68  3
         List<String> criteriaValues = new ArrayList<String>();
 69  3
         criteriaValues.add(null);
 70  3
         criteriaValues.add("N");
 71  
 
 72  3
         final Map<String, Object> map = new HashMap<String, Object>();
 73  3
         map.put(KRADPropertyConstants.POSTAL_COUNTRY_RESTRICTED_INDICATOR, criteriaValues);
 74  3
         map.put("active", Boolean.TRUE);
 75  
 
 76  3
         Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map));
 77  
 
 78  3
         return convertListOfBosToImmutables(countryBos);
 79  
     }
 80  
 
 81  
     @Override
 82  
     public List<Country> findAllCountries() {
 83  1
         final Map<String, Object> map = new HashMap<String, Object>();
 84  1
         map.put("active", Boolean.TRUE);
 85  
 
 86  1
         Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map));
 87  1
         return convertListOfBosToImmutables(countryBos);
 88  
     }
 89  
 
 90  
     /**
 91  
      * Sets the businessObjectServiceMockFor attribute value.
 92  
      *
 93  
      * @param businessObjectService The businessObjectServiceMockFor to set.
 94  
      */
 95  
     public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
 96  8
         this.businessObjectService = businessObjectService;
 97  8
     }
 98  
 
 99  
     /**
 100  
      * Converts a List<CountryBo> to an Unmodifiable List<Country>
 101  
      *
 102  
      * @param countryBos a mutable List<CountryBo> to made completely immutable.
 103  
      * @return An unmodifiable List<Country>
 104  
      */
 105  
     List<Country> convertListOfBosToImmutables(final Collection<CountryBo> countryBos) {
 106  4
         ArrayList<Country> countries = new ArrayList<Country>();
 107  4
         for (CountryBo bo : countryBos) {
 108  10
             Country country = CountryBo.to(bo);
 109  10
             countries.add(country);
 110  10
         }
 111  4
         return Collections.unmodifiableList(countries);
 112  
     }
 113  
 }