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;
  }