// MabuhayClient.java
//
//    Created: August 2005
//
// 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 Mabuhay,
 * it uses generated data types and it benefits from various features
 * provided by the BaseClient class. Because it is an example how to
 * code such client, it intentionally has a very limited command-line
 * options. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: MabuhayClient.java,v 1.2 2006/02/12 18:47:40 senger Exp $
 */

public class MabuhayClient
    extends BaseClient {

    // from the command-line
    String serviceEndpoint;
    Regex regex;

    /**************************************************************************
     * Constructor. It expects arguments: language [service-endpoint].
     *************************************************************************/
    public MabuhayClient (String[] args) {
	regex = new Regex();
	regex.set_regex (args[0]);
	if (args.length > 1)
	    serviceEndpoint = args[1];
    }

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

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

    /**************************************************************************
     *
     *************************************************************************/
    public boolean useResponse (MobyJob response,
				MobyPackage responseContext)
	throws MobyException {
	MobyObject[] hellos = response.getDataSet ("hello");
	for (int i = 0; i < hellos.length; i++)
	    System.out.println ("(" + (i+1) + ") " + hellos[i].toString());
	return true;
    }

    /**************************************************************************
     *
     *************************************************************************/
    public static void main (String [] args) {
	if (args.length == 0) {
	    System.err.println ("Usage: java MabuhayClient <language> [<service-endpoint>]\n" +
				"where <language> is a regular expression defining a languge.");
	    System.exit (1);
	}
	MabuhayClient client = new MabuhayClient (args);
	try {
	    client.process();
	} catch (MobyException e) {
	    System.err.println (e.getMessage());
	    System.exit (1);
	}
    }

}
