// UnitTestingServiceCallermodel.java
//
// Created: February 2006
//
// This file is a component of the BioMoby project.
// Copyright Edward Kawas (edward.kawas@gmail.com).
//

package org.biomoby.service.dashboard;

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.client.ExtendedProtocolClient;
import org.biomoby.client.MobyServiceLocator;
import org.biomoby.client.ExtendedServiceLocator;
import org.biomoby.service.dashboard.data.DataContainer;

import org.tulsoft.shared.UUtils;

/**
 * A model that achieves its task by calling Biomoby service. It delegates the
 * task of calling a service to a true Biomoby client (that extends
 * <tt>BaseClient</tt>).
 * <p>
 * This Class uses the ServiceCallerModel as a template and makes slight
 * modifications to it.
 * 
 * @author Edward Kawas
 * @author Martin Senger
 * @version $Id: ServiceCallerModel.java,v 1.6 2008/05/14 20:37:10 walexander
 *          Exp $
 */

public class UnitTestingServiceCallermodel extends AbstractModel {

    // private static org.apache.commons.logging.Log log =
    // org.apache.commons.logging.LogFactory.getLog (ServiceCallerModel.class);

    /***************************************************************************
     * Call a service. This is the main purpose of this model.
     * <p>
     * 
     * @param data
     *                contains input data and this method replaces them by
     *                result data
     **************************************************************************/
    public void runIt(DataContainer data) throws MobyException {
	SimpleClient worker = new SimpleClient(data);
	worker.process();
    }

    /***************************************************************************
     * 
     * A real client - the main worker for this model..
     * 
     **************************************************************************/
    protected class SimpleClient extends ExtendedProtocolClient {

	DataContainer data;

	/***********************************************************************
	 * Constructor.
	 **********************************************************************/
	public SimpleClient(DataContainer data) {
	    super();
	    this.data = data;
	}

	/***********************************************************************
	 * What service to call and where to find it.
	 **********************************************************************/
	public MobyServiceLocator getServiceLocator() throws MobyException {
	    return new MyServiceLocator();
	}

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

	/***********************************************************************
	 * Not used here...
	 **********************************************************************/
	public boolean useResponse(MobyJob response, MobyPackage responseContext)
		throws MobyException {
	    return true;
	}

	/***********************************************************************
	 * Return input XML (from a data container obtained in the constructor).
	 **********************************************************************/
	public String fillRequest() throws MobyException {

	    if (data == null)
		throw new MobyException("No input data given.");

	    return (String) data.getData();
	}

	/***********************************************************************
	 * 
	 **********************************************************************/
	public boolean useResponse(String xmlResponse) throws MobyException {

	    data.setData(xmlResponse);

	    // do nothing more if it is just an input echo
	    if (((ExtendedServiceLocator) getServiceLocator()).isLoop())
		return false;

	    return false;
	}
    }

    /***************************************************************************
     * 
     * A service locator filled from the property channel.
     * 
     **************************************************************************/
    protected class MyServiceLocator extends ExtendedServiceLocator {

	public MyServiceLocator() throws MobyException {
	    super();

	    // fill this locator by a service
	    MobyService selService = (MobyService) propertyChannel
		    .get(MobyUnitTestingPanel.UT_SC_SERVICE);
	    if (selService == null)
		throw new MobyException("No service given.");
	    MobyService clonedService = new MobyService(selService.getName(),
		    selService.getAuthority());
	    clonedService.setURL(selService.getURL());
	    setService(clonedService);

	    // fill how to call this service
	    String howToCall = propertyChannel.getString(DP_CALL_SERVICE);
	    if (DP_CS_NEWURL.equals(howToCall)) {
		String sEndpoint = propertyChannel.getString(DP_ENDPOINT);
		if (!UUtils.isEmpty(sEndpoint))
		    clonedService.setURL(sEndpoint);

	    } else if (DP_CS_REGISTRY.equals(howToCall)) {
		clonedService.setURL(null);
		setRegistryEndpoint(propertyChannel
			.getString(DP_REGISTRY_ENDPOINT));
		setRegistryNamespace(propertyChannel
			.getString(DP_REGISTRY_NAMESPACE));

	    }
	    setAsBytes(propertyChannel.getBoolean(DP_INP_ASBYTES, false));

	}
    }

}
