<commit_before>/*
 * Copyright 2007 The Kuali Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.rice.kns.bo.options;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.kuali.rice.kns.bo.Country;
import org.kuali.rice.kns.service.KNSServiceLocator;
import org.kuali.rice.kns.service.CountryService;
import org.kuali.rice.kns.lookup.keyvalues.KeyValuesBase;
import org.kuali.rice.kns.web.ui.KeyLabelPair;

/**
 * This class returns list of country value pairs.
 */
public class CountryNotRestrictedValuesFinder extends KeyValuesBase {

    /*
     * @see org.kuali.keyvalues.KeyValuesFinder#getKeyValues()
     */
    public List getKeyValues() {
        List<Country> boList = KNSServiceLocator.getCountryService().findAllCountriesNotRestricted();
        List<KeyLabelPair> keyValues = new ArrayList<KeyLabelPair>();

        Country defaultCountry = null;
        for (Country element : boList) {
            String defaultCountryCode = KNSServiceLocator.getCountryService().getDefaultCountry().getPostalCountryCode();
            
            // Find default country code and pull it out so we can set it first in the results list later.
            if (StringUtils.equals(defaultCountryCode, element.getPostalCountryCode())) {
                defaultCountry = element;
            }
            else {
                if(element.isActive()) {
                    keyValues.add(new KeyLabelPair(element.getPostalCountryCode(), element.getPostalCountryName()));
                }
            }
        }

        List<KeyLabelPair> keyValueUSFirst = new ArrayList<KeyLabelPair>();
        keyValueUSFirst.add(new KeyLabelPair("", ""));
        keyValueUSFirst.add(new KeyLabelPair(defaultCountry.getPostalCountryCode(), defaultCountry.getPostalCountryName()));
        keyValueUSFirst.addAll(keyValues);

        return keyValueUSFirst;
    }

}<commit_msg>Prevented country value finder from re-retrieving the default country code from the database on EVERY iteration of the country list when building the KeyValuePair objects.<commit_after>/*
 * Copyright 2007 The Kuali Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.rice.kns.bo.options;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.kuali.rice.kns.bo.Country;
import org.kuali.rice.kns.lookup.keyvalues.KeyValuesBase;
import org.kuali.rice.kns.service.CountryService;
import org.kuali.rice.kns.service.KNSServiceLocator;
import org.kuali.rice.kns.web.ui.KeyLabelPair;

/**
 * This class returns list of country value pairs.
 */
public class CountryNotRestrictedValuesFinder extends KeyValuesBase {

    private static String defaultCountryCode;
    private static CountryService countryService;
    
    protected String getDefaultCountryCode() {
        if ( defaultCountryCode == null ) {
            defaultCountryCode = getCountryService().getDefaultCountry().getPostalCountryCode(); 
        }
        return defaultCountryCode;
    }
    
    protected CountryService getCountryService() {
        if ( countryService == null ) {
            countryService = KNSServiceLocator.getCountryService(); 
        }
        return countryService;
    }
	
    /*
     * @see org.kuali.keyvalues.KeyValuesFinder#getKeyValues()
     */
    public List getKeyValues() {
        List<Country> boList = getCountryService().findAllCountriesNotRestricted();
        List<KeyLabelPair> keyValues = new ArrayList<KeyLabelPair>();

        Country defaultCountry = null;
        for (Country element : boList) {
            String defaultCountryCode = getDefaultCountryCode();
            
            // Find default country code and pull it out so we can set it first in the results list later.
            if (StringUtils.equals(defaultCountryCode, element.getPostalCountryCode())) {
                defaultCountry = element;
            }
            else {
                if(element.isActive()) {
                    keyValues.add(new KeyLabelPair(element.getPostalCountryCode(), element.getPostalCountryName()));
                }
            }
        }

        List<KeyLabelPair> keyValueUSFirst = new ArrayList<KeyLabelPair>();
        keyValueUSFirst.add(new KeyLabelPair("", ""));
        keyValueUSFirst.add(new KeyLabelPair(defaultCountry.getPostalCountryCode(), defaultCountry.getPostalCountryName()));
        keyValueUSFirst.addAll(keyValues);

        return keyValueUSFirst;
    }

}
{'fraction_non_alphanumeric': 0.08796130419204586, 'fraction_numerical': 0.0025080616266571123, 'mean_word_length': 3.154017857142857, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6747840', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1462, 'n_words': 410}