com.seajas.search.utilities.ui.InterfaceQueryUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.utilities.ui.InterfaceQueryUtils.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.utilities.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

/**
 * Interface query utilities.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
public class InterfaceQueryUtils {
    /**
     * Construct a list of search parameters from the given parameter map.
     * 
     * @param parameters
     * @return List<String>
     */
    public static List<String> createSearchParameterListFromParameters(final Map<String, String> parameters) {
        List<String> resultParameters = new ArrayList<String>();

        // Add the remaining parameters

        for (Entry<String, String> parameter : parameters.entrySet())
            if (parameter.getKey().equalsIgnoreCase("dcterms_coverage")
                    || parameter.getKey().equalsIgnoreCase("dcterms_format")
                    || parameter.getKey().equalsIgnoreCase("dcterms_type"))
                resultParameters.add((parameter.getKey().equalsIgnoreCase("dcterms_type") ? "-" : "")
                        + parameter.getKey() + ":(" + parameter.getValue() + ")");
            else
                resultParameters.add(parameter.getKey() + ":\"" + parameter.getValue() + "\"");

        return resultParameters;
    }

    /**
     * Construct a user interface query to be appended after the hash.
     * 
     * @param query
     * @param parameters
     * @return String
     */
    public static String createQueryFromParameters(final String query, final Map<String, String> parameters) {
        List<NameValuePair> resultParameters = new ArrayList<NameValuePair>();

        // Add the query

        resultParameters.add(new BasicNameValuePair("q", query));

        // Add the remaining parameters

        for (Entry<String, String> parameter : parameters.entrySet())
            if (parameter.getKey().equalsIgnoreCase("dcterms_coverage")
                    || parameter.getKey().equalsIgnoreCase("dcterms_format")
                    || parameter.getKey().equalsIgnoreCase("dcterms_type"))
                resultParameters.add(new BasicNameValuePair("fq",
                        (parameter.getKey().equalsIgnoreCase("dcterms_type") ? "-" : "") + parameter.getKey() + ":("
                                + parameter.getValue() + ")"));
            else
                resultParameters.add(
                        new BasicNameValuePair("fq", parameter.getKey() + ":\"" + parameter.getValue() + "\""));

        return URLEncodedUtils.format(resultParameters, "UTF-8").replace("+", "%20");
    }

    /**
     * Create a combined map containing both the parameters and taxonomy IDs.
     * 
     * @param parameters
     * @param taxonomyIdentifiers
     * @return Map<String, String>
     */
    public static Map<String, String> combineParametersAndTaxonomies(final Map<String, String> parameters,
            final List<Integer> taxonomyIdentifiers) {
        Map<String, String> result = new HashMap<String, String>();

        if (taxonomyIdentifiers.size() > 0) {
            String taxonomyIds = "";

            for (Integer taxonomyIdentifier : taxonomyIdentifiers)
                taxonomyIds += (taxonomyIds.equals("") ? "" : " ") + taxonomyIdentifier;

            if (!StringUtils.isBlank(taxonomyIds))
                result.put("dcterms_coverage", taxonomyIds.trim());
            else
                result.put("dcterms_coverage", "1 2"); // Active, Unassigned
        }

        result.putAll(parameters);

        return result;
    }
}