// DataServiceEdge.java
//
//    senger@ebi.ac.uk
//    October 2004
//

package org.biomoby.client;

import org.biomoby.shared.MobyDataType;
import org.biomoby.shared.MobyService;
import org.biomoby.shared.Utils;

/**
 * An instance of this class is a container for a pair of a data type
 * and a Moby service definitions, and for an information how they can
 * be connected. The parent class, a {@link ServicesEdge}, connects
 * two services, this class connects a data type with one
 * service:
 *
 * <pre> dataType --> targetService </pre>
 * or
 * <pre> sourceService --> dataType </pre>
 *
 * Therefore, only one service (either source, or target one) should
 * be defined - otherwise an instance of this class actually
 * represents two edges (which may not be expected by those who use
 * this class):
 *
 * <pre> sourceService --> dataType --> targetService </pre>
 *
 * You can imagine that instances of this class represent starting or
 * ending edges in a graph of services.  <p>
 *
 * @see ServiceConnections
 * @author <A HREF="mailto:senger@ebi.ac.uk">Martin Senger</A>
 * @version $Id: DataServiceEdge.java,v 1.2 2005/04/07 16:37:02 kawas Exp $
 */

public class DataServiceEdge
    extends ServicesEdge {

    // private members
    MobyDataType dataType = null;

    /*************************************************************************
     * Constructs an instance with data type as a source and a service
     * as a target, and a connector.
     *************************************************************************/
    public DataServiceEdge (MobyDataType sourceDataType, MobyService targetService,
			    String connector) {
	super (null, targetService, connector);
	dataType = sourceDataType;
    }

    /*************************************************************************
     * Constructs an instance with data type as a target and a service
     * as a source, and a connector.
     *************************************************************************/
    public DataServiceEdge (MobyService sourceService, MobyDataType targetDataType,
			    String connector) {
	super (sourceService, null, connector);
	dataType = targetDataType;
    }

    /*************************************************************************
     * Retrieves the data type (regardless if it is a source or a
     * target data type, there is always only one data type in this
     * class).
     *
     * @return the data type of this edge
     *************************************************************************/
    public MobyDataType getDataType() {
	return dataType;
    }

    /*************************************************************************
     * Retrieves the service from this edge. Only source or target
     * service should be present - and that one is returned. <p>
     *
     * @return the service that is present; it returns null if both
     * services (target and source) are null
     *************************************************************************/
    public MobyService getService() {
 	if (sourceService != null) return getSourceService();
 	if (targetService != null) return getTargetService();
	return null;
    }

    /*************************************************************************
     * Indicate if this edge is a starting or ending edge of a
     * graph. <p>
     *
     * @return return true if this edge has a non-null target service
     *************************************************************************/
    public boolean isEndingEdge() {
	return (targetService == null);
    }

    /*************************************************************************
     *
     *************************************************************************/
    public String toString() {
	StringBuffer buf = new StringBuffer();
	if (sourceService == null)
	    buf.append ( (dataType == null ? "null" : Utils.pureName (dataType.getName())) );
	else
	    buf.append (sourceService.getName());
	buf.append (" ---( ");
	buf.append (connector);
	buf.append (" )--> ");
	if (targetService == null)
	    buf.append ( (dataType == null ? "null" : Utils.pureName (dataType.getName())) );
	else
	    buf.append (targetService.getName());
	return new String (buf);
    }
}

