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

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

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

    // Stage
    valueObject.setStage(
        ims.icp.vo.domain.ICPStageLiteVoAssembler.create(map, domainObject.getStage()));
    // StagePhases
    valueObject.setStagePhases(
        ims.icp.vo.domain.PatientICPPhasesLiteVoAssembler
            .createPatientICPPhasesLiteVoCollectionFromPatientICPPhase(
                map, domainObject.getStagePhases()));
    // CurrentStatus
    valueObject.setCurrentStatus(
        ims.icp.vo.domain.PatientICPStageStatusVoAssembler.create(
            map, domainObject.getCurrentStatus()));
    return valueObject;
  }
  public static ims.icps.instantiation.domain.objects.PatientICPStage extractPatientICPStage(
      ims.domain.ILightweightDomainFactory domainFactory,
      ims.icp.vo.PatientICPStagesLiteVo valueObject,
      HashMap domMap) {
    if (null == valueObject) {
      return null;
    }
    Integer id = valueObject.getID_PatientICPStage();
    ims.icps.instantiation.domain.objects.PatientICPStage domainObject = null;
    if (null == id) {
      if (domMap.get(valueObject) != null) {
        return (ims.icps.instantiation.domain.objects.PatientICPStage) domMap.get(valueObject);
      }
      // ims.icp.vo.PatientICPStagesLiteVo ID_PatientICPStage field is unknown
      domainObject = new ims.icps.instantiation.domain.objects.PatientICPStage();
      domMap.put(valueObject, domainObject);
    } else {
      String key = (valueObject.getClass().getName() + "__" + valueObject.getID_PatientICPStage());
      if (domMap.get(key) != null) {
        return (ims.icps.instantiation.domain.objects.PatientICPStage) domMap.get(key);
      }
      domainObject =
          (ims.icps.instantiation.domain.objects.PatientICPStage)
              domainFactory.getDomainObject(
                  ims.icps.instantiation.domain.objects.PatientICPStage.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_PatientICPStage());

    // SaveAsRefVO - treated as a refVo in extract methods
    ims.icps.configuration.domain.objects.ICPStage value1 = null;
    if (null != valueObject.getStage()) {
      if (valueObject.getStage().getBoId() == null) {
        if (domMap.get(valueObject.getStage()) != null) {
          value1 =
              (ims.icps.configuration.domain.objects.ICPStage) domMap.get(valueObject.getStage());
        }
      } else {
        value1 =
            (ims.icps.configuration.domain.objects.ICPStage)
                domainFactory.getDomainObject(
                    ims.icps.configuration.domain.objects.ICPStage.class,
                    valueObject.getStage().getBoId());
      }
    }
    domainObject.setStage(value1);

    // SaveAsRefVO treated as RefValueObject
    ims.icps.instantiation.vo.PatientICPPhaseRefVoCollection refCollection2 =
        new ims.icps.instantiation.vo.PatientICPPhaseRefVoCollection();
    if (valueObject.getStagePhases() != null) {
      for (int i2 = 0; i2 < valueObject.getStagePhases().size(); i2++) {
        ims.icps.instantiation.vo.PatientICPPhaseRefVo ref2 =
            (ims.icps.instantiation.vo.PatientICPPhaseRefVo) valueObject.getStagePhases().get(i2);
        refCollection2.add(ref2);
      }
    }
    int size2 = (null == refCollection2) ? 0 : refCollection2.size();
    java.util.Set domainStagePhases2 = domainObject.getStagePhases();
    if (domainStagePhases2 == null) {
      domainStagePhases2 = new java.util.HashSet();
    }
    java.util.Set newSet2 = new java.util.HashSet();
    for (int i = 0; i < size2; i++) {
      ims.icps.instantiation.vo.PatientICPPhaseRefVo vo = refCollection2.get(i);
      ims.icps.instantiation.domain.objects.PatientICPPhase dom = null;
      if (null != vo) {
        if (vo.getBoId() == null) {
          if (domMap.get(vo) != null) {
            dom = (ims.icps.instantiation.domain.objects.PatientICPPhase) domMap.get(vo);
          }
        } else {
          dom =
              (ims.icps.instantiation.domain.objects.PatientICPPhase)
                  domainFactory.getDomainObject(
                      ims.icps.instantiation.domain.objects.PatientICPPhase.class, vo.getBoId());
        }
      }

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

    domainObject.setStagePhases(domainStagePhases2);
    // SaveAsRefVO - treated as a refVo in extract methods
    ims.icps.instantiation.domain.objects.PatientICPStageStatus value3 = null;
    if (null != valueObject.getCurrentStatus()) {
      if (valueObject.getCurrentStatus().getBoId() == null) {
        if (domMap.get(valueObject.getCurrentStatus()) != null) {
          value3 =
              (ims.icps.instantiation.domain.objects.PatientICPStageStatus)
                  domMap.get(valueObject.getCurrentStatus());
        }
      } else {
        value3 =
            (ims.icps.instantiation.domain.objects.PatientICPStageStatus)
                domainFactory.getDomainObject(
                    ims.icps.instantiation.domain.objects.PatientICPStageStatus.class,
                    valueObject.getCurrentStatus().getBoId());
      }
    }
    domainObject.setCurrentStatus(value3);

    return domainObject;
  }
  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);
    }
  }