/*
 * The Apache Software License, Version 1.1
 *
 *
 * Copyright (c) 2001 The Apache Software Foundation.  All rights 
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:  
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "AXIS" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written 
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * .
 */

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)
 */
public class RubySoapServiceTest {

	
       public RubyInfo[] rubyQuery(String query)
	throws Exception
	{
		System.setProperty(
			"org.xml.sax.driver", "org.apache.xerces.parsers.SAXParser");

		URL sesameURL = new URL("http://localhost:8081/sesame/");
		SesameClient client = new SesameClient(sesameURL);
	
		// uncomment these lines if using authentication
		// client.setUserName("testuser");
		// client.setPassword("opensesame");
		
		//
		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);
				
				// need to trim the first and last characters of each 
				// value returned because sesame adds extra pairs of quotations for all strings
				StringBuffer sb = new StringBuffer(v.toString());
				
				if (sb.charAt(0) == '"') {
					sb.deleteCharAt(0);
				}
				if (sb.charAt(sb.length() - 1) == '"') {
					sb.deleteCharAt(sb.length() - 1);
				}
					
				switch (col) {
					
					case 0:  
					// begin to fill the Product bean
					product_bean.setProduct_Name(sb.toString()); 
					System.out.print(product_bean.getProduct_Name() + "\t");
					break;
					// carry on filling the Product bean ...
					case 1:  product_bean.setVersion(sb.toString());
					System.out.print(product_bean.getVersion() + "\t");
					break;
					// carry on filling the Product bean ...
					case 2:  product_bean.setStatus(sb.toString()); 
					System.out.print(product_bean.getStatus() + "\t");
					break;
					// carry on filling the Product bean ...
					case 3:  product_bean.setHomepage(sb.toString()); 
					System.out.print(product_bean.getHomepage() + "\t");
					break;
					case 4:  product_bean.setDownload(sb.toString()); 
					System.out.print(product_bean.getDownload() + "\t");
					break;
					case 5:  product_bean.setLicense(sb.toString()); 
					System.out.print(product_bean.getLicense() + "\t");
					break;
					case 6:  product_bean.setDesc(sb.toString()); 
					System.out.print(product_bean.getDesc() + "\t");
					break;
					case 7:  owner_bean.setEmail(sb.toString()); 
					System.out.print(owner_bean.getEmail() + "\t");
					break;
					case 8:  owner_bean.setOwner_Name(sb.toString()); 
					System.out.print(owner_bean.getOwner_Name() + "\t");
					break;
					case 9:  owner_bean.setOwner_Id(sb.toString()); 
					System.out.print(owner_bean.getOwner_Id() + "\t");
					break;
					case 10:  category_bean.setCategory_Major(sb.toString()); 
					System.out.print(category_bean.getCategory_Major() + "\t");
					break;
					case 11:  category_bean.setCategory_Minor(sb.toString()); 
					System.out.print(category_bean.getCategory_Minor() + "\t");
					break;
					
				}	
				
			    
				
			}
			
			// 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;
				
		}


			return result_array;		
	}
		
		
		
		
}