package app.reporter.util;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import app.reporter.dataobjects.ApiKey;
import app.reporter.dataobjects.RestResource;

public class RestResourceCache {

	private static Map<String, RestResource> restResourcesCache = null;
	private static Map<String, ApiKey> restResourcesAPIKeys = null;

	public static boolean refreshCache() {

		boolean status = false;
		try {
			if (restResourcesCache == null)
				restResourcesCache = new ConcurrentHashMap<String, RestResource>();

			// load rest resources cache
			ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
			try {
				List<RestResource> restResources = mapper.readValue(Thread.currentThread().getContextClassLoader().getResourceAsStream("rest-resources-config.yaml"),new TypeReference<List<RestResource>>(){});
				restResources.forEach(r -> System.out.println(r.getName()+" - "+ ReflectionToStringBuilder.toString(r, ToStringStyle.MULTI_LINE_STYLE)));
				restResources.forEach(r -> restResourcesCache.put(r.getResourceId(),r));
				//System.out.println(ReflectionToStringBuilder.toString(restResources, ToStringStyle.MULTI_LINE_STYLE));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			 if(restResourcesAPIKeys == null)
			 restResourcesAPIKeys = new ConcurrentHashMap<String,ApiKey>();
			
			// load resources keys cache
			try {
				List<ApiKey> restResourcesApiKeys = mapper.readValue(Thread.currentThread().getContextClassLoader().getResourceAsStream("rest-resources-keys.yaml"),new TypeReference<List<ApiKey>>(){});
				restResourcesApiKeys.forEach(r -> System.out.println(r.getResourceId()+" - "+ ReflectionToStringBuilder.toString(r, ToStringStyle.MULTI_LINE_STYLE)));
				restResourcesApiKeys.forEach(r -> restResourcesAPIKeys.put(r.getResourceId(),r));
				//System.out.println(ReflectionToStringBuilder.toString(restResources, ToStringStyle.MULTI_LINE_STYLE));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			status = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return status;
	}

	public static RestResource getResource(String resourceId) {
		if(restResourcesCache == null)
			refreshCache();
		return restResourcesCache.get(resourceId);
	}

	public static ApiKey getResourceApiKey(String resourceId) {
		if(restResourcesAPIKeys == null)
			refreshCache();
		return restResourcesAPIKeys.get(resourceId);
	}

}

@@ -21,16 +21,44 @@
 .util;%0A%0A
+import java.io.InputStream;%0A
 import j
@@ -1751,61 +1751,41 @@
 %09%09%09%09
-List%3CApiKey%3E restResourcesApiKeys = mapper.readValue(
+InputStream apiKeysInputStream = 
 Thre
@@ -1870,24 +1870,139 @@
 -keys.yaml%22)
+;%0A%09%09%09%09if(apiKeysInputStream != null) %7B%0A%09%09%09%09%09List%3CApiKey%3E restResourcesApiKeys = mapper.readValue(apiKeysInputStream
 ,new TypeRef
@@ -2020,32 +2020,33 @@
 %3CApiKey%3E%3E()%7B%7D);%0A
+%09
 %09%09%09%09restResource
@@ -2180,32 +2180,33 @@
 E_STYLE)));%0A%09%09%09%09
+%09
 restResourcesApi
@@ -2267,34 +2267,50 @@
 ceId(),r));%0A%09%09%09%09
-//
+%7D%0A%09%09%09%09else %7B%0A%09%09%09%09%09
 System.out.print
@@ -2316,56 +2316,38 @@
 tln(
-ReflectionToStringBuilder.toString(
+%22Api Keys file: 
 rest
-R
+-r
 esources
 , To
@@ -2346,43 +2346,87 @@
 rces
-, ToStringStyle.MULTI_LINE_STYLE));
+-keys.yaml was NOT found - make sure you have this placed in classpath%22);%0A%09%09%09%09%7D
 %0A%09%09%09

print msg instead of npe if no api key file