<commit_before>package org.gbif.portal.action.species;

import org.gbif.api.model.checklistbank.NameUsage;
import org.gbif.api.model.common.paging.PagingResponse;
import org.gbif.api.model.registry.Dataset;
import org.gbif.api.service.occurrence.OccurrenceDatasetIndexService;
import org.gbif.api.vocabulary.DatasetType;

import java.util.SortedMap;
import java.util.UUID;

import com.google.common.collect.Lists;
import com.google.inject.Inject;

public class DatasetAction extends UsageBaseAction {

  public class DatasetResult implements Comparable<DatasetResult> {

    private final Dataset dataset;
    private final Integer numOccurrences;
    private final NameUsage usage;

    public DatasetResult(Dataset dataset, Integer numOccurrences, NameUsage usage) {
      this.dataset = dataset;
      this.numOccurrences = numOccurrences;
      this.usage = usage;
    }

    @Override
    public int compareTo(DatasetResult that) {
      if (this == that) {
        return 0;
      }
      return dataset.getTitle().toLowerCase().compareTo(that.getDataset().getTitle().toLowerCase());
    }

    public Dataset getDataset() {
      return dataset;
    }

    public Integer getNumOccurrences() {
      return numOccurrences;
    }

    public NameUsage getUsage() {
      return usage;
    }
  }

  @Inject
  private OccurrenceDatasetIndexService occurrenceDatasetService;
  private DatasetType type;
  private PagingResponse<DatasetResult> page;
  private int offset = 0;

  private static final int pageSize = 25;

  @Override
  public String execute() {
    loadUsage();

    page = new PagingResponse<DatasetResult>(offset, pageSize);
    long count = 0;
    if (type == null || type == DatasetType.CHECKLIST) {
      PagingResponse<NameUsage> resp = usageService.listRelated(usage.getNubKey(), getLocale(), page);
      count = resp.getCount();
      for (NameUsage u : resp.getResults()) {
        // remove nub usage itself
        if (!u.getKey().equals(usage.getKey())) {
          page.getResults().add(new DatasetResult(datasetService.get(u.getDatasetKey()), null, u));
        } else {
          count--;
        }
      }
    }

    if ((type == null || type == DatasetType.OCCURRENCE) && usage.getNubKey() != null) {
      SortedMap<UUID, Integer> occurrenceDatasetCounts = occurrenceDatasetService.occurrenceDatasetsForNubKey(usage.getNubKey());
      count += occurrenceDatasetCounts.size();
      for (UUID uuid : sublist(Lists.newArrayList(occurrenceDatasetCounts.keySet()), offset, offset + pageSize)) {
        page.getResults().add(new DatasetResult(datasetService.get(uuid), occurrenceDatasetCounts.get(uuid), null));
      }
    }

    page.setCount(count);
    return SUCCESS;
  }

  public int getOffset() {
    return offset;
  }

  public PagingResponse<DatasetResult> getPage() {
    return page;
  }

  public DatasetType getType() {<commit_msg>Fix NPE as in http://dev.gbif.org/issues/browse/PF-2613<commit_after>package org.gbif.portal.action.species;

import org.gbif.api.model.checklistbank.NameUsage;
import org.gbif.api.model.common.paging.PagingResponse;
import org.gbif.api.model.registry.Dataset;
import org.gbif.api.service.occurrence.OccurrenceDatasetIndexService;
import org.gbif.api.vocabulary.DatasetType;

import java.util.SortedMap;
import java.util.UUID;

import com.google.common.collect.Lists;
import com.google.inject.Inject;

public class DatasetAction extends UsageBaseAction {

  public class DatasetResult implements Comparable<DatasetResult> {

    private final Dataset dataset;
    private final Integer numOccurrences;
    private final NameUsage usage;

    public DatasetResult(Dataset dataset, Integer numOccurrences, NameUsage usage) {
      this.dataset = dataset;
      this.numOccurrences = numOccurrences;
      this.usage = usage;
    }

    @Override
    public int compareTo(DatasetResult that) {
      if (this == that) {
        return 0;
      }
      return dataset.getTitle().toLowerCase().compareTo(that.getDataset().getTitle().toLowerCase());
    }

    public Dataset getDataset() {
      return dataset;
    }

    public Integer getNumOccurrences() {
      return numOccurrences;
    }

    public NameUsage getUsage() {
      return usage;
    }
  }

  @Inject
  private OccurrenceDatasetIndexService occurrenceDatasetService;
  private DatasetType type;
  private PagingResponse<DatasetResult> page;
  private int offset = 0;

  private static final int pageSize = 25;

  @Override
  public String execute() {
    loadUsage();

    page = new PagingResponse<DatasetResult>(offset, pageSize);
    long count = 0;
    if ((type == null || type == DatasetType.CHECKLIST) && usage.getNubKey() != null) {
      PagingResponse<NameUsage> resp = usageService.listRelated(usage.getNubKey(), getLocale(), page);
      count = resp.getCount() == null ? resp.getResults().size() : resp.getCount();
      for (NameUsage u : resp.getResults()) {
        // remove nub usage itself
        if (!u.getKey().equals(usage.getKey())) {
          page.getResults().add(new DatasetResult(datasetService.get(u.getDatasetKey()), null, u));
        } else {
          count--;
        }
      }
    }

    if ((type == null || type == DatasetType.OCCURRENCE) && usage.getNubKey() != null) {
      SortedMap<UUID, Integer> occurrenceDatasetCounts = occurrenceDatasetService.occurrenceDatasetsForNubKey(usage.getNubKey());
      count += occurrenceDatasetCounts.size();
      for (UUID uuid : sublist(Lists.newArrayList(occurrenceDatasetCounts.keySet()), offset, offset + pageSize)) {
        page.getResults().add(new DatasetResult(datasetService.get(uuid), occurrenceDatasetCounts.get(uuid), null));
      }
    }

    page.setCount(count);
    return SUCCESS;
  }

  public int getOffset() {
    return offset;
  }

  public PagingResponse<DatasetResult> getPage() {
    return page;
  }

  public DatasetType getType() {
{'fraction_non_alphanumeric': 0.11233854520734195, 'fraction_numerical': 0.002379333786539769, 'mean_word_length': 3.5165003837298543, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '980711', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1771, 'n_words': 373}