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.postalcode;
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.postalcode.PostalCode;
025import org.kuali.rice.location.api.postalcode.PostalCodeQueryResults;
026import org.kuali.rice.location.api.postalcode.PostalCodeService;
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 PostalCodeServiceImpl implements PostalCodeService {
036    private DataObjectService dataObjectService;
037
038    @Override
039    public PostalCode getPostalCode(String countryCode, 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        final Map<String, Object> map = new HashMap<String, Object>();
049        map.put("countryCode", countryCode);
050        map.put("code", code);
051
052        return PostalCodeBo.to(getDataObjectService().find(PostalCodeBo.class,new CompoundKey(map)));
053    }
054
055    @Override
056    public List<PostalCode> findAllPostalCodesInCountry(String countryCode) {
057        if (StringUtils.isBlank(countryCode)) {
058            throw new RiceIllegalArgumentException(("countryCode is null"));
059        }
060
061        final Map<String, Object> map = new HashMap<String, Object>();
062        map.put("countryCode", countryCode);
063        map.put("active", Boolean.TRUE);
064
065        QueryResults<PostalCodeBo> postalCodeBoQueryResults = getDataObjectService().findMatching(PostalCodeBo.class,
066                    QueryByCriteria.Builder.andAttributes(map).build());
067        if (postalCodeBoQueryResults == null) {
068            return Collections.emptyList();
069        }
070
071        final List<PostalCode> toReturn = new ArrayList<PostalCode>();
072        for (PostalCodeBo bo : postalCodeBoQueryResults.getResults()) {
073            if (bo != null && bo.isActive()) {
074                toReturn.add(PostalCodeBo.to(bo));
075            }
076        }
077
078        return Collections.unmodifiableList(toReturn);
079    }
080
081    @Override
082    public PostalCodeQueryResults findPostalCodes(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
083        incomingParamCheck(queryByCriteria, "queryByCriteria");
084
085        QueryResults<PostalCodeBo> results = getDataObjectService().findMatching(PostalCodeBo.class, queryByCriteria);
086
087        PostalCodeQueryResults.Builder builder = PostalCodeQueryResults.Builder.create();
088        builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
089        builder.setTotalRowCount(results.getTotalRowCount());
090
091        final List<PostalCode.Builder> ims = new ArrayList<PostalCode.Builder>();
092        for (PostalCodeBo bo : results.getResults()) {
093            ims.add(PostalCode.Builder.create(bo));
094        }
095
096        builder.setResults(ims);
097        return builder.build();
098    }
099
100    private void incomingParamCheck(Object object, String name) {
101        if (object == null) {
102            throw new RiceIllegalArgumentException(name + " was null");
103        } else if (object instanceof String
104                && StringUtils.isBlank((String) object)) {
105            throw new RiceIllegalArgumentException(name + " was blank");
106        }
107    }
108
109    public DataObjectService getDataObjectService() {
110        return dataObjectService;
111    }
112
113    @Required
114    public void setDataObjectService(DataObjectService dataObjectService) {
115        this.dataObjectService = dataObjectService;
116    }
117}