package org.apache.flume.interceptor.service;

import java.io.IOException;
import java.io.FileNotFoundException;
import org.apache.flume.Event;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import org.apache.flume.interceptor.EnrichedEventBody;

import java.util.*;

import org.apache.flume.serialization.JSONStringSerializer;

import java.io.FileReader;
 
import com.opencsv.CSVReader;

@Component
public class FlumeCacheService implements ICacheService<Event> {

    private static final Logger logger = LoggerFactory.getLogger(FlumeCacheService.class);
    private String externalFile;

    @Override
    @Cacheable(value = "FlumeCachedEvent")
    public Event intercept(Event d) {
        byte[] payload = d.getBody();
        try {
         EnrichedEventBody enrichedEventBody =  JSONStringSerializer.fromJSONString(new String(payload), EnrichedEventBody.class);
         Map<String, String> exData = enrichedEventBody.getExtraData();
         enrichedEventBody.setExtraData(enrichMap(externalFile, exData));
         byte[] newPayload = enrichedEventBody.toByteArray();
         d.setBody(newPayload);
        } catch (IOException e) {
            logger.error("IOexception" ,e);
        }
        logger.info("Flume cache Service intercepting: " + d.toString());
        return d;
    }
    
    
    /*
    @return Map
    @param String to enrich data according key named 'hostname'.
    @param data to be enriched
    */
    public Map<String, String> enrichMap(String filename, Map<String,String> data){
        String newValue = null;
        try {
        CSVReader reader = new CSVReader(new FileReader(filename)); //default constructor uses comma separator
        List<String[]> allRows = reader.readAll();
         for(String[] row : allRows){
             newValue += Arrays.toString(row);
        }
         data.put("hostname", newValue);
        } catch (FileNotFoundException e ) {
            logger.error("FileNotFoundException", e);
        } catch (IOException e){
            logger.error("IOException", e);
        }
        
        return  data;
    }
    
    
     
}

@@ -44,74 +44,8 @@
 e;%0A%0A
-import java.io.IOException;%0Aimport java.io.FileNotFoundException;%0A
 impo
@@ -237,206 +237,8 @@
 t;%0A%0A
-import org.apache.flume.interceptor.EnrichedEventBody;%0A%0Aimport java.util.*;%0A%0Aimport org.apache.flume.serialization.JSONStringSerializer;%0A%0Aimport java.io.FileReader;%0A %0Aimport com.opencsv.CSVReader;%0A%0A
 @Com
@@ -404,41 +404,8 @@
 ss);
-%0A    private String externalFile;
 %0A%0A  
@@ -509,1410 +509,124 @@
     
-byte%5B%5D payload = d.getBody();%0A        try %7B%0A         EnrichedEventBody enrichedEventBody =  JSONStringSerializer.fromJSONString(new String(payload), EnrichedEventBody.class);%0A         Map%3CString, String%3E exData = enrichedEventBody.getExtraData();%0A         enrichedEventBody.setExtraData(enrichMap(externalFile, exData));%0A         byte%5B%5D newPayload = enrichedEventBody.toByteArray();%0A         d.setBody(newPayload);%0A        %7D catch (IOException e) %7B%0A            logger.error(%22IOexception%22 ,e);%0A        %7D%0A        logger.info(%22Flume cache Service intercepting: %22 + d.toString());%0A        return d;%0A    %7D%0A    %0A    %0A    /*%0A    @return Map%0A    @param String to enrich data according key named 'hostname'.%0A    @param data to be enriched%0A    */%0A    public Map%3CString, String%3E enrichMap(String filename, Map%3CString,String%3E data)%7B%0A        String newValue = null;%0A        try %7B%0A        CSVReader reader = new CSVReader(new FileReader(filename)); //default constructor uses comma separator%0A        List%3CString%5B%5D%3E allRows = reader.readAll();%0A         for(String%5B%5D row : allRows)%7B%0A             newValue += Arrays.toString(row);%0A        %7D%0A         data.put(%22hostname%22, newValue);%0A        %7D catch (FileNotFoundException e ) %7B%0A            logger.error(%22FileNotFoundException%22, e);%0A        %7D catch (IOException e)%7B%0A            logger.error(%22IOException%22, e);%0A        %7D%0A        %0A        return  data;%0A    %7D%0A    %0A    %0A     
+// TODO: implement!%0A        logger.info(%22Flume cache Service intercepting: %22 + d.toString());%0A        return d;%0A    %7D
 %0A%7D%0A

Revert "added cacheable method interceptor's implementation"