package gov.nih.nci.ncicb.cadsr.loader.ext;
import gov.nih.nci.ncicb.cadsr.domain.Concept;
import gov.nih.nci.ncicb.cadsr.domain.DomainObjectFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import gov.nih.nci.ncicb.cadsr.evs.*;

public class EvsModule 
{

  private static EVSQueryService evsService = new EVSQueryService();

  public EvsModule()
  {
  }
  
  public EvsResult findByConceptCode(String code) 
  {

    try {
      List<EVSConcept> evsConcepts = (List<EVSConcept>)evsService.findConceptsByCode(code, false, 100);
      
      for(EVSConcept evsConcept : evsConcepts) {
        return evsConceptToEvsResult(evsConcept);
      }
    } catch (Exception e){
      e.printStackTrace();
    } // end of try-catch

    return null;
  }
  
  public Collection<EvsResult> findBySynonym(String s) 
  {
    Collection<EvsResult> result = new ArrayList();

    try {
      List<EVSConcept> evsConcepts = (List<EVSConcept>)evsService.findConceptsBySynonym(s, false, 100);
      
      for(EVSConcept evsConcept : evsConcepts) {
        result.add(evsConceptToEvsResult(evsConcept));
      }
    } catch (Exception e){
      e.printStackTrace();
    } // end of try-catch

    return result;
  }

  private EvsResult evsConceptToEvsResult(EVSConcept evsConcept) {

    Concept c = DomainObjectFactory.newConcept();
    c.setPreferredName(evsConcept.getCode());
    c.setLongName(evsConcept.getPreferredName());
    
    gov.nih.nci.evs.domain.Definition def = (gov.nih.nci.evs.domain.Definition)evsConcept.getDefinitions().get(0);
    if(def != null) {
      c.setPreferredDefinition(def.getDefinition());
      c.setDefinitionSource(def.getSource().getAbbreviation());
    }
    
    
    String[] syns = new String[evsConcept.getSynonyms().size()];
    evsConcept.getSynonyms().toArray(syns);
    
    return new EvsResult(c, syns);
    

  }
}

@@ -440,16 +440,40 @@
 ing code
+, boolean includeRetired
 ) %0A  %7B%0A%0A
@@ -573,21 +573,30 @@
 e(code, 
-false
+includeRetired
 , 100);%0A
@@ -867,16 +867,40 @@
 String s
+, boolean includeRetired
 ) %0A  %7B%0A 
@@ -1056,13 +1056,22 @@
 (s, 
-false
+includeRetired
 , 10
@@ -1512,20 +1512,16 @@
 ame());%0A
-    
 %0A    gov
@@ -1556,16 +1556,84 @@
 on def =
+ null;%0A%0A    if(evsConcept.getDefinitions().size() %3E 0) %7B%0A      def =
  (gov.ni
@@ -1692,24 +1692,31 @@
 s().get(0);%0A
+    %7D%0A%0A
     if(def !
@@ -1847,21 +1847,58 @@
 );%0A    %7D
-%0A    
+ else %0A      c.setPreferredDefinition(%22%22);
 %0A    %0A  

include retired