001/**
002 * Copyright 2005-2016 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.rice.location.impl.county;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.criteria.CriteriaLookupService;
020import org.kuali.rice.core.api.criteria.GenericQueryResults;
021import org.kuali.rice.core.api.criteria.QueryByCriteria;
022import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
023import org.kuali.rice.krad.service.BusinessObjectService;
024import org.kuali.rice.location.api.county.County;
025import org.kuali.rice.location.api.county.CountyQueryResults;
026import org.kuali.rice.location.api.county.CountyService;
027
028import java.util.ArrayList;
029import java.util.Collection;
030import java.util.Collections;
031import java.util.HashMap;
032import java.util.List;
033import java.util.Map;
034
035public class CountyServiceImpl implements CountyService {
036
037    private BusinessObjectService businessObjectService;
038    private CriteriaLookupService criteriaLookupService;
039
040    @Override
041    public County getCounty(String countryCode, String stateCode, String code) {
042        if (StringUtils.isBlank(countryCode)) {
043            throw new RiceIllegalArgumentException(("countryCode is null"));
044        }
045
046        if (StringUtils.isBlank(code)) {
047            throw new RiceIllegalArgumentException(("code is null"));
048        }
049
050        if (StringUtils.isBlank(stateCode)) {
051            throw new RiceIllegalArgumentException(("stateCode is null"));
052        }
053
054        final Map<String, Object> map = new HashMap<String, Object>();
055        map.put("countryCode", countryCode);
056        map.put("stateCode", stateCode);
057        map.put("code", code);
058
059        return CountyBo.to(businessObjectService.findByPrimaryKey(CountyBo.class, Collections.unmodifiableMap(map)));
060    }
061
062    @Override
063    public List<County> findAllCountiesInCountryAndState(String countryCode, String stateCode) {
064        if (StringUtils.isBlank(countryCode)) {
065            throw new RiceIllegalArgumentException(("countryCode is null"));
066        }
067
068        if (StringUtils.isBlank(stateCode)) {
069            throw new RiceIllegalArgumentException(("stateCode is null"));
070        }
071
072        final Map<String, Object> map = new HashMap<String, Object>();
073        map.put("countryCode", countryCode);
074        map.put("stateCode", stateCode);
075        map.put("active", Boolean.TRUE);
076
077        final Collection<CountyBo> bos = businessObjectService.findMatching(CountyBo.class, Collections.unmodifiableMap(map));
078        if (bos == null) {
079            return Collections.emptyList();
080        }
081
082        final List<County> toReturn = new ArrayList<County>();
083        for (CountyBo bo : bos) {
084            if (bo != null && bo.isActive()) {
085                toReturn.add(CountyBo.to(bo));
086            }
087        }
088
089        return Collections.unmodifiableList(toReturn);
090    }
091
092    @Override
093    public CountyQueryResults findCounties(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
094        incomingParamCheck(queryByCriteria, "queryByCriteria");
095
096        GenericQueryResults<CountyBo> results = criteriaLookupService.lookup(CountyBo.class, queryByCriteria);
097
098        CountyQueryResults.Builder builder = CountyQueryResults.Builder.create();
099        builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
100        builder.setTotalRowCount(results.getTotalRowCount());
101
102        final List<County.Builder> ims = new ArrayList<County.Builder>();
103        for (CountyBo bo : results.getResults()) {
104            ims.add(County.Builder.create(bo));
105        }
106
107        builder.setResults(ims);
108        return builder.build();
109    }
110
111    public void setBusinessObjectService(BusinessObjectService businessObjectService) {
112        this.businessObjectService = businessObjectService;
113    }
114
115    private void incomingParamCheck(Object object, String name) {
116        if (object == null) {
117            throw new RiceIllegalArgumentException(name + " was null");
118        } else if (object instanceof String
119                && StringUtils.isBlank((String) object)) {
120            throw new RiceIllegalArgumentException(name + " was blank");
121        }
122    }
123
124    /**
125     * Sets the criteriaLookupService attribute value.
126     *
127     * @param criteriaLookupService The criteriaLookupService to set.
128     */
129    public void setCriteriaLookupService(final CriteriaLookupService criteriaLookupService) {
130        this.criteriaLookupService = criteriaLookupService;
131    }
132
133}