<commit_before>package fi.nls.oskari.control.data;

import fi.nls.oskari.annotation.OskariActionRoute;
import fi.nls.oskari.control.ActionException;
import fi.nls.oskari.control.ActionHandler;
import fi.nls.oskari.control.ActionParameters;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.util.JSONHelper;
import fi.nls.oskari.util.ResponseHelper;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Constructs a JSON object describing the metadata search form selectable values.
 */
@OskariActionRoute("GetMetadataSearchOptions")
public class GetMetadataSearchOptionsHandler extends ActionHandler {

    private static final Logger log = LogFactory.getLogger(GetMetadataSearchOptionsHandler.class);

    public enum FIELD {

        TYPE("type", true),
        SERVICE_TYPE("serviceType", true),
        SERVICE_NAME("serviceName", false),
        ORGANIZATION("organization", false),
        COVERAGE("coverage", false),
        INSPIRE_THEME("inspireTheme", false),
        KEYWORD("keyword", false),
        TOPIC("topic", false);

        private String name = null;
        private boolean multi = false;

        private FIELD(String name, boolean isMulti) {
            this.name = name;
            this.multi = isMulti;
        }
        public boolean isMulti() {
            return multi;
        }
        public String getName() {
            return name;
        }
    }

    @Override
    public void handleAction(ActionParameters params) throws ActionException {

        // root object
        final JSONObject result = new JSONObject();

        // fields for form
        final JSONArray fields = new JSONArray();
        for(FIELD field : FIELD.values()) {
            final JSONObject node = JSONHelper.createJSONObject("field", field.getName());
            JSONHelper.putValue(node, "multi", field.isMulti());
            JSONHelper.putValue(node, "values", getValues(field));
            fields.put(node);
        }

        // write response
        JSONHelper.putValue(result, "fields", fields);
        ResponseHelper.writeResponse(params, result);
    }

    private JSONArray getValues(final FIELD field) {
        JSONArray values = new JSONArray();
        // TODO: generates dummy content for now
        int count = 20;
        if(field.isMulti()) {
            count = 5;
        }
        for(int i = 0; i < count; ++i) {
            final JSONObject value = JSONHelper.createJSONObject("val", "value " + (i + 1));
            values.put(value);
        }
        return values;
    }
}<commit_msg>Added locale key for field values<commit_after>package fi.nls.oskari.control.data;

import fi.nls.oskari.annotation.OskariActionRoute;
import fi.nls.oskari.control.ActionException;
import fi.nls.oskari.control.ActionHandler;
import fi.nls.oskari.control.ActionParameters;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.util.JSONHelper;
import fi.nls.oskari.util.ResponseHelper;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Constructs a JSON object describing the metadata search form selectable values.
 */
@OskariActionRoute("GetMetadataSearchOptions")
public class GetMetadataSearchOptionsHandler extends ActionHandler {

    private static final Logger log = LogFactory.getLogger(GetMetadataSearchOptionsHandler.class);

    public enum FIELD {

        TYPE("type", true),
        SERVICE_TYPE("serviceType", true),
        SERVICE_NAME("serviceName", false),
        ORGANIZATION("organization", false),
        COVERAGE("coverage", false),
        INSPIRE_THEME("inspireTheme", false),
        KEYWORD("keyword", false),
        TOPIC("topic", false);

        private String name = null;
        private boolean multi = false;

        private FIELD(String name, boolean isMulti) {
            this.name = name;
            this.multi = isMulti;
        }
        public boolean isMulti() {
            return multi;
        }
        public String getName() {
            return name;
        }
    }

    @Override
    public void handleAction(ActionParameters params) throws ActionException {

        // root object
        final JSONObject result = new JSONObject();

        // fields for form
        final JSONArray fields = new JSONArray();
        for(FIELD field : FIELD.values()) {
            final JSONObject node = JSONHelper.createJSONObject("field", field.getName());
            JSONHelper.putValue(node, "multi", field.isMulti());
            JSONHelper.putValue(node, "values", getValues(field));
            fields.put(node);
        }

        // write response
        JSONHelper.putValue(result, "fields", fields);
        ResponseHelper.writeResponse(params, result);
    }

    private JSONArray getValues(final FIELD field) {
        JSONArray values = new JSONArray();
        // TODO: generates dummy content for now
        int count = 20;
        if(field.isMulti()) {
            count = 5;
        }
        for(int i = 0; i < count; ++i) {
            final String dummyValue = "value " + (i + 1);
            final JSONObject value = JSONHelper.createJSONObject("val", dummyValue);
            JSONHelper.putValue(value, "locale", dummyValue);
            values.put(value);
        }
        return values;
    }
}
{'fraction_non_alphanumeric': 0.10591782887297399, 'fraction_numerical': 0.0018846588767433095, 'mean_word_length': 2.564137004701142, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '263903', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1446, 'n_words': 356}