/*
 * The Gemma project
 * 
 * Copyright (c) 2008 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package ubic.gemma.web.services;

import java.util.Collection;
import java.util.HashSet;

import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import ubic.gemma.model.analysis.expression.ExpressionExperimentSet;
import ubic.gemma.model.analysis.expression.ExpressionExperimentSetService;
import ubic.gemma.model.expression.experiment.BioAssaySet;

/**
 * A service to return the full list of Expression Experiment Set IDs along with the corresponding name and experiment
 * ids it contains.
 * 
 * @author gavin
 * @version$Id$
 */

public class ExpressionExperimentSetIDsEndpoint extends AbstractGemmaEndpoint {

    private static Log log = LogFactory.getLog( ExpressionExperimentSetIDsEndpoint.class );

    private ExpressionExperimentSetService expressionExperimentSetService;

    /**
     * The local name of the expected Request/Response.
     */
    public static final String LOCAL_NAME = "expressionExperimentSetIDs";

    /**
     * Sets the "business service" to delegate to.
     */
    public void setExpressionExperimentSetService( ExpressionExperimentSetService expressionExperimentSetService ) {
        this.expressionExperimentSetService = expressionExperimentSetService;
    }

    /**
     * Reads the given <code>requestElement</code>, and sends a the response back.
     * 
     * @param requestElement the contents of the SOAP message as DOM elements
     * @param document a DOM document to be used for constructing <code>Node</code>s
     * @return the response element
     */
    @Override
    protected Element invokeInternal( Element requestElement, Document document ) throws Exception {
        authenticate();
        StopWatch watch = new StopWatch();
        watch.start();

        setLocalName( LOCAL_NAME );
        // no input is used since I can't properly filter using the taxon
        // TODO include taxon as input and use it to filter the analyses returned.
        // String taxString = "";
        //
        // Collection<String> taxonInput = getNodeValues( requestElement, "taxon_id" );
        // for ( String tax : taxonInput ) {
        // taxString = tax;
        // }
        //
        // log.info( "XML input read: taxon, " + taxString);
        // //Collection<Gene> genes = geneService.findByOfficialSymbolInexact( geneName );
        // Taxon taxon = taxonService.load( Long.parseLong( taxString ) );
        //
        // if ( taxon == null ) {
        // String msg = "Taxon, " + taxon + "can't be found.";
        // return buildBadResponse( document, msg );
        // }

        Collection<ExpressionExperimentSet> eesCol = expressionExperimentSetService.loadAllMultiExperimentSets();

        // retain expression experiment sets that have a name assigned
        Collection<ExpressionExperimentSet> eesColToUse = new HashSet<ExpressionExperimentSet>();
        for ( ExpressionExperimentSet ees : eesCol ) {
            if ( ees.getName() != null )
            // if ( ees.getTaxon().equals( taxon ) ) eesColToUse.add( ees );
                eesColToUse.add( ees );
        }

        // start building the wrapper
        // build xml manually for mapped result rather than use buildWrapper inherited from AbstractGemmeEndpoint

        Element responseWrapper = document.createElementNS( NAMESPACE_URI, LOCAL_NAME );
        Element responseElement = document.createElementNS( NAMESPACE_URI, LOCAL_NAME + RESPONSE );
        responseWrapper.appendChild( responseElement );

        for ( ExpressionExperimentSet ees : eesColToUse ) {

            Element e1 = document.createElement( "expression_experiment_set_id" );
            e1.appendChild( document.createTextNode( ees.getId().toString() ) );
            responseElement.appendChild( e1 );

            Element e2 = document.createElement( "ees_name" );
            e2.appendChild( document.createTextNode( ees.getName() ) );
            responseElement.appendChild( e2 );

            Collection<Long> eeIds = getExperimentIDs( ees );
            Element e3 = document.createElement( "datasets" );
            e3.appendChild( document.createTextNode( encode( eeIds.toArray() ) ) );
            responseElement.appendChild( e3 );

            // taxon not populate
            // Element e4 = document.createElement( "taxon" );
            // e4.appendChild( document.createTextNode( ees.getTaxon().getId().toString() ) );
            // responseElement.appendChild( e4 );

        }

        watch.stop();
        Long time = watch.getTime();
        // log.info( "Finished generating result. Sending response to client." );
        log.info( "XML response for Experiment Set IDs results built in " + time + "ms." );
        return responseWrapper;

    }

    private Collection<Long> getExperimentIDs( ExpressionExperimentSet ees ) {
        Collection<Long> eeIds = new HashSet<Long>();
        for ( BioAssaySet bas : ees.getExperiments() )
            eeIds.add( bas.getId() );
        return eeIds;
    }

}
@@ -2530,796 +2530,8 @@
 E );
-%0A        // no input is used since I can't properly filter using the taxon%0A        // TODO include taxon as input and use it to filter the analyses returned.%0A        // String taxString = %22%22;%0A        //%0A        // Collection%3CString%3E taxonInput = getNodeValues( requestElement, %22taxon_id%22 );%0A        // for ( String tax : taxonInput ) %7B%0A        // taxString = tax;%0A        // %7D%0A        //%0A        // log.info( %22XML input read: taxon, %22 + taxString);%0A        // //Collection%3CGene%3E genes = geneService.findByOfficialSymbolInexact( geneName );%0A        // Taxon taxon = taxonService.load( Long.parseLong( taxString ) );%0A        //%0A        // if ( taxon == null ) %7B%0A        // String msg = %22Taxon, %22 + taxon + %22can't be found.%22;%0A        // return buildBadResponse( document, msg );%0A        // %7D
 %0A%0A  
@@ -4163,45 +4163,8 @@
     
- // taxon not populate%0A            //
  Ele
@@ -4204,25 +4204,25 @@
  %22taxon%22 );%0A
- 
+%0A
            /
@@ -4220,18 +4220,16 @@
         
-//
  e4.appe
@@ -4312,19 +4312,16 @@
         
- //
  respons

include the taxon in the output