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.state;
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.core.api.exception.RiceIllegalStateException;
023import org.kuali.rice.krad.data.CompoundKey;
024import org.kuali.rice.krad.data.DataObjectService;
025import org.kuali.rice.location.api.country.Country;
026import org.kuali.rice.location.api.country.CountryService;
027import org.kuali.rice.location.api.services.LocationApiServiceLocator;
028import org.kuali.rice.location.api.state.State;
029import org.kuali.rice.location.api.state.StateQueryResults;
030import org.kuali.rice.location.api.state.StateService;
031import org.springframework.beans.factory.annotation.Required;
032
033import java.util.ArrayList;
034import java.util.Collections;
035import java.util.HashMap;
036import java.util.List;
037import java.util.Map;
038
039public class StateServiceImpl implements StateService {
040    private CountryService countryService;
041    private DataObjectService dataObjectService;
042
043    @Override
044    public State getState(String countryCode, String code) {
045        if (StringUtils.isBlank(countryCode)) {
046            throw new RiceIllegalArgumentException(("countryCode is null"));
047        }
048
049        if (StringUtils.isBlank(code)) {
050            throw new RiceIllegalArgumentException(("code is null"));
051        }
052
053        final Map<String, Object> map = new HashMap<String, Object>();
054        map.put("countryCode", countryCode);
055        map.put("code", code);
056
057        return StateBo.to(getDataObjectService().find(StateBo.class,new CompoundKey(map)));
058    }
059
060    @Override
061    public List<State> findAllStatesInCountry(String countryCode) {
062        if (StringUtils.isBlank(countryCode)) {
063            throw new RiceIllegalArgumentException(("countryCode is null"));
064        }
065
066        final Map<String, Object> map = new HashMap<String, Object>();
067        map.put("countryCode", countryCode);
068        map.put("active", Boolean.TRUE);
069
070        QueryResults<StateBo> stateBos = getDataObjectService().findMatching(StateBo.class,
071                QueryByCriteria.Builder.andAttributes(map).build());
072
073        if (stateBos == null) {
074            return Collections.emptyList();
075        }
076
077        final List<State> toReturn = new ArrayList<State>();
078        for (StateBo bo : stateBos.getResults()) {
079            if (bo != null && bo.isActive()) {
080                toReturn.add(StateBo.to(bo));
081            }
082        }
083
084        return Collections.unmodifiableList(toReturn);
085    }
086    
087    @Override
088    public List<State> findAllStatesInCountryByAltCode(String alternateCode) {
089        if (StringUtils.isBlank(alternateCode)) {
090            throw new RiceIllegalArgumentException(("alternateCode is null"));
091        }
092        
093        Country country = getCountryService().getCountryByAlternateCode(alternateCode);
094        
095        if(country == null) {
096            throw new RiceIllegalStateException("The alternateCode is not a valid Alternate Postal Country Code.  alternateCode: " + alternateCode);
097        }
098        
099        final List<State> toReturn = findAllStatesInCountry(country.getCode());
100        return Collections.unmodifiableList(toReturn);
101    }
102
103    @Override
104    public StateQueryResults findStates(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
105        incomingParamCheck(queryByCriteria, "queryByCriteria");
106
107        QueryResults<StateBo> results = getDataObjectService().findMatching(StateBo.class, queryByCriteria);
108
109        StateQueryResults.Builder builder = StateQueryResults.Builder.create();
110        builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
111        builder.setTotalRowCount(results.getTotalRowCount());
112
113        final List<State.Builder> ims = new ArrayList<State.Builder>();
114        for (StateBo bo : results.getResults()) {
115            ims.add(State.Builder.create(bo));
116        }
117
118        builder.setResults(ims);
119        return builder.build();
120    }
121
122    public CountryService getCountryService() {
123        if (countryService == null) {
124            countryService = LocationApiServiceLocator.getCountryService();
125        }
126        return countryService;
127    }   
128
129    public void setCountryService(CountryService countryService) {
130        this.countryService = countryService;
131    }
132
133    private void incomingParamCheck(Object object, String name) {
134        if (object == null) {
135            throw new RiceIllegalArgumentException(name + " was null");
136        } else if (object instanceof String
137                && StringUtils.isBlank((String) object)) {
138            throw new RiceIllegalArgumentException(name + " was blank");
139        }
140    }
141
142    public DataObjectService getDataObjectService() {
143        return dataObjectService;
144    }
145
146    @Required
147    public void setDataObjectService(DataObjectService dataObjectService) {
148        this.dataObjectService = dataObjectService;
149    }
150
151}