<commit_before>package com.e2open.falcon.framework.model;

import com.e2open.falcon.framework.Configuration;
import com.e2open.falcon.framework.annotations.AutoPopulateOff;
import org.apache.commons.lang.StringUtils;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class Model {
    @AutoPopulateOff
    private static final String testRunUUID = initializeUUID();

    @AutoPopulateOff
    private String instanceUUID = null;

    @AutoPopulateOff
    public UIDStrategy uidStrategy;

    public enum UIDStrategy {NONE, BY_TEST_RUN, BY_CLASS_INSTANCE}

    public Model() {
        instanceUUID = initializeUUID();
        String defaultStrategy = Configuration.INSTANCE.getProperty("model.default.uuid.strategy");
        if (defaultStrategy != null) {
            uidStrategy = UIDStrategy.valueOf(defaultStrategy.toUpperCase());
        } else {
            uidStrategy = UIDStrategy.NONE;
        }
    }

    public Model(UIDStrategy uidStrategy) {
        instanceUUID = initializeUUID();
        this.uidStrategy = uidStrategy;
    }

    public String getTestRunUUID() {
        return testRunUUID;
    }

    public String getInstanceUUID() {
        return instanceUUID;
    }

    public String getUUID() {
        if (uidStrategy == null) uidStrategy = UIDStrategy.NONE;
        if (uidStrategy == UIDStrategy.NONE) {
            return null;
        } else if (uidStrategy == UIDStrategy.BY_TEST_RUN) {
            return testRunUUID;
        } else if (uidStrategy == UIDStrategy.BY_CLASS_INSTANCE) {
            return instanceUUID;
        } else {
            throw new RuntimeException("Unknown strategy: " + uidStrategy.toString());
        }
    }

    protected String getUniqueValue(String value) {
        if (uidStrategy == null) uidStrategy = UIDStrategy.NONE;
        String uuid = getUUID();
        return (uuid == null) ? value : value + getUUID();
    }

    private static String initializeUUID() {
        String uuid = Configuration.INSTANCE.getProperty("model.uuid");
        if (StringUtils.isNotBlank(uuid)) {
            return uuid;
        } else {
            uuid = Long.toString(UUID.randomUUID().getMostSignificantBits());
            System.out.println(String.format("UUID Created %s for class", uuid));
            return uuid;
        }
    }

    // TODO: there has to be a better way
    protected String transformConfigurationReferences(String rawValue) {
        if (StringUtils.isNotBlank(rawValue)) {
            String patternStr = "__CONFIGURATION.(.+)__";<commit_msg>fix issue in getting default strategy and add some better logging of uid when used<commit_after>package com.e2open.falcon.framework.model;

import com.e2open.falcon.framework.Configuration;
import com.e2open.falcon.framework.annotations.AutoPopulateOff;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class Model {
    private static final Logger log = Logger.getLogger(Model.class);
    private static List<String> userNotifiedOfUUID = new ArrayList<String>();

    @AutoPopulateOff
    public static final String testRunUUID = getNewUUID();

    @AutoPopulateOff
    private String instanceUUID = null;

    @AutoPopulateOff
    public UIDStrategy uidStrategy;

    public enum UIDStrategy {NONE, BY_TEST_RUN, BY_CLASS_INSTANCE}

    public Model() {
        instanceUUID = getNewUUID();
        uidStrategy = getDefaultStrategy();
    }

    private UIDStrategy getDefaultStrategy() {
        String defaultStrategy = Configuration.INSTANCE.getProperty("model.default.uuid.strategy");
        if (defaultStrategy != null) {
            return UIDStrategy.valueOf(defaultStrategy.toUpperCase());
        } else {
            return UIDStrategy.NONE;
        }
    }

    public Model(UIDStrategy uidStrategy) {
        instanceUUID = getNewUUID();
        this.uidStrategy = uidStrategy;
    }

    public String getTestRunUUID() {
        return testRunUUID;
    }

    public String getInstanceUUID() {
        return instanceUUID;
    }

    public String getUUID() {
        if (uidStrategy == null) uidStrategy = getDefaultStrategy();
        if (uidStrategy == UIDStrategy.NONE) {
            return null;
        } else if (uidStrategy == UIDStrategy.BY_TEST_RUN) {
            logUUID(testRunUUID);
            return testRunUUID;
        } else if (uidStrategy == UIDStrategy.BY_CLASS_INSTANCE) {
            logUUID(instanceUUID);
            return instanceUUID;
        } else {
            throw new RuntimeException("Unknown strategy: " + uidStrategy.toString());
        }
    }

    private void logUUID(String uid) {
        if (!userNotifiedOfUUID.contains(uid)) {
            switch (uidStrategy) {
                case NONE:
                    break;
                case BY_TEST_RUN:
                    if (!userNotifiedOfUUID.contains(uid)) {
                        log.info(String.format("UUID Created %s for test run", instanceUUID));
                        userNotifiedOfUUID.add(uid);
                    }
                    break;
                case BY_CLASS_INSTANCE:
                    if (!userNotifiedOfUUID.contains(uid)) {
                        log.info(String.format("UUID Created %s for class instance of %s", instanceUUID, getClass().getName()));
                        userNotifiedOfUUID.add(uid);
                    }
                    break;
            }
        }
    }

    protected String getUniqueValue(String value) {
        if (StringUtils.isBlank(value)) return null;
        if (uidStrategy == null) uidStrategy = getDefaultStrategy();
        String uuid = getUUID();
        if (uuid == null || value.endsWith(uuid)) {
            return value;
        } else {
            return value + getUUID();
        }
    }

    private static String getNewUUID() {
        String uuid = Configuration.INSTANCE.getProperty("model.uuid");
        if (StringUtils.isNotBlank(uuid)) {
            return uuid;
        } else {
            uuid = Long.toString(UUID.randomUUID().getMostSignificantBits());
            return uuid;
        }
    }

    // TODO: there has to be a better way
    protected String transformConfigurationReferences(String rawValue) {
        if (StringUtils.isNotBlank(rawValue)) {
            String patternStr = "__CONFIGURATION.(.+)__";
{'fraction_non_alphanumeric': 0.09446756048697796, 'fraction_numerical': 0.0010787486515641855, 'mean_word_length': 2.3714285714285714, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2011936', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1749, 'n_words': 400}