<commit_before>package uk.ac.ebi.ae15.components;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import uk.ac.ebi.ae15.app.Application;
import uk.ac.ebi.ae15.app.ApplicationComponent;
import uk.ac.ebi.ae15.utils.persistence.PersistableDocumentContainer;
import uk.ac.ebi.ae15.utils.persistence.PersistableString;
import uk.ac.ebi.ae15.utils.persistence.TextFilePersistence;
import uk.ac.ebi.ae15.utils.search.ExperimentSearch;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Experiments extends ApplicationComponent
{
    // logging machinery
    private final Log log = LogFactory.getLog(getClass());

    private TextFilePersistence<PersistableDocumentContainer> experiments;
    private TextFilePersistence<PersistableString> species;
    private TextFilePersistence<PersistableString> arrays;

    private ExperimentSearch experimentSearch;
    private String dataSource;

    public Experiments( Application app )
    {
        super(app, "Experiments");
    }

    public void initialize()
    {
        experiments = new TextFilePersistence<PersistableDocumentContainer>(
                new PersistableDocumentContainer()
                , new File(System.getProperty("java.io.tmpdir"), getPreferences().getString("ae.experiments.cache.filename"))
        );

        experimentSearch = new ExperimentSearch();

        species = new TextFilePersistence<PersistableString>(
                new PersistableString()
                , new File(System.getProperty("java.io.tmpdir"), getPreferences().getString("ae.species.cache.filename"))

        );

        arrays = new TextFilePersistence<PersistableString>(
                new PersistableString()
                , new File(System.getProperty("java.io.tmpdir"), getPreferences().getString("ae.arrays.cache.filename"))
        );
    }

    public void terminate()
    {
    }

    public Document getExperiments()
    {
        Document doc = experiments.getObject().getDocument();

        if (experimentSearch.isEmpty()) {
            experimentSearch.buildText(doc);
        }

        return doc;
    }

    public String getSpecies()
    {
        return species.getObject().get();
    }

    public String getArrays()
    {
        return arrays.getObject().get();
    }

    public ExperimentSearch getSearch()
    {
        return experimentSearch;
    }

    public String getDataSource()
    {
        if (null == dataSource) {
            dataSource = getPreferences().getString("ae.experiments.datasources");
        }

        return dataSource;
    }

    public void setDataSource( String ds )
    {
        dataSource = ds;
    }

    public void reload( String xmlString )
    {
        experiments.setObject(
                new PersistableDocumentContainer(
                        loadExperimentsFromString(xmlString)
                )
        );
        species.setObject(
                new PersistableString(
                        ((XsltHelper) getApplication().getComponent("XsltHelper")).transformDocumentToString(
                                experiments.getObject().getDocument()
                                , "preprocess-species-html.xsl"
                                , null
                        )
                )
        );

        arrays.setObject(
                new PersistableString(
                        ((XsltHelper) getApplication().getComponent("XsltHelper")).transformDocumentToString(
                                experiments.getObject().getDocument()
                                , "preprocess-arrays-html.xsl"
                                , null
                        )
                )
        );

        experimentSearch.buildText(experiments.getObject().getDocument());

    }

    public boolean isFilePublic( String file )
    {
        boolean result = true;

        Pattern p = Pattern.compile("/(E-[^-]+-[0-9]+)/");
        Matcher m = p.matcher(file);
        if (m.find()) {
            String accession = m.group(1);
            result = getSearch().doesPresent(accession);
        }
        return result;
    }

    private Document loadExperimentsFromString( String xmlString )
    {
        Document doc = ((XsltHelper) getComponent("XsltHelper")).transformStringToDocument(xmlString, "preprocess-experiments-xml.xsl", null);
        if (null == doc) {<commit_msg>Attempting to make parts of code thread-safe<commit_after>package uk.ac.ebi.ae15.components;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import uk.ac.ebi.ae15.app.Application;
import uk.ac.ebi.ae15.app.ApplicationComponent;
import uk.ac.ebi.ae15.utils.persistence.PersistableDocumentContainer;
import uk.ac.ebi.ae15.utils.persistence.PersistableString;
import uk.ac.ebi.ae15.utils.persistence.TextFilePersistence;
import uk.ac.ebi.ae15.utils.search.ExperimentSearch;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Experiments extends ApplicationComponent
{
    // logging machinery
    private final Log log = LogFactory.getLog(getClass());

    private TextFilePersistence<PersistableDocumentContainer> experiments;
    private TextFilePersistence<PersistableString> species;
    private TextFilePersistence<PersistableString> arrays;

    private ExperimentSearch experimentSearch;
    private String dataSource;

    public Experiments( Application app )
    {
        super(app, "Experiments");
    }

    public void initialize()
    {
        experiments = new TextFilePersistence<PersistableDocumentContainer>(
                new PersistableDocumentContainer()
                , new File(System.getProperty("java.io.tmpdir"), getPreferences().getString("ae.experiments.cache.filename"))
        );

        experimentSearch = new ExperimentSearch();

        species = new TextFilePersistence<PersistableString>(
                new PersistableString()
                , new File(System.getProperty("java.io.tmpdir"), getPreferences().getString("ae.species.cache.filename"))

        );

        arrays = new TextFilePersistence<PersistableString>(
                new PersistableString()
                , new File(System.getProperty("java.io.tmpdir"), getPreferences().getString("ae.arrays.cache.filename"))
        );
    }

    public void terminate()
    {
    }

    public synchronized Document getExperiments()
    {
        Document doc = experiments.getObject().getDocument();

        if (experimentSearch.isEmpty()) {
            experimentSearch.buildText(doc);
        }

        return doc;
    }

    public synchronized ExperimentSearch getSearch()
    {
        return experimentSearch;
    }

    public String getSpecies()
    {
        return species.getObject().get();
    }

    public String getArrays()
    {
        return arrays.getObject().get();
    }

    public String getDataSource()
    {
        if (null == dataSource) {
            dataSource = getPreferences().getString("ae.experiments.datasources");
        }

        return dataSource;
    }

    public void setDataSource( String ds )
    {
        dataSource = ds;
    }

    public void reload( String xmlString )
    {
        setExperiments(loadExperimentsFromString(xmlString));

        species.setObject(
                new PersistableString(
                        ((XsltHelper) getApplication().getComponent("XsltHelper")).transformDocumentToString(
                                experiments.getObject().getDocument()
                                , "preprocess-species-html.xsl"
                                , null
                        )
                )
        );

        arrays.setObject(
                new PersistableString(
                        ((XsltHelper) getApplication().getComponent("XsltHelper")).transformDocumentToString(
                                experiments.getObject().getDocument()
                                , "preprocess-arrays-html.xsl"
                                , null
                        )
                )
        );

    }

    public boolean isFilePublic( String file )
    {
        boolean result = true;

        Pattern p = Pattern.compile("/(E-[^-]+-[0-9]+)/");
        Matcher m = p.matcher(file);
        if (m.find()) {
            String accession = m.group(1);
            result = getSearch().doesPresent(accession);
        }
        return result;
    }

    private synchronized void setExperiments( Document doc )
    {
        experiments.setObject(new PersistableDocumentContainer(doc));
        experimentSearch.buildText(doc);
    }

    private Document loadExperimentsFromString( String xmlString )
    {
        Document doc = ((XsltHelper) getComponent("XsltHelper")).transformStringToDocument(xmlString, "preprocess-experiments-xml.xsl", null);
        if (null == doc) {
{'fraction_non_alphanumeric': 0.10118647862099843, 'fraction_numerical': 0.004029550033579583, 'mean_word_length': 2.1899321670831844, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2557208', 'n_tokens_mistral': 2382, 'n_tokens_neox': 2262, 'n_words': 409}