class FacetedSearchService {

    public String getFacetKeys()
    {
        List<String> facetKeys = new ArrayList<>();

        String jsonString = searchConfig.getTimePeriod();
        JSONArray timePeriodList = new JSONArray(jsonString);
        JSONObject timePeriodJSONObject;

        try
        {
            for (Map.Entry<String, String> e : searchConfig.getFacets().entrySet())
            {
                String facetKey = e.getKey();
                {
                    if (facetKey.contains(SearchConstants.DATE_FACET_PRE_KEY))
                    {
                        facetKey = facetKey.replaceFirst(SearchConstants.DATE_FACET_PRE_KEY, "");
                        for (int i = 0; i < timePeriodList.length(); i++)
                        {
                            timePeriodJSONObject = timePeriodList.getJSONObject(i);

                            String timePeriod = URLEncoder.encode(
                                    "{" + SearchConstants.SOLR_FACET_NAME_CHANGE_COMMAND + "'" + e.getValue() + ", "
                                            + timePeriodJSONObject.getString(SearchConstants.TIME_PERIOD_DESCRIPTION) + "'}",
                                    SearchConstants.FACETED_SEARCH_ENCODING);
                            String timePeriodValue = URLEncoder.encode(timePeriodJSONObject.getString(SearchConstants.TIME_PERIOD_VALUE),
                                    SearchConstants.FACETED_SEARCH_ENCODING);
                            facetKeys.add(
                                    SearchConstants.FACET_QUERY + timePeriod + facetKey + SearchConstants.DOTS_SPLITTER + timePeriodValue);

                        }
                    }
                    else
                    {
                        String encoded = URLEncoder.encode("{" + SearchConstants.SOLR_FACET_NAME_CHANGE_COMMAND + "'" + e.getValue() + "'}",
                                SearchConstants.FACETED_SEARCH_ENCODING);
                        facetKeys.add(SearchConstants.FACET_FILED + encoded + facetKey);

                    }
                }
            }
        }
        catch (UnsupportedEncodingException e1)
        {

            log.error("Encoding problem while building the facet key list: " + e1.getMessage(), e1);
        }

        return String.join(SearchConstants.AND_SPLITTER, facetKeys);
    }

}