package de.mpg.mpiz_koeln.featureClient.test;

import java.util.Collection;
import java.util.Map;

import org.biomoby.shared.MobyException;
import org.biomoby.shared.MobyService;
import org.biomoby.shared.datatypes.MobyObject;

import de.mpg.mpiz_koeln.featureClient.FeatureClient;
import de.mpg.mpiz_koeln.featureClient.FeatureClientException;
import de.mpg.mpiz_koeln.featureClient.FeatureClientResult;

/**
 * This example shows how to call web services multiple times, which will be found by querying the central repository
 * given a specific pattern. This means the client can call all services which are found by providing input and output
 * definitions.
 * 
 * It also shows how to filter out services or authorities which shall be not called at all.
 */
class MultiCallByDefinition {
    public static void main( String[] args ) throws MobyException, FeatureClientException {
        // create a client to the default moby central in calgary with a timeout of 30 seconds
        FeatureClient client = new FeatureClient();
        // search for services which return objects with the namespace 'MapManBinCode'
        // you can give more than one namespace but always only datatype
        client.addOutput( "Object", "MapMan_BinCode" );
        // exclude services identified by service name or authority
        client.add2Filter( "Locus2GeneAliases", "getCor", "bioinfo.icapture.ubc.ca" );
        // input for the first call -> it also defines now we search for service which consumes an AGI code
        client.addMultipleCallInput( "AGI_LocusCode", "AT4G30110" );
        // input for the second call
        client.addMultipleCallInput( "AGI_LocusCode", "AT1G12000" );

        // call the service, which will be found
        Collection< FeatureClientResult > result = client.call();

        for ( FeatureClientResult mobyServiceResult : result ) {
            // as we have called the service multiple times we get back for each identifier the returned results
            Map< String, Collection< MobyObject > > terms = mobyServiceResult.getMultipleCallResult();

            // you can get the service which returned the current result
            MobyService mobyService = mobyServiceResult.getMobyService();
            System.out.println( "The service " + mobyService.getName() + " returned: " );

            for ( String identifier : terms.keySet() ) {
                System.out.println( "For the id " + identifier );
                // prints out the found mapman bins
                for ( MobyObject mapman : terms.get( identifier ) ) {
                    System.out.println( "ID: " + mapman.getId() );
                }
                System.out.println();
            }
        }
        System.exit( 0 );
    }
}
