import java.net.URL;
import java.util.Collection;
import java.util.Iterator;

import org.egso.common.context.EGSOContext;
import org.egso.consumer.EGSOLiterals;
import org.egso.consumer.api.Data;
import org.egso.consumer.api.Query;
import org.egso.consumer.api.async.Session;
import org.egso.consumer.wsclient.WSQuery;
import org.egso.consumer.wsclient.session.WSSessionFactory;

/*
 * MostSimpleMain.java
 * Created on Jan 20, 2005
 */

/**
 * 
 * 
 * @author Philipp Kunz
 * @version Feb 8, 2005-PK
 */
public class egsotest implements EGSOLiterals
{
    public static void main(String[] args) throws Exception
    {
    	URL endpoint;
    	if (args.length == 1) endpoint = new URL(args[0]);
    	else endpoint = new URL("http://147.86.108.123:8080/egso-ws/query");
        Session s = new WSSessionFactory(endpoint).login(null);
        
        Query q = s.newQuery();
        String id = q.id();
        System.out.println("query id = " + id);
        System.out.println("all param and result names = " + q.findAll());
        System.out.println("param names only = " + q.findAllParameters());
        System.out.println("result names only = " + q.findAllResults());
        
        Collection allParamNames = q.findAll();
        Iterator it = allParamNames.iterator();
        while (it.hasNext()) {
			String pName = (String) it.next();
			System.out.println(pName + " is affected by " + q.affectedBy(pName));
			System.out.println(pName + " affects " + q.affects(pName));
			System.out.println(pName + " requires " + q.requires(pName));
			System.out.println(pName + " is " + (q.readonly(pName) ? "" : "not ") + "readonly.");
			System.out.println("parameter " + pName + " is " + (q.isSet(pName) ? "" : "not ") + "set");
			// System.out.println("unset parameter " + pName); q.unset(pName);
		}
        
        Collection allCatNames = q.catalogues();
        it = allCatNames.iterator();
        while (it.hasNext()) {
			String cName = (String) it.next();
			System.out.println("catalogue " + cName + " contains parameters " + q.findByCatalogue(cName));
		}
        
        q.setValue(DATE, new String[] {"2003-06-13 11:00:00", "2003-06-17 11:00:00"});
        // should be the same as
        Data date = q.get(DATE);
        date.setValue(new String[] {"2003-06-13 11:00:00", "2003-06-17 11:00:00"});
        q.set(DATE, date);
        
        Data flares = q.getResult(GOES_XRAY_FLARE);
        EGSOContext ctx = q.getContext(null); System.out.println("context was:\n" + ctx + "\n");
        String src = q.getQuerySource(null); System.out.println("query source code was:\n" + src + "\n");
        
        System.out.println("number of events = " + flares.size());
        System.out.println("flare result list is " + (flares.readonly() ? "" : "not ") + "readonly.");
        System.out.println("there is " + (flares.contains("3") ? "a" : "no") + " third event.");
        System.out.print  ("make a copy of the list... "); flares.clone(); System.out.println("done.");
        System.out.println("the contents of the list prettyprinted:\n"); flares.prettyPrint(System.out);
        
        Data flareselect = q.get(GOES_XRAY_FLARE);
        flareselect.add(flares.get("2"));
        flareselect.add(flares.get(1));
        flareselect.remove(flareselect.get(0));
        q.set(GOES_XRAY_FLARE, flareselect);
        
        
        
        ((WSQuery) q).dispose();
    }
}
