<commit_before>package uk.ac.ebi.atlas.trader;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.UncheckedExecutionException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import uk.ac.ebi.atlas.experimentimport.ExperimentDAO;
import uk.ac.ebi.atlas.experimentimport.ExperimentDTO;
import uk.ac.ebi.atlas.model.Experiment;
import uk.ac.ebi.atlas.model.ExperimentType;
import uk.ac.ebi.atlas.trader.cache.*;
import uk.ac.ebi.atlas.web.controllers.ResourceNotFoundException;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.Set;
import java.util.concurrent.ExecutionException;

@Named
public class ExperimentTrader {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ExperimentTrader.class);
    private RnaSeqBaselineExperimentsCache rnaSeqBaselineExperimentsCache;
    private RnaSeqDiffExperimentsCache rnaSeqDiffExperimentsCache;
    private MicroarrayExperimentsCache microarrayExperimentsCache;
    private ExperimentDAO experimentDAO;
    private ProteomicsBaselineExperimentsCache proteomicsBaselineExperimentsCache;
    private PublicExperimentTypesCache publicExperimentTypesCache;


    @Inject
    public ExperimentTrader(ExperimentDAO experimentDAO,
                            RnaSeqBaselineExperimentsCache rnaSeqBaselineExperimentsCache,
                            RnaSeqDiffExperimentsCache rnaSeqDiffExperimentsCache,
                            MicroarrayExperimentsCache microarrayExperimentsCache,
                            ProteomicsBaselineExperimentsCache proteomicsBaselineExperimentsCache,
                            PublicExperimentTypesCache publicExperimentTypesCache) {

        this.experimentDAO = experimentDAO;
        this.rnaSeqBaselineExperimentsCache = rnaSeqBaselineExperimentsCache;
        this.rnaSeqDiffExperimentsCache = rnaSeqDiffExperimentsCache;
        this.microarrayExperimentsCache = microarrayExperimentsCache;
        this.proteomicsBaselineExperimentsCache = proteomicsBaselineExperimentsCache;
        this.publicExperimentTypesCache = publicExperimentTypesCache;
    }


    public Experiment getPublicExperiment(String experimentAccession) {
        ExperimentType experimentType;
        try {
            experimentType = publicExperimentTypesCache.getExperimentType(experimentAccession);
        } catch (UncheckedExecutionException | NullPointerException e) {
            LOGGER.error(e.getMessage());
            throw new ResourceNotFoundException("Experiment: " + experimentAccession + " not found");
        }

        return getExperimentFromCache(experimentAccession, experimentType);
    }


    public Experiment getExperiment(String experimentAccession, String accessKey) {
        if (StringUtils.isBlank(accessKey)){
            return getPublicExperiment(experimentAccession);
        }
        ExperimentDTO experimentDTO = experimentDAO.findExperiment(experimentAccession, accessKey);

        return getExperimentFromCache(experimentAccession, experimentDTO.getExperimentType());
    }


    public void removeExperimentFromCache(String experimentAccession, ExperimentType type) {

        switch (type) {
            case RNASEQ_MRNA_BASELINE:
                rnaSeqBaselineExperimentsCache.evictExperiment(experimentAccession);
                break;
            case RNASEQ_MRNA_DIFFERENTIAL:
                rnaSeqDiffExperimentsCache.evictExperiment(experimentAccession);
                break;
            case MICROARRAY_1COLOUR_MRNA_DIFFERENTIAL:
            case MICROARRAY_2COLOUR_MRNA_DIFFERENTIAL:
            case MICROARRAY_1COLOUR_MICRORNA_DIFFERENTIAL:
                microarrayExperimentsCache.evictExperiment(experimentAccession);
                break;
            case PROTEOMICS_BASELINE:
                proteomicsBaselineExperimentsCache.evictExperiment(experimentAccession);
                break;
            default:
                throw new IllegalStateException("invalid enum value: " + type);
        }
        publicExperimentTypesCache.evictExperiment(experimentAccession);

    }


    public void removeAllExperimentsFromCache() {
        rnaSeqBaselineExperimentsCache.evictAll();
        rnaSeqDiffExperimentsCache.evictAll();
        microarrayExperimentsCache.evictAll();
        proteomicsBaselineExperimentsCache.evictAll();
        publicExperimentTypesCache.evictAll();
    }


    public Experiment getExperimentFromCache(String experimentAccession, ExperimentType experimentType) {

        try {
            switch (experimentType) {
                case RNASEQ_MRNA_BASELINE:
                    return rnaSeqBaselineExperimentsCache.getExperiment(experimentAccession);
                case RNASEQ_MRNA_DIFFERENTIAL:
                    return rnaSeqDiffExperimentsCache.getExperiment(experimentAccession);
                case MICROARRAY_1COLOUR_MRNA_DIFFERENTIAL:
                case MICROARRAY_2COLOUR_MRNA_DIFFERENTIAL:
                case MICROARRAY_1COLOUR_MICRORNA_DIFFERENTIAL:
                    return microarrayExperimentsCache.getExperiment(experimentAccession);
                case PROTEOMICS_BASELINE:
                    return proteomicsBaselineExperimentsCache.getExperiment(experimentAccession);
                default:
                    throw new IllegalStateException("Invalid enum value: " + experimentType);
            }
        } catch (ExecutionException e) {
            throw new IllegalStateException("Failed to load experiment from cache: " + experimentAccession, e);
        }
    }

    public Set<String> getBaselineExperimentAccessions() {
        return getPublicExperimentAccessions(ExperimentType.RNASEQ_MRNA_BASELINE);
    }


    public Set<String> getProteomicsBaselineExperimentAccessions() {
        return getPublicExperimentAccessions(ExperimentType.PROTEOMICS_BASELINE);
    }


    public Set<String> getAllBaselineExperimentAccessions() {
        return ImmutableSet.<String>builder().
                addAll(getBaselineExperimentAccessions()).
                addAll(getProteomicsBaselineExperimentAccessions()).
                build();

    }


    public Set<String> getRnaSeqDifferentialExperimentAccessions() {
        return getPublicExperimentAccessions(ExperimentType.RNASEQ_MRNA_DIFFERENTIAL);
    }<commit_msg>Catch the unchecked exceptions from the cache as well, since it's easier to debug if we log which experiment has failed<commit_after>package uk.ac.ebi.atlas.trader;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.common.util.concurrent.UncheckedExecutionException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import uk.ac.ebi.atlas.experimentimport.ExperimentDAO;
