// TestExtractGFF.java
//
//    Created: May 2006
//
// This file is a component of the BioMoby project.
// Copyright Martin Senger (martin.senger@gmail.com).
//

package org.jmoby.tutorial.client;

import org.biomoby.client.BaseClient;
import org.biomoby.shared.MobyException;
import org.biomoby.shared.MobyService;
import org.biomoby.shared.parser.MobyPackage;
import org.biomoby.shared.parser.MobyJob;
import org.biomoby.shared.datatypes.MobyObject;
import org.biomoby.client.MobyServiceLocator;
import org.biomoby.shared.datatypes.*;

/**
 * This is an example of a client. It calls a Biomoby service
 * "getInsertionsAsGFFByAGICode" it uses generated data types and it
 * benefits from various features provided by the BaseClient
 * class. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: TestExtractGFF.java,v 1.1 2006/05/05 21:11:46 senger Exp $
 */

public class TestExtractGFF
    extends BaseClient {

    // from the command-line
    String serviceEndpoint;
    MobyObject input;

    /**************************************************************************
     * Constructor. It expects arguments: AGI_Locus_code [service-endpoint].
     *************************************************************************/
    public TestExtractGFF (String[] args) {
	input = new MobyObject();
	String agiLocusCode = "At1g44446";
	if (args.length > 0)
	    agiLocusCode = args[0];
	input.setId (agiLocusCode);
	input.setNamespace ("AGI_LocusCode");

	if (args.length > 1)
	    serviceEndpoint = args[1];
    }

    /**************************************************************************
     *
     *************************************************************************/
    public MobyServiceLocator getServiceLocator()
	throws MobyException {
	MobyService service = new MobyService ("getInsertionsAsGFFByAGICode");
	if (serviceEndpoint != null)
	    service.setURL (serviceEndpoint);
	return new MobyServiceLocator (service);
    }

    /**************************************************************************
     *
     *************************************************************************/
    public boolean fillRequest (MobyJob request, MobyPackage inputContext)
	throws MobyException {
	request.setData (input);
	return true;
    }

    /**************************************************************************
     *
     *************************************************************************/
    public boolean useResponse (MobyJob response,
				MobyPackage responseContext)
	throws MobyException {

 	MobyObject[] gff = response.getDataSet ("gff");
 	for (int i = 0; i < gff.length; i++) {
	    BasicGFFSequenceFeature feature = (BasicGFFSequenceFeature)gff[i];
	    System.out.println ("\n" + feature.getId() + "\t" + feature.getNamespace());
	    System.out.println ("Frame:     " + feature.get_frame());
	    System.out.println ("Source:    " + feature.get_source());
	    System.out.println ("Method:    " + feature.get_method());
	    System.out.println ("Reference: " + feature.get_reference());
	    System.out.println ("Strand:    " + feature.get_strand());
	    System.out.println ("Score:     " + feature.getMoby_score().getValue());
	    System.out.println ("Start:     " + feature.getMoby_start().getValue());
	    System.out.println ("Stop:      " + feature.getMoby_stop().getValue());
	    multi_key_value_pair[] pairs = feature.getMoby_column9_tag_value();
	    for (int j = 0; j < pairs.length; j++) {
		System.out.print (pairs[j].get_key());
		String[] values = pairs[j].get_the_value();
		for (int k = 0; k < values.length; k++)
		    System.out.print ("\t" + values[k]);
		System.out.println();
	    }
	}
	return true;
    }

    /**************************************************************************
     *
     *************************************************************************/
    public static void main (String [] args) {
	if (args.length > 2) {
	    System.err.println ("Usage: java TestExtractGFF [<AGI_LocusCode> [<service-endpoint>]]\n");
	    System.exit (1);
	}
	TestExtractGFF client = new TestExtractGFF (args);
	try {
	    client.process();
	} catch (MobyException e) {
	    System.err.println (e.getMessage());
	    System.exit (1);
	}
    }

}
