/**
 * 
 */
package org.gbif.metrics.ws.client;

import org.gbif.api.service.occurrence.OccurrenceDatasetIndexService;
import org.gbif.api.vocabulary.Country;
import org.gbif.ws.client.BaseWsClient;
import org.gbif.ws.client.QueryParamBuilder;

import java.util.Map;
import java.util.SortedMap;
import java.util.UUID;
import javax.ws.rs.core.MultivaluedMap;

import com.google.common.base.Functions;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Ordering;
import com.google.inject.Inject;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;


/**
 * A web service client to support the accession of occurrence dataset indexes.
 */
public class OccurrenceDatasetIndexWsClient extends BaseWsClient implements OccurrenceDatasetIndexService {

  private static final String DATASETS_PATH = "occurrence/counts/datasets";

  private static final String NUBKEY_PARAM = "nubKey";
  private static final String COUNTRY_PARAM = "country";

  private static final GenericType<Map<UUID, Integer>> GENERIC_TYPE = new GenericType<Map<UUID, Integer>>() {
  };

  @Inject
  public OccurrenceDatasetIndexWsClient(WebResource resource) {
    super(resource);
  }

  @Override
  public SortedMap<UUID, Integer> occurrenceDatasetsForCountry(Country country) {
    return getRequest(QueryParamBuilder.create(COUNTRY_PARAM, country.getIso2LetterCode()).build(),
      DATASETS_PATH);
  }

  @Override
  public SortedMap<UUID, Integer> occurrenceDatasetsForNubKey(int nubKey) {
    return getRequest(QueryParamBuilder.create(NUBKEY_PARAM, nubKey).build(), DATASETS_PATH);
  }

  /**
   * Executes a get request whose returned value is a SortedMap<UUID, Integer>.
   */
  private SortedMap<UUID, Integer> getRequest(MultivaluedMap<String, String> params, String path) {
    final Map<UUID, Integer> res = get(GENERIC_TYPE, params, path);
    return ImmutableSortedMap.copyOf(res,
      Ordering.natural().onResultOf(Functions.forMap(res)).compound(Ordering.natural()).reverse());
  }
}

@@ -1048,23 +1048,20 @@
 p%3CUUID, 
-Integer
+Long
 %3E%3E GENER
@@ -1096,23 +1096,20 @@
 p%3CUUID, 
-Integer
+Long
 %3E%3E() %7B%0A 
@@ -1242,39 +1242,36 @@
 SortedMap%3CUUID, 
-Integer
+Long
 %3E occurrenceData
@@ -1468,23 +1468,20 @@
 p%3CUUID, 
-Integer
+Long
 %3E occurr
@@ -1733,23 +1733,20 @@
 p%3CUUID, 
-Integer
+Long
 %3E getReq
@@ -1824,23 +1824,20 @@
 p%3CUUID, 
-Integer
+Long
 %3E res = 

Update for type change.