package com.almende.dialog.agent;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import com.almende.dialog.util.ServerUtils;
import com.almende.eve.agent.Agent;
import com.almende.eve.protocol.jsonrpc.annotation.Access;
import com.almende.eve.protocol.jsonrpc.annotation.AccessType;
import com.almende.eve.protocol.jsonrpc.annotation.Name;
import com.almende.eve.protocol.jsonrpc.annotation.Optional;
import com.askfast.commons.entity.HttpLog;
import com.askfast.commons.entity.RequestLog;
import com.askfast.commons.entity.ResponseLog;
import com.askfast.commons.intf.LoggerAgentInterface;
import com.askfast.commons.utils.EnvironmentUtil;

/**
 * Simple proxy agent which forwards the request from dialog agent to the
 * loggerAgent
 * 
 * @author shravan
 */
@Access(AccessType.PUBLIC)
public class LoggerProxyAgent extends Agent {

    public static final String HTTP_LOGGER_EVE_PATH = "eve_logger.yaml";

    public HttpLog createLog(@Name("request") RequestLog request, @Name("response") ResponseLog response,
        @Name("sessionKey") String sessionKey, @Name("accountId") String accountId,
        @Name("ddrRecordId") @Optional String ddrRecordId, @Name("isSuccessful") @Optional Boolean isSuccessful) {

        if (!ServerUtils.isInUnitTestingEnvironment()) {
            return getLoggerAgentInterface().createLog(request, response, sessionKey, accountId, ddrRecordId,
                                                       isSuccessful);
        }
        return null;
    }

    public ArrayList<HttpLog> getLogs(@Name("id") @Optional String logId, @Name("accountId") String accountId,
            @Name("sessionKeys") @Optional Collection<String> sessionKeys,
            @Name("ddrRecordId") @Optional String ddrRecordId) throws Exception {

        return getLoggerAgentInterface().getLogs(logId, accountId, sessionKeys, ddrRecordId);
    }

    public void deleteLogs(@Name("id") String logId, @Name("accountId") String accountId) {

        getLoggerAgentInterface().deleteLogs(logId, accountId);
    }

    private LoggerAgentInterface getLoggerAgentInterface() {

        String url = getConfig().get("environment", EnvironmentUtil.getEnvironment(), "http_logger_agent_url");
        if (url != null) {
            return createAgentProxy(URI.create(url), LoggerAgentInterface.class);
        }
        return null;
    }
}

@@ -28,16 +28,44 @@
 agent;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -130,24 +130,58 @@
 Collection;%0A
+import java.util.logging.Logger;%0A%0A
 import com.a
@@ -488,16 +488,53 @@
 tional;%0A
+import com.almende.util.jackson.JOM;%0A
 import c
@@ -764,16 +764,123 @@
 entUtil;
+%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0Aimport com.fasterxml.jackson.databind.node.ObjectNode;
 %0A%0A/**%0A *
@@ -1063,24 +1063,116 @@
 ds Agent %7B%0A%0A
+    private static final Logger log = Logger.getLogger( LoggerProxyAgent.class.getName() );%0A
     public s
@@ -1244,23 +1244,20 @@
  public 
-HttpLog
+void
  createL
@@ -1609,68 +1609,242 @@
     
-return getLoggerA
+%0A            ObjectMapper om = JOM.
 ge
-n
 tIn
-terface().createLog(
+stance();%0A            %0A            ObjectNode params = om.createObjectNode();%0A            params.set( %22
 request
+%22
 , 
+om.convertValue( request, ObjectNode.class ) );%0A            params.set( %22
 response
 , se
@@ -1839,18 +1839,93 @@
 response
+%22
 , 
+om.convertValue( response, ObjectNode.class ) );%0A            params.put( %22
 sessionK
@@ -1926,18 +1926,58 @@
 ssionKey
+%22
 , 
+sessionKey );%0A            params.put( %22
 accountI
@@ -1981,38 +1981,134 @@
 ntId
+%22
 , 
-ddrRecordId,%0A               
+accountId );%0A            params.put( %22ddrRecordId%22, ddrRecordId );%0A            params.put( %22isSuccessful%22, isSuccessful );%0A
     
@@ -2107,32 +2107,33 @@
  );%0A            
+%0A
                 
@@ -2120,32 +2120,38 @@
    %0A            
+try %7B%0A
                 
@@ -2154,53 +2154,243 @@
     
-isSuccessful);%0A        %7D%0A        return null;
+URI url = getLoggerAgentURL();%0A                call( url, %22createLog%22, params );%0A            %7D catch(IOException e) %7B%0A                log.warning(%22Failed to create log e: %22);%0A                e.printStackTrace();%0A            %7D%0A        %7D
 %0A   
@@ -2985,24 +2985,293 @@
 erface() %7B%0A%0A
+        URI url = getLoggerAgentURL();%0A        if (url != null) %7B%0A            return createAgentProxy(url, LoggerAgentInterface.class);%0A        %7D%0A        log.warning( %22No logger agent url found!%22 );%0A        return null;%0A    %7D%0A    %0A    private URI getLoggerAgentURL() %7B%0A
         Stri
@@ -3420,80 +3420,87 @@
 urn 
-createAgentProxy(URI.create(url), L
+URI.create(url);%0A        %7D%0A        log.warning( %22No l
 ogger
-A
+ a
 gent
-Interface.class);%0A        %7D
+ url found!%22 );
 %0A   

Send create log calls async