/**
   * Create the ValueObject collection to hold the set of DomainObjects.
   *
   * @param map - maps DomainObjects to created ValueObjects
   * @param domainObjectSet - Set of ims.core.clinical.domain.objects.Medication objects.
   */
  public static ims.core.vo.MedicationVoCollection createMedicationVoCollectionFromMedication(
      DomainObjectMap map, java.util.Set domainObjectSet) {
    ims.core.vo.MedicationVoCollection voList = new ims.core.vo.MedicationVoCollection();
    if (null == domainObjectSet) {
      return voList;
    }
    int rieCount = 0;
    int activeCount = 0;
    java.util.Iterator iterator = domainObjectSet.iterator();
    while (iterator.hasNext()) {
      ims.core.clinical.domain.objects.Medication domainObject =
          (ims.core.clinical.domain.objects.Medication) iterator.next();
      ims.core.vo.MedicationVo vo = create(map, domainObject);

      if (vo != null) voList.add(vo);

      if (domainObject != null) {
        if (domainObject.getIsRIE() != null && domainObject.getIsRIE().booleanValue() == true)
          rieCount++;
        else activeCount++;
      }
    }
    voList.setRieCount(rieCount);
    voList.setActiveCount(activeCount);
    return voList;
  }
  /**
   * Create the ValueObject collection to hold the list of DomainObjects.
   *
   * @param map - maps DomainObjects to created ValueObjects
   * @param domainObjectList - List of ims.core.clinical.domain.objects.Medication objects.
   */
  public static ims.core.vo.MedicationVoCollection createMedicationVoCollectionFromMedication(
      DomainObjectMap map, java.util.List domainObjectList) {
    ims.core.vo.MedicationVoCollection voList = new ims.core.vo.MedicationVoCollection();
    if (null == domainObjectList) {
      return voList;
    }
    int rieCount = 0;
    int activeCount = 0;
    for (int i = 0; i < domainObjectList.size(); i++) {
      ims.core.clinical.domain.objects.Medication domainObject =
          (ims.core.clinical.domain.objects.Medication) domainObjectList.get(i);
      ims.core.vo.MedicationVo vo = create(map, domainObject);

      if (vo != null) voList.add(vo);

      if (domainObject != null) {
        if (domainObject.getIsRIE() != null && domainObject.getIsRIE().booleanValue() == true)
          rieCount++;
        else activeCount++;
      }
    }

    voList.setRieCount(rieCount);
    voList.setActiveCount(activeCount);
    return voList;
  }
  /**
   * Create the ValueObject from the ims.core.clinical.domain.objects.Medication object.
   *
   * @param map DomainObjectMap of DomainObjects to already created ValueObjects.
   * @param domainObject
   */
  public static ims.core.vo.MedicationVo create(
      DomainObjectMap map, ims.core.clinical.domain.objects.Medication domainObject) {
    if (null == domainObject) {
      return null;
    }
    // check if the domainObject already has a valueObject created for it
    ims.core.vo.MedicationVo valueObject =
        (ims.core.vo.MedicationVo) map.getValueObject(domainObject, ims.core.vo.MedicationVo.class);
    if (null == valueObject) {
      valueObject = new ims.core.vo.MedicationVo(domainObject.getId(), domainObject.getVersion());
      map.addValueObject(domainObject, valueObject);

      valueObject = insert(map, valueObject, domainObject);
    }
    return valueObject;
  }
  public static ims.core.clinical.domain.objects.Medication extractMedication(
      ims.domain.ILightweightDomainFactory domainFactory,
      ims.core.vo.MedicationVo valueObject,
      HashMap domMap) {
    if (null == valueObject) {
      return null;
    }
    Integer id = valueObject.getID_Medication();
    ims.core.clinical.domain.objects.Medication domainObject = null;
    if (null == id) {
      if (domMap.get(valueObject) != null) {
        return (ims.core.clinical.domain.objects.Medication) domMap.get(valueObject);
      }
      // ims.core.vo.MedicationVo ID_Medication field is unknown
      domainObject = new ims.core.clinical.domain.objects.Medication();
      domMap.put(valueObject, domainObject);
    } else {
      String key = (valueObject.getClass().getName() + "__" + valueObject.getID_Medication());
      if (domMap.get(key) != null) {
        return (ims.core.clinical.domain.objects.Medication) domMap.get(key);
      }
      domainObject =
          (ims.core.clinical.domain.objects.Medication)
              domainFactory.getDomainObject(ims.core.clinical.domain.objects.Medication.class, id);

      // TODO: Not sure how this should be handled. Effectively it must be a staleobject exception,
      // but maybe should be handled as that further up.
      if (domainObject == null) return null;

      domMap.put(key, domainObject);
    }
    domainObject.setVersion(valueObject.getVersion_Medication());

    domainObject.setTaxonomyMap(
        ims.core.vo.domain.TaxonomyMapAssembler.extractTaxonomyMapList(
            domainFactory, valueObject.getTaxonomyMap(), domainObject.getTaxonomyMap(), domMap));
    domainObject.setKeywords(
        ims.core.vo.domain.KeywordVoAssembler.extractKeywordList(
            domainFactory, valueObject.getKeywords(), domainObject.getKeywords(), domMap));
    // This is to overcome a bug in both Sybase and Oracle which prevents them from storing an empty
    // string correctly
    // Sybase stores it as a single space, Oracle stores it as NULL. This fix will make them
    // consistent at least.
    if (valueObject.getMedicationName() != null && valueObject.getMedicationName().equals("")) {
      valueObject.setMedicationName(null);
    }
    domainObject.setMedicationName(valueObject.getMedicationName());
    domainObject.setIsActive(valueObject.getIsActive());
    domainObject.setIsControlledDrug(valueObject.getIsControlledDrug());
    // create LookupInstance from vo LookupType
    ims.domain.lookups.LookupInstance value6 = null;
    if (null != valueObject.getFrequencyDefault()) {
      value6 = domainFactory.getLookupInstance(valueObject.getFrequencyDefault().getID());
    }
    domainObject.setFrequencyDefault(value6);
    domainObject.setNoOfDaysSupplyDefault(valueObject.getNoOfDaysSupplyDefault());

    return domainObject;
  }
  /**
   * Update the ValueObject with the Domain Object.
   *
   * @param map DomainObjectMap of DomainObjects to already created ValueObjects.
   * @param valueObject to be updated
   * @param domainObject ims.core.clinical.domain.objects.Medication
   */
  public static ims.core.vo.MedicationVo insert(
      DomainObjectMap map,
      ims.core.vo.MedicationVo valueObject,
      ims.core.clinical.domain.objects.Medication domainObject) {
    if (null == domainObject) {
      return valueObject;
    }
    if (null == map) {
      map = new DomainObjectMap();
    }

    valueObject.setID_Medication(domainObject.getId());
    valueObject.setIsRIE(domainObject.getIsRIE());

    // If this is a recordedInError record, and the domainObject
    // value isIncludeRecord has not been set, then we return null and
    // not the value object
    if (valueObject.getIsRIE() != null
        && valueObject.getIsRIE().booleanValue() == true
        && !domainObject.isIncludeRecord()) return null;

    // If this is not a recordedInError record, and the domainObject
    // value isIncludeRecord has been set, then we return null and
    // not the value object
    if ((valueObject.getIsRIE() == null || valueObject.getIsRIE().booleanValue() == false)
        && domainObject.isIncludeRecord()) return null;

    // TaxonomyMap
    valueObject.setTaxonomyMap(
        ims.core.vo.domain.TaxonomyMapAssembler.createTaxonomyMapCollectionFromTaxonomyMap(
            map, domainObject.getTaxonomyMap()));
    // Keywords
    valueObject.setKeywords(
        ims.core.vo.domain.KeywordVoAssembler.createKeywordVoCollectionFromKeyword(
            map, domainObject.getKeywords()));
    // MedicationName
    valueObject.setMedicationName(domainObject.getMedicationName());
    // isActive
    valueObject.setIsActive(domainObject.isIsActive());
    // isControlledDrug
    valueObject.setIsControlledDrug(domainObject.isIsControlledDrug());
    // FrequencyDefault
    ims.domain.lookups.LookupInstance instance6 = domainObject.getFrequencyDefault();
    if (null != instance6) {
      ims.framework.utils.ImagePath img = null;
      ims.framework.utils.Color color = null;
      img = null;
      if (instance6.getImage() != null) {
        img =
            new ims.framework.utils.ImagePath(
                instance6.getImage().getImageId(), instance6.getImage().getImagePath());
      }
      color = instance6.getColor();
      if (color != null) color.getValue();

      ims.core.vo.lookups.MedicationFrequency voLookup6 =
          new ims.core.vo.lookups.MedicationFrequency(
              instance6.getId(), instance6.getText(), instance6.isActive(), null, img, color);
      ims.core.vo.lookups.MedicationFrequency parentVoLookup6 = voLookup6;
      ims.domain.lookups.LookupInstance parent6 = instance6.getParent();
      while (parent6 != null) {
        if (parent6.getImage() != null) {
          img =
              new ims.framework.utils.ImagePath(
                  parent6.getImage().getImageId(), parent6.getImage().getImagePath());
        } else {
          img = null;
        }
        color = parent6.getColor();
        if (color != null) color.getValue();
        parentVoLookup6.setParent(
            new ims.core.vo.lookups.MedicationFrequency(
                parent6.getId(), parent6.getText(), parent6.isActive(), null, img, color));
        parentVoLookup6 = parentVoLookup6.getParent();
        parent6 = parent6.getParent();
      }
      valueObject.setFrequencyDefault(voLookup6);
    }
    // NoOfDaysSupplyDefault
    valueObject.setNoOfDaysSupplyDefault(domainObject.getNoOfDaysSupplyDefault());
    return valueObject;
  }