public static ims.icps.instantiation.domain.objects.PatientICP extractPatientICP(
      ims.domain.ILightweightDomainFactory domainFactory,
      ims.icp.vo.PatientICPVo valueObject,
      HashMap domMap) {
    if (null == valueObject) {
      return null;
    }
    Integer id = valueObject.getID_PatientICP();
    ims.icps.instantiation.domain.objects.PatientICP domainObject = null;
    if (null == id) {
      if (domMap.get(valueObject) != null) {
        return (ims.icps.instantiation.domain.objects.PatientICP) domMap.get(valueObject);
      }
      // ims.icp.vo.PatientICPVo ID_PatientICP field is unknown
      domainObject = new ims.icps.instantiation.domain.objects.PatientICP();
      domMap.put(valueObject, domainObject);
    } else {
      String key = (valueObject.getClass().getName() + "__" + valueObject.getID_PatientICP());
      if (domMap.get(key) != null) {
        return (ims.icps.instantiation.domain.objects.PatientICP) domMap.get(key);
      }
      domainObject =
          (ims.icps.instantiation.domain.objects.PatientICP)
              domainFactory.getDomainObject(
                  ims.icps.instantiation.domain.objects.PatientICP.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_PatientICP());

    ims.core.patient.domain.objects.Patient value1 = null;
    if (null != valueObject.getPatient()) {
      if (valueObject.getPatient().getBoId() == null) {
        if (domMap.get(valueObject.getPatient()) != null) {
          value1 = (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
      {
        value1 = domainObject.getPatient();
      } else {
        value1 =
            (ims.core.patient.domain.objects.Patient)
                domainFactory.getDomainObject(
                    ims.core.patient.domain.objects.Patient.class,
                    valueObject.getPatient().getBoId());
      }
    }
    domainObject.setPatient(value1);
    ims.core.admin.domain.objects.CareContext value2 = null;
    if (null != valueObject.getCareContext()) {
      if (valueObject.getCareContext().getBoId() == null) {
        if (domMap.get(valueObject.getCareContext()) != null) {
          value2 =
              (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
      {
        value2 = domainObject.getCareContext();
      } else {
        value2 =
            (ims.core.admin.domain.objects.CareContext)
                domainFactory.getDomainObject(
                    ims.core.admin.domain.objects.CareContext.class,
                    valueObject.getCareContext().getBoId());
      }
    }
    domainObject.setCareContext(value2);
    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 if (valueObject.getBoVersion()
          == -1) // RefVo was not modified since obtained from the Assembler, no need to update the
                 // BO field
      {
        value3 = domainObject.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.pas.domain.objects.PASEvent value4 = null;
    if (null != valueObject.getPasEvent()) {
      if (valueObject.getPasEvent().getBoId() == null) {
        if (domMap.get(valueObject.getPasEvent()) != null) {
          value4 =
              (ims.core.admin.pas.domain.objects.PASEvent) domMap.get(valueObject.getPasEvent());
        }
      } else if (valueObject.getBoVersion()
          == -1) // RefVo was not modified since obtained from the Assembler, no need to update the
                 // BO field
      {
        value4 = domainObject.getPasEvent();
      } else {
        value4 =
            (ims.core.admin.pas.domain.objects.PASEvent)
                domainFactory.getDomainObject(
                    ims.core.admin.pas.domain.objects.PASEvent.class,
                    valueObject.getPasEvent().getBoId());
      }
    }
    domainObject.setPasEvent(value4);
    // SaveAsRefVO - treated as a refVo in extract methods
    ims.icps.configuration.domain.objects.ICP value5 = null;
    if (null != valueObject.getICP()) {
      if (valueObject.getICP().getBoId() == null) {
        if (domMap.get(valueObject.getICP()) != null) {
          value5 = (ims.icps.configuration.domain.objects.ICP) domMap.get(valueObject.getICP());
        }
      } else {
        value5 =
            (ims.icps.configuration.domain.objects.ICP)
                domainFactory.getDomainObject(
                    ims.icps.configuration.domain.objects.ICP.class,
                    valueObject.getICP().getBoId());
      }
    }
    domainObject.setICP(value5);
    // SaveAsRefVO - treated as a refVo in extract methods
    ims.core.clinical.domain.objects.AuthoringInformation value6 = null;
    if (null != valueObject.getAuthoringInformation()) {
      if (valueObject.getAuthoringInformation().getBoId() == null) {
        if (domMap.get(valueObject.getAuthoringInformation()) != null) {
          value6 =
              (ims.core.clinical.domain.objects.AuthoringInformation)
                  domMap.get(valueObject.getAuthoringInformation());
        }
      } else {
        value6 =
            (ims.core.clinical.domain.objects.AuthoringInformation)
                domainFactory.getDomainObject(
                    ims.core.clinical.domain.objects.AuthoringInformation.class,
                    valueObject.getAuthoringInformation().getBoId());
      }
    }
    domainObject.setAuthoringInformation(value6);

    // SaveAsRefVO treated as RefValueObject
    ims.icps.instantiation.vo.PatientICPStageRefVoCollection refCollection7 =
        new ims.icps.instantiation.vo.PatientICPStageRefVoCollection();
    if (valueObject.getStages() != null) {
      for (int i7 = 0; i7 < valueObject.getStages().size(); i7++) {
        ims.icps.instantiation.vo.PatientICPStageRefVo ref7 =
            (ims.icps.instantiation.vo.PatientICPStageRefVo) valueObject.getStages().get(i7);
        refCollection7.add(ref7);
      }
    }
    int size7 = (null == refCollection7) ? 0 : refCollection7.size();
    java.util.Set domainStages7 = domainObject.getStages();
    if (domainStages7 == null) {
      domainStages7 = new java.util.HashSet();
    }
    java.util.Set newSet7 = new java.util.HashSet();
    for (int i = 0; i < size7; i++) {
      ims.icps.instantiation.vo.PatientICPStageRefVo vo = refCollection7.get(i);
      ims.icps.instantiation.domain.objects.PatientICPStage dom = null;
      if (null != vo) {
        if (vo.getBoId() == null) {
          if (domMap.get(vo) != null) {
            dom = (ims.icps.instantiation.domain.objects.PatientICPStage) domMap.get(vo);
          }
        } else {
          dom =
              (ims.icps.instantiation.domain.objects.PatientICPStage)
                  domainFactory.getDomainObject(
                      ims.icps.instantiation.domain.objects.PatientICPStage.class, vo.getBoId());
        }
      }

      // Trying to avoid the hibernate collection being marked as dirty via its public interface
      // methods. (like add)
      if (!domainStages7.contains(dom)) {
        domainStages7.add(dom);
      }
      newSet7.add(dom);
    }
    java.util.Set removedSet7 = new java.util.HashSet();
    java.util.Iterator iter7 = domainStages7.iterator();
    // Find out which objects need to be removed
    while (iter7.hasNext()) {
      ims.domain.DomainObject o = (ims.domain.DomainObject) iter7.next();
      if ((o == null || o.getIsRIE() == null || !o.getIsRIE().booleanValue())
          && !newSet7.contains(o)) {
        removedSet7.add(o);
      }
    }
    iter7 = removedSet7.iterator();
    // Remove the unwanted objects
    while (iter7.hasNext()) {
      domainStages7.remove(iter7.next());
    }

    domainObject.setStages(domainStages7);
    domainObject.setHasOutstandingAdminActions(valueObject.getHasOutstandingAdminActions());
    domainObject.setHasOutstandingNursingActions(valueObject.getHasOutstandingNursingActions());
    domainObject.setHasOutstandingPhysioActions(valueObject.getHasOutstandingPhysioActions());
    domainObject.setHasOutstandingClinicalActions(valueObject.getHasOutstandingClinicalActions());

    ims.scheduling.vo.Booking_AppointmentRefVoCollection refCollection12 =
        valueObject.getAppointments();
    int size12 = (null == refCollection12) ? 0 : refCollection12.size();
    java.util.Set domainAppointments12 = domainObject.getAppointments();
    if (domainAppointments12 == null) {
      domainAppointments12 = new java.util.HashSet();
    }
    java.util.Set newSet12 = new java.util.HashSet();
    for (int i = 0; i < size12; i++) {
      ims.scheduling.vo.Booking_AppointmentRefVo vo = refCollection12.get(i);
      ims.scheduling.domain.objects.Booking_Appointment dom = null;
      if (null != vo) {
        if (vo.getBoId() == null) {
          if (domMap.get(vo) != null) {
            dom = (ims.scheduling.domain.objects.Booking_Appointment) domMap.get(vo);
          }
        } else {
          dom =
              (ims.scheduling.domain.objects.Booking_Appointment)
                  domainFactory.getDomainObject(
                      ims.scheduling.domain.objects.Booking_Appointment.class, vo.getBoId());
        }
      }

      // Trying to avoid the hibernate collection being marked as dirty via its public interface
      // methods. (like add)
      if (!domainAppointments12.contains(dom)) {
        domainAppointments12.add(dom);
      }
      newSet12.add(dom);
    }
    java.util.Set removedSet12 = new java.util.HashSet();
    java.util.Iterator iter12 = domainAppointments12.iterator();
    // Find out which objects need to be removed
    while (iter12.hasNext()) {
      ims.domain.DomainObject o = (ims.domain.DomainObject) iter12.next();
      if ((o == null || o.getIsRIE() == null || !o.getIsRIE().booleanValue())
          && !newSet12.contains(o)) {
        removedSet12.add(o);
      }
    }
    iter12 = removedSet12.iterator();
    // Remove the unwanted objects
    while (iter12.hasNext()) {
      domainAppointments12.remove(iter12.next());
    }

    domainObject.setAppointments(domainAppointments12);

    ims.icps.instantiation.vo.PatientCriticalEventsRefVoCollection refCollection13 =
        valueObject.getCriticalEvents();
    int size13 = (null == refCollection13) ? 0 : refCollection13.size();
    java.util.Set domainCriticalEvents13 = domainObject.getCriticalEvents();
    if (domainCriticalEvents13 == null) {
      domainCriticalEvents13 = new java.util.HashSet();
    }
    java.util.Set newSet13 = new java.util.HashSet();
    for (int i = 0; i < size13; i++) {
      ims.icps.instantiation.vo.PatientCriticalEventsRefVo vo = refCollection13.get(i);
      ims.icps.instantiation.domain.objects.PatientCriticalEvents dom = null;
      if (null != vo) {
        if (vo.getBoId() == null) {
          if (domMap.get(vo) != null) {
            dom = (ims.icps.instantiation.domain.objects.PatientCriticalEvents) domMap.get(vo);
          }
        } else {
          dom =
              (ims.icps.instantiation.domain.objects.PatientCriticalEvents)
                  domainFactory.getDomainObject(
                      ims.icps.instantiation.domain.objects.PatientCriticalEvents.class,
                      vo.getBoId());
        }
      }

      // Trying to avoid the hibernate collection being marked as dirty via its public interface
      // methods. (like add)
      if (!domainCriticalEvents13.contains(dom)) {
        domainCriticalEvents13.add(dom);
      }
      newSet13.add(dom);
    }
    java.util.Set removedSet13 = new java.util.HashSet();
    java.util.Iterator iter13 = domainCriticalEvents13.iterator();
    // Find out which objects need to be removed
    while (iter13.hasNext()) {
      ims.domain.DomainObject o = (ims.domain.DomainObject) iter13.next();
      if ((o == null || o.getIsRIE() == null || !o.getIsRIE().booleanValue())
          && !newSet13.contains(o)) {
        removedSet13.add(o);
      }
    }
    iter13 = removedSet13.iterator();
    // Remove the unwanted objects
    while (iter13.hasNext()) {
      domainCriticalEvents13.remove(iter13.next());
    }

    domainObject.setCriticalEvents(domainCriticalEvents13);
    ims.framework.utils.DateTime dateTime14 = valueObject.getStartedDateTime();
    java.util.Date value14 = null;
    if (dateTime14 != null) {
      value14 = dateTime14.getJavaDate();
    }
    domainObject.setStartedDateTime(value14);
    ims.framework.utils.DateTime dateTime15 = valueObject.getCompletedDateTime();
    java.util.Date value15 = null;
    if (dateTime15 != null) {
      value15 = dateTime15.getJavaDate();
    }
    domainObject.setCompletedDateTime(value15);

    ims.icps.instantiation.vo.PatientICPEvaluationNoteRefVoCollection refCollection16 =
        valueObject.getEvaluationNotes();
    int size16 = (null == refCollection16) ? 0 : refCollection16.size();
    java.util.Set domainEvaluationNotes16 = domainObject.getEvaluationNotes();
    if (domainEvaluationNotes16 == null) {
      domainEvaluationNotes16 = new java.util.HashSet();
    }
    java.util.Set newSet16 = new java.util.HashSet();
    for (int i = 0; i < size16; i++) {
      ims.icps.instantiation.vo.PatientICPEvaluationNoteRefVo vo = refCollection16.get(i);
      ims.icps.instantiation.domain.objects.PatientICPEvaluationNote dom = null;
      if (null != vo) {
        if (vo.getBoId() == null) {
          if (domMap.get(vo) != null) {
            dom = (ims.icps.instantiation.domain.objects.PatientICPEvaluationNote) domMap.get(vo);
          }
        } else {
          dom =
              (ims.icps.instantiation.domain.objects.PatientICPEvaluationNote)
                  domainFactory.getDomainObject(
                      ims.icps.instantiation.domain.objects.PatientICPEvaluationNote.class,
                      vo.getBoId());
        }
      }

      // Trying to avoid the hibernate collection being marked as dirty via its public interface
      // methods. (like add)
      if (!domainEvaluationNotes16.contains(dom)) {
        domainEvaluationNotes16.add(dom);
      }
      newSet16.add(dom);
    }
    java.util.Set removedSet16 = new java.util.HashSet();
    java.util.Iterator iter16 = domainEvaluationNotes16.iterator();
    // Find out which objects need to be removed
    while (iter16.hasNext()) {
      ims.domain.DomainObject o = (ims.domain.DomainObject) iter16.next();
      if ((o == null || o.getIsRIE() == null || !o.getIsRIE().booleanValue())
          && !newSet16.contains(o)) {
        removedSet16.add(o);
      }
    }
    iter16 = removedSet16.iterator();
    // Remove the unwanted objects
    while (iter16.hasNext()) {
      domainEvaluationNotes16.remove(iter16.next());
    }

    domainObject.setEvaluationNotes(domainEvaluationNotes16);

    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.icps.instantiation.domain.objects.PatientICP
   */
  public static ims.icp.vo.PatientICPVo insert(
      DomainObjectMap map,
      ims.icp.vo.PatientICPVo valueObject,
      ims.icps.instantiation.domain.objects.PatientICP domainObject) {
    if (null == domainObject) {
      return valueObject;
    }
    if (null == map) {
      map = new DomainObjectMap();
    }

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

    // 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()));
      }
    }
    // 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()));
      }
    }
    // 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()));
      }
    }
    // PasEvent
    if (domainObject.getPasEvent() != null) {
      if (domainObject.getPasEvent()
          instanceof
          HibernateProxy) // If the proxy is set, there is no need to lazy load, the proxy knows the
                          // id already.
      {
        HibernateProxy p = (HibernateProxy) domainObject.getPasEvent();
        int id = Integer.parseInt(p.getHibernateLazyInitializer().getIdentifier().toString());
        valueObject.setPasEvent(new ims.core.admin.pas.vo.PASEventRefVo(id, -1));
      } else {
        valueObject.setPasEvent(
            new ims.core.admin.pas.vo.PASEventRefVo(
                domainObject.getPasEvent().getId(), domainObject.getPasEvent().getVersion()));
      }
    }
    // ICP
    valueObject.setICP(ims.icp.vo.domain.ICPLiteVoAssembler.create(map, domainObject.getICP()));
    // AuthoringInformation
    valueObject.setAuthoringInformation(
        ims.core.vo.domain.AuthoringInformationVoAssembler.create(
            map, domainObject.getAuthoringInformation()));
    // Stages
    valueObject.setStages(
        ims.icp.vo.domain.PatientICPStageVoAssembler
            .createPatientICPStageVoCollectionFromPatientICPStage(map, domainObject.getStages()));
    // hasOutstandingAdminActions
    valueObject.setHasOutstandingAdminActions(domainObject.isHasOutstandingAdminActions());
    // hasOutstandingNursingActions
    valueObject.setHasOutstandingNursingActions(domainObject.isHasOutstandingNursingActions());
    // hasOutstandingPhysioActions
    valueObject.setHasOutstandingPhysioActions(domainObject.isHasOutstandingPhysioActions());
    // hasOutstandingClinicalActions
    valueObject.setHasOutstandingClinicalActions(domainObject.isHasOutstandingClinicalActions());
    // Appointments
    ims.scheduling.vo.Booking_AppointmentRefVoCollection Appointments =
        new ims.scheduling.vo.Booking_AppointmentRefVoCollection();
    for (java.util.Iterator iterator = domainObject.getAppointments().iterator();
        iterator.hasNext(); ) {
      ims.scheduling.domain.objects.Booking_Appointment tmp =
          (ims.scheduling.domain.objects.Booking_Appointment) iterator.next();
      if (tmp != null)
        Appointments.add(
            new ims.scheduling.vo.Booking_AppointmentRefVo(tmp.getId(), tmp.getVersion()));
    }
    valueObject.setAppointments(Appointments);
    // CriticalEvents
    ims.icps.instantiation.vo.PatientCriticalEventsRefVoCollection CriticalEvents =
        new ims.icps.instantiation.vo.PatientCriticalEventsRefVoCollection();
    for (java.util.Iterator iterator = domainObject.getCriticalEvents().iterator();
        iterator.hasNext(); ) {
      ims.icps.instantiation.domain.objects.PatientCriticalEvents tmp =
          (ims.icps.instantiation.domain.objects.PatientCriticalEvents) iterator.next();
      if (tmp != null)
        CriticalEvents.add(
            new ims.icps.instantiation.vo.PatientCriticalEventsRefVo(
                tmp.getId(), tmp.getVersion()));
    }
    valueObject.setCriticalEvents(CriticalEvents);
    // StartedDateTime
    java.util.Date StartedDateTime = domainObject.getStartedDateTime();
    if (null != StartedDateTime) {
      valueObject.setStartedDateTime(new ims.framework.utils.DateTime(StartedDateTime));
    }
    // CompletedDateTime
    java.util.Date CompletedDateTime = domainObject.getCompletedDateTime();
    if (null != CompletedDateTime) {
      valueObject.setCompletedDateTime(new ims.framework.utils.DateTime(CompletedDateTime));
    }
    // EvaluationNotes
    ims.icps.instantiation.vo.PatientICPEvaluationNoteRefVoCollection EvaluationNotes =
        new ims.icps.instantiation.vo.PatientICPEvaluationNoteRefVoCollection();
    for (java.util.Iterator iterator = domainObject.getEvaluationNotes().iterator();
        iterator.hasNext(); ) {
      ims.icps.instantiation.domain.objects.PatientICPEvaluationNote tmp =
          (ims.icps.instantiation.domain.objects.PatientICPEvaluationNote) iterator.next();
      if (tmp != null)
        EvaluationNotes.add(
            new ims.icps.instantiation.vo.PatientICPEvaluationNoteRefVo(
                tmp.getId(), tmp.getVersion()));
    }
    valueObject.setEvaluationNotes(EvaluationNotes);
    return valueObject;
  }