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

import org.gbif.api.model.checklistbank.DatasetMetrics;
import org.gbif.api.model.metrics.cube.OccurrenceCube;
import org.gbif.api.model.metrics.cube.ReadBuilder;
import org.gbif.api.model.registry.Dataset;
import org.gbif.api.model.registry.Installation;
import org.gbif.api.model.registry.Organization;
import org.gbif.api.model.registry.eml.TaxonomicCoverage;
import org.gbif.api.model.registry.eml.TaxonomicCoverages;
import org.gbif.api.service.checklistbank.DatasetMetricsService;
import org.gbif.api.service.metrics.CubeService;
import org.gbif.api.service.registry.DatasetService;
import org.gbif.api.service.registry.InstallationService;
import org.gbif.api.service.registry.OrganizationService;
import org.gbif.api.vocabulary.Rank;
import org.gbif.portal.action.dataset.util.DisplayableTaxonomicCoverage;
import org.gbif.portal.action.dataset.util.OrganizedTaxonomicCoverage;
import org.gbif.portal.action.dataset.util.OrganizedTaxonomicCoverages;
import org.gbif.portal.action.member.MemberBaseAction;
import org.gbif.portal.action.member.MemberType;
import org.gbif.portal.exception.NotFoundException;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.UUID;

import javax.annotation.Nullable;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.CharMatcher;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A base action for common dataset pages.
 * TODO: This will be removed as there is no need for such hierarchy.
 */
@SuppressWarnings("serial")
public class DatasetBaseAction extends MemberBaseAction<Dataset> {

  private static final Logger LOG = LoggerFactory.getLogger(DatasetBaseAction.class);
  protected static final String EMPTY_RANK = "RANK_NOT_SPECIFIED";
  protected DatasetMetrics metrics;
  protected Installation installation;
  protected Organization publisher;
  protected Organization host;
  protected Dataset parentDataset;

  private List<OrganizedTaxonomicCoverages> organizedCoverages = Lists.newArrayList();
  @Nullable
  private Long numOccurrences;
  @Nullable
  private Long numGeoreferencedOccurrences;

  @Inject
  protected DatasetMetricsService metricsService;
  @Inject
  protected OrganizationService organizationService;
  @Inject
  protected InstallationService installationService;
  @Inject
  protected CubeService occurrenceCubeService;

  protected DatasetService datasetService;

  public DatasetBaseAction(DatasetService datasetService, CubeService occurrenceCubeService,
    DatasetMetricsService metricsService, OrganizationService organizationService) {
    super(MemberType.DATASET, datasetService, occurrenceCubeService, metricsService, organizationService);
    this.datasetService = datasetService;
  }


  /**
   * @return true only if the string has content when trimmed
   */
  private static boolean hasNonWhitespace(final String s) {
    return !Strings.isNullOrEmpty(s) && !s.trim().isEmpty();
  }

  public Dataset getDataset() {
    return member;
  }

  public DatasetService getDatasetService() {
    return datasetService;
  }

  public Organization getHost() {
    return host;
  }

  @Override
  public UUID getId() {
    return id;
  }

  public Installation getInstallation() {
    return installation;
  }

  public DatasetMetrics getMetrics() {
    return metrics;
  }

  @Nullable
  public Long getNumGeoreferencedOccurrences() {
    return numGeoreferencedOccurrences;
  }

  @Nullable
  public Long getNumOccurrences() {
    return numOccurrences;
  }

  /**
   * @return the Dataset's taxonomic coverages organized by rank
   */
  public List<OrganizedTaxonomicCoverages> getOrganizedCoverages() {
    return organizedCoverages;
  }

  public Dataset getParentDataset() {
    return parentDataset;
  }

  public Organization getPublisher() {
    return publisher;
  }

  /**
   * Exposed to simplify Freemarker.
   */
  public Rank getSpeciesRank() {
    return Rank.SPECIES;
  }

  @Override
  public void setId(String id) {
    try {
      this.id = UUID.fromString(id);
    } catch (IllegalArgumentException e) {
      this.id = null;
    }
  }

