package org.cbioportal.service;

import org.cbioportal.persistence.util.CustomEhCachingProvider;
import org.cbioportal.persistence.util.EhCacheStatistics;
import org.cbioportal.service.exception.CacheNotFoundException;

import java.lang.String;
import java.util.*;
import javax.cache.*;
import org.ehcache.jsr107.EhcacheCachingProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;

@Service
public class CacheStatisticsServiceImpl implements CacheStatisticsService {

    @Autowired
    public CacheManager cacheManager;

    @Autowired
    public EhCacheStatistics ehCacheStatistics;

    @Value("${cache.statistics_endpoint_enabled:false}")
    public boolean cacheStatisticsEndpointEnabled;
    
    private void checkIfCacheStatisticsEndpointEnabled() {
        if (!cacheStatisticsEndpointEnabled) {
            throw new AccessDeniedException("Cache statistics is not enabled for this instance of the portal.");
        }
    }
 
    @Override
    public List<String> getKeyCountsPerClass(String cacheName) throws CacheNotFoundException {
        checkIfCacheStatisticsEndpointEnabled();
        Cache cache = cacheManager.getCache(cacheName);
        if (cache == null) {
            throw new CacheNotFoundException(cacheName);
        }
        Map<String, Integer> classToKeyCount = new HashMap<String, Integer>();
        Iterator<Cache.Entry<String, Object>> iterator = cache.iterator();
        while(iterator.hasNext()) {
            Cache.Entry<String,Object> entry = iterator.next();
            String cacheKey = entry.getKey();
            String className = cacheKey.split("_")[0];
            int keyCount = classToKeyCount.containsKey(className) ? classToKeyCount.get(className) : 0;
            classToKeyCount.put(className, keyCount + 1);
        }
        List<String> keyCountsPerClass = new ArrayList<String>();
        for (Map.Entry<String, Integer> entry : classToKeyCount.entrySet()) {
           keyCountsPerClass.add(entry.getKey().toString() + ": " + entry.getValue().toString() + " keys");
        }
        return keyCountsPerClass;
    }

    @Override
    public List<String> getKeysInCache(String cacheName) throws CacheNotFoundException {
        checkIfCacheStatisticsEndpointEnabled();
        Cache cache = cacheManager.getCache(cacheName);
        if (cache == null) {
            throw new CacheNotFoundException(cacheName);
        }
        Integer numberOfKeys = 0;
        List<String> keysInCache = new ArrayList<String>(); 
        Iterator<Cache.Entry<String,Object>> iterator = cache.iterator();
        while (iterator.hasNext()) {
            Cache.Entry<String,Object> entry = iterator.next();
            keysInCache.add(entry.getKey());
            numberOfKeys += 1;
        }
        keysInCache.add("Total Number of Keys: " + numberOfKeys.toString());
        return keysInCache;
    }

    @Override
    public String getCacheStatistics() {
        checkIfCacheStatisticsEndpointEnabled();
        return ehCacheStatistics.getCacheStatistics();
    }
}

@@ -27,74 +27,15 @@
 vice
-;%0A%0Aimport org.cbioportal.persistence.util.CustomEhCachingProvider;
+.impl;%0A
 %0Aimp
@@ -89,16 +89,70 @@
 istics;%0A
+import org.cbioportal.service.CacheStatisticsService;%0A
 import o
@@ -279,58 +279,8 @@
 .*;%0A
-import org.ehcache.jsr107.EhcacheCachingProvider;%0A
 impo
@@ -823,20 +823,16 @@
 nabled;%0A
-    
 %0A    pri
@@ -1059,17 +1059,16 @@
 %7D%0A    %7D%0A
- 
 %0A    @Ov
@@ -1223,32 +1223,48 @@
 );%0A        Cache
+%3CString, Object%3E
  cache = cacheMa
@@ -1553,16 +1553,17 @@
    while
+ 
 (iterato
@@ -1600,32 +1600,33 @@
 he.Entry%3CString,
+ 
 Object%3E entry = 
@@ -2070,16 +2070,17 @@
         
+ 
 keyCount
@@ -2384,16 +2384,32 @@
    Cache
+%3CString, Object%3E
  cache =
@@ -2632,17 +2632,16 @@
 ring%3E();
- 
 %0A       
@@ -2665,24 +2665,25 @@
 ntry%3CString,
+ 
 Object%3E%3E ite
@@ -2776,16 +2776,17 @@
 %3CString,
+ 
 Object%3E 

Fix CacheStatisticsServiceImpl