package rubysoapservices;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.net.URL;

import nl.aidministrator.rdf.client.*;

import java.io.*;
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.util.*;

import nl.aidministrator.rdf.client.model.*;
import nl.aidministrator.rdf.client.query.*;
import nl.aidministrator.rdf.client.repositorylist.*;
import nl.aidministrator.rdf.client.admin.*;
import nl.aidministrator.util.http.*;


import org.xml.sax.*;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 
 * @author Nikki Rogers (nikki.rogers@bristol.ac.uk)

 Call this class from the commandline e.g. C:\Program Files\axis-1_1RC1>java rubysoapservices/SesameTestStandalone
 It issues a hardcoded query against the sesame database "rubyInfodb" in which you can upload data 
 in order to test we can soap<->rdf roundtrip
 */
public class SesameTestStandalone {

	public static void main(String[] args)
		throws Exception
      
	{
			
		System.setProperty(
			"org.xml.sax.driver", "org.apache.xerces.parsers.SAXParser");

		// private SesRecord result_bean;
		
		URL sesameURL = new URL("http://localhost:8080/sesame/");
		SesameClient client = new SesameClient(sesameURL);
		System.out.println("here goes ... please wait ...");	
		// uncomment these lines if using authentication
		// client.setUserName("testuser");
		// client.setPassword("opensesame");
		
		// because this is a test, hardcode the query
		String query = "select X, PRODUCT_NAME, VERSION, STATUS, HOMEPAGE, DOWNLOAD, LICENSE, DESC, EMAIL,OWNER_NAME, OWNER_ID, CATEGORY_MAJOR, CATEGORY_MINOR from {X} ruby:ruby_product . ruby:product_name {PRODUCT_NAME} , {X} ruby:ruby_product . ruby:version {VERSION} ,{X} ruby:ruby_product . ruby:status {STATUS}, {X} ruby:ruby_product . ruby:homepage {HOMEPAGE}, {X} ruby:ruby_product . ruby:download {DOWNLOAD}, {X} ruby:ruby_product . ruby:license {LICENSE}, {X} ruby:ruby_product . ruby:desc {DESC}, {X} ruby:ruby_owner . ruby:email {EMAIL} , {X} ruby:ruby_owner . ruby:owner_name {OWNER_NAME} , {X} ruby:ruby_owner . ruby:owner_id {OWNER_ID} , {X} ruby:ruby_category  . ruby:category_major {CATEGORY_MAJOR} , {X} ruby:ruby_category  . ruby:category_minor {CATEGORY_MINOR} using namespace ruby = http://example.ruby-language.org/rda-vocab/test2.rdf#";
		
		
		// Note that the value "mysql-rubyInfo" has a corresponding entry in the config file for sesame
		QueryResultsTable queryanswer = client.evalRqlQuery(query, "mysql-rubyInfo");
		
		int rowcount = queryanswer.getRowCount();
		
		RubyInfo [] result_array = new RubyInfo [rowcount];
		
		for (int row = 0; row < rowcount; row++) {
			
			RubyInfo result_bean = new RubyInfo();
			Product product_bean = new Product();
			Owner owner_bean = new Owner();
			Category category_bean = new Category();
			
			for (int col = 0; col < queryanswer.getColumnCount(); col++) {
				Value v = queryanswer.getValue(row, col);
										
				switch (col) {
					
					case 0:  
					// begin to fill the Product bean
					product_bean.setProduct_Name(v.toString()); 
					System.out.print(product_bean.getProduct_Name() + "\t");
					break;
					// carry on filling the Product bean ...
					case 1:  product_bean.setVersion(v.toString());
					System.out.print(product_bean.getVersion() + "\t");
					break;
					// carry on filling the Product bean ...
					case 2:  product_bean.setStatus(v.toString()); 
					System.out.print(product_bean.getStatus() + "\t");
					break;
					// carry on filling the Product bean ...
					case 3:  product_bean.setHomepage(v.toString()); 
					System.out.print(product_bean.getHomepage() + "\t");
					break;
					case 4:  product_bean.setDownload(v.toString()); 
					System.out.print(product_bean.getDownload() + "\t");
					break;
					case 5:  product_bean.setLicense(v.toString()); 
					System.out.print(product_bean.getLicense() + "\t");
					break;
					case 6:  product_bean.setDesc(v.toString()); 
					System.out.print(product_bean.getDesc() + "\t");
					break;
					case 7:  owner_bean.setEmail(v.toString()); 
					System.out.print(owner_bean.getEmail() + "\t");
					break;
					case 8:  owner_bean.setOwner_Name(v.toString()); 
					System.out.print(owner_bean.getOwner_Name() + "\t");
					break;
					case 9:  owner_bean.setOwner_Id(v.toString()); 
					System.out.print(owner_bean.getOwner_Id() + "\t");
					break;
					case 10:  category_bean.setCategory_Major(v.toString()); 
					System.out.print(category_bean.getCategory_Major() + "\t");
					break;
					case 11:  category_bean.setCategory_Minor(v.toString()); 
					System.out.print(category_bean.getCategory_Minor() + "\t");
					break;
					// should add the update field in here
				}	
			}
		
			// now add the nested beans to the result_bean (which is of type Ruby_info)
			result_bean.setRuby_Product(product_bean);
			result_bean.setRuby_Category(category_bean);
			result_bean.setRuby_Owner(owner_bean);
			// ok, we should have filled up our bean by now, so lets add it to the array of beans	
			result_array[row] = result_bean;
				
		}

		System.out.print("FINISHED - all beans should be full now!");
		
	}
		
			
	

}