package gov.nih.nci.ncicb.cadsr.loader.validator;
import gov.nih.nci.ncicb.cadsr.domain.DataElement;
import gov.nih.nci.ncicb.cadsr.domain.DataElementConcept;
import gov.nih.nci.ncicb.cadsr.domain.DomainObjectFactory;
import gov.nih.nci.ncicb.cadsr.domain.ObjectClass;
import gov.nih.nci.ncicb.cadsr.loader.ElementsLists;
import gov.nih.nci.ncicb.cadsr.loader.event.ProgressListener;
import gov.nih.nci.ncicb.cadsr.loader.util.*;
import java.util.*;

public class DuplicateValidator implements Validator 
{
  private ElementsLists elements = ElementsLists.getInstance();
  
  private ValidationItems items = ValidationItems.getInstance();
  
  public DuplicateValidator()
  {
  }
  
  public void addProgressListener(ProgressListener l) {

  }
  
  public ValidationItems validate() {
    List<ObjectClass> ocs = elements.getElements(DomainObjectFactory.newObjectClass());
    Map<String, ObjectClass> listed = new HashMap<String, ObjectClass>();
    Map<String, ObjectClass> prefNameList = new HashMap<String, ObjectClass>();
    Map<String, ObjectClass> deListed = new HashMap<String, ObjectClass>();
    
    if(ocs != null) {
      for(ObjectClass oc : ocs) {  
        if(oc.getPublicId() != null) {
        if(listed.containsKey(oc.getPublicId()))
          items.addItem(new ValidationError
                        (PropertyAccessor.getProperty
                          ("class.same.mapping", oc.getLongName(),(listed.get(oc.getPublicId())).getLongName()),oc));
        else
          listed.put(oc.getPublicId(), oc);
        }
        else if(!StringUtil.isEmpty(oc.getPreferredName())) {
          if(prefNameList.containsKey(oc.getPreferredName()))
            items.addItem(new ValidationError
                          (PropertyAccessor.getProperty
                            ("class.same.mapping", oc.getLongName(),(prefNameList.get(oc.getPreferredName())).getLongName()),oc));
          else
            prefNameList.put(oc.getPreferredName(), oc);
        }
      } 
    }
    
    
    List<DataElement> des = elements.getElements(DomainObjectFactory.newDataElement());
    if(des != null && ocs != null) {
      for(ObjectClass oc : ocs) {
        Map<String, DataElement> deList = new HashMap<String, DataElement>();
        for(DataElement de : des) {
          if(de.getDataElementConcept().getObjectClass() == oc)
            if(deList.containsKey(de.getDataElementConcept().getProperty().getPreferredName()))
              items.addItem(new ValidationError
                            (PropertyAccessor.getProperty
                             ("de.same.mapping", de.getDataElementConcept().getLongName(),
                                (deList.get(de.getDataElementConcept().getProperty().getPreferredName())).getDataElementConcept().getLongName()),de));
            else if (!StringUtil.isEmpty(de.getDataElementConcept().getProperty().getPreferredName())){
              deList.put(de.getDataElementConcept().getProperty().getPreferredName(), de);
            }
        }
      }
    }

    return items;
  }
}
@@ -262,16 +262,63 @@
 tClass;%0A
+import gov.nih.nci.ncicb.cadsr.domain.Concept;%0A
 import g
@@ -470,16 +470,134 @@
 util.*;%0A
+import gov.nih.nci.ncicb.cadsr.loader.ext.CadsrModule;%0Aimport gov.nih.nci.ncicb.cadsr.loader.ext.CadsrModuleListener;%0A
 import j
@@ -661,17 +661,37 @@
 alidator
- 
+, CadsrModuleListener
 %0A%7B%0A  pri
@@ -812,24 +812,60 @@
 tInstance();
+%0A%0A  private CadsrModule cadsrModule;
 %0A  %0A  public
@@ -1427,16 +1427,18 @@
         
+  
 if(liste
@@ -1472,32 +1472,34 @@
 d()))%0A          
+  
 items.addItem(ne
@@ -1532,32 +1532,34 @@
                 
+ 
+ 
 (PropertyAccesso
@@ -1564,32 +1564,33 @@
 sor.getProperty%0A
+ 
                 
@@ -1691,37 +1691,43 @@
 ),oc));%0A        
+  
 else
-%0A
+ %7B%0A  
           listed
@@ -1745,32 +1745,309 @@
 PublicId(), oc);
+%0A%0A            // we also need to add the concept lists so it can be validated against OC that are mapped to concepts%0A            List%3CConcept%3E concepts = cadsrModule.getConcepts(oc);%0A            prefNameList.put(ConceptUtil.preferredNameFromConcepts(concepts), oc);%0A          %7D
 %0A        %7D%0A     
@@ -2286,33 +2286,32 @@
                 
- 
 (%22class.same.map
@@ -3540,10 +3540,99 @@
 ems;%0A  %7D
+%0A%0A  public void setCadsrModule(CadsrModule module) %7B%0A    this.cadsrModule = module;%0A  %7D%0A%0A
 %0A%7D

check duplicate mapping for OC ID vs OC Concepts