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.QueryByCriteria;
020import org.kuali.rice.core.api.criteria.QueryResults;
021import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
022import org.kuali.rice.krad.data.CompoundKey;
023import org.kuali.rice.krad.data.DataObjectService;
024import org.kuali.rice.location.api.county.County;
025import org.kuali.rice.location.api.county.CountyQueryResults;
026import org.kuali.rice.location.api.county.CountyService;
027import org.springframework.beans.factory.annotation.Required;
028
029import java.util.ArrayList;
030import java.util.Collections;
031import java.util.HashMap;
032import java.util.List;
033import java.util.Map;
034
035public class CountyServiceImpl implements CountyService {
036    private DataObjectService dataObjectService;
037
038    @Override
039    public County getCounty(String countryCode, String stateCode, String code) {
040        if (StringUtils.isBlank(countryCode)) {
041            throw new RiceIllegalArgumentException(("countryCode is null"));
042        }
043
044        if (StringUtils.isBlank(code)) {
045            throw new RiceIllegalArgumentException(("code is null"));
046        }
047
048        if (StringUtils.isBlank(stateCode)) {
049            throw new RiceIllegalArgumentException(("stateCode is null"));
050        }
051
052        final Map<String, Object> map = new HashMap<String, Object>();
053        map.put("countryCode", countryCode);
054        map.put("stateCode", stateCode);
055        map.put("code", code);
056
057        return CountyBo.to(getDataObjectService().find(CountyBo.class, new CompoundKey(map)));
058    }
059
060    @Override
061    public List<County> findAllCountiesInCountryAndState(String countryCode, String stateCode) {
062        if (StringUtils.isBlank(countryCode)) {
063            throw new RiceIllegalArgumentException(("countryCode is null"));
064        }
065
066        if (StringUtils.isBlank(stateCode)) {
067            throw new RiceIllegalArgumentException(("stateCode is null"));
068        }
069
070        final Map<String, Object> map = new HashMap<String, Object>();
071        map.put("countryCode", countryCode);
072        map.put("stateCode", stateCode);
073        map.put("active", Boolean.TRUE);
074
075        QueryResults<CountyBo> countyBos = getDataObjectService().findMatching(CountyBo.class,
076                QueryByCriteria.Builder.andAttributes(map).build());
077
078        if (countyBos == null) {
079            return Collections.emptyList();
080        }
081
082        final List<County> toReturn = new ArrayList<County>();
083
084        List<CountyBo> countyBoList = countyBos.getResults();
085
086
087        for(CountyBo countyBo : countyBoList){
088            if(countyBo != null && countyBo.isActive()){
089                toReturn.add(CountyBo.to(countyBo));
090            }
091        }
092
093//        for (CountyBo bo : countyBoList) {
094//            if (bo != null && bo.isActive()) {
095//                toReturn.add(CountyBo.to(bo));
096//            }
097//        }
098
099        return Collections.unmodifiableList(toReturn);
100    }
101
102    @Override
103    public CountyQueryResults findCounties(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
104        incomingParamCheck(queryByCriteria, "queryByCriteria");
105
106        QueryResults<CountyBo> results = getDataObjectService().findMatching(CountyBo.class, queryByCriteria);
107
108        CountyQueryResults.Builder builder = CountyQueryResults.Builder.create();
109        builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
110        builder.setTotalRowCount(results.getTotalRowCount());
111
112        final List<County.Builder> ims = new ArrayList<County.Builder>();
113        for (CountyBo bo : results.getResults()) {
114            ims.add(County.Builder.create(bo));
115        }
116
117        builder.setResults(ims);
118        return builder.build();
119    }
120
121    private void incomingParamCheck(Object object, String name) {
122        if (object == null) {
123            throw new RiceIllegalArgumentException(name + " was null");
124        } else if (object instanceof String
125                && StringUtils.isBlank((String) object)) {
126            throw new RiceIllegalArgumentException(name + " was blank");
127        }
128    }
129
130    public DataObjectService getDataObjectService() {
131        return dataObjectService;
132    }
133
134    @Required
135    public void setDataObjectService(DataObjectService dataObjectService) {
136        this.dataObjectService = dataObjectService;
137    }
138
139}