001/**
002 * Copyright 2005-2018 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.framework.state;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.util.ConcreteKeyValue;
020import org.kuali.rice.core.api.util.KeyValue;
021import org.kuali.rice.krad.keyvalues.KeyValuesBase;
022import org.kuali.rice.location.api.country.Country;
023import org.kuali.rice.location.api.services.LocationApiServiceLocator;
024import org.kuali.rice.location.api.state.State;
025
026import java.util.ArrayList;
027import java.util.Collections;
028import java.util.Comparator;
029import java.util.List;
030
031public class StateValuesFinder extends KeyValuesBase {
032
033    private String countryCode = "";
034
035    @Override
036        public List<KeyValue> getKeyValues() {
037        List<KeyValue> labels = new ArrayList<KeyValue>();
038        if (StringUtils.isEmpty(this.countryCode)) {
039            Country defaultCountry = LocationApiServiceLocator.getCountryService().getDefaultCountry();
040            if (defaultCountry != null) {
041                countryCode = defaultCountry.getCode();
042            }
043        }
044        List<State> baseCodes = LocationApiServiceLocator.getStateService().findAllStatesInCountry(countryCode);
045        List<State> codes = new ArrayList<State>( baseCodes );
046        Collections.sort(codes, new Comparator<State> () {
047            @Override
048            public int compare(State o1, State o2) {
049                return o1.getName().compareTo(o2.getName());
050            }
051        });
052
053        List<KeyValue> newLabels = new ArrayList<KeyValue>();
054        newLabels.add(new ConcreteKeyValue("", ""));
055        for (State state : codes) {
056            if(state.isActive()) {
057                newLabels.add(new ConcreteKeyValue(state.getCode(), state.getName()));
058            }
059        }
060        labels = newLabels;
061
062        return labels;
063    }
064
065    public void setCountryCode(String countryCode) {
066        this.countryCode = countryCode;
067    }
068
069
070    @Override
071    public void clearInternalCache() {
072    }
073}