// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package nosql.orientdb.graph;

import com.orientechnologies.orient.core.db.ODatabaseSession;
import com.orientechnologies.orient.core.db.OrientDB;
import com.orientechnologies.orient.core.record.ODirection;
import com.orientechnologies.orient.core.record.OEdge;
import com.orientechnologies.orient.core.record.OVertex;
import com.orientechnologies.orient.core.sql.executor.OResultSet;
import com.orientechnologies.orient.core.sql.executor.OResult;
import com.orientechnologies.orient.core.id.ORID;

public class TestG {
	private static String vstring(OVertex v) {
		return String.format("%d (%s)", v.getProperty("id"), v.getProperty("text"));
	}
	private static void showConnects(OVertex v) {
		System.out.printf("Node %s is connected to:\n", vstring(v));
		for(OVertex vc : v.getVertices(ODirection.BOTH)) {
			System.out.printf("  Node %s\n", vstring(vc));
		}
	}
	private static void showConnects(ODatabaseSession db, int id) {
		OVertex v = (OVertex)db.query("SELECT FROM V WHERE id = ?", id).next().getRecord().get();
		showConnects(v);
	}
	private static void walk(ODatabaseSession db, OVertex v) {
        OResultSet rs = db.query("TRAVERSE out() FROM " + v.getIdentity());
        System.out.print("Walk: ");
        boolean first = true;
        while(rs.hasNext()) {
        	if(first) {
        		first = false;
        	} else {
        		System.out.print(" -> ");
        	}
        	OVertex vc = (OVertex)rs.next().getRecord().get();
        	System.out.print(vstring(vc));
        }
        System.out.println();
    	rs.close();
	}
	private static void walk(ODatabaseSession db, int id) {
		OVertex v = (OVertex)db.query("SELECT FROM V WHERE id = ?", id).next().getRecord().get();
		walk(db, v);
	}
	private static void findWay(ODatabaseSession db, OVertex vfrom, OVertex vto) {
        OResultSet rs = db.query("SELECT SHORTESTPATH(?,?) AS path UNWIND path", vfrom.getIdentity(), vto.getIdentity());
        System.out.print("Find: ");
        boolean first = true;
        while(rs.hasNext()) {
        	OResult r = rs.next();
        	ORID z = r.getProperty("path");
        	OVertex vc = db.load(z);
        	if(first) {
        		first = false;
        	} else {
        		System.out.print(" -> ");
        	}
        	System.out.print(vstring(vc));
        }
    	rs.close();
        System.out.println();
	}
	public static void main(String[] args) {
        // open
    	OrientDB client = new OrientDB("remote:localhost", null);
    	ODatabaseSession db = client.open("testg", "admin", "hemmeligt");
db.query("SELECT FROM V").forEachRemaining(o -> { o.getRecord().get().delete(); }); 
    	// put
    	OVertex v1 = db.newVertex("V");
    	v1.setProperty("id", 1);
    	v1.setProperty("text", "This is V1");
    	db.save(v1);
    	OVertex v2 = db.newVertex("V");
    	v2.setProperty("id", 2);
    	v2.setProperty("text", "This is V2");
    	db.save(v2);
    	OVertex v3 = db.newVertex("V");
    	v3.setProperty("id", 3);
    	v3.setProperty("text", "This is V3");
    	db.save(v3);
    	OVertex v4 = db.newVertex("V");
    	v4.setProperty("id", 4);
    	v4.setProperty("text", "This is V4");
    	db.save(v4);
    	OVertex v5 = db.newVertex("V");
    	v5.setProperty("id", 5);
    	v5.setProperty("text", "This is V5");
    	db.save(v5);
    	OVertex v6 = db.newVertex("V");
    	v6.setProperty("id", 6);
    	v6.setProperty("text", "This is V6");
    	db.save(v6);
    	OEdge e12 = db.newEdge(v1, v2);
    	db.save(e12);
    	OEdge e23 = db.newEdge(v2, v3);
    	db.save(e23);
    	OEdge e24 = db.newEdge(v2, v4);
    	db.save(e24);
    	OEdge e35 = db.newEdge(v3, v5);
    	db.save(e35);
    	OEdge e36 = db.newEdge(v3, v6);
    	db.save(e36);
    	OEdge e56 = db.newEdge(v5, v6);
    	db.save(e56);
    	// query connections
    	showConnects(v1);
    	showConnects(v2);
    	showConnects(v3);
    	showConnects(v4);
    	showConnects(v5);
    	showConnects(v6);
    	showConnects(db, 1);
    	showConnects(db, 2);
    	showConnects(db, 3);
    	showConnects(db, 4);
    	showConnects(db, 5);
    	showConnects(db, 6);
    	// traverse
    	walk(db, v1);
    	walk(db, v2);
    	walk(db, v3);
    	walk(db, v4);
    	walk(db, v5);
    	walk(db, v6);
    	walk(db, 1);
    	walk(db, 2);
    	walk(db, 3);
    	walk(db, 4);
    	walk(db, 5);
    	walk(db, 6);
    	// shortest path
    	findWay(db, v1, v2);
    	findWay(db, v1, v3);
    	findWay(db, v1, v4);
    	findWay(db, v1, v5);
    	findWay(db, v1, v6);
        // close
        db.close();
        client.close();
	}

}
