private void add(
      CodingSchemeReference codingSchemeReference,
      ValueSetDefinitionEntry cts2Entry,
      org.LexGrid.valueSets.ValueSetDefinition definition) {
    CompleteCodeSystemReference ccr = new CompleteCodeSystemReference();

    String codeSystemName = codingSchemeReference.getCodingScheme();

    SupportedCodingScheme supportedCodingScheme = this.findCodingScheme(definition, codeSystemName);

    ccr.setCodeSystem(
        this.getTransformUtils()
            .toCodeSystemReference(
                supportedCodingScheme.getLocalId(), supportedCodingScheme.getUri()));

    cts2Entry.setCompleteCodeSystem(ccr);
  }
  private void add(
      PropertyReference propertyReference,
      ValueSetDefinitionEntry cts2Entry,
      org.LexGrid.valueSets.ValueSetDefinition definition) {
    PropertyQueryReference pqr = new PropertyQueryReference();

    String codingScheme = propertyReference.getCodingScheme();
    SupportedCodingScheme supportedCodingScheme = this.findCodingScheme(definition, codingScheme);
    pqr.setCodeSystem(
        this.getTransformUtils()
            .toCodeSystemReference(
                supportedCodingScheme.getLocalId(), supportedCodingScheme.getUri()));

    String propertyName = propertyReference.getPropertyName();
    String matchAlgorithm = propertyReference.getPropertyMatchValue().getMatchAlgorithm();
    String matchValue = propertyReference.getPropertyMatchValue().getContent();

    SupportedProperty supportedProperty = this.findProperty(definition.getMappings(), propertyName);

    String propName;
    String propUri;
    if (StringUtils.isBlank(propertyName)) {
      // TODO: What is the correct behavior here?
      propName = "UNSPECIFIED";
      propUri = "UNSPECIFIED";
    } else {
      propName = supportedProperty.getLocalId();
      propUri = supportedProperty.getUri();
    }

    FilterComponent filter = new FilterComponent();
    filter.setMatchAlgorithm(new MatchAlgorithmReference());
    filter.getMatchAlgorithm().setContent(matchAlgorithm);
    filter.setPropertyReference(new URIAndEntityName());
    filter.getPropertyReference().setName(propName);
    filter.getPropertyReference().setUri(propUri);

    filter.setMatchValue(matchValue);

    pqr.setFilter(filter);
    cts2Entry.setPropertyQuery(pqr);
  }
  private void add(
      ValueSetDefinitionReference valueSetDefinitionReference, ValueSetDefinitionEntry cts2Entry) {
    String uri = valueSetDefinitionReference.getValueSetDefinitionURI();

    String name;
    try {
      name =
          this.lexEVSValueSetDefinitionServices
              .getValueSetDefinition(new URI(uri), null)
              .getValueSetDefinitionName();
    } catch (Exception e) {
      // couldn't find it in LexEVS -- use the URI.
      name = uri;
    }

    ValueSetReference ref = this.getTransformUtils().toValueSetReference(name);

    CompleteValueSetReference vsr = new CompleteValueSetReference();
    vsr.setValueSet(ref);

    cts2Entry.setCompleteValueSet(vsr);
  }
  public ValueSetDefinitionListEntry transformFullDescription(
      org.LexGrid.valueSets.ValueSetDefinition lexEvsVSD) {
    if (lexEvsVSD == null) {
      return null;
    }

    ValueSetDefinition cts2VSD = new ValueSetDefinition();

    cts2VSD.setAbout(lexEvsVSD.getValueSetDefinitionURI());
    cts2VSD.setDocumentURI(lexEvsVSD.getValueSetDefinitionURI());

    cts2VSD.setFormalName(lexEvsVSD.getValueSetDefinitionURI());

    SourceAndNotation sourceAndNotation = new SourceAndNotation();
    sourceAndNotation.setSourceAndNotationDescription("LexEVS");
    cts2VSD.setSourceAndNotation(sourceAndNotation);

    if (lexEvsVSD.getEntityDescription() != null) {
      String content = lexEvsVSD.getEntityDescription().getContent();
      EntryDescription description = new EntryDescription();
      description.setValue(ModelUtils.toTsAnyType(content));

      cts2VSD.setResourceSynopsis(description);
    }

    cts2VSD.setDefinedValueSet(
        this.getTransformUtils().toValueSetReference(lexEvsVSD.getValueSetDefinitionName()));

    for (DefinitionEntry entry : lexEvsVSD.getDefinitionEntry()) {
      ValueSetDefinitionEntry cts2Entry = new ValueSetDefinitionEntry();

      if (entry.getCodingSchemeReference() != null) {
        this.add(entry.getCodingSchemeReference(), cts2Entry, lexEvsVSD);
      }
      if (entry.getEntityReference() != null) {
        this.add(entry.getEntityReference(), cts2Entry, lexEvsVSD);
      }
      if (entry.getPropertyReference() != null) {
        this.add(entry.getPropertyReference(), cts2Entry, lexEvsVSD);
      }
      if (entry.getValueSetDefinitionReference() != null) {
        this.add(entry.getValueSetDefinitionReference(), cts2Entry);
      }

      Long order = entry.getRuleOrder();
      if (order == null || order == 0) {
        order = 1l;
      }
      cts2Entry.setEntryOrder(order);
      cts2Entry.setOperator(this.toSetOperator(entry.getOperator()));

      cts2VSD.addEntry(cts2Entry);
    }

    ValueSetDefinitionListEntry listEntry = new ValueSetDefinitionListEntry();
    listEntry.addEntry(cts2VSD);

    ValueSetNamePair pair =
        this.valueSetNameTranslator.getDefinitionNameAndVersion(
            lexEvsVSD.getValueSetDefinitionURI());

    listEntry.setResourceName(pair.getDefinitionLocalId());

    listEntry.setHref(
        this.getUrlConstructor()
            .createValueSetDefinitionUrl(pair.getValueSetName(), pair.getDefinitionLocalId()));

    return listEntry;
  }
  private void add(
      EntityReference entityReference,
      ValueSetDefinitionEntry cts2Entry,
      org.LexGrid.valueSets.ValueSetDefinition lexEvsVSD) {
    URIAndEntityName uriAndName = new URIAndEntityName();
    uriAndName.setName(entityReference.getEntityCode());

    String namespace = entityReference.getEntityCodeNamespace();
    if (StringUtils.isBlank(namespace)) {
      namespace = lexEvsVSD.getDefaultCodingScheme();
    }

    uriAndName.setNamespace(namespace);

    SupportedNamespace supportedNamespace = this.findNamespace(lexEvsVSD.getMappings(), namespace);

    if (supportedNamespace == null) {
      log.warn(
          "EntityRefernece: "
              + entityReference.getEntityCodeNamespace()
              + ":"
              + entityReference.getEntityCode()
              + " does not have a valid namespace.");

      try {
        CodingScheme cs = this.lexBigService.resolveCodingScheme(namespace, null);
        SupportedNamespace sns = new SupportedNamespace();
        sns.setContent(cs.getCodingSchemeName());
        sns.setEquivalentCodingScheme(cs.getCodingSchemeName());
        sns.setLocalId(cs.getCodingSchemeName());
        sns.setUri(cs.getCodingSchemeURI());

        supportedNamespace = sns;
      } catch (LBException e) {
        log.info(e);

        // we have to punt here
        SupportedNamespace sns = new SupportedNamespace();
        sns.setContent("unknown");
        sns.setEquivalentCodingScheme("unknown");
        sns.setLocalId("unknown");
        sns.setUri("unknown");
      }
    }

    uriAndName.setUri(
        UriUtils.combine(supportedNamespace.getUri(), entityReference.getEntityCode()));

    if (entityReference.getReferenceAssociation() != null) {
      AssociatedEntitiesReference ref = new AssociatedEntitiesReference();
      ref.setReferencedEntity(uriAndName);
      ref.setDirection(
          entityReference.getTargetToSource()
              ? AssociationDirection.TARGET_TO_SOURCE
              : AssociationDirection.SOURCE_TO_TARGET);

      String cs = supportedNamespace.getEquivalentCodingScheme();
      SupportedCodingScheme supportedCodingScheme = this.findCodingScheme(lexEvsVSD, cs);

      CodeSystemReference codingSchemeRef =
          this.getTransformUtils()
              .toCodeSystemReference(
                  supportedCodingScheme.getLocalId(), supportedCodingScheme.getUri());

      ref.setCodeSystem(codingSchemeRef);

      ref.setTransitivity(
          entityReference.getTransitiveClosure()
              ? TransitiveClosure.TRANSITIVE_CLOSURE
              : TransitiveClosure.DIRECTLY_ASSOCIATED);

      ref.setLeafOnly(
          entityReference.getLeafOnly() ? LeafOrAll.LEAF_ONLY : LeafOrAll.ALL_INTERMEDIATE_NODES);

      String association = entityReference.getReferenceAssociation();
      PredicateReference predicate = new PredicateReference();
      predicate.setName(association);

      // namespace for a predicate is not guaranteed in LexEVS... not sure
      // how we want to handle this. For now, assume the same namespace
      // as the starting entity.
      SupportedAssociation supportedAssociation =
          this.findAssociation(lexEvsVSD.getMappings(), association);

      if (supportedAssociation == null) {
        predicate.setNamespace(supportedCodingScheme.getLocalId());
        predicate.setUri(UriUtils.combine(supportedCodingScheme.getUri(), association));
      } else {
        predicate.setNamespace(supportedAssociation.getEntityCodeNamespace());
        if (StringUtils.isNotBlank(supportedAssociation.getUri())) {
          predicate.setUri(supportedAssociation.getUri());
        } else {
          predicate.setUri(UriUtils.combine(supportedCodingScheme.getUri(), association));
        }
      }

      ref.setPredicate(predicate);

      cts2Entry.setAssociatedEntities(ref);
    } else {
      SpecificEntityList sel = new SpecificEntityList();

      sel.addReferencedEntity(uriAndName);

      cts2Entry.setEntityList(sel);
    }
  }