package com.altamiracorp.lumify.web.routes.entity;

import com.altamiracorp.lumify.core.user.User;
import com.altamiracorp.lumify.core.util.LumifyLogger;
import com.altamiracorp.lumify.core.util.LumifyLoggerFactory;
import com.altamiracorp.lumify.web.BaseRequestHandler;
import com.altamiracorp.miniweb.HandlerChain;
import com.altamiracorp.securegraph.*;
import com.google.inject.Inject;
import org.json.JSONArray;
import org.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

public class EntityRelationships extends BaseRequestHandler {
    private static final LumifyLogger LOGGER = LumifyLoggerFactory.getLogger(EntityRelationships.class);
    private final Graph graph;

    @Inject
    public EntityRelationships(final Graph graph) {
        this.graph = graph;
    }

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, HandlerChain chain) throws Exception {
        User user = getUser(request);

        long startTime = System.nanoTime();

        String[] ids = request.getParameterValues("ids[]");
        if (ids == null) {
            ids = new String[0];
        }

        List<String> allIds = new ArrayList<String>();
        for (int i = 0; i < ids.length; i++) {
            allIds.add(ids[i]);
        }

        JSONArray resultsJson = new JSONArray();

        Collection<Edge> edges = getAllEdges(allIds, user.getAuthorizations());
        for (Edge edge : edges) {
            JSONObject rel = new JSONObject();
            rel.put("from", edge.getVertexId(Direction.OUT));
            rel.put("to", edge.getVertexId(Direction.IN));
            rel.put("relationshipType", edge.getLabel());
            rel.put("id", edge.getId());
            resultsJson.put(rel);
        }

        long endTime = System.nanoTime();
        LOGGER.debug("Retrieved %d in %dms", edges.size(), (endTime - startTime) / 1000 / 1000);

        respondWithJson(response, resultsJson);
    }

    private Collection<Edge> getAllEdges(List<String> allVertexIds, Authorizations authorizations) {
        Set<Edge> results = new HashSet<Edge>();
        Map<String, Vertex> vertices = toVertices(allVertexIds, authorizations);

        for (Vertex sourceVertex : vertices.values()) {
            for (Vertex destVertex : vertices.values()) {
                Iterable<Edge> edges = sourceVertex.getEdges(destVertex, Direction.BOTH, authorizations);
                for (Edge edge : edges) {
                    results.add(edge);
                }
            }
        }
        return results;
    }

    private Map<String, Vertex> toVertices(List<String> allVertexIds, Authorizations authorizations) {
        Map<String, Vertex> vertices = new HashMap<String, Vertex>();
        for (String vertexId : allVertexIds) {
            vertices.put(vertexId, graph.getVertex(vertexId, authorizations));
        }
        return vertices;
    }
}

@@ -552,16 +552,88 @@
 til.*;%0A%0A
+import static com.altamiracorp.lumify.core.util.CollectionUtil.toList;%0A%0A
 public c
@@ -1280,30 +1280,30 @@
        List%3C
-String
+Object
 %3E allIds = n
@@ -1315,22 +1315,22 @@
 rayList%3C
-String
+Object
 %3E();%0A   
@@ -2125,38 +2125,38 @@
 etAllEdges(List%3C
-String
+Object
 %3E allVertexIds, 
@@ -2241,36 +2241,29 @@
 ();%0A        
-Map%3CString, 
+List%3C
 Vertex%3E vert
@@ -2271,16 +2271,30 @@
 ces = to
+List(graph.get
 Vertices
@@ -2323,16 +2323,17 @@
 zations)
+)
 ;%0A%0A     
@@ -2366,33 +2366,24 @@
 x : vertices
-.values()
 ) %7B%0A        
@@ -2423,17 +2423,8 @@
 ices
-.values()
 ) %7B%0A
@@ -2685,348 +2685,7 @@
    %7D
-%0A%0A    private Map%3CString, Vertex%3E toVertices(List%3CString%3E allVertexIds, Authorizations authorizations) %7B%0A        Map%3CString, Vertex%3E vertices = new HashMap%3CString, Vertex%3E();%0A        for (String vertexId : allVertexIds) %7B%0A            vertices.put(vertexId, graph.getVertex(vertexId, authorizations));%0A        %7D%0A        return vertices;%0A    %7D
 %0A%7D%0A

use new securegraph batch gets