/**
   * Create the ValueObject collection to hold the list of DomainObjects.
   *
   * @param map - maps DomainObjects to created ValueObjects
   * @param domainObjectList - List of ims.icps.instantiation.domain.objects.PatientICP objects.
   */
  public static ims.icp.vo.PatientICPVoCollection createPatientICPVoCollectionFromPatientICP(
      DomainObjectMap map, java.util.List domainObjectList) {
    ims.icp.vo.PatientICPVoCollection voList = new ims.icp.vo.PatientICPVoCollection();
    if (null == domainObjectList) {
      return voList;
    }
    int rieCount = 0;
    int activeCount = 0;
    for (int i = 0; i < domainObjectList.size(); i++) {
      ims.icps.instantiation.domain.objects.PatientICP domainObject =
          (ims.icps.instantiation.domain.objects.PatientICP) domainObjectList.get(i);
      ims.icp.vo.PatientICPVo 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 set of DomainObjects.
   *
   * @param map - maps DomainObjects to created ValueObjects
   * @param domainObjectSet - Set of ims.icps.instantiation.domain.objects.PatientICP objects.
   */
  public static ims.icp.vo.PatientICPVoCollection createPatientICPVoCollectionFromPatientICP(
      DomainObjectMap map, java.util.Set domainObjectSet) {
    ims.icp.vo.PatientICPVoCollection voList = new ims.icp.vo.PatientICPVoCollection();
    if (null == domainObjectSet) {
      return voList;
    }
    int rieCount = 0;
    int activeCount = 0;
    java.util.Iterator iterator = domainObjectSet.iterator();
    while (iterator.hasNext()) {
      ims.icps.instantiation.domain.objects.PatientICP domainObject =
          (ims.icps.instantiation.domain.objects.PatientICP) iterator.next();
      ims.icp.vo.PatientICPVo 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.icps.instantiation.domain.objects.PatientICP object.
   *
   * @param map DomainObjectMap of DomainObjects to already created ValueObjects.
   * @param domainObject
   */
  public static ims.icp.vo.PatientICPVo create(
      DomainObjectMap map, ims.icps.instantiation.domain.objects.PatientICP domainObject) {
    if (null == domainObject) {
      return null;
    }
    // check if the domainObject already has a valueObject created for it
    ims.icp.vo.PatientICPVo valueObject =
        (ims.icp.vo.PatientICPVo) map.getValueObject(domainObject, ims.icp.vo.PatientICPVo.class);
    if (null == valueObject) {
      valueObject = new ims.icp.vo.PatientICPVo(domainObject.getId(), domainObject.getVersion());
      map.addValueObject(domainObject, valueObject);

      valueObject = insert(map, valueObject, domainObject);
    }
    return valueObject;
  }
  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;
  }
  public void bringPhaseIntoScope(
      ims.domain.DomainFactory factory, ims.rules.engine.RulesRuntimeEngine engine, String phase) {
    // Private keys for engine hash map
    final String IsFitKey = "IsFitForSugery";

    java.util.ArrayList<java.lang.String> keys = new java.util.ArrayList<java.lang.String>();
    java.util.ArrayList<java.lang.Object> values = new java.util.ArrayList<java.lang.Object>();

    keys.add("ID");
    values.add(this.careContext.getId());
    keys.add("DATE");
    values.add(this.authoringInformation.getAuthoringDateTime());

    // Check if the FitForSurgery assessment is in stage
    if (engine.getValue(IsFitKey) == null) {
      // Get latest fit for surgery assessment into engine hash map
      java.lang.String hqlQuery =
          "from FitForSurgeryAssesment as fit where (fit.careContext.id = :ID and fit.authoringInformation.authoringDateTime <= :DATE and fit.isRIE is null) order by fit.authoringInformation.authoringDateTime desc top 1";

      java.util.List<ims.RefMan.domain.objects.FitForSurgeryAssesment> list =
          factory.find(hqlQuery, keys, values);

      if (list != null && list.size() != 0) {
        engine.setValue(IsFitKey, list.get(0));
      }
    }

    ims.RefMan.domain.objects.FitForSurgeryAssesment fitForSurgery =
        (ims.RefMan.domain.objects.FitForSurgeryAssesment) engine.getValue(IsFitKey);

    // Check if fit for surgery conditions are met
    if (fitForSurgery == null || !fitForSurgery.isFitForSurgery()) return;

    keys.remove("DATE");

    // Get the patient ICP
    java.lang.String hqlIcpQuery =
        "from PatientICP as icp where (icp.careContext.id = :ID and icp.isRIE is null)";
    java.util.List<ims.icps.instantiation.domain.objects.PatientICP> icps =
        factory.find(hqlIcpQuery, keys, values);

    // Check if there is an ICP present
    if (icps == null || icps.size() == 0) return;

    // Get an PatientICP
    ims.icps.instantiation.domain.objects.PatientICP patientICP = icps.get(0);

    ims.icps.instantiation.domain.objects.PatientICPAction action = null;

    java.util.Iterator<ims.icps.instantiation.domain.objects.PatientICPStage> stageIterator =
        patientICP.getStages().iterator();

    while (stageIterator.hasNext()) {
      ims.icps.instantiation.domain.objects.PatientICPStage patientStage = stageIterator.next();

      java.util.Iterator<ims.icps.instantiation.domain.objects.PatientICPPhase> phaseIterator =
          patientStage.getStagePhases().iterator();

      while (phaseIterator.hasNext()) {
        ims.icps.instantiation.domain.objects.PatientICPPhase patientPhase = phaseIterator.next();

        java.util.Iterator<ims.icps.instantiation.domain.objects.PatientICPAction> actionsIterator =
            patientPhase.getPhaseActions().iterator();

        while (actionsIterator.hasNext()) {
          action = actionsIterator.next();

          if (action != null) break;
        }

        if (action != null) break;
      }

      if (action != null) break;
    }

    // Bring phase into scope
    if (action != null) {
      action.bringPhaseIntoScope(factory, engine, phase);
    }
  }