package com.thinkbiganalytics.feedmgr.nifi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;

/**
 * Created by sr186054 on 5/3/16.
 *
 * Helper class to get Environment Properties
 */
@RefreshScope
public class SpringEnvironmentProperties {

    private Map<String, Object> properties = new HashMap<>();

    private Map<String,Map<String,Object>> propertiesStartingWith = new HashMap<>();



    public SpringEnvironmentProperties() {

    }

    @Autowired
    private Environment env;

    /**
     * Get All Properties that start with a prefix
     * @param key
     * @return
     */
    public Map<String,Object> getPropertiesStartingWith(String key){

        if(propertiesStartingWith.containsKey(key)){
            return propertiesStartingWith.get(key);
        }
        else {
            Map<String, Object> props = getAllProperties();
            if (props != null) {
                NavigableMap m = new TreeMap(props);
                Map<String,Object> properties = m.subMap(key, key + Character.MAX_VALUE);
                propertiesStartingWith.put(key,properties);
                return properties;

            }
            return null;
        }
    }

    public Object getPropertyValue(String key){
        return getAllProperties().get(key);
    }

    public String getPropertyValueAsString(String key){
        Object obj =  getPropertyValue(key);
        if(obj != null){
            return obj.toString();
        }
        return null;
    }

    public void reset() {
        this.propertiesStartingWith.clear();
        this.properties.clear();
    }


    /**
     * get All properties
     * @return
     */
    public  Map<String,Object> getAllProperties(  )
    {
        if (properties == null || properties.isEmpty()) {
            Map<String, Object> map = new HashMap();
            for(Iterator it = ((AbstractEnvironment) env).getPropertySources().iterator(); it.hasNext(); ) {
                PropertySource propertySource = (PropertySource) it.next();
                if (propertySource instanceof MapPropertySource) {
                    map.putAll(((MapPropertySource) propertySource).getSource());
                }
            }

            properties = map;
        }
        return properties;
    }

}

@@ -38,16 +38,73 @@
 .nifi;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -723,24 +723,123 @@
 operties %7B%0A%0A
+    private static final Logger log = LoggerFactory.getLogger(SpringEnvironmentProperties.class);%0A%0A
     private 
@@ -1609,16 +1609,431 @@
 VALUE);%0A
+                Map%3CString, Object%3E decryptedProperties = new HashMap%3C%3E();%0A                if (properties != null && !properties.isEmpty()) %7B%0A                    properties.keySet().stream().forEach(k -%3E %7B%0A                                                             decryptedProperties.put(k, env.getProperty(k));%0A                                                         %7D%0A                    );%0A                %7D%0A
         
@@ -2071,17 +2071,27 @@
 put(key,
-p
+ decryptedP
 ropertie
@@ -2277,32 +2277,33 @@
 et(key);%0A    %7D%0A%0A
+%0A
     public Strin
@@ -3184,24 +3184,221 @@
            %7D
+%0A            //decrypt%0A            Map%3CString, Object%3E decryptedMap = new HashMap();%0A            map.keySet().forEach(k -%3E %7B%0A                decryptedMap.put(k, env.getProperty(k));%0A            %7D);
 %0A%0A          
@@ -3412,17 +3412,26 @@
 rties = 
-m
+decryptedM
 ap;%0A    

fix getting encrypted properties