package eu.dnetlib.efg1914.commons;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.JAXBException;
import javax.xml.xquery.XQException;

import org.apache.log4j.BasicConfigurator;
import org.junit.Before;
import org.junit.Test;

import eu.dnetlib.efg1914.commons.domain.XMLResource;
import eu.dnetlib.efg1914.commons.store.SednaStore;
import eu.dnetlib.efg1914.commons.store.XMLResourceBinder;
import eu.dnetlib.efg1914.commons.store.XMLStoreException;
import eu.dnetlib.efg1914.commons.store.XQueryGenerator;

public class SednaStoreTest {
	SednaStore st= new SednaStore();
	XMLResourceBinder<XMLResource> binder;
	XQueryGenerator<XMLResource> xQueryGenerator;

	@Before
	public void before() throws XMLStoreException, JAXBException {
		BasicConfigurator.configure();
		// TODO fix this(String dbHost, String dbUser, String dbPassword,
		// String dbName)
		 st.init();
		binder = new XMLResourceBinder<XMLResource>(XMLResource.class);
		xQueryGenerator = new XQueryGenerator<XMLResource>(XMLResource.class);

		// st.setBinder(binder);
		// st.setxQueryGenerator(xQueryGenerator);

		/*
		 * st.createCollection("item"); // st.createCollection("frameitem");
		 * st.createCollection("frame"); st.createCollection("topic");
		 * st.createCollection("theme"); st.createCollection("user");
		 * st.createCollection("role");
		 */
	}
	@Test
	public void index_test() throws XMLStoreException {
		 
		String xQuery1 = "for $theme in collection('theme') "
				+ " where  where $theme/theme/lastModified > 01.01.2013  "
	 			+ " return $theme";
		
		  Timestamp stamp = new Timestamp(System.currentTimeMillis());
		  Date date = new Date(stamp.getTime());
		  System.out.println(date);
		  
		  
		  Date dNow = new Date( );
		      SimpleDateFormat ft = 
		      new SimpleDateFormat ("dd.MM.yyyy hh:mm:ss ");

		    System.out.println("Current Date: " + ft.format(dNow));
		    
		    
//		    dd-MM-yyyy"
		        
		st.executeCommand(xQuery1);
		

	}
	