  @Override
  protected void loadDetail() {
    if (id == null) {
      throw new NotFoundException("No identifier provided to load the dataset");
    }

    member = datasetService.get(id);
    if (member == null) {
      throw new NotFoundException("No dataset found with id " + id);
    }

    parentDataset = member.getParentDatasetKey() != null ? datasetService.get(member.getParentDatasetKey()) : null;

    publisher = member.getPublishingOrganizationKey() != null ?
      organizationService.get(member.getPublishingOrganizationKey()) : publisher;

    installation = installationService.get(member.getInstallationKey());

    host = organizationService.get(installation.getOrganizationKey());

    organizedCoverages = member.getTaxonomicCoverages() != null ?
      constructOrganizedTaxonomicCoverages(member.getTaxonomicCoverages()) : organizedCoverages;
<commit_msg>Remove old GBIF PORTAL ids from dataset detail views<commit_after>package org.gbif.portal.action.dataset;

import org.gbif.api.model.checklistbank.DatasetMetrics;
import org.gbif.api.model.metrics.cube.OccurrenceCube;
import org.gbif.api.model.metrics.cube.ReadBuilder;
import org.gbif.api.model.registry.Dataset;
import org.gbif.api.model.registry.Identifier;
import org.gbif.api.model.registry.Installation;
import org.gbif.api.model.registry.Organization;
import org.gbif.api.model.registry.eml.TaxonomicCoverage;
import org.gbif.api.model.registry.eml.TaxonomicCoverages;
import org.gbif.api.service.checklistbank.DatasetMetricsService;
import org.gbif.api.service.metrics.CubeService;
import org.gbif.api.service.registry.DatasetService;
import org.gbif.api.service.registry.InstallationService;
import org.gbif.api.service.registry.OrganizationService;
import org.gbif.api.vocabulary.IdentifierType;
import org.gbif.api.vocabulary.Rank;
import org.gbif.portal.action.dataset.util.DisplayableTaxonomicCoverage;
import org.gbif.portal.action.dataset.util.OrganizedTaxonomicCoverage;
import org.gbif.portal.action.dataset.util.OrganizedTaxonomicCoverages;
import org.gbif.portal.action.member.MemberBaseAction;
import org.gbif.portal.action.member.MemberType;
import org.gbif.portal.exception.NotFoundException;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.UUID;
import javax.annotation.Nullable;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.CharMatcher;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A base action for common dataset pages.
 * TODO: This will be removed as there is no need for such hierarchy.
 */
@SuppressWarnings("serial")
public class DatasetBaseAction extends MemberBaseAction<Dataset> {

  private static final Logger LOG = LoggerFactory.getLogger(DatasetBaseAction.class);
  protected static final String EMPTY_RANK = "RANK_NOT_SPECIFIED";
  protected DatasetMetrics metrics;
  protected Installation installation;
  protected Organization publisher;
  protected Organization host;
  protected Dataset parentDataset;

  private List<OrganizedTaxonomicCoverages> organizedCoverages = Lists.newArrayList();
  @Nullable
  private Long numOccurrences;
  @Nullable
  private Long numGeoreferencedOccurrences;

  @Inject
  protected DatasetMetricsService metricsService;
  @Inject
  protected OrganizationService organizationService;
  @Inject
  protected InstallationService installationService;
  @Inject
  protected CubeService occurrenceCubeService;

  protected DatasetService datasetService;

  public DatasetBaseAction(DatasetService datasetService, CubeService occurrenceCubeService,
    DatasetMetricsService metricsService, OrganizationService organizationService) {
    super(MemberType.DATASET, datasetService, occurrenceCubeService, metricsService, organizationService);
    this.datasetService = datasetService;
  }


  /**
   * @return true only if the string has content when trimmed
   */
  private static boolean hasNonWhitespace(final String s) {
    return !Strings.isNullOrEmpty(s) && !s.trim().isEmpty();
  }

  public Dataset getDataset() {
    return member;
  }

  public DatasetService getDatasetService() {
    return datasetService;
  }

  public Organization getHost() {
    return host;
  }

  @Override
  public UUID getId() {
    return id;
  }

  public Installation getInstallation() {
    return installation;
  }

  public DatasetMetrics getMetrics() {
    return metrics;
  }

  @Nullable
  public Long getNumGeoreferencedOccurrences() {
    return numGeoreferencedOccurrences;
  }

  @Nullable
  public Long getNumOccurrences() {
    return numOccurrences;
  }

  /**
   * @return the Dataset's taxonomic coverages organized by rank
   */
  public List<OrganizedTaxonomicCoverages> getOrganizedCoverages() {
    return organizedCoverages;
  }

  public Dataset getParentDataset() {
    return parentDataset;
  }

  public Organization getPublisher() {
    return publisher;
  }

  /**
   * Exposed to simplify Freemarker.
   */
  public Rank getSpeciesRank() {
    return Rank.SPECIES;
  }

  @Override
  public void setId(String id) {
    try {
      this.id = UUID.fromString(id);
    } catch (IllegalArgumentException e) {
      this.id = null;
    }
  }

  @Override
  protected void loadDetail() {
    if (id == null) {
      throw new NotFoundException("No identifier provided to load the dataset");
    }

    member = datasetService.get(id);
    if (member == null) {
      throw new NotFoundException("No dataset found with id " + id);
    }

    // remove GBIF_PORTAL ids
    member.setIdentifiers(Lists.newArrayList(Collections2.filter(member.getIdentifiers(), new Predicate<Identifier>() {
          @Override
          public boolean apply(@Nullable Identifier input) {
              return IdentifierType.GBIF_PORTAL != input.getType();
          }
      }
    )));
      
    parentDataset = member.getParentDatasetKey() != null ? datasetService.get(member.getParentDatasetKey()) : null;

    publisher = member.getPublishingOrganizationKey() != null ?
      organizationService.get(member.getPublishingOrganizationKey()) : publisher;

    installation = installationService.get(member.getInstallationKey());

    host = organizationService.get(installation.getOrganizationKey());

    organizedCoverages = member.getTaxonomicCoverages() != null ?
      constructOrganizedTaxonomicCoverages(member.getTaxonomicCoverages()) : organizedCoverages;

{'fraction_non_alphanumeric': 0.09106482733748084, 'fraction_numerical': 0.0005409791723018664, 'mean_word_length': 4.653414882772681, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '533372', 'n_tokens_mistral': 3277, 'n_tokens_neox': 3190, 'n_words': 681}