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

    valueObject.setID_PatientProcedureChecklist(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;

    // ClinicalContact
    if (domainObject.getClinicalContact() != null) {
      if (domainObject.getClinicalContact()
          instanceof
          HibernateProxy) // If the proxy is set, there is no need to lazy load, the proxy knows the
                          // id already.
      {
        HibernateProxy p = (HibernateProxy) domainObject.getClinicalContact();
        int id = Integer.parseInt(p.getHibernateLazyInitializer().getIdentifier().toString());
        valueObject.setClinicalContact(new ims.core.admin.vo.ClinicalContactRefVo(id, -1));
      } else {
        valueObject.setClinicalContact(
            new ims.core.admin.vo.ClinicalContactRefVo(
                domainObject.getClinicalContact().getId(),
                domainObject.getClinicalContact().getVersion()));
      }
    }
    // PresentProcedures
    valueObject.setPresentProcedures(
        ims.core.vo.domain.PatientProcedureVoAssembler
            .createPatientProcedureVoCollectionFromPatientProcedure(
                map, domainObject.getPresentProcedures()));
    // OtherProcedures
    valueObject.setOtherProcedures(
        ims.clinical.vo.domain.PatientAbsentProcedureVoAssembler
            .createPatientAbsentProcedureVoCollectionFromPatientAbsentProcedure(
                map, domainObject.getOtherProcedures()));
    // AuthoringInformation
    valueObject.setAuthoringInformation(
        ims.core.vo.domain.AuthoringInformationVoAssembler.create(
            map, domainObject.getAuthoringInformation()));
    return valueObject;
  }
  public static java.util.Set extractPatientAbsentProcedureSet(
      ims.domain.ILightweightDomainFactory domainFactory,
      ims.clinical.vo.PatientAbsentProcedureVoCollection voCollection,
      java.util.Set domainObjectSet,
      HashMap domMap) {
    int size = (null == voCollection) ? 0 : voCollection.size();
    if (domainObjectSet == null) {
      domainObjectSet = new java.util.HashSet();
    }
    java.util.Set newSet = new java.util.HashSet();
    for (int i = 0; i < size; i++) {
      ims.clinical.vo.PatientAbsentProcedureVo vo = voCollection.get(i);
      ims.clinical.domain.objects.PatientAbsentProcedure domainObject =
          PatientAbsentProcedureVoAssembler.extractPatientAbsentProcedure(
              domainFactory, vo, domMap);

      // TODO: This can only occur in the situation of a stale object exception. For now leave it to
      // the Interceptor to handle it.
      if (domainObject == null) {
        continue;
      }

      // Trying to avoid the hibernate collection being marked as dirty via its public interface
      // methods. (like add)
      if (!domainObjectSet.contains(domainObject)) domainObjectSet.add(domainObject);
      newSet.add(domainObject);
    }
    java.util.Set removedSet = new java.util.HashSet();
    java.util.Iterator iter = domainObjectSet.iterator();
    // Find out which objects need to be removed
    while (iter.hasNext()) {
      ims.domain.DomainObject o = (ims.domain.DomainObject) iter.next();
      if ((o == null || o.getIsRIE() == null || !o.getIsRIE().booleanValue())
          && !newSet.contains(o)) {
        removedSet.add(o);
      }
    }
    iter = removedSet.iterator();
    // Remove the unwanted objects
    while (iter.hasNext()) {
      domainObjectSet.remove(iter.next());
    }
    return domainObjectSet;
  }
  public static java.util.List extractPatientAbsentProcedureList(
      ims.domain.ILightweightDomainFactory domainFactory,
      ims.clinical.vo.PatientAbsentProcedureVoCollection voCollection,
      java.util.List domainObjectList,
      HashMap domMap) {
    int size = (null == voCollection) ? 0 : voCollection.size();
    if (domainObjectList == null) {
      domainObjectList = new java.util.ArrayList();
    }
    for (int i = 0; i < size; i++) {
      ims.clinical.vo.PatientAbsentProcedureVo vo = voCollection.get(i);
      ims.clinical.domain.objects.PatientAbsentProcedure domainObject =
          PatientAbsentProcedureVoAssembler.extractPatientAbsentProcedure(
              domainFactory, vo, domMap);

      // TODO: This can only occur in the situation of a stale object exception. For now leave it to
      // the Interceptor to handle it.
      if (domainObject == null) {
        continue;
      }

      int domIdx = domainObjectList.indexOf(domainObject);
      if (domIdx == -1) {
        domainObjectList.add(i, domainObject);
      } else if (i != domIdx && i < domainObjectList.size()) {
        Object tmp = domainObjectList.get(i);
        domainObjectList.set(i, domainObjectList.get(domIdx));
        domainObjectList.set(domIdx, tmp);
      }
    }

    // Remove all ones in domList where index > voCollection.size() as these should
    // now represent the ones removed from the VO collection. No longer referenced.
    int i1 = domainObjectList.size();
    while (i1 > size) {
      domainObjectList.remove(i1 - 1);
      i1 = domainObjectList.size();
    }
    return domainObjectList;
  }
  public static ims.clinical.domain.objects.PatientProcedureChecklist
      extractPatientProcedureChecklist(
          ims.domain.ILightweightDomainFactory domainFactory,
          ims.clinical.vo.PatientProcedureChecklistVo valueObject,
          HashMap domMap) {
    if (null == valueObject) {
      return null;
    }
    Integer id = valueObject.getID_PatientProcedureChecklist();
    ims.clinical.domain.objects.PatientProcedureChecklist domainObject = null;
    if (null == id) {
      if (domMap.get(valueObject) != null) {
        return (ims.clinical.domain.objects.PatientProcedureChecklist) domMap.get(valueObject);
      }
      // ims.clinical.vo.PatientProcedureChecklistVo ID_PatientProcedureChecklist field is unknown
      domainObject = new ims.clinical.domain.objects.PatientProcedureChecklist();
      domMap.put(valueObject, domainObject);
    } else {
      String key =
          (valueObject.getClass().getName() + "__" + valueObject.getID_PatientProcedureChecklist());
      if (domMap.get(key) != null) {
        return (ims.clinical.domain.objects.PatientProcedureChecklist) domMap.get(key);
      }
      domainObject =
          (ims.clinical.domain.objects.PatientProcedureChecklist)
              domainFactory.getDomainObject(
                  ims.clinical.domain.objects.PatientProcedureChecklist.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_PatientProcedureChecklist());

    ims.core.admin.domain.objects.ClinicalContact value1 = null;
    if (null != valueObject.getClinicalContact()) {
      if (valueObject.getClinicalContact().getBoId() == null) {
        if (domMap.get(valueObject.getClinicalContact()) != null) {
          value1 =
              (ims.core.admin.domain.objects.ClinicalContact)
                  domMap.get(valueObject.getClinicalContact());
        }
      } else if (valueObject.getBoVersion()
          == -1) // RefVo was not modified since obtained from the Assembler, no need to update the
                 // BO field
      {
        value1 = domainObject.getClinicalContact();
      } else {
        value1 =
            (ims.core.admin.domain.objects.ClinicalContact)
                domainFactory.getDomainObject(
                    ims.core.admin.domain.objects.ClinicalContact.class,
                    valueObject.getClinicalContact().getBoId());
      }
    }
    domainObject.setClinicalContact(value1);
    domainObject.setPresentProcedures(
        ims.core.vo.domain.PatientProcedureVoAssembler.extractPatientProcedureSet(
            domainFactory,
            valueObject.getPresentProcedures(),
            domainObject.getPresentProcedures(),
            domMap));
    domainObject.setOtherProcedures(
        ims.clinical.vo.domain.PatientAbsentProcedureVoAssembler.extractPatientAbsentProcedureSet(
            domainFactory,
            valueObject.getOtherProcedures(),
            domainObject.getOtherProcedures(),
            domMap));
    domainObject.setAuthoringInformation(
        ims.core.vo.domain.AuthoringInformationVoAssembler.extractAuthoringInformation(
            domainFactory, valueObject.getAuthoringInformation(), domMap));

    return domainObject;
  }