package ubic.gemma.model.genome.gene.phenotype.valueObject;

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

import ubic.gemma.model.association.phenotype.ExperimentalEvidence;
import ubic.gemma.model.common.description.BibliographicReferenceService;
import ubic.gemma.model.common.description.Characteristic;
import ubic.gemma.model.common.description.VocabCharacteristicImpl;

public class ExperimentalEvidenceValueObject extends EvidenceValueObject {
    
    
    // *********************************************
    // field used to create the Bibliographic object
    // *********************************************
    // The primary pubmed id
    private String primaryPublication = "";
    // other relevant pubmed id
    private Collection<String> relevantPublication = new HashSet<String>();
    // TODO find correct name of variable
    private Collection<CharacteristicValueObject> experimentCharacteristics = new HashSet<CharacteristicValueObject>();

    // *********************************************
    // fields that are returned view of the object
    // *********************************************
    private BibliographicReferenceValueObject primaryPublicationValueObject = null;
    private Collection<BibliographicReferenceValueObject> relevantPublicationsValueObjects = new HashSet<BibliographicReferenceValueObject>();
    private BibliographicReferenceCitationValueObject primaryPublicationCitationValueObject= null;


    public ExperimentalEvidenceValueObject( String description, CharacteristicValueObject associationType,
            Boolean isNegativeEvidence, String evidenceCode, Collection<CharacteristicValueObject> phenotypes,
            String primaryPublication, Collection<String> relevantPublication,
            Collection<CharacteristicValueObject> experimentCharacteristics ) {
        super( description, associationType, isNegativeEvidence, evidenceCode, phenotypes );
        this.primaryPublication = primaryPublication;
        this.relevantPublication = relevantPublication;
        this.experimentCharacteristics = experimentCharacteristics;
    }

    
    public ExperimentalEvidenceValueObject(){
    }
    
    /** Entity to Value Object */
    public ExperimentalEvidenceValueObject( ExperimentalEvidence experimentalEvidence ) {
        super( experimentalEvidence );

        
        this.primaryPublicationCitationValueObject = new BibliographicReferenceCitationValueObject( experimentalEvidence
                .getExperiment().getPrimaryPublication() );
        
        this.primaryPublicationValueObject = new BibliographicReferenceValueObject( experimentalEvidence
                .getExperiment().getPrimaryPublication() );
        
        this.primaryPublication = primaryPublicationValueObject.getPubAccession();
        
        this.relevantPublicationsValueObjects = BibliographicReferenceValueObject
                .convert2ValueObjects( experimentalEvidence.getExperiment().getOtherRelevantPublications() );
        
        for(BibliographicReferenceValueObject bibli :relevantPublicationsValueObjects){
            relevantPublication.add( bibli.getPubAccession() );
        }

        Collection<Characteristic> collectionCharacteristics = experimentalEvidence.getExperiment()
                .getCharacteristics();

        if ( collectionCharacteristics != null ) {
            this.experimentCharacteristics = new ArrayList<CharacteristicValueObject>();
            for ( Characteristic c : collectionCharacteristics ) {
                if ( c instanceof VocabCharacteristicImpl ) {
                    VocabCharacteristicImpl voCha = ( VocabCharacteristicImpl ) c;
                    experimentCharacteristics.add( new CharacteristicValueObject( voCha.getValue(),
                            voCha.getCategory(), voCha.getValueUri(), voCha.getCategoryUri() ) );
                } else {
                    experimentCharacteristics.add( new CharacteristicValueObject( c.getValue(), c.getCategory() ) );
                }
            }
        }
    }

    public BibliographicReferenceValueObject getPrimaryPublicationValueObject() {
        return primaryPublicationValueObject;
    }

    public Collection<BibliographicReferenceValueObject> getRelevantPublicationsValueObjects() {
        return relevantPublicationsValueObjects;
    }

    public String getPrimaryPublication() {
        return primaryPublication;
    }

    public Collection<String> getRelevantPublication() {
        return relevantPublication;
    }

    public Collection<CharacteristicValueObject> getExperimentCharacteristics() {
        return experimentCharacteristics;
    }

