package com.thinkbiganalytics.feedmgr.nifi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.PropertySource;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.function.Consumer;

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

    private  Map<String,Object> properties;

    public SpringEnvironmentProperties() {

    }

    @Autowired
    private ConfigurableEnvironment environment;

    /**
     * Get All Properties that start with a prefix
     * @param key
     * @return
     */
    public Map<String,Object> getPropertiesStartingWith(String key){
        Map<String,Object> props = getAllProperties();
        if(props != null) {
            NavigableMap m = new TreeMap(props);
            return m.subMap( key, key + Character.MAX_VALUE );
        }
        return null;
    }

    /**
     * get All properties
     * @return
     */
    public  Map<String,Object> getAllProperties(  )
    {
        if(properties == null) {
            final Map<String, Object> result = new HashMap<>();
            environment.getPropertySources().forEach(new Consumer<PropertySource<?>>() {
                @Override
                public void accept(PropertySource<?> propertySource) {
                    addAll(result, getAllProperties(propertySource));
                }
            });
            properties = result;
        }
        return properties;
    }

    private  Map<String,Object> getAllProperties( PropertySource<?> aPropSource )
    {
        final Map<String,Object> result = new HashMap<>();

        if ( aPropSource instanceof CompositePropertySource)
        {
            CompositePropertySource cps = (CompositePropertySource) aPropSource;
            cps.getPropertySources().forEach(new Consumer<PropertySource<?>>() {
                @Override
                public void accept(PropertySource<?> propertySource) {
                    addAll(result, getAllProperties(propertySource));
                }
            });
            return result;
        }

        if ( aPropSource instanceof EnumerablePropertySource<?>)
        {
           final  EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) aPropSource;
            Arrays.asList(ps.getPropertyNames()).forEach(new Consumer<String>() {
                @Override
                public void accept(String key) {
                    result.put(key, ps.getProperty(key));
                }
            });
            return result;
        }
        return result;

    }

    private  void addAll( Map<String, Object> aBase, Map<String, Object> aToBeAdded )
    {
        for (Map.Entry<String, Object> entry : aToBeAdded.entrySet())
        {
            if ( aBase.containsKey( entry.getKey() ) )
            {
                continue;
            }

            aBase.put( entry.getKey(), entry.getValue() );
        }
    }

}

@@ -141,401 +141,30 @@
 env.
-CompositePropertySource;%0Aimport org.springframework.core.env.ConfigurableEnvironment;%0Aimport org.springframework.core.env.EnumerablePropertySource;%0Aimport org.springframework.core.env.PropertySource;%0A%0Aimport java.util.Arrays;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.NavigableMap;%0Aimport java.util.SortedMap;%0Aimport java.util.TreeMap;%0Aimport java.util.function.Consumer
+*;%0A%0Aimport java.util.*
 ;%0A%0A/
@@ -1087,63 +1087,56 @@
     
- environment.getPropertySources().forEach(new Consumer%3C
+MutablePropertySources sources = environment.get
 Prop
@@ -1141,32 +1141,28 @@
 opertySource
-%3C?%3E%3E() %7B
+s();
 %0A           
@@ -1166,57 +1166,61 @@
     
-    @Override%0A                public void accept(
+while(sources.iterator().hasNext())%7B%0A                
 Prop
@@ -1221,35 +1221,32 @@
   PropertySource
-%3C?%3E
  propertySource)
@@ -1240,32 +1240,54 @@
 opertySource
-) %7B%0A    
+ = sources.iterator().next();%0A
             
@@ -1352,37 +1352,17 @@
         
-    %7D%0A            %7D);
+%7D
 %0A       
@@ -1747,139 +1747,28 @@
     
-cps.getPropertySources().forEach(new Consumer%3CPropertySource%3C?%3E%3E() %7B%0A                @Override%0A                public void accept(P
+for(PropertySource p
 rope
@@ -1776,21 +1776,26 @@
 tySource
-%3C?%3E p
+: cps.getP
 ropertyS
@@ -1799,24 +1799,22 @@
 tySource
-) %7B%0A    
+s())%7B%0A
         
@@ -1883,37 +1883,18 @@
         
-    %7D%0A            %7D);
+%7D%0A
 %0A       
@@ -2010,22 +2010,16 @@
         
-final 
  Enumera
@@ -2103,22 +2103,25 @@
     
-Arrays.asList(
+if(ps != null && 
 ps.g
@@ -2141,103 +2141,114 @@
 es()
-).forEach(new Consumer%3CString%3E() %7B%0A                @Override%0A                public void accept
+ != null) %7B%0A                List%3CString%3E keys = Arrays.asList(ps.getPropertyNames());%0A                for 
 (Str
@@ -2254,16 +2254,23 @@
 ring key
+ : keys
 ) %7B%0A    
@@ -2354,18 +2354,16 @@
        %7D
-);
 %0A       

fix build remove Java 8 function