/*
 * 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.expression.experiment.service.ExpressionExperimentSetService;
import ubic.gemma.model.analysis.expression.ExpressionExperimentSet;
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 );

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

        // 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 );

            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." );
        if ( time > 1000 ) {
            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;
    }

}
@@ -1031,17 +1031,8 @@
 del.
-analysis.
 expr
@@ -1042,58 +1042,20 @@
 ion.
-ExpressionExperimentSet;%0Aimport ubic.gemma.model.e
+experiment.E
 xpre
@@ -1051,34 +1051,33 @@
 iment.Expression
-.e
+E
 xperiment.BioAss
@@ -1073,19 +1073,21 @@
 ment
-.BioAssaySe
+SetValueObjec
 t;%0A%0A
@@ -2520,32 +2520,43 @@
 ionExperimentSet
+ValueObject
 %3E eesCol = expre
@@ -2580,16 +2580,33 @@
 tService
+%0A                
 .loadAll
@@ -2618,16 +2618,27 @@
 imentSet
+ValueObject
 sWithTax
@@ -2714,16 +2714,42 @@
 assigned
+ (probably not necessary?)
 %0A       
@@ -2783,16 +2783,27 @@
 imentSet
+ValueObject
 %3E eesCol
@@ -2845,16 +2845,27 @@
 imentSet
+ValueObject
 %3E();%0A   
@@ -2890,32 +2890,43 @@
 ionExperimentSet
+ValueObject
  ees : eesCol ) 
@@ -2971,101 +2971,8 @@
 ll )
-%0A            // if ( ees.getTaxon().equals( taxon ) ) eesColToUse.add( ees );%0A               
  ees
@@ -3435,24 +3435,35 @@
 xperimentSet
+ValueObject
  ees : eesCo
@@ -3906,30 +3906,44 @@
 eeIds = 
+ees.
 getExp
+ressionExp
 erimentI
 Ds( ees 
@@ -3938,16 +3938,11 @@
 entI
-D
+d
 s(
- ees 
 );%0A 
@@ -4191,24 +4191,68 @@
  %22taxon%22 );%0A
+            assert ees.getTaxonId() != null;
 %0A           
@@ -4309,14 +4309,8 @@
 axon
-().get
 Id()
@@ -4705,260 +4705,5 @@
  %7D%0A%0A
-    private Collection%3CLong%3E getExperimentIDs( ExpressionExperimentSet ees ) %7B%0A        Collection%3CLong%3E eeIds = new HashSet%3CLong%3E();%0A        for ( BioAssaySet bas : ees.getExperiments() )%0A            eeIds.add( bas.getId() );%0A        return eeIds;%0A    %7D%0A%0A
 %7D

fix lazy load problem (gemscape revealed)