<commit_before>/*
 * #%L
 * Bitrepository Core
 * %%
 * Copyright (C) 2010 - 2013 The State and University Library, The Royal Library and The State Archives, Denmark
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */
package org.bitrepository.common.utils;

import org.bitrepository.common.settings.Settings;
import org.bitrepository.settings.referencesettings.IntegrityServiceSettings;
import org.bitrepository.settings.referencesettings.PillarIntegrityDetails;
import org.bitrepository.settings.referencesettings.PillarType;
import org.bitrepository.settings.repositorysettings.Collection;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Utility method for handling the settings.
 */
public class SettingsUtils {
    public final static Integer DEFAULT_MAX_CLIENT_PAGE_SIZE = 10000;
    private static Settings settings;

    /**
     * @param injectedSettings The settings to use.
     */
    public static void initialize(Settings injectedSettings) {
        settings = injectedSettings;
    }

    /**
     * Finds the collections, which the given pillar is part of.
     *
     * @param pillarID The id of the pillar.
     * @return The list of collection ids, which this pillar is part of.
     */
    public static List<String> getCollectionIDsForPillar(String pillarID) {
        List<String> res = new ArrayList<>();
        for (Collection c : settings.getRepositorySettings().getCollections().getCollection()) {
            if (c.getPillarIDs().getPillarID().contains(pillarID)) {
                res.add(c.getID());
            }
        }

        return res;
    }

    /**
     * Finds the complete list of collections in the repository.
     *
     * @return all collections in the repository
     */
    public static List<String> getAllCollectionsIDs() {
        List<String> res = new ArrayList<>();
        for (Collection c : settings.getRepositorySettings().getCollections().getCollection()) {
            res.add(c.getID());
        }
        return res;
    }

    /**
     * Gets the name of a given collection. If no name is given in the settings,
     * the ID of the collection is returned
     *
     * @param collectionID the collection id
     * @return The name of the collection
     */
    public static String getCollectionName(String collectionID) {
        String name = null;

        for (Collection collection : settings.getRepositorySettings().getCollections().getCollection()) {
            if (collection.getID().equals(collectionID)) {
                if (collection.isSetName()) {
                    name = collection.getName();
                } else {
                    name = collection.getID();
                }
                break;
            }
        }

        return name;
    }

    /**
     * Get the human-readable pillar name for the given pillarID from the ReferenceSettings.
     *
     * @param pillarID The pillarID for which the pillar name is wanted.
     * @return Returns the pillar name for the given pillar ID.
     */
    public static String getPillarName(String pillarID) {
        PillarIntegrityDetails details = settings.getReferenceSettings().getIntegrityServiceSettings().getPillarIntegrityDetails();
        if (details != null) {
            for (PillarIntegrityDetails.PillarDetails d : details.getPillarDetails()) {
                if (d.getPillarID().equals(pillarID)) {
                    return d.getPillarName();
                }
            }
        }
        return null;
    }

    public static IntegrityServiceSettings getIntegrityServiceSettings() {
        return settings.getReferenceSettings().getIntegrityServiceSettings();
    }

    /**
     * Get the configured max age for checksums for the given pillarID from the ReferenceSettings.
     *
     * @param pillarID The pillarID for which the hostname is wanted.
     * @return human-readable maximum age for checksums for the given pillar ID.
     */
    public static String getMaxAgeForChecksums(String pillarID) {
        // TODO how to use pillarID??
        BigInteger maxAge = settings.getReferenceSettings().getPillarSettings().getMaxAgeForChecksums();
        if (maxAge == null) {
            return null;
        }
        try {
            return TimeUtils.millisecondsToHuman(maxAge.longValueExact());
        }
        catch (ArithmeticException) {
            return "Extremely long";
        }
    }