	@Test
	public void test() throws XMLStoreException, XQException, JAXBException {

		/*
		 * String xQuery = "for $item in collection('item') " + "return $item";
		 * 
		 * String xQuery2 =
		 * "for $frame in collection('frame')//frame[@id='18de0322-406b-4100-bfc4-5c8c20dde5b3'] "
		 * +
		 * "return <frame> {$frame/title} {$frame/description}  <frameItems>  {  "
		 * + "for $frameItem in $frame/frameItems/frameitem " +
		 * "return collection('frameitem')//frameitem[@id=$frameItem/@id] " +
		 * "}</frameItems>  </frame>";
		 * 
		 * String xQuery3 =
		 * "for $topic in collection('topic')//topic[@id='f2fc9b2d-3018-4d16-97c8-99b549780894'] "
		 * + "return <topic> {$topic/title} {$topic/description}  <frames> {" +
		 * "for $frame in $topic/frames/frame " +
		 * "return <frame> {$frame/title} {$frame/description}  <frameItems>  {  "
		 * + "for $frameItem in $frame/frameitems/frameitem " +
		 * "return collection('frameitem')//frameitem[@id=$frameItem/@id] " +
		 * 
		 * "}</frameItems>  </frame>" + "} </frames> </topic>";
		 */
		String xQuery4 = "for $theme in collection('theme')//theme[@id='"
				+ "8ab8abd6-b18c-4844-bcbd-72e6d9eea3d7"
				+ "'] "
				+ "return <theme> {$theme/title} {$theme/description} {$theme/cssClass} <topics> { "
				+

				"for $topic in collection('topic')//topic[@id=$theme/topics/topic/@id]"
				+ " return <topic> {$topic/title} {$topic/description} <frames> { "
				+

				" for $frame in collection('frame')//frame[@id=$topic/frames/frame/@id] "
				+ " return <frame> {$frame/@id} {$frame/title} {$frame/description} {$frame/template} <frameitems> { "
				+

				"for $item in collection('item')//item[@id=$frame/items/item/@id] "
				+ "return <frameitem> {$item/@type} {$item/thumbnail} {$item/source} </frameitem>"
				+

				"} </frameitems> </frame>" + " } </frames> </topic> "
				+ " } </topics> </theme>";
		/*
		 * String xQuery5 ="for $theme in collection('theme')" +
		 * "return <collection> {$theme/theme/@id} {$theme//title} {$theme//description} {$theme//thumbnail} </collection>"
		 * ;
		 */
		String xQuery1 = "for $theme in collection('theme')//theme"

		+ " return $theme";

		// st.getSearchResults(xQuery1);
		System.out.println("st.getSearchResults(xQuery1)"
				+ st.getSearchResults(xQuery1));

		
		
		
		String xQuery6 = "for $frame in collection('frame')//frame[@id='"
				+ "0ad993b8-26c2-4783-a330-07e8d074f438" + "'] "
				+ " return $frame";

		String xQuery1821 = "for $item in collection('item')//item[@id='"
				+ "d2565ee1-61bd-4a32-bf24-1bee39a6abb5" + "'] "
				+ " return $item";

		String xQuery18 = "for $topic in collection('topic')//topic[@id='"
				+ "65b91ea8-1a6a-4746-b87f-6799946dd74b" + "'] "
				+ " return $topic";

		/*
		 * ""<frame> {$frame/@id} {$frame/title} {$frame/description}
		 * {$frame/template} <frameitems> { " +
		 * 
		 * "for $frameItem in collection('frameitem')//frameitem[@id=$frame/frameitems/frameitem/@id] "
		 * +
		 * "return <frameitem> {$frameItem/@type} {$frameItem/thumbnail} {$frameItem/source} "
		 * + " {$frameItem} </frameitem>" +
		 * 
		 * "} </frameitems> </frame>"; /*
		 * 
		 * String xQuery7 =
		 * "for $frameitem in collection('frameitem'), $frame in collection('frame'),"
		 * + " $topic in collection('topic'), $theme in collection('theme')" +
		 * " where $theme/theme/topics/topic/@id = $topic/topic/@id" +
		 * " and $topic/topic/frames/frame/@id = $frame/frame/@id " +
		 * " and $frame/frame/frameitems/frameitem/@id = $frameitem/frameitem/@id "
		 * + " and $frameitem/frameitem/itemId = '" +
		 * "4866b629-d5f2-46a9-a62b-0cfd51e10110" + "'" +
		 * " return <result> {$frameitem} <exhibition>{$theme/theme/@id}{$theme/theme/title}</exhibition></result>"
		 * ;
		 */

		/*
		 * String xQuery7 =
		 * "for $frameitem in collection('frameitem'), $frame in collection('frame'),"
		 * + " $topic in collection('topic'), $theme in collection('theme')" +
		 * " where $theme/theme/topics/topic/@id = $topic/topic/@id" +
		 * " and $topic/topic/frames/frame/@id = $frame/frame/@id " +
		 * " and $frame/frame/frameitems/frameitem/@id = $frameitem/frameitem/@id "
		 * + " and $frameitem/frameitem/itemId = '" +
		 * "988d8871-ddf0-44fb-83bc-fa3f39721b7a" + "'" +
		 * " return <result> {$frameitem} <exhibition>{$theme/theme/@id}{$theme/theme/title}</exhibition></result>"
		 * ; //System.out.println(xQuery7); //
		 * "return <collection> {$theme/header/@id} {$theme//title} {$theme//description} {$theme//thumbnail} </collection>"
		 * ;
		 * 
		 * xQuery = "for $frame in collection('frame')" + "return $frame";
		 * 
		 * System.out.println(st.evaluateXQuery(xQuery1));
		 * System.out.println("eval: " + st.evaluateXQuery(xQuery6));
		 * System.out.println("eval: " + st.evaluateXQuery(xQuery18));
		 * System.out.println("eval: " + st.evaluateXQuery(xQuery1821));
		 */

		// st.getById("cff31350-cf99-46eb-bab7-338c7e037dd0");
		/*
		 * LinkedHashMap<String, String> Bindings = new LinkedHashMap<String,
		 * String>();
		 * 
		 * Bindings.put("user_name", "John");
		 */
		// st.getSearchResults(query, Bindings);

	}
}
