View Javadoc
1   /**
2    * Copyright 2005-2015 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.location.impl.country;
17  
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.core.api.criteria.QueryByCriteria;
21  import org.kuali.rice.core.api.criteria.QueryResults;
22  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
23  import org.kuali.rice.core.api.exception.RiceIllegalStateException;
24  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
25  import org.kuali.rice.krad.data.DataObjectService;
26  import org.kuali.rice.krad.util.KRADConstants;
27  import org.kuali.rice.krad.util.KRADPropertyConstants;
28  import org.kuali.rice.location.api.LocationConstants;
29  import org.kuali.rice.location.api.country.Country;
30  import org.kuali.rice.location.api.country.CountryQueryResults;
31  import org.kuali.rice.location.api.country.CountryService;
32  import org.springframework.beans.factory.annotation.Required;
33  
34  import java.util.ArrayList;
35  import java.util.Collection;
36  import java.util.Collections;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  public final class CountryServiceImpl implements CountryService {
42      private ParameterService parameterService;
43      private DataObjectService dataObjectService;
44  
45      @Override
46      public Country getCountry(final String code) {
47          if (StringUtils.isBlank(code)) {
48              throw new RiceIllegalArgumentException("code is blank");
49          }
50          CountryBo countryBo = getDataObjectService().find(CountryBo.class,code);
51          return CountryBo.to(countryBo);
52      }
53  
54      @Override
55      public Country getCountryByAlternateCode(final String alternateCode) {
56          if (StringUtils.isBlank(alternateCode)) {
57              throw new RiceIllegalArgumentException("alt code is blank");
58          }
59          QueryByCriteria qbc = QueryByCriteria.Builder.forAttribute(KRADPropertyConstants.ALTERNATE_POSTAL_COUNTRY_CODE,
60                  alternateCode).build();
61          QueryResults<CountryBo> countryBoQueryResults = getDataObjectService().findMatching(CountryBo.class,qbc);
62          List<CountryBo> countryList = countryBoQueryResults.getResults();
63          if (countryList == null || countryList.isEmpty()) {
64              return null;
65          } else if (countryList.size() == 1) {
66              return CountryBo.to(countryList.iterator().next());
67          } else throw new RiceIllegalStateException("Multiple countries found with same alternateCode");
68      }
69  
70      @Override
71      public List<Country> findAllCountriesNotRestricted() {
72          List<Boolean> criteriaValues = new ArrayList<Boolean>();
73          criteriaValues.add(null);
74          criteriaValues.add(Boolean.FALSE);
75  
76          final Map<String, Object> map = new HashMap<String, Object>();
77  
78          map.put(KRADPropertyConstants.POSTAL_COUNTRY_RESTRICTED_INDICATOR, criteriaValues);
79          map.put("active", Boolean.TRUE);
80          QueryResults<CountryBo> countryBos = dataObjectService.findMatching(CountryBo.class,QueryByCriteria.Builder.andAttributes(
81                  map).build());
82  
83          return convertListOfBosToImmutables(countryBos.getResults());
84      }
85  
86      @Override
87      public List<Country> findAllCountries() {
88          QueryResults<CountryBo> countryBoQueryResults = dataObjectService.findMatching(CountryBo.class,
89                  QueryByCriteria.Builder.forAttribute("active",Boolean.TRUE).build());
90          //Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map));
91          return convertListOfBosToImmutables(countryBoQueryResults.getResults());
92      }
93  
94      @Override
95      public Country getDefaultCountry() {
96          String defaultCountryCode = parameterService.getParameterValueAsString(LocationConstants.NAMESPACE_CODE,
97                  KRADConstants.DetailTypes.ALL_DETAIL_TYPE, LocationConstants.ParameterKey.DEFAULT_COUNTRY);
98          if (StringUtils.isBlank(defaultCountryCode)) {
99              return null;
100         }
101         return getCountry(defaultCountryCode);
102     }
103 
104     @Override
105     public CountryQueryResults findCountries(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
106         incomingParamCheck(queryByCriteria, "queryByCriteria");
107 
108         QueryResults<CountryBo> results = dataObjectService.findMatching(CountryBo.class, queryByCriteria);
109 
110         CountryQueryResults.Builder builder = CountryQueryResults.Builder.create();
111         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
112         builder.setTotalRowCount(results.getTotalRowCount());
113 
114         final List<Country.Builder> ims = new ArrayList<Country.Builder>();
115         for (CountryBo bo : results.getResults()) {
116             ims.add(Country.Builder.create(bo));
117         }
118 
119         builder.setResults(ims);
120         return builder.build();
121     }
122 
123     public ParameterService getParameterService() {
124         return parameterService;
125     }
126 
127     public void setParameterService(ParameterService parameterService) {
128         this.parameterService = parameterService;
129     }
130 
131     /**
132      * Converts a List<CountryBo> to an Unmodifiable List<Country>
133      *
134      * @param countryBos a mutable List<CountryBo> to made completely immutable.
135      * @return An unmodifiable List<Country>
136      */
137     List<Country> convertListOfBosToImmutables(final Collection<CountryBo> countryBos) {
138         ArrayList<Country> countries = new ArrayList<Country>();
139         for (CountryBo bo : countryBos) {
140             Country country = CountryBo.to(bo);
141             countries.add(country);
142         }
143         return Collections.unmodifiableList(countries);
144     }
145 
146     private void incomingParamCheck(Object object, String name) {
147         if (object == null) {
148             throw new RiceIllegalArgumentException(name + " was null");
149         } else if (object instanceof String
150                 && StringUtils.isBlank((String) object)) {
151             throw new RiceIllegalArgumentException(name + " was blank");
152         }
153     }
154 
155 
156     public DataObjectService getDataObjectService() {
157         return dataObjectService;
158     }
159     @Required
160     public void setDataObjectService(DataObjectService dataObjectService) {
161         this.dataObjectService = dataObjectService;
162     }
163 
164 }