/**
   * 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.SupportNetwork objects.
   */
  public static ims.core.vo.SupportNetworkVoCollection
      createSupportNetworkVoCollectionFromSupportNetwork(
          DomainObjectMap map, java.util.Set domainObjectSet) {
    ims.core.vo.SupportNetworkVoCollection voList = new ims.core.vo.SupportNetworkVoCollection();
    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.SupportNetwork domainObject =
          (ims.core.clinical.domain.objects.SupportNetwork) iterator.next();
      ims.core.vo.SupportNetworkVo 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.SupportNetwork objects.
   */
  public static ims.core.vo.SupportNetworkVoCollection
      createSupportNetworkVoCollectionFromSupportNetwork(
          DomainObjectMap map, java.util.List domainObjectList) {
    ims.core.vo.SupportNetworkVoCollection voList = new ims.core.vo.SupportNetworkVoCollection();
    if (null == domainObjectList) {
      return voList;
    }
    int rieCount = 0;
    int activeCount = 0;
    for (int i = 0; i < domainObjectList.size(); i++) {
      ims.core.clinical.domain.objects.SupportNetwork domainObject =
          (ims.core.clinical.domain.objects.SupportNetwork) domainObjectList.get(i);
      ims.core.vo.SupportNetworkVo 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.SupportNetwork object.
   *
   * @param map DomainObjectMap of DomainObjects to already created ValueObjects.
   * @param domainObject
   */
  public static ims.core.vo.SupportNetworkVo create(
      DomainObjectMap map, ims.core.clinical.domain.objects.SupportNetwork domainObject) {
    if (null == domainObject) {
      return null;
    }
    // check if the domainObject already has a valueObject created for it
    ims.core.vo.SupportNetworkVo valueObject =
        (ims.core.vo.SupportNetworkVo)
            map.getValueObject(domainObject, ims.core.vo.SupportNetworkVo.class);
    if (null == valueObject) {
      valueObject =
          new ims.core.vo.SupportNetworkVo(domainObject.getId(), domainObject.getVersion());
      map.addValueObject(domainObject, valueObject);

      valueObject = insert(map, valueObject, domainObject);
    }
    return valueObject;
  }
  public static ims.core.clinical.domain.objects.SupportNetwork extractSupportNetwork(
      ims.domain.ILightweightDomainFactory domainFactory,
      ims.core.vo.SupportNetworkVo valueObject,
      HashMap domMap) {
    if (null == valueObject) {
      return null;
    }
    Integer id = valueObject.getID_SupportNetwork();
    ims.core.clinical.domain.objects.SupportNetwork domainObject = null;
    if (null == id) {
      if (domMap.get(valueObject) != null) {
        return (ims.core.clinical.domain.objects.SupportNetwork) domMap.get(valueObject);
      }
      // ims.core.vo.SupportNetworkVo ID_SupportNetwork field is unknown
      domainObject = new ims.core.clinical.domain.objects.SupportNetwork();
      domMap.put(valueObject, domainObject);
    } else {
      String key = (valueObject.getClass().getName() + "__" + valueObject.getID_SupportNetwork());
      if (domMap.get(key) != null) {
        return (ims.core.clinical.domain.objects.SupportNetwork) domMap.get(key);
      }
      domainObject =
          (ims.core.clinical.domain.objects.SupportNetwork)
              domainFactory.getDomainObject(
                  ims.core.clinical.domain.objects.SupportNetwork.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_SupportNetwork());

    domainObject.setProfessional(
        ims.core.vo.domain.SupportNetworkProfessionalVoAssembler
            .extractSupportNetworkProfessionalSet(
                domainFactory,
                valueObject.getProfessional(),
                domainObject.getProfessional(),
                domMap));
    domainObject.setServices(
        ims.core.vo.domain.SupportNetworkServicesVoAssembler.extractSupportNetworkServicesSet(
            domainFactory, valueObject.getServices(), domainObject.getServices(), domMap));
    // SaveAsRefVO - treated as a refVo in extract methods
    ims.core.admin.domain.objects.ClinicalContact value3 = null;
    if (null != valueObject.getClinicalContact()) {
      if (valueObject.getClinicalContact().getBoId() == null) {
        if (domMap.get(valueObject.getClinicalContact()) != null) {
          value3 =
              (ims.core.admin.domain.objects.ClinicalContact)
                  domMap.get(valueObject.getClinicalContact());
        }
      } else {
        value3 =
            (ims.core.admin.domain.objects.ClinicalContact)
                domainFactory.getDomainObject(
                    ims.core.admin.domain.objects.ClinicalContact.class,
                    valueObject.getClinicalContact().getBoId());
      }
    }
    domainObject.setClinicalContact(value3);
    ims.core.admin.domain.objects.CareContext value4 = null;
    if (null != valueObject.getCareContext()) {
      if (valueObject.getCareContext().getBoId() == null) {
        if (domMap.get(valueObject.getCareContext()) != null) {
          value4 =
              (ims.core.admin.domain.objects.CareContext) domMap.get(valueObject.getCareContext());
        }
      } else if (valueObject.getBoVersion()
          == -1) // RefVo was not modified since obtained from the Assembler, no need to update the
      // BO field
      {
        value4 = domainObject.getCareContext();
      } else {
        value4 =
            (ims.core.admin.domain.objects.CareContext)
                domainFactory.getDomainObject(
                    ims.core.admin.domain.objects.CareContext.class,
                    valueObject.getCareContext().getBoId());
      }
    }
    domainObject.setCareContext(value4);
    ims.core.patient.domain.objects.Patient value5 = null;
    if (null != valueObject.getPatient()) {
      if (valueObject.getPatient().getBoId() == null) {
        if (domMap.get(valueObject.getPatient()) != null) {
          value5 = (ims.core.patient.domain.objects.Patient) domMap.get(valueObject.getPatient());
        }
      } else if (valueObject.getBoVersion()
          == -1) // RefVo was not modified since obtained from the Assembler, no need to update the
      // BO field
      {
        value5 = domainObject.getPatient();
      } else {
        value5 =
            (ims.core.patient.domain.objects.Patient)
                domainFactory.getDomainObject(
                    ims.core.patient.domain.objects.Patient.class,
                    valueObject.getPatient().getBoId());
      }
    }
    domainObject.setPatient(value5);

    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.SupportNetwork
   */
  public static ims.core.vo.SupportNetworkVo insert(
      DomainObjectMap map,
      ims.core.vo.SupportNetworkVo valueObject,
      ims.core.clinical.domain.objects.SupportNetwork domainObject) {
    if (null == domainObject) {
      return valueObject;
    }
    if (null == map) {
      map = new DomainObjectMap();
    }

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

    // Professional
    valueObject.setProfessional(
        ims.core.vo.domain.SupportNetworkProfessionalVoAssembler
            .createSupportNetworkProfessionalVoCollectionFromSupportNetworkProfessional(
                map, domainObject.getProfessional()));
    // Services
    valueObject.setServices(
        ims.core.vo.domain.SupportNetworkServicesVoAssembler
            .createSupportNetworkServicesVoCollectionFromSupportNetworkServices(
                map, domainObject.getServices()));
    // ClinicalContact
    valueObject.setClinicalContact(
        ims.core.vo.domain.ClinicalContactVoAssembler.create(
            map, domainObject.getClinicalContact()));
    // CareContext
    if (domainObject.getCareContext() != null) {
      if (domainObject.getCareContext()
          instanceof
          HibernateProxy) // If the proxy is set, there is no need to lazy load, the proxy knows the
      // id already.
      {
        HibernateProxy p = (HibernateProxy) domainObject.getCareContext();
        int id = Integer.parseInt(p.getHibernateLazyInitializer().getIdentifier().toString());
        valueObject.setCareContext(new ims.core.admin.vo.CareContextRefVo(id, -1));
      } else {
        valueObject.setCareContext(
            new ims.core.admin.vo.CareContextRefVo(
                domainObject.getCareContext().getId(), domainObject.getCareContext().getVersion()));
      }
    }
    // Patient
    if (domainObject.getPatient() != null) {
      if (domainObject.getPatient()
          instanceof
          HibernateProxy) // If the proxy is set, there is no need to lazy load, the proxy knows the
      // id already.
      {
        HibernateProxy p = (HibernateProxy) domainObject.getPatient();
        int id = Integer.parseInt(p.getHibernateLazyInitializer().getIdentifier().toString());
        valueObject.setPatient(new ims.core.patient.vo.PatientRefVo(id, -1));
      } else {
        valueObject.setPatient(
            new ims.core.patient.vo.PatientRefVo(
                domainObject.getPatient().getId(), domainObject.getPatient().getVersion()));
      }
    }
    return valueObject;
  }