import uk.ac.ebi.atlas.experimentimport.ExperimentDTO;
import uk.ac.ebi.atlas.model.Experiment;
import uk.ac.ebi.atlas.model.ExperimentType;
import uk.ac.ebi.atlas.trader.cache.*;
import uk.ac.ebi.atlas.web.controllers.ResourceNotFoundException;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.Set;
import java.util.concurrent.ExecutionException;

@Named
public class ExperimentTrader {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ExperimentTrader.class);
    private RnaSeqBaselineExperimentsCache rnaSeqBaselineExperimentsCache;
    private RnaSeqDiffExperimentsCache rnaSeqDiffExperimentsCache;
    private MicroarrayExperimentsCache microarrayExperimentsCache;
    private ExperimentDAO experimentDAO;
    private ProteomicsBaselineExperimentsCache proteomicsBaselineExperimentsCache;
    private PublicExperimentTypesCache publicExperimentTypesCache;


    @Inject
    public ExperimentTrader(ExperimentDAO experimentDAO,
                            RnaSeqBaselineExperimentsCache rnaSeqBaselineExperimentsCache,
                            RnaSeqDiffExperimentsCache rnaSeqDiffExperimentsCache,
                            MicroarrayExperimentsCache microarrayExperimentsCache,
                            ProteomicsBaselineExperimentsCache proteomicsBaselineExperimentsCache,
                            PublicExperimentTypesCache publicExperimentTypesCache) {

        this.experimentDAO = experimentDAO;
        this.rnaSeqBaselineExperimentsCache = rnaSeqBaselineExperimentsCache;
        this.rnaSeqDiffExperimentsCache = rnaSeqDiffExperimentsCache;
        this.microarrayExperimentsCache = microarrayExperimentsCache;
        this.proteomicsBaselineExperimentsCache = proteomicsBaselineExperimentsCache;
        this.publicExperimentTypesCache = publicExperimentTypesCache;
    }


    public Experiment getPublicExperiment(String experimentAccession) {
        ExperimentType experimentType;
        try {
            experimentType = publicExperimentTypesCache.getExperimentType(experimentAccession);
        } catch (UncheckedExecutionException | NullPointerException e) {
            LOGGER.error(e.getMessage());
            throw new ResourceNotFoundException("Experiment: " + experimentAccession + " not found");
        }

        return getExperimentFromCache(experimentAccession, experimentType);
    }


    public Experiment getExperiment(String experimentAccession, String accessKey) {
        if (StringUtils.isBlank(accessKey)){
            return getPublicExperiment(experimentAccession);
        }
        ExperimentDTO experimentDTO = experimentDAO.findExperiment(experimentAccession, accessKey);

        return getExperimentFromCache(experimentAccession, experimentDTO.getExperimentType());
    }


    public void removeExperimentFromCache(String experimentAccession, ExperimentType type) {

        switch (type) {
            case RNASEQ_MRNA_BASELINE:
                rnaSeqBaselineExperimentsCache.evictExperiment(experimentAccession);
                break;
            case RNASEQ_MRNA_DIFFERENTIAL:
                rnaSeqDiffExperimentsCache.evictExperiment(experimentAccession);
                break;
            case MICROARRAY_1COLOUR_MRNA_DIFFERENTIAL:
            case MICROARRAY_2COLOUR_MRNA_DIFFERENTIAL:
            case MICROARRAY_1COLOUR_MICRORNA_DIFFERENTIAL:
                microarrayExperimentsCache.evictExperiment(experimentAccession);
                break;
            case PROTEOMICS_BASELINE:
                proteomicsBaselineExperimentsCache.evictExperiment(experimentAccession);
                break;
            default:
                throw new IllegalStateException("invalid enum value: " + type);
        }
        publicExperimentTypesCache.evictExperiment(experimentAccession);

    }


    public void removeAllExperimentsFromCache() {
        rnaSeqBaselineExperimentsCache.evictAll();
        rnaSeqDiffExperimentsCache.evictAll();
        microarrayExperimentsCache.evictAll();
        proteomicsBaselineExperimentsCache.evictAll();
        publicExperimentTypesCache.evictAll();
    }


    public Experiment getExperimentFromCache(String experimentAccession, ExperimentType experimentType) {

        try {
            switch (experimentType) {
                case RNASEQ_MRNA_BASELINE:
                    return rnaSeqBaselineExperimentsCache.getExperiment(experimentAccession);
                case RNASEQ_MRNA_DIFFERENTIAL:
                    return rnaSeqDiffExperimentsCache.getExperiment(experimentAccession);
                case MICROARRAY_1COLOUR_MRNA_DIFFERENTIAL:
                case MICROARRAY_2COLOUR_MRNA_DIFFERENTIAL:
                case MICROARRAY_1COLOUR_MICRORNA_DIFFERENTIAL:
                    return microarrayExperimentsCache.getExperiment(experimentAccession);
                case PROTEOMICS_BASELINE:
                    return proteomicsBaselineExperimentsCache.getExperiment(experimentAccession);
                default:
                    throw new IllegalStateException("Invalid enum value: " + experimentType);
            }
        } catch (ExecutionException | UncheckedExecutionException e) {
            throw new IllegalStateException("Failed to load experiment from cache: " + experimentAccession, e);
        }
    }

    public Set<String> getBaselineExperimentAccessions() {
        return getPublicExperimentAccessions(ExperimentType.RNASEQ_MRNA_BASELINE);
    }


    public Set<String> getProteomicsBaselineExperimentAccessions() {
        return getPublicExperimentAccessions(ExperimentType.PROTEOMICS_BASELINE);
    }


    public Set<String> getAllBaselineExperimentAccessions() {
        return ImmutableSet.<String>builder().
                addAll(getBaselineExperimentAccessions()).
                addAll(getProteomicsBaselineExperimentAccessions()).
                build();

    }


    public Set<String> getRnaSeqDifferentialExperimentAccessions() {
        return getPublicExperimentAccessions(ExperimentType.RNASEQ_MRNA_DIFFERENTIAL);
    }
{'fraction_non_alphanumeric': 0.05828793774319066, 'fraction_numerical': 0.001245136186770428, 'mean_word_length': 3.2793872793872794, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3401608', 'n_tokens_mistral': 3441, 'n_tokens_neox': 3220, 'n_words': 505}