/*
 *  Copyright (C) 2011 The Animo Project
 *  http://animotron.org
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 3
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.animotron;

import java.util.List;

import org.animotron.instruction.ml.ELEMENT;
import org.junit.Test;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.index.IndexManager;
import org.neo4j.graphdb.index.RelationshipIndex;
import org.neo4j.kernel.EmbeddedGraphDatabase;


/**
 * @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
 *
 */
public class OrderTest {
	
	private static final String ORDER = "order"; 
	
	GraphDatabaseService graphDb;
	RelationshipIndex order;
	
	@Test
	public void orderedRelationships() {
		Node ROOT;
		
		graphDb = new EmbeddedGraphDatabase("data-tests");
		
		IndexManager index = graphDb.index();
		
		Transaction tx;
		
		if (false) {
			tx = graphDb.beginTx();
			try {
				order = index.forRelationships( ORDER );
		
				ROOT = graphDb.getReferenceNode();
				
				List<Node> children = createChild(ROOT, 10);
				
				tx.success();
			} finally {
				tx.finish();
			}
		}
		
		tx = graphDb.beginTx();
		try {
			ROOT = graphDb.getReferenceNode();
			order = index.forRelationships( ORDER );

			System.out.println("reading ...");
			for (Relationship r : order.query(ORDER+":*", ROOT, null) ) {
				System.out.println(r.getEndNode().getProperty(ORDER));
			}
		} finally {
			tx.finish();
		}
	}

	private List<Node> createChild(Node parent, int num) {
		
		for (int i = 1; i <= num; i++) {
			Node child = graphDb.createNode();
			child.setProperty(ORDER, i);
			
			Relationship r = parent.createRelationshipTo(child, RT.CHILD);
			
			order.add(r, ORDER, i);
		}

		return null;
	}

	enum RT implements RelationshipType {
		CHILD
	}
}

@@ -850,53 +850,8 @@
 t;%0A%0A
-import org.animotron.instruction.ml.ELEMENT;%0A
 impo
@@ -869,16 +869,16 @@
 t.Test;%0A
+
 import o
@@ -1067,16 +1067,58 @@
 action;%0A
+import org.neo4j.graphdb.index.IndexHits;%0A
 import o
@@ -1204,16 +1204,60 @@
 pIndex;%0A
+import org.neo4j.index.lucene.QueryContext;%0A
 import o
@@ -1884,10 +1884,10 @@
 OT, 
-10
+25
 );%0A%09
@@ -2110,21 +2110,26 @@
 .%22);%0A%09%09%09
-for (
+IndexHits%3C
 Relation
@@ -2136,12 +2136,13 @@
 ship
- r :
+%3E q =
  ord
@@ -2159,31 +2159,157 @@
 RDER
-+%22:*%22, ROOT, null) ) %7B%0A
+, new QueryContext( %22*%22 ).sort( ORDER ), ROOT, null);%0A%09%09%09try %7B%0A%09%09%09%09for (Relationship r : q ) %7B%0A%09%09%09%09%09%0A%09%09%09%09%09System.out.print(q.currentScore() + %22 %22);%0A%09
 %09%09%09%09
@@ -2359,17 +2359,58 @@
 ty(ORDER
-)
++%22-P%22));%0A%09%09%09%09%7D%0A%09%09%09%7D finally %7B%0A%09%09%09%09q.close(
 );%0A%09%09%09%7D%0A
@@ -2579,16 +2579,16 @@
 Node();%0A
-
 %09%09%09child
@@ -2601,24 +2601,29 @@
 operty(ORDER
++%22-P%22
 , i);%0A%09%09%09%0A%09%09

add sort