package com.networknt.light.server;

import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
import com.networknt.light.util.ServiceLocator;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.serialization.serializer.OJSONWriter;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;

import java.io.InputStream;
import java.lang.String;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by steve on 24/02/15.
 */
public class GraphTest {

    public static void main(String[] args) {

        String sql = "select @RID, id, content, out_Has as children, in_Has as parent from (traverse out('Has') from #11:0) where @class = 'Comment'";

        OrientGraphFactory factory = new OrientGraphFactory("plocal:/home/steve/lightdb").setupPool(1,10);

        OrientGraph graph = factory.getTx();
        try {
            //System.out.println(graph.getVertex("#11:0").getRecord().toJSON("rid, fetchPlan:*:-1"));

            //List<ODocument> result = graph.getRawGraph().query(
            //        new OSQLSynchQuery(sql));
            //System.out.println(OJSONWriter.listToJSON(result, "fetchPlan:children:-1"));
            System.out.println(getMenu(graph, "example"));
        } finally {
            graph.shutdown();
        }
    }

    protected static String getMenu(OrientGraph graph, String host) {
        String json = null;
        Map<String, Object> menuMap = (Map<String, Object>) ServiceLocator.getInstance().getMemoryImage("menuMap");
        ConcurrentMap<Object, Object> cache = (ConcurrentMap<Object, Object>)menuMap.get("cache");
        if(cache != null) {
            json = (String)cache.get(host);
        }
        if(json == null) {
            Vertex menu = graph.getVertexByKey("Menu.host", host);
            if(menu != null) {
                json = ((OrientVertex)menu).getRecord().toJSON("rid,fetchPlan:out_Own.in_Create:-2 out_Own:2");
            }
            if(json != null) {
                if(cache == null) {
                    cache = new ConcurrentLinkedHashMap.Builder<Object, Object>()
                            .maximumWeightedCapacity(1000)
                            .build();
                    menuMap.put("cache", cache);
                }
                cache.put(host, json);
            }
        }
        return json;
    }

}

@@ -638,16 +638,48 @@
 tVertex;
+%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -943,16 +943,101 @@
 phTest %7B
+%0A    static final org.slf4j.Logger logger = LoggerFactory.getLogger(GraphTest.class);
 %0A%0A    pu
@@ -1496,16 +1496,157 @@
 *:-1%22));
+%0A            String result = graph.getVertex(%22#37:0%22).getRecord().toJSON(%22rid,fetchPlan:out_Own:-1%22);%0A            System.out.println(result);
 %0A%0A      
@@ -1848,32 +1848,34 @@
 ));%0A            
+//
 System.out.print
@@ -1901,24 +1901,90 @@
 example%22));%0A
+            //System.out.println(getBfnTree(%22forum%22, %22example%22));%0A
         %7D fi
@@ -3036,16 +3036,16 @@
  json);%0A
-
         
@@ -3088,10 +3088,791 @@
 %0A    %7D%0A%0A
+    protected static String getBfnTree(String bfnType, String host) %7B%0A        String json = null;%0A        String sql = %22SELECT FROM %22 + bfnType + %22 WHERE host = ? and in_Own IS NULL ORDER BY id%22;%0A        OrientGraph graph = ServiceLocator.getInstance().getGraph();%0A        try %7B%0A            OSQLSynchQuery%3CODocument%3E query = new OSQLSynchQuery%3CODocument%3E(sql);%0A            List%3CODocument%3E docs = graph.getRawGraph().command(query).execute(host);%0A            if(docs.size() %3E 0) %7B%0A                json = OJSONWriter.listToJSON(docs, %22rid,fetchPlan:out_Own.in_Create:-2 out_Own.out_Create:-2 out_Own:-1%22);%0A            %7D%0A        %7D catch (Exception e) %7B%0A            logger.error(%22Exception:%22, e);%0A        %7D finally %7B%0A            graph.shutdown();%0A        %7D%0A        return json;%0A    %7D%0A%0A
 %7D%0A

update for graphtest