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

import com.altamiracorp.lumify.core.user.User;
import com.altamiracorp.lumify.core.model.graph.GraphRepository;
import com.altamiracorp.lumify.core.model.graph.GraphVertex;
import com.altamiracorp.lumify.core.model.ontology.OntologyRepository;
import com.altamiracorp.lumify.web.BaseRequestHandler;
import com.altamiracorp.miniweb.HandlerChain;
import com.google.inject.Inject;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

public class VertexToVertexRelationship extends BaseRequestHandler {
    private final GraphRepository graphRepository;
    private final OntologyRepository ontologyRepository;

    @Inject
    public VertexToVertexRelationship(final OntologyRepository ontologyRepo, final GraphRepository graphRepo) {
        ontologyRepository = ontologyRepo;
        graphRepository = graphRepo;
    }

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, HandlerChain chain) throws Exception {
        final String source = getRequiredParameter(request, "source");
        final String target = getRequiredParameter(request, "target");
        final String label = getRequiredParameter(request, "label");

        User user = getUser(request);
        Map<String, String> properties = graphRepository.getEdgeProperties(source, target, label, user);
        GraphVertex sourceVertex = graphRepository.findVertex(source, user);
        GraphVertex targetVertex = graphRepository.findVertex(target, user);

        JSONObject results = new JSONObject();
        results.put("source", resultsToJson(source, sourceVertex));
        results.put("target", resultsToJson(target, targetVertex));

        JSONArray propertyJson = new JSONArray();
        for (Map.Entry<String, String> p : properties.entrySet()) {
            JSONObject property = new JSONObject();
            property.put("key", p.getKey());
            String displayName = ontologyRepository.getDisplayNameForLabel(p.getValue(), user);
            if (displayName == null) {
                property.put("value", p.getValue());
            } else {
                property.put("value", displayName);
            }
            propertyJson.put(property);
        }
        results.put("properties", propertyJson);

        respondWithJson(response, results);
    }

    private JSONObject resultsToJson(String id, GraphVertex graphVertex) throws JSONException {
        JSONObject json = new JSONObject();
        json.put("id", id);
        for (String property : graphVertex.getPropertyKeys()) {
            json.put(property, graphVertex.getProperty(property));
        }

        return json;
    }
}

@@ -96,134 +96,8 @@
 er;%0A
-import com.altamiracorp.lumify.core.model.graph.GraphRepository;%0Aimport com.altamiracorp.lumify.core.model.graph.GraphVertex;%0A
 impo
@@ -264,16 +264,55 @@
 rChain;%0A
+import com.altamiracorp.securegraph.*;%0A
 import c
@@ -516,16 +516,43 @@
 sponse;%0A
+import java.util.Iterator;%0A
 import j
@@ -662,34 +662,14 @@
 raph
-Repository graphRepository
+ graph
 ;%0A  
@@ -828,28 +828,14 @@
 raph
-Repository
  graph
-Repo
 ) %7B%0A
@@ -889,31 +889,26 @@
     
+this.
 graph
-Repository
  = graph
 Repo
@@ -903,20 +903,16 @@
  = graph
-Repo
 ;%0A    %7D%0A
@@ -1308,118 +1308,8 @@
     
-Map%3CString, String%3E properties = graphRepository.getEdgeProperties(source, target, label, user);%0A        Graph
 Vert
@@ -1327,39 +1327,28 @@
 rtex = graph
-Repository.find
+.get
 Vertex(sourc
@@ -1350,24 +1350,44 @@
 source, user
+.getAuthorizations()
 );%0A        G
@@ -1385,21 +1385,16 @@
         
-Graph
 Vertex t
@@ -1416,23 +1416,12 @@
 raph
-Repository.find
+.get
 Vert
@@ -1435,16 +1435,36 @@
 et, user
+.getAuthorizations()
 );%0A%0A    
@@ -1655,170 +1655,501 @@
     
-JSONArray propertyJson = new JSONArray();%0A        for (Map.Entry%3CString, String%3E p : properties.entrySet()) %7B%0A            JSONObject property = new JSONObjec
+Iterator%3CEdge%3E edges = sourceVertex.getEdges(targetVertex, Direction.IN, label, user.getAuthorizations()).iterator();%0A        JSONArray propertyJson = new JSONArray();%0A        while (edges.hasNext()) %7B%0A            Edge edge = edges.next();%0A            JSONObject property = new JSONObject();%0A            Iterator%3CProperty%3E propertyIterator = edge.getProperties().iterator();%0A            while (propertyIterator.hasNext()) %7B%0A                Property edgeProperty = propertyIterator.nex
 t();%0A
+    
     
@@ -2180,21 +2180,37 @@
 y%22, 
-p.getKey
+edgeProperty.getName
 ());%0A
+    
     
@@ -2280,26 +2280,48 @@
 orLabel(
-p.getValue
+edgeProperty.getValue().toString
 (), user
@@ -2335,16 +2335,20 @@
         
+   
+ 
 if (disp
@@ -2374,32 +2374,36 @@
                 
+    
 property.put(%22va
@@ -2408,17 +2408,28 @@
 value%22, 
-p
+edgeProperty
 .getValu
@@ -2446,16 +2446,20 @@
         
+    
 %7D else %7B
@@ -2459,16 +2459,20 @@
  else %7B%0A
+    
         
@@ -2519,34 +2519,42 @@
 e);%0A            
+   
+ 
 %7D%0A
+    
             prop
@@ -2577,16 +2577,30 @@
 perty);%0A
+            %7D%0A
         
@@ -2754,26 +2754,16 @@
 id, 
-Graph
 Vertex 
-graphV
+v
 erte
@@ -2872,63 +2872,164 @@
     
-for (String property : graphVertex.getPropertyKeys()) %7B
+Iterator%3CProperty%3E properties = vertex.getProperties().iterator();%0A        while (properties.hasNext()) %7B%0A            Property property = properties.next();
 %0A   
@@ -3058,16 +3058,21 @@
 erty
-, graphV
+.getName(), v
 erte
@@ -3084,16 +3084,21 @@
 Property
+Value
 (propert
@@ -3098,16 +3098,29 @@
 property
+.getName(), 0
 ));%0A    

fix vertex to vertex relationship