/*
 * The Gemma project.
 * 
 * Copyright (c) 2006 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.model.association.coexpression;

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

import ubic.gemma.model.common.quantitationtype.QuantitationType;
import ubic.gemma.model.expression.experiment.ExpressionExperiment;
import ubic.gemma.model.genome.Gene;

/**
 * @see ubic.gemma.model.association.coexpression.Probe2ProbeCoexpressionService
 */
public class Probe2ProbeCoexpressionServiceImpl extends
        ubic.gemma.model.association.coexpression.Probe2ProbeCoexpressionServiceBase {

    /**
     * @see ubic.gemma.model.association.coexpression.Probe2ProbeCoexpressionService#create(ubic.gemma.model.association.coexpression.Probe2ProbeCoexpression)
     */
    @Override
    protected ubic.gemma.model.association.coexpression.Probe2ProbeCoexpression handleCreate(
            ubic.gemma.model.association.coexpression.Probe2ProbeCoexpression p2pCoexpression )
            throws java.lang.Exception {

        if ( p2pCoexpression instanceof RatProbeCoExpression )
            return ( RatProbeCoExpression ) this.getRatProbeCoExpressionDao().create(
                    ( RatProbeCoExpression ) p2pCoexpression );
        else if ( p2pCoexpression instanceof MouseProbeCoExpression )
            return ( MouseProbeCoExpression ) this.getMouseProbeCoExpressionDao().create(
                    ( MouseProbeCoExpression ) p2pCoexpression );
        else if ( p2pCoexpression instanceof HumanProbeCoExpression )
            return ( HumanProbeCoExpression ) this.getHumanProbeCoExpressionDao().create(
                    ( HumanProbeCoExpression ) p2pCoexpression );
        else if ( p2pCoexpression instanceof OtherProbeCoExpression )
            return ( OtherProbeCoExpression ) this.getOtherProbeCoExpressionDao().create(
                    ( OtherProbeCoExpression ) p2pCoexpression );
        else
            throw new java.lang.Exception( "p2pCoexpression isn't of a known type. don't know how to create."
                    + p2pCoexpression );

    }

    /**
     * @see ubic.gemma.model.association.coexpression.Probe2ProbeCoexpressionServiceBase#handleCreate(java.util.Collection)
     */
    @Override
    protected java.util.Collection handleCreate( java.util.Collection p2pExpressions ) throws java.lang.Exception {

        if ( !this.validCollection( p2pExpressions ) ) return null;

        Object check = p2pExpressions.iterator().next();
        if ( check instanceof RatProbeCoExpression )
            return this.getRatProbeCoExpressionDao().create( p2pExpressions );
        else if ( check instanceof MouseProbeCoExpression )
            return this.getMouseProbeCoExpressionDao().create( p2pExpressions );
        else if ( check instanceof HumanProbeCoExpression )
            return this.getHumanProbeCoExpressionDao().create( p2pExpressions );
        else if ( check instanceof OtherProbeCoExpression )
            return this.getOtherProbeCoExpressionDao().create( p2pExpressions );
        else
            throw new IllegalArgumentException( "Collection contains objects that it can't persist:" + check.getClass()
                    + " no service method for persisting." );

    }

    /**
     * Performs the core logic for {@link #delete(ubic.gemma.model.association.coexpression.Probe2ProbeCoexpression)}
     */
    protected void handleDelete( ubic.gemma.model.association.coexpression.Probe2ProbeCoexpression toDelete )
            throws java.lang.Exception {

        if ( toDelete instanceof RatProbeCoExpression )
            this.getRatProbeCoExpressionDao().remove( toDelete );
        else if ( toDelete instanceof MouseProbeCoExpression )
            this.getMouseProbeCoExpressionDao().remove( toDelete );
        else if ( toDelete instanceof HumanProbeCoExpression )
            this.getHumanProbeCoExpressionDao().remove( toDelete );
        else if ( toDelete instanceof OtherProbeCoExpression )
            this.getOtherProbeCoExpressionDao().remove( toDelete );
        else
            throw new IllegalArgumentException( "Collection contains objects that it can't persist:"
                    + toDelete.getClass() + " no service method for persisting." );

        return;

    }

    /**
     * Performs the core logic for {@link #delete(java.util.Collection)}
     */
    protected void handleDelete( java.util.Collection deletes ) throws java.lang.Exception {

        if ( !this.validCollection( deletes ) ) return;

        Object check = deletes.iterator().next();

        if ( check instanceof RatProbeCoExpression )
            this.getRatProbeCoExpressionDao().remove( deletes );
        else if ( check instanceof MouseProbeCoExpression )
            this.getMouseProbeCoExpressionDao().remove( deletes );
        else if ( check instanceof HumanProbeCoExpression )
            this.getHumanProbeCoExpressionDao().remove( deletes );
        else if ( check instanceof OtherProbeCoExpression )
            this.getOtherProbeCoExpressionDao().remove( deletes );
        else
            throw new IllegalArgumentException( "Collection contains objects that it can't persist:" + check.getClass()
                    + " no service method for persisting." );

        return;

    }

    private Boolean validCollection( java.util.Collection p2pExpressions ) throws IllegalArgumentException {
        // sanity check.
        if ( ( p2pExpressions == null ) || ( p2pExpressions.size() == 0 ) ) return false;

        // Make sure that the collections passed in is all of the same Class

        Object last = p2pExpressions.iterator().next();

        for ( Object next : p2pExpressions ) {

            if ( last.getClass() != next.getClass() ) {
                throw new IllegalArgumentException(
                        "Given collection doesn't contain objects of uniform type. Contains an object of type "
                                + last.getClass() + " and another of type " + next.getClass() );
            }

            last = next;
        }

        return true;
    }

    @Override
    protected Collection handleFindCoexpressionRelationships( Gene givenG, Collection ees, QuantitationType qt )
            throws Exception {
        return this.getProbe2ProbeCoexpressionDao().findCoexpressionRelationships( givenG, ees, qt );
    }

    @Override
    protected Map handleFindCoexpressionRelationships( Collection genes, QuantitationType qt, Collection ees )
            throws Exception {
        return this.getProbe2ProbeCoexpressionDao().findCoexpressionRelationships( genes, qt, ees );
    }

    /*
     * (non-Javadoc)
     * 
     * @see ubic.gemma.model.association.coexpression.Probe2ProbeCoexpressionServiceBase#handleDeleteLinks(ubic.gemma.model.expression.experiment.ExpressionExperiment)
     */
    @Override
    protected void handleDeleteLinks( ExpressionExperiment ee ) throws Exception {
        this.getProbe2ProbeCoexpressionDao().deleteLinks( ee );

    }

    @Override
    protected Integer handleCountLinks( ExpressionExperiment expressionExperiment ) throws Exception {
        return this.getProbe2ProbeCoexpressionDao().countLinks( expressionExperiment );
    }

}
@@ -4179,32 +4179,57 @@
 ionDao().remove(
+ ( RatProbeCoExpression )
  toDelete );%0A   
@@ -4335,32 +4335,59 @@
 ionDao().remove(
+ ( MouseProbeCoExpression )
  toDelete );%0A   
@@ -4493,32 +4493,59 @@
 ionDao().remove(
+ ( HumanProbeCoExpression )
  toDelete );%0A   
@@ -4659,16 +4659,43 @@
 .remove(
+ ( OtherProbeCoExpression )
  toDelet

add casts