package org.gbif.checklistbank.index.service;

import org.gbif.api.model.checklistbank.search.NameUsageSearchParameter;
import org.gbif.api.model.checklistbank.search.NameUsageSearchRequest;

import java.util.Map;
import java.util.UUID;

import com.google.common.base.Strings;
import com.google.common.collect.BiMap;
import com.google.common.collect.EnumHashBiMap;
import com.google.common.collect.ImmutableMap;

/**
 *
 */
public class SolrMapping {

  public static final BiMap<NameUsageSearchParameter, String> FACET_MAPPING = EnumHashBiMap.create(NameUsageSearchParameter.class);
  static {
    FACET_MAPPING.put(NameUsageSearchParameter.DATASET_KEY, "dataset_key");
    FACET_MAPPING.put(NameUsageSearchParameter.CONSTITUENT_KEY, "constituent_key");
    FACET_MAPPING.put(NameUsageSearchParameter.HIGHERTAXON_KEY, "higher_taxon_key");
    FACET_MAPPING.put(NameUsageSearchParameter.STATUS, "taxonomic_status_key");
    FACET_MAPPING.put(NameUsageSearchParameter.RANK, "rank_key");
    FACET_MAPPING.put(NameUsageSearchParameter.THREAT, "threat_status_key");
    FACET_MAPPING.put(NameUsageSearchParameter.IS_EXTINCT, "extinct");
    FACET_MAPPING.put(NameUsageSearchParameter.NOMENCLATURAL_STATUS, "nomenclatural_status_key");
    FACET_MAPPING.put(NameUsageSearchParameter.NAME_TYPE, "name_type");
    FACET_MAPPING.put(NameUsageSearchParameter.HABITAT, "habitat_key");
    FACET_MAPPING.put(NameUsageSearchParameter.ISSUE, "issues");
  }

  public static final Map<NameUsageSearchRequest.QueryField, String> HIGHLIGHT_FIELDS = ImmutableMap.of(
      NameUsageSearchRequest.QueryField.DESCRIPTION, "description",
      NameUsageSearchRequest.QueryField.VERNACULAR, "vernacular_name"
  );

  /**
   * Converts a solr string value into the proper java instance.
   */
  public static String interpretSolrValue(NameUsageSearchParameter param, String value) {
    if (Strings.isNullOrEmpty(value)) return null;

    if (Enum.class.isAssignableFrom(param.type())) {
      Class<Enum<?>> vocab = (Class<Enum<?>>) param.type();
      return vocab.getEnumConstants()[Integer.valueOf(value)].name();

    } else if (UUID.class.isAssignableFrom(param.type())) {
      return UUID.fromString(value).toString();

    } else if (Double.class.isAssignableFrom(param.type())) {
      return String.valueOf(Double.parseDouble(value));

    } else if (Integer.class.isAssignableFrom(param.type())) {
      return String.valueOf(Integer.parseInt(value));

    } else if (Boolean.class.isAssignableFrom(param.type())) {
      return String.valueOf(Boolean.parseBoolean(value));

    }
    return value;
  }

}

@@ -40,16 +40,190 @@
 rvice;%0A%0A
+import com.google.common.base.Strings;%0Aimport com.google.common.collect.BiMap;%0Aimport com.google.common.collect.EnumHashBiMap;%0Aimport com.google.common.collect.ImmutableMap;%0A
 import o
@@ -409,183 +409,8 @@
 D;%0A%0A
-import com.google.common.base.Strings;%0Aimport com.google.common.collect.BiMap;%0Aimport com.google.common.collect.EnumHashBiMap;%0Aimport com.google.common.collect.ImmutableMap;%0A%0A
 /**%0A
@@ -824,32 +824,102 @@
 er_taxon_key%22);%0A
+    FACET_MAPPING.put(NameUsageSearchParameter.ORIGIN, %22origin_key%22);%0A
     FACET_MAPPIN

Add origin filter mapping