<commit_before>/*
 * Copyright 2009 Kantega AS
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * 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 no.kantega.publishing.common.ao;

import no.kantega.publishing.common.data.ListOption;
import no.kantega.publishing.common.util.database.dbConnectionFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Locale;

public class EditableListAO {

    private static Locale defaultLocale = new Locale("no", "NO");

    /**
     * Fetches all list options from for a list with a given key.
     *
     * @param attributeKey Attribute's name or value from parameter "key"
     * @param locale Norwegian Bokmål is assumed is assumed if null
     * @param ignoreVariant Locale variant is ignored if true.
     * @return List of ListOption
     */
    public static List<ListOption> getOptions(String attributeKey, Locale locale, boolean ignoreVariant) {
        if(attributeKey == null) {
            return null;
        }
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dbConnectionFactory.getDataSource());
        String language = (ignoreVariant)? getLocaleAsString(locale, true) + '%' : getLocaleAsString(locale, false);
        return jdbcTemplate.query("SELECT * FROM attribute_editablelist WHERE AttributeKey = ? AND Language LIKE ? ORDER BY Value", new Object[]{attributeKey.toLowerCase(), language}, new RowMapper<ListOption>(){
            public ListOption mapRow(ResultSet rs, int i) throws SQLException {
                ListOption option = new ListOption();
                option.setText(rs.getString("Value"));
                option.setValue(rs.getString("Value"));
                option.setDefaultSelected(rs.getInt("DefaultSelected") == 1);
                return option;
            }
        });<commit_msg>AP-1987 resolved bug: setDefault in keyed editable list is not set as default<commit_after>/*
 * Copyright 2009 Kantega AS
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * 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 no.kantega.publishing.common.ao;

import no.kantega.publishing.common.data.ListOption;
import no.kantega.publishing.common.util.database.dbConnectionFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Locale;

public class EditableListAO {

    private static Locale defaultLocale = new Locale("no", "NO");

    /**
     * Fetches all list options from for a list with a given key.
     *
     * @param attributeKey Attribute's name or value from parameter "key"
     * @param locale Norwegian Bokmål is assumed is assumed if null
     * @param ignoreVariant Locale variant is ignored if true.
     * @return List of ListOption
     */
    public static List<ListOption> getOptions(String attributeKey, Locale locale, boolean ignoreVariant) {
        if(attributeKey == null) {
            return null;
        }
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dbConnectionFactory.getDataSource());
        String language = (ignoreVariant)? getLocaleAsString(locale, true) + '%' : getLocaleAsString(locale, false);

        List<ListOption> options = jdbcTemplate.query("SELECT * FROM attribute_editablelist WHERE AttributeKey = ? AND Language LIKE ? ORDER BY DefaultSelected DESC, Value", new Object[]{attributeKey.toLowerCase(), language}, new RowMapper<ListOption>(){
            public ListOption mapRow(ResultSet rs, int i) throws SQLException {
                ListOption option = new ListOption();
                option.setText(rs.getString("Value"));
                option.setValue(rs.getString("Value"));
                option.setDefaultSelected(rs.getInt("DefaultSelected") == 1);
                return option;
            }
        });
        return options;
{'fraction_non_alphanumeric': 0.08917326833231769, 'fraction_numerical': 0.004468819825309771, 'mean_word_length': 3.392506690454951, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '4443474', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1252, 'n_words': 443}