public static ims.RefMan.domain.objects.CatsReferral extractCatsReferral(
      ims.domain.ILightweightDomainFactory domainFactory,
      ims.core.vo.CatsReferralRepatriationRequestVo valueObject,
      HashMap domMap) {
    if (null == valueObject) {
      return null;
    }
    Integer id = valueObject.getID_CatsReferral();
    ims.RefMan.domain.objects.CatsReferral domainObject = null;
    if (null == id) {
      if (domMap.get(valueObject) != null) {
        return (ims.RefMan.domain.objects.CatsReferral) domMap.get(valueObject);
      }
      // ims.core.vo.CatsReferralRepatriationRequestVo ID_CatsReferral field is unknown
      domainObject = new ims.RefMan.domain.objects.CatsReferral();
      domMap.put(valueObject, domainObject);
    } else {
      String key = (valueObject.getClass().getName() + "__" + valueObject.getID_CatsReferral());
      if (domMap.get(key) != null) {
        return (ims.RefMan.domain.objects.CatsReferral) domMap.get(key);
      }
      domainObject =
          (ims.RefMan.domain.objects.CatsReferral)
              domainFactory.getDomainObject(ims.RefMan.domain.objects.CatsReferral.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_CatsReferral());

    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);
    domainObject.setReferralDetails(
        ims.core.vo.domain.CatsReferralDetailsRepatriationRequestVoAssembler
            .extractReferralLetterDetails(domainFactory, valueObject.getReferralDetails(), domMap));
    ims.core.admin.domain.objects.EpisodeOfCare value3 = null;
    if (null != valueObject.getEpisodeOfCare()) {
      if (valueObject.getEpisodeOfCare().getBoId() == null) {
        if (domMap.get(valueObject.getEpisodeOfCare()) != null) {
          value3 =
              (ims.core.admin.domain.objects.EpisodeOfCare)
                  domMap.get(valueObject.getEpisodeOfCare());
        }
      } else if (valueObject.getBoVersion()
          == -1) // RefVo was not modified since obtained from the Assembler, no need to update the
                 // BO field
      {
        value3 = domainObject.getEpisodeOfCare();
      } else {
        value3 =
            (ims.core.admin.domain.objects.EpisodeOfCare)
                domainFactory.getDomainObject(
                    ims.core.admin.domain.objects.EpisodeOfCare.class,
                    valueObject.getEpisodeOfCare().getBoId());
      }
    }
    domainObject.setEpisodeOfCare(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);
    domainObject.setCurrentStatus(
        ims.RefMan.vo.domain.CatsReferralStatusVoAssembler.extractCATSReferralStatus(
            domainFactory, valueObject.getCurrentStatus(), domMap));
    domainObject.setStatusHistory(
        ims.RefMan.vo.domain.CatsReferralStatusVoAssembler.extractCATSReferralStatusSet(
            domainFactory,
            valueObject.getStatusHistory(),
            domainObject.getStatusHistory(),
            domMap));
    ims.core.configuration.domain.objects.ContractConfig value7 = null;
    if (null != valueObject.getContract()) {
      if (valueObject.getContract().getBoId() == null) {
        if (domMap.get(valueObject.getContract()) != null) {
          value7 =
              (ims.core.configuration.domain.objects.ContractConfig)
                  domMap.get(valueObject.getContract());
        }
      } else if (valueObject.getBoVersion()
          == -1) // RefVo was not modified since obtained from the Assembler, no need to update the
                 // BO field
      {
        value7 = domainObject.getContract();
      } else {
        value7 =
            (ims.core.configuration.domain.objects.ContractConfig)
                domainFactory.getDomainObject(
                    ims.core.configuration.domain.objects.ContractConfig.class,
                    valueObject.getContract().getBoId());
      }
    }
    domainObject.setContract(value7);
    // create LookupInstance from vo LookupType
    ims.domain.lookups.LookupInstance value8 = null;
    if (null != valueObject.getUrgency()) {
      value8 = domainFactory.getLookupInstance(valueObject.getUrgency().getID());
    }
    domainObject.setUrgency(value8);
    ims.pathways.domain.objects.PatientPathwayJourney value9 = null;
    if (null != valueObject.getJourney()) {
      if (valueObject.getJourney().getBoId() == null) {
        if (domMap.get(valueObject.getJourney()) != null) {
          value9 =
              (ims.pathways.domain.objects.PatientPathwayJourney)
                  domMap.get(valueObject.getJourney());
        }
      } else if (valueObject.getBoVersion()
          == -1) // RefVo was not modified since obtained from the Assembler, no need to update the
                 // BO field
      {
        value9 = domainObject.getJourney();
      } else {
        value9 =
            (ims.pathways.domain.objects.PatientPathwayJourney)
                domainFactory.getDomainObject(
                    ims.pathways.domain.objects.PatientPathwayJourney.class,
                    valueObject.getJourney().getBoId());
      }
    }
    domainObject.setJourney(value9);
    // This is to overcome a bug in both Sybase and Oracle which prevents them from storing an empty
    // string correctly
    // Sybase stores it as a single space, Oracle stores it as NULL. This fix will make them
    // consistent at least.
    if (valueObject.getPathwayID() != null && valueObject.getPathwayID().equals("")) {
      valueObject.setPathwayID(null);
    }
    domainObject.setPathwayID(valueObject.getPathwayID());
    domainObject.setRTTClockImpact(valueObject.getRTTClockImpact());
    // SaveAsRefVO - treated as a refVo in extract methods
    ims.pathways.domain.objects.PathwayRTTStatus value12 = null;
    if (null != valueObject.getCurrentRTTStatus()) {
      if (valueObject.getCurrentRTTStatus().getBoId() == null) {
        if (domMap.get(valueObject.getCurrentRTTStatus()) != null) {
          value12 =
              (ims.pathways.domain.objects.PathwayRTTStatus)
                  domMap.get(valueObject.getCurrentRTTStatus());
        }
      } else {
        value12 =
            (ims.pathways.domain.objects.PathwayRTTStatus)
                domainFactory.getDomainObject(
                    ims.pathways.domain.objects.PathwayRTTStatus.class,
                    valueObject.getCurrentRTTStatus().getBoId());
      }
    }
    domainObject.setCurrentRTTStatus(value12);
    domainObject.setRepatriationRequest(
        ims.core.vo.domain.PendingEmergencyAdmissionRepatriationVoAssembler
            .extractPendingNoEDEmergencyAdmit(
                domainFactory, valueObject.getRepatriationRequest(), domMap));
    // create LookupInstance from vo LookupType
    ims.domain.lookups.LookupInstance value14 = null;
    if (null != valueObject.getPatientCategory()) {
      value14 = domainFactory.getLookupInstance(valueObject.getPatientCategory().getID());
    }
    domainObject.setPatientCategory(value14);
    domainObject.setIsEmergencyReferral(valueObject.getIsEmergencyReferral());

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

    valueObject.setID_CatsReferral(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()));
      }
    }
    // ReferralDetails
    valueObject.setReferralDetails(
        ims.core.vo.domain.CatsReferralDetailsRepatriationRequestVoAssembler.create(
            map, domainObject.getReferralDetails()));
    // EpisodeOfCare
    if (domainObject.getEpisodeOfCare() != null) {
      if (domainObject.getEpisodeOfCare()
          instanceof
          HibernateProxy) // If the proxy is set, there is no need to lazy load, the proxy knows the
                          // id already.
      {
        HibernateProxy p = (HibernateProxy) domainObject.getEpisodeOfCare();
        int id = Integer.parseInt(p.getHibernateLazyInitializer().getIdentifier().toString());
        valueObject.setEpisodeOfCare(new ims.core.admin.vo.EpisodeOfCareRefVo(id, -1));
      } else {
        valueObject.setEpisodeOfCare(
            new ims.core.admin.vo.EpisodeOfCareRefVo(
                domainObject.getEpisodeOfCare().getId(),
                domainObject.getEpisodeOfCare().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()));
      }
    }
    // CurrentStatus
    valueObject.setCurrentStatus(
        ims.RefMan.vo.domain.CatsReferralStatusVoAssembler.create(
            map, domainObject.getCurrentStatus()));
    // StatusHistory
    valueObject.setStatusHistory(
        ims.RefMan.vo.domain.CatsReferralStatusVoAssembler
            .createCatsReferralStatusVoCollectionFromCATSReferralStatus(
                map, domainObject.getStatusHistory()));
    // Contract
    if (domainObject.getContract() != null) {
      if (domainObject.getContract()
          instanceof
          HibernateProxy) // If the proxy is set, there is no need to lazy load, the proxy knows the
                          // id already.
      {
        HibernateProxy p = (HibernateProxy) domainObject.getContract();
        int id = Integer.parseInt(p.getHibernateLazyInitializer().getIdentifier().toString());
        valueObject.setContract(new ims.core.configuration.vo.ContractConfigRefVo(id, -1));
      } else {
        valueObject.setContract(
            new ims.core.configuration.vo.ContractConfigRefVo(
                domainObject.getContract().getId(), domainObject.getContract().getVersion()));
      }
    }
    // Urgency
    ims.domain.lookups.LookupInstance instance8 = domainObject.getUrgency();
    if (null != instance8) {
      ims.framework.utils.ImagePath img = null;
      ims.framework.utils.Color color = null;
      img = null;
      if (instance8.getImage() != null) {
        img =
            new ims.framework.utils.ImagePath(
                instance8.getImage().getImageId(), instance8.getImage().getImagePath());
      }
      color = instance8.getColor();
      if (color != null) color.getValue();

      ims.RefMan.vo.lookups.ReferralUrgency voLookup8 =
          new ims.RefMan.vo.lookups.ReferralUrgency(
              instance8.getId(), instance8.getText(), instance8.isActive(), null, img, color);
      ims.RefMan.vo.lookups.ReferralUrgency parentVoLookup8 = voLookup8;
      ims.domain.lookups.LookupInstance parent8 = instance8.getParent();
      while (parent8 != null) {
        if (parent8.getImage() != null) {
          img =
              new ims.framework.utils.ImagePath(
                  parent8.getImage().getImageId(), parent8.getImage().getImagePath());
        } else {
          img = null;
        }
        color = parent8.getColor();
        if (color != null) color.getValue();
        parentVoLookup8.setParent(
            new ims.RefMan.vo.lookups.ReferralUrgency(
                parent8.getId(), parent8.getText(), parent8.isActive(), null, img, color));
        parentVoLookup8 = parentVoLookup8.getParent();
        parent8 = parent8.getParent();
      }
      valueObject.setUrgency(voLookup8);
    }
    // Journey
    if (domainObject.getJourney() != null) {
      if (domainObject.getJourney()
          instanceof
          HibernateProxy) // If the proxy is set, there is no need to lazy load, the proxy knows the
                          // id already.
      {
        HibernateProxy p = (HibernateProxy) domainObject.getJourney();
        int id = Integer.parseInt(p.getHibernateLazyInitializer().getIdentifier().toString());
        valueObject.setJourney(new ims.pathways.vo.PatientPathwayJourneyRefVo(id, -1));
      } else {
        valueObject.setJourney(
            new ims.pathways.vo.PatientPathwayJourneyRefVo(
                domainObject.getJourney().getId(), domainObject.getJourney().getVersion()));
      }
    }
    // PathwayID
    valueObject.setPathwayID(domainObject.getPathwayID());
    // RTTClockImpact
    valueObject.setRTTClockImpact(domainObject.isRTTClockImpact());
    // CurrentRTTStatus
    valueObject.setCurrentRTTStatus(
        ims.pathways.vo.domain.PathwayRTTStatusVoAssembler.create(
            map, domainObject.getCurrentRTTStatus()));
    // RepatriationRequest
    valueObject.setRepatriationRequest(
        ims.core.vo.domain.PendingEmergencyAdmissionRepatriationVoAssembler.create(
            map, domainObject.getRepatriationRequest()));
    // PatientCategory
    ims.domain.lookups.LookupInstance instance14 = domainObject.getPatientCategory();
    if (null != instance14) {
      ims.framework.utils.ImagePath img = null;
      ims.framework.utils.Color color = null;
      img = null;
      if (instance14.getImage() != null) {
        img =
            new ims.framework.utils.ImagePath(
                instance14.getImage().getImageId(), instance14.getImage().getImagePath());
      }
      color = instance14.getColor();
      if (color != null) color.getValue();

      ims.core.vo.lookups.PatientStatus voLookup14 =
          new ims.core.vo.lookups.PatientStatus(
              instance14.getId(), instance14.getText(), instance14.isActive(), null, img, color);
      ims.core.vo.lookups.PatientStatus parentVoLookup14 = voLookup14;
      ims.domain.lookups.LookupInstance parent14 = instance14.getParent();
      while (parent14 != null) {
        if (parent14.getImage() != null) {
          img =
              new ims.framework.utils.ImagePath(
                  parent14.getImage().getImageId(), parent14.getImage().getImagePath());
        } else {
          img = null;
        }
        color = parent14.getColor();
        if (color != null) color.getValue();
        parentVoLookup14.setParent(
            new ims.core.vo.lookups.PatientStatus(
                parent14.getId(), parent14.getText(), parent14.isActive(), null, img, color));
        parentVoLookup14 = parentVoLookup14.getParent();
        parent14 = parent14.getParent();
      }
      valueObject.setPatientCategory(voLookup14);
    }
    // IsEmergencyReferral
    valueObject.setIsEmergencyReferral(domainObject.isIsEmergencyReferral());
    return valueObject;
  }