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();
        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.(.+)__";
            Pattern p = Pattern.compile(patternStr);
            Matcher m = p.matcher(rawValue);
            if (m.find(0)) {
                String matchedText = m.group(1);
                String property = Configuration.INSTANCE.getProperty(matchedText);
                rawValue = rawValue.replace(m.group(0), property);
            }
        }
        return rawValue;
    }

}


@@ -336,30 +336,29 @@
 ateOff%0A    p
-rivate
+ublic
  static fina
@@ -662,32 +662,354 @@
    uidStrategy =
+ getDefaultStrategy();%0A    %7D%0A%0A    private UIDStrategy getDefaultStrategy() %7B%0A        String defaultStrategy = Configuration.INSTANCE.getProperty(%22model.default.uuid.strategy%22);%0A        if (defaultStrategy != null) %7B%0A            return UIDStrategy.valueOf(defaultStrategy.toUpperCase());%0A        %7D else %7B%0A            return
  UIDStrategy.NON
@@ -1007,24 +1007,34 @@
 ategy.NONE;%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -1379,35 +1379,42 @@
 dStrategy = 
-UID
+getDefault
 Strategy
 .NONE;%0A     
@@ -1393,37 +1393,34 @@
 tDefaultStrategy
-.NONE
+()
 ;%0A        if (ui
@@ -1835,32 +1835,85 @@
 String value) %7B%0A
+        if (StringUtils.isBlank(value)) return null;%0A
         if (uidS
@@ -1943,27 +1943,34 @@
 ategy = 
-UID
+getDefault
 Strategy
 .NONE;%0A 
@@ -1953,37 +1953,34 @@
 tDefaultStrategy
-.NONE
+()
 ;%0A        String
@@ -2006,22 +2006,18 @@
         
-return
+if
  (uuid =
@@ -2026,19 +2026,97 @@
 null
-) ? value :
+ %7C%7C value.endsWith(uuid)) %7B%0A            return value;%0A        %7D else %7B%0A            return
  val
@@ -2127,24 +2127,34 @@
  getUUID();%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -2433,90 +2433,8 @@
 ));%0A
-            System.out.println(String.format(%22UUID Created %25s for class%22, uuid));%0A
     

fix how the default strategy is handled. Was not always setting the strategy properly