    public BibliographicReferenceCitationValueObject getPrimaryPublicationCitationValueObject() {
        return primaryPublicationCitationValueObject;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + ( ( experimentCharacteristics == null ) ? 0 : experimentCharacteristics.hashCode() );
        result = prime * result + ( ( primaryPublication == null ) ? 0 : primaryPublication.hashCode() );
        result = prime * result + ( ( relevantPublication == null ) ? 0 : relevantPublication.hashCode() );
        return result;
    }

    @Override
    public boolean equals( Object obj ) {
        if ( this == obj ) return true;
        if ( !super.equals( obj ) ) return false;
        if ( getClass() != obj.getClass() ) return false;
        ExperimentalEvidenceValueObject other = ( ExperimentalEvidenceValueObject ) obj;

        if ( experimentCharacteristics == null ) {
            if ( other.experimentCharacteristics != null ) return false;
        } else {

            HashSet<CharacteristicValueObject> set1 = new HashSet<CharacteristicValueObject>();
            HashSet<CharacteristicValueObject> set2 = new HashSet<CharacteristicValueObject>();
            set1.addAll( experimentCharacteristics );
            set2.addAll( other.experimentCharacteristics );

            if ( !set1.equals( set2 ) ) return false;
        }
        if ( primaryPublication == null ) {
            if ( other.primaryPublication != null ) return false;
        } else if ( !primaryPublication.equals( other.primaryPublication ) ) return false;

        if ( relevantPublication == null ) {
            if ( other.relevantPublication != null ) return false;
        } else {
            HashSet<String> set3 = new HashSet<String>();
            HashSet<String> set4 = new HashSet<String>();
            set3.addAll( relevantPublication );
            set4.addAll( other.relevantPublication );
            if ( !set3.equals( set4 ) ) return false;  
        }
        return true;
    }

}
@@ -58,36 +58,8 @@
 t;%0A%0A
-import java.util.ArrayList;%0A
 impo
@@ -83,16 +83,16 @@
 ection;%0A
+
 import j
@@ -182,82 +182,8 @@
 ce;%0A
-import ubic.gemma.model.common.description.BibliographicReferenceService;%0A
 impo
@@ -385,18 +385,9 @@
 t %7B%0A
-    %0A    
 %0A
+
     
@@ -1360,16 +1360,17 @@
 ueObject
+ 
 = null;%0A
@@ -1370,17 +1370,16 @@
  null;%0A%0A
-%0A
     publ
@@ -2025,21 +2025,16 @@
 %0A    %7D%0A%0A
-    %0A
     publ
@@ -2073,16 +2073,17 @@
 ct()
+ 
 %7B%0A    %7D%0A
     
@@ -2078,21 +2078,17 @@
 %7B%0A    %7D%0A
-    
 %0A
+
     /** 
@@ -2243,25 +2243,16 @@
 nce );%0A%0A
-        %0A
         
@@ -2334,32 +2334,48 @@
 tionValueObject(
+%0A               
  experimentalEvi
@@ -2379,33 +2379,16 @@
 Evidence
-%0A                
 .getExpe
@@ -2419,32 +2419,24 @@
 cation() );%0A
-        
 %0A        thi
@@ -2585,33 +2585,25 @@
 cation() );%0A
-        
 %0A
+
         this
@@ -2673,25 +2673,17 @@
 sion();%0A
-        
 %0A
+
         
@@ -2866,24 +2866,16 @@
 ns() );%0A
-        
 %0A       
@@ -2878,17 +2878,19 @@
      for
-(
+ ( 
 Bibliogr
@@ -2922,16 +2922,17 @@
  bibli :
+ 
 relevant
@@ -2955,17 +2955,19 @@
 eObjects
-)
+ ) 
 %7B%0A      
@@ -3230,97 +3230,8 @@
 ) %7B%0A
-            this.experimentCharacteristics = new ArrayList%3CCharacteristicValueObject%3E();%0A
     
@@ -6414,32 +6414,32 @@
 tPublication );%0A
+
             if (
@@ -6475,18 +6475,16 @@
 n false;
-  
 %0A       

change the ExperimentalEvidenceValueObject equals method