    /**
     * Get the {@link PillarType} for the given Pillar ID.
     *
     * @param pillarID The pillar ID for which the {@link PillarType} is wanted.
     * @return Returns the {@link PillarType} for the given pillar ID.<commit_msg>Fix syntax error<commit_after>/*
 * #%L
 * Bitrepository Core
 * %%
 * Copyright (C) 2010 - 2013 The State and University Library, The Royal Library and The State Archives, Denmark
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */
package org.bitrepository.common.utils;

import org.bitrepository.common.settings.Settings;
import org.bitrepository.settings.referencesettings.IntegrityServiceSettings;
import org.bitrepository.settings.referencesettings.PillarIntegrityDetails;
import org.bitrepository.settings.referencesettings.PillarType;
import org.bitrepository.settings.repositorysettings.Collection;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Utility method for handling the settings.
 */
public class SettingsUtils {
    public final static Integer DEFAULT_MAX_CLIENT_PAGE_SIZE = 10000;
    private static Settings settings;

    /**
     * @param injectedSettings The settings to use.
     */
    public static void initialize(Settings injectedSettings) {
        settings = injectedSettings;
    }

    /**
     * Finds the collections, which the given pillar is part of.
     *
     * @param pillarID The id of the pillar.
     * @return The list of collection ids, which this pillar is part of.
     */
    public static List<String> getCollectionIDsForPillar(String pillarID) {
        List<String> res = new ArrayList<>();
        for (Collection c : settings.getRepositorySettings().getCollections().getCollection()) {
            if (c.getPillarIDs().getPillarID().contains(pillarID)) {
                res.add(c.getID());
            }
        }

        return res;
    }

    /**
     * Finds the complete list of collections in the repository.
     *
     * @return all collections in the repository
     */
    public static List<String> getAllCollectionsIDs() {
        List<String> res = new ArrayList<>();
        for (Collection c : settings.getRepositorySettings().getCollections().getCollection()) {
            res.add(c.getID());
        }
        return res;
    }

    /**
     * Gets the name of a given collection. If no name is given in the settings,
     * the ID of the collection is returned
     *
     * @param collectionID the collection id
     * @return The name of the collection
     */
    public static String getCollectionName(String collectionID) {
        String name = null;

        for (Collection collection : settings.getRepositorySettings().getCollections().getCollection()) {
            if (collection.getID().equals(collectionID)) {
                if (collection.isSetName()) {
                    name = collection.getName();
                } else {
                    name = collection.getID();
                }
                break;
            }
        }

        return name;
    }

    /**
     * Get the human-readable pillar name for the given pillarID from the ReferenceSettings.
     *
     * @param pillarID The pillarID for which the pillar name is wanted.
     * @return Returns the pillar name for the given pillar ID.
     */
    public static String getPillarName(String pillarID) {
        PillarIntegrityDetails details = settings.getReferenceSettings().getIntegrityServiceSettings().getPillarIntegrityDetails();
        if (details != null) {
            for (PillarIntegrityDetails.PillarDetails d : details.getPillarDetails()) {
                if (d.getPillarID().equals(pillarID)) {
                    return d.getPillarName();
                }
            }
        }
        return null;
    }

    public static IntegrityServiceSettings getIntegrityServiceSettings() {
        return settings.getReferenceSettings().getIntegrityServiceSettings();
    }

    /**
     * Get the configured max age for checksums for the given pillarID from the ReferenceSettings.
     *
     * @param pillarID The pillarID for which the hostname is wanted.
     * @return human-readable maximum age for checksums for the given pillar ID.
     */
    public static String getMaxAgeForChecksums(String pillarID) {
        // TODO how to use pillarID??
        BigInteger maxAge = settings.getReferenceSettings().getPillarSettings().getMaxAgeForChecksums();
        if (maxAge == null) {
            return null;
        }
        try {
            return TimeUtils.millisecondsToHuman(maxAge.longValueExact());
        }
        catch (ArithmeticException ae) {
            return "Extremely long";
        }
    }

    /**
     * Get the {@link PillarType} for the given Pillar ID.
     *
     * @param pillarID The pillar ID for which the {@link PillarType} is wanted.
     * @return Returns the {@link PillarType} for the given pillar ID.
{'fraction_non_alphanumeric': 0.08591707134852447, 'fraction_numerical': 0.003175196115054165, 'mean_word_length': 2.5840026773761715, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1092781', 'n_tokens_mistral': 2868, 'n_tokens_neox': 2673, 'n_words': 930}