public ElementVerification verifyMessages(Profile p, Profile baseP, String id, String type) {
   ElementVerification evm = new ElementVerification(id, type);
   for (Message m : p.getMessages().getChildren()) {
     evm.addChildrenVerification(verifyMessages(p, baseP, m.getId(), m.getType()));
   }
   return evm;
 }
  public ElementVerification verifyMessage(Profile p, Profile baseP, String id, String type) {
    ElementVerification evm = new ElementVerification(id, type);
    Message m = p.getMessages().findOne(id);

    for (SegmentRefOrGroup srog : m.getChildren()) {
      evm.addChildrenVerification(verifySegmentOrGroup(p, baseP, srog.getId(), srog.getType()));
    }
    return evm;
  }
 public ElementVerification verifySegment(Profile p, Profile baseP, String id, String type) {
   ElementVerification evs = new ElementVerification(id, type);
   Segment s = segmentRepository.findOne(id);
   for (Field f : s.getFields()) {
     ElementVerification evf = verifyField(p, baseP, f.getId(), f.getType());
     evs.addChildrenVerification(evf);
   }
   return evs;
 }
 public ElementVerification verifySegments(Profile p, Profile baseP, String id, String type) {
   ElementVerification evsLib = new ElementVerification(id, type);
   for (SegmentLink sl : p.getSegmentLibrary().getChildren()) {
     Segment s = segmentRepository.findOne(sl.getId());
     ElementVerification evs = verifySegment(p, baseP, s.getId(), s.getType());
     evs.addChildrenVerification(evsLib);
   }
   return evsLib;
 }
 public ElementVerification verifySegmentOrGroup(
     Profile p, Profile baseP, String id, String type) {
   SegmentRefOrGroup srog = p.getMessages().findOneSegmentRefOrGroup(id);
   ElementVerification evSrog = verifySegmentRef(srog); // verify usage, min and max for segref and
   // group
   if (srog instanceof Group) {
     evSrog.addChildrenVerification(verifyGroup(srog));
   }
   return evSrog;
 }
 private ElementVerification verifyGroup(SegmentRefOrGroup srog) {
   ElementVerification evsrog = verifySegmentRef(srog);
   for (SegmentRefOrGroup child : ((Group) srog).getChildren()) {
     if (child instanceof SegmentRef) {
       evsrog.addChildrenVerification(verifySegmentRef(child));
     } else if (child instanceof Group) {
       evsrog.addChildrenVerification(verifyGroup(child));
     }
   }
   return evsrog;
 }
  public ElementVerification verifyUsage(
      Profile p, Profile baseP, String id, String type, String eltName, String eltValue) {
    // Type can be SegmentRef, Group, Field, Component, Code
    // EltName is Usage
    String hl7Version = p.getMetaData().getHl7Version();
    Usage referenceUsage = Usage.R;
    Usage currentUsage = Usage.R;

    switch (type) {
      case "segmentRef":
        SegmentRefOrGroup srog = p.getMessages().findOneSegmentRefOrGroup(id);
        currentUsage = srog.getUsage();
        SegmentRefOrGroup basesrog = baseP.getMessages().findOneSegmentRefOrGroup(id);
        referenceUsage = basesrog.getUsage();
        break;
      case "group":
        SegmentRefOrGroup srog_ = p.getMessages().findOneSegmentRefOrGroup(id);
        currentUsage = srog_.getUsage();
        SegmentRefOrGroup basesrog_ = baseP.getMessages().findOneSegmentRefOrGroup(id);
        referenceUsage = basesrog_.getUsage();
        break;
      case "field":
        // Field f = p.getSegmentLibrary().findOneField(id);
        // currentUsage = f.getUsage();
        // Field basef = baseP.getSegmentLibrary().findOneField(id);
        // referenceUsage = basef.getUsage();
        break;
      case "component":
        // Component c = p.getDatatypeLibrary().findOneComponent(id);
        // currentUsage = c.getUsage();
        // Component basec = baseP.getDatatypeLibrary().findOneComponent(id);
        // referenceUsage = basec.getUsage();
        break;
      case "code":
        // Code cd = p.getTableLibrary().findOneCodeById(id);
        // currentUsage = Usage.fromValue(cd.getCodeUsage());
        // Code basecd = baseP.getTableLibrary().findOneCodeById(id);
        // referenceUsage = Usage.fromValue(basecd.getCodeUsage());
        break;
    }

    String result = this.validateChangeUsage(hl7Version, referenceUsage, currentUsage);
    ElementVerification ev = new ElementVerification(id, type);
    ElementVerificationResult evRst = new ElementVerificationResult(eltName, eltValue, result);
    ev.addElementVerifications(evRst);

    return ev;
  }
  private ElementVerification verifySegmentRef(SegmentRefOrGroup srog) {
    String result = "";
    ElementVerification evsrog = new ElementVerification(srog.getId(), srog.getType());
    ElementVerificationResult evsrogRst =
        new ElementVerificationResult("usage", srog.getUsage().value(), result);
    evsrog.addElementVerifications(evsrogRst);

    result =
        this.validateChangeCardinality(
            String.valueOf(srog.getMin()), srog.getMax(), srog.getUsage());
    evsrogRst = new ElementVerificationResult("min", String.valueOf(srog.getMin()), result);
    evsrog.addElementVerifications(evsrogRst);

    result =
        this.validateChangeCardinality(
            String.valueOf(srog.getMin()), srog.getMax(), srog.getUsage());
    evsrogRst = new ElementVerificationResult("max", String.valueOf(srog.getMax()), result);
    evsrog.addElementVerifications(evsrogRst);

    return evsrog;
  }