package gov.nih.nci.ncicb.cadsr.loader.validator;

import java.util.*;

import gov.nih.nci.ncicb.cadsr.domain.*;
import gov.nih.nci.ncicb.cadsr.dao.ValueDomainDAO;

import gov.nih.nci.ncicb.cadsr.loader.ElementsLists;
import gov.nih.nci.ncicb.cadsr.loader.event.ProgressListener;
import gov.nih.nci.ncicb.cadsr.loader.event.ProgressEvent;

import gov.nih.nci.ncicb.cadsr.loader.util.*;

/**
 * Validate issues that come from mapping inherited attributes <ul>
 * <li>Inherited attributes CDE mapping must have matching properties
 * all the way down the inheritance path.
 * <ul>
 */
public class InheritanceValidator implements Validator {
  private ElementsLists elements = ElementsLists.getInstance();

  private ValidationItems items = ValidationItems.getInstance();

  private ProgressListener progressListener;

  private InheritedAttributeList inheritedList = InheritedAttributeList.getInstance();

  public InheritanceValidator() {
  }

  public void addProgressListener(ProgressListener l) {
    progressListener = l;
  }

  /**
   * returns a list of Validation errors.
   */
  public ValidationItems validate() {
    
    Collection<DataElement> inheritedDEs = inheritedList.getAllInherited();

    for(DataElement inheritedDE : inheritedDEs) {
      if(inheritedDE.getPublicId() != null) {
        DataElement parentDE = inheritedList.getParent(inheritedDE);
        while(parentDE != null) {
          if(parentDE.getPublicId() != null) {
            if(!inheritedDE.getPublicId().equals(parentDE.getPublicId())
               || !inheritedDE.getVersion().equals(parentDE.getVersion())) {
              items.addItem(new ValidationError
                            (PropertyAccessor.getProperty
                             ("inherited.and.parent.property.mismatch", LookupUtil.lookupFullName(inheritedDE), LookupUtil.lookupFullName(parentDE)),inheritedDE));
            }
          } else {
//             if(!inheritedList.isInherited(parentDE)) {
//               items.addItem(new ValidationError
//                             (PropertyAccessor.getProperty
//                              ("parent.attribute.not.mapped", LookupUtil.lookupFullName(inheritedDE)), inheritedDE));
              
//             }
          }
          parentDE = inheritedList.getParent(parentDE);
        }
      }
    }
    return items;
  }

}
@@ -371,22 +371,173 @@
 .loader.
-util.*
+ext.CadsrModule;%0Aimport gov.nih.nci.ncicb.cadsr.loader.ext.CadsrModuleListener;%0Aimport gov.nih.nci.ncicb.cadsr.loader.util.*;%0A%0Aimport org.apache.log4j.Logger
 ;%0A%0A/**%0A 
@@ -781,16 +781,37 @@
 alidator
+, CadsrModuleListener
  %7B%0A  pri
@@ -1070,16 +1070,142 @@
 nce();%0A%0A
+  private CadsrModule cadsrModule = null;%0A%0A  private Logger logger = Logger.getLogger(InheritanceValidator.class.getName());%0A%0A
   public
@@ -2194,16 +2194,939 @@
 %7D else %7B
+ // parentDE mapped to concept. Verify that concepts match%0A            String conceptConcat = parentDE.getDataElementConcept().getProperty().getPreferredName();%0A            String%5B%5D revCodes = conceptConcat.split(%22:%22);%0A            String%5B%5D conceptCodes = new String%5BrevCodes.length%5D;%0A            for(int i=0; i%3CrevCodes.length; i++)%0A              conceptCodes%5Bi%5D = revCodes%5BrevCodes.length - 1 - i%5D;%0A%0A            if(conceptCodes.length %3E 0) %7B%0A              try %7B%0A                if(!cadsrModule.matchDEToPropertyConcepts(inheritedDE, conceptCodes)) %7B%0A                  items.addItem(new ValidationError%0A                                (PropertyAccessor.getProperty%0A                                 (%22inherited.concept.mismatch%22, LookupUtil.lookupFullName(inheritedDE)), inheritedDE));%0A                %7D%0A              %7D catch (Exception e) %7B%0A                logger.error(e);%0A              %7D // end of try-catch%0A            %7D
 %0A//     
@@ -3528,24 +3528,24 @@
     %7D%0A    %7D%0A
-
     return i
@@ -3555,9 +3555,96 @@
 s;%0A  %7D%0A%0A
+  public void setCadsrModule(CadsrModule module) %7B%0A    this.cadsrModule = module;%0A  %7D%0A%0A
 %7D

verify deID to concept