package uk.ac.ebi.quickgo.solr.mapper.annotation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import uk.ac.ebi.quickgo.annotation.Annotation;
import uk.ac.ebi.quickgo.solr.mapper.EntityMapper;
import uk.ac.ebi.quickgo.solr.model.SolrDocumentType;
import uk.ac.ebi.quickgo.solr.model.annotation.SolrAnnotation;
import uk.ac.ebi.quickgo.solr.model.annotation.SolrAnnotation.SolrAnnotationDocumentType;

/**
 * To convert Solr annotations into "QuickGO" Annotations
 * @author cbonill
 */
public class EntityAnnotationMapper implements EntityMapper<SolrAnnotation, Annotation>{

	@Override
	public Annotation toEntityObject(Collection<SolrAnnotation> solrObjects) {
		return toEntityObject(solrObjects, SolrAnnotationDocumentType.getAsInterfaces());
	}

	@Override
	public Annotation toEntityObject(Collection<SolrAnnotation> solrObjects,
			List<SolrDocumentType> solrDocumentTypes) {
		
		Annotation annotation = new Annotation();

		for (SolrDocumentType annotationDocumentType : solrDocumentTypes) {
			SolrAnnotationDocumentType solrAnnotationDocumentType = ((SolrAnnotationDocumentType) annotationDocumentType);

			switch (solrAnnotationDocumentType) {
			case ANNOTATION:
				if(getAssociatedSolrTerms(solrObjects,SolrAnnotationDocumentType.ANNOTATION).size() > 0){
					mapAnnotation(getAssociatedSolrTerms(solrObjects,
								SolrAnnotationDocumentType.ANNOTATION).get(0), annotation);
				}
				break;				
			}
		}
		return annotation;
	}

	
	/**
	 * Maps a Solr annotation to an Annotation
	 * @param associatedSolrTerm Solr annotations
	 * @param annotation Annotation
	 */
	private void mapAnnotation(SolrAnnotation associatedSolrTerm,
			Annotation annotation) {
		annotation.setAssignedBy(associatedSolrTerm.getAssignedBy());
		annotation.setDate(associatedSolrTerm.getDate());
		annotation.setDb(associatedSolrTerm.getDb());
		annotation.setDbObjectID(associatedSolrTerm.getDbObjectID());
		annotation.setDbObjectName(associatedSolrTerm.getDbObjectName());
		annotation.setDbObjectSymbol(associatedSolrTerm.getDbObjectSymbol());
		annotation.setDbObjectType(associatedSolrTerm.getDbObjectType());
		annotation.setDbObjectSynonyms(associatedSolrTerm.getDbObjectSynonyms());
		annotation.setEcoID(associatedSolrTerm.getEcoID());
		annotation.setTermName(associatedSolrTerm.getTermName());
		annotation.setExtensions(associatedSolrTerm.getExtensions());
		annotation.setGoAspect(associatedSolrTerm.getGoAspect());
		annotation.setGoEvidence(associatedSolrTerm.getGoEvidence());
		annotation.setGoID(associatedSolrTerm.getGoID());
		annotation.setInteractingTaxID(associatedSolrTerm.getInteractingTaxID());
		annotation.setProperties(associatedSolrTerm.getProperties());
		annotation.setQualifiers(associatedSolrTerm.getQualifiers());
		annotation.setReferences(associatedSolrTerm.getDbXrefs());
		annotation.setTaxonomyId(associatedSolrTerm.getTaxonomyId());
		annotation.setTaxonomyName(associatedSolrTerm.getTermName());
		annotation.setWith(associatedSolrTerm.getWith());
		annotation.setSequenceLength(associatedSolrTerm.getSequenceLength());
		annotation.setGp2protein(associatedSolrTerm.getGp2proteinList());
		annotation.setSubset(associatedSolrTerm.getSubSets());
		annotation.setAncestorsIPO(associatedSolrTerm.getAncestorsIPO());
		annotation.setEcoAncestorsI(associatedSolrTerm.getEcoAncestorsI());
	}

	/**
	 * Given a list of Solr annotations, returns the ones that match with the
	 * specified document type
	 * 
	 * @param solrObjects
	 *            Solr annotations objects
	 * @param solrAnnotationDocumentType
	 *            Type to check
	 * @return Solr annotations that match with the specified document type
	 */
	protected List<SolrAnnotation> getAssociatedSolrTerms(Collection<SolrAnnotation> solrObjects,
			SolrAnnotationDocumentType solrAnnotationDocumentType) {
		List<SolrAnnotation> solrAnnotations = new ArrayList<>();
		for (SolrAnnotation solrAnnotation : solrObjects) {
			if (SolrAnnotationDocumentType.valueOf(solrAnnotation.getDocType().toUpperCase()) == solrAnnotationDocumentType) {
				solrAnnotations.add(solrAnnotation);
			}
		}
		return solrAnnotations;
	}	
}
@@ -914,19 +914,17 @@
 ypes) %7B%0A
-%09%09
 %0A
+
 %09%09Annota
@@ -1439,20 +1439,16 @@
 %09%09break;
-%09%09%09%09
 %0A%09%09%09%7D%0A%09%09
@@ -1474,18 +1474,17 @@
 on;%0A%09%7D%0A%0A
-%09
 %0A
+
 %09/**%0A%09 *
@@ -2731,17 +2731,16 @@
 ualifier
-s
 (associa
@@ -2763,17 +2763,16 @@
 ualifier
-s
 ());%0A%09%09a
@@ -2793,17 +2793,16 @@
 eference
-s
 (associa
@@ -2822,17 +2822,16 @@
 etDbXref
-s
 ());%0A%09%09a
@@ -3457,17 +3457,16 @@
 type%0A%09 *
- 
 %0A%09 * @pa
@@ -4135,7 +4135,7 @@
 ;%0A%09%7D
-%09
 %0A%7D
+%0A

Change Qualifiers and References from multi-value to single value.