001    /**
002     * Copyright 2005-2011 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     */
016    package org.kuali.rice.location.framework.country;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.core.api.util.ConcreteKeyValue;
020    import org.kuali.rice.core.api.util.KeyValue;
021    import org.kuali.rice.krad.keyvalues.KeyValuesBase;
022    import org.kuali.rice.location.api.country.Country;
023    
024    import java.util.ArrayList;
025    import java.util.Collections;
026    import java.util.Comparator;
027    import java.util.List;
028    
029    /**
030     * This is a description of what this class does - wliang don't forget to fill this in.
031     *
032     * @author Kuali Rice Team (rice.collab@kuali.org)
033     */
034    public abstract class AbstractCountryValuesFinderBase extends KeyValuesBase {
035    
036        /**
037         * Returns a list of countries that will be added to the result of {@link #getKeyValues()}.  Note that the result may
038         * be filtered by active status
039         *
040         * @return
041         */
042        protected abstract List<Country> retrieveCountriesForValuesFinder();
043    
044        @Override
045        public List<KeyValue> getKeyValues() {
046            List<Country> boList = new ArrayList<Country>(retrieveCountriesForValuesFinder());
047            List<KeyValue> labels = new ArrayList<KeyValue>(boList.size() + 1);
048    
049            labels.add(new ConcreteKeyValue("", ""));
050    
051            Collections.sort(boList, new Comparator<Country>() {
052                @Override
053                public int compare(Country o1, Country o2) {
054                    // some institutions may prefix the country name with an asterisk if the country no longer exists
055                    // the country names will be compared without the asterisk
056                    String sortValue1 = StringUtils.trim(StringUtils.removeStart(o1.getName(), "*"));
057                    String sortValue2 = StringUtils.trim(StringUtils.removeStart(o2.getName(), "*"));
058                    return sortValue1.compareToIgnoreCase(sortValue2);
059                }
060    
061            });
062    
063            for (Country country : boList) {
064                if (country.isActive()) {
065                    labels.add(new ConcreteKeyValue(country.getCode(), country.getName()));
066                }
067            }
068            return labels;
069        }
070    }