/**
   * Update the ValueObject with the Domain Object.
   *
   * @param map DomainObjectMap of DomainObjects to already created ValueObjects.
   * @param valueObject to be updated
   * @param domainObject ims.clinical.domain.objects.PatientProcedureChecklist
   */
  public static ims.clinical.vo.PatientProcedureChecklistVo insert(
      DomainObjectMap map,
      ims.clinical.vo.PatientProcedureChecklistVo valueObject,
      ims.clinical.domain.objects.PatientProcedureChecklist domainObject) {
    if (null == domainObject) {
      return valueObject;
    }
    if (null == map) {
      map = new DomainObjectMap();
    }

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

    // 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()));
      }
    }
    // PresentProcedures
    valueObject.setPresentProcedures(
        ims.core.vo.domain.PatientProcedureVoAssembler
            .createPatientProcedureVoCollectionFromPatientProcedure(
                map, domainObject.getPresentProcedures()));
    // OtherProcedures
    valueObject.setOtherProcedures(
        ims.clinical.vo.domain.PatientAbsentProcedureVoAssembler
            .createPatientAbsentProcedureVoCollectionFromPatientAbsentProcedure(
                map, domainObject.getOtherProcedures()));
    // AuthoringInformation
    valueObject.setAuthoringInformation(
        ims.core.vo.domain.AuthoringInformationVoAssembler.create(
            map, domainObject.getAuthoringInformation()));
    return valueObject;
  }
  public EmergencyPatientPresentingProblemsVo saveEmergencyPresentingProblems(
      EmergencyPatientPresentingProblemsVo problems)
      throws DomainInterfaceException, StaleObjectException, ForeignKeyViolationException {
    if (problems == null) throw new CodingRuntimeException("No record to save supplied !");

    if (problems.getCareContext() == null)
      throw new CodingRuntimeException("The care context supplied cannot be null !");

    DomainFactory domainFactory = getDomainFactory();

    if (problems.getID_PresProblemsProceduresPerformed() == null) {
      List count =
          domainFactory.find(
              "select count(p.id) from PresProblemsProceduresPerformed p where p.careContext.id = :careContextId",
              new String[] {"careContextId"},
              new Object[] {problems.getCareContext().getID_CareContext()});

      if (!count.get(0).toString().equals("0"))
        throw new DomainInterfaceException(
            "There is already a record saved for this care context !");
    }

    PresProblemsProceduresPerformed patientPresentingProblems =
        EmergencyPatientPresentingProblemsVoAssembler.extractPresProblemsProceduresPerformed(
            domainFactory, problems);

    domainFactory.save(patientPresentingProblems);

    if (problems.getOrphanProblems() != null) {
      for (int i = 0; i < problems.getOrphanProblems().size(); i++) {
        PatientProblemVo orphanVo = problems.getOrphanProblems().get(i);

        PatientProblem orphan =
            PatientProblemVoAssembler.extractPatientProblem(domainFactory, orphanVo);

        domainFactory.delete(orphan);
      }
    }

    if (problems.getOrphanProcedures() != null) {
      for (int i = 0; i < problems.getOrphanProcedures().size(); i++) {
        PatientProcedureVo orphanVo = problems.getOrphanProcedures().get(i);

        PatientProcedure orphan =
            PatientProcedureVoAssembler.extractPatientProcedure(domainFactory, orphanVo);

        domainFactory.delete(orphan);
      }
    }

    return EmergencyPatientPresentingProblemsVoAssembler.create(patientPresentingProblems);
  }
  public static ims.clinical.domain.objects.PatientProcedureChecklist
      extractPatientProcedureChecklist(
          ims.domain.ILightweightDomainFactory domainFactory,
          ims.clinical.vo.PatientProcedureChecklistVo valueObject,
          HashMap domMap) {
    if (null == valueObject) {
      return null;
    }
    Integer id = valueObject.getID_PatientProcedureChecklist();
    ims.clinical.domain.objects.PatientProcedureChecklist domainObject = null;
    if (null == id) {
      if (domMap.get(valueObject) != null) {
        return (ims.clinical.domain.objects.PatientProcedureChecklist) domMap.get(valueObject);
      }
      // ims.clinical.vo.PatientProcedureChecklistVo ID_PatientProcedureChecklist field is unknown
      domainObject = new ims.clinical.domain.objects.PatientProcedureChecklist();
      domMap.put(valueObject, domainObject);
    } else {
      String key =
          (valueObject.getClass().getName() + "__" + valueObject.getID_PatientProcedureChecklist());
      if (domMap.get(key) != null) {
        return (ims.clinical.domain.objects.PatientProcedureChecklist) domMap.get(key);
      }
      domainObject =
          (ims.clinical.domain.objects.PatientProcedureChecklist)
              domainFactory.getDomainObject(
                  ims.clinical.domain.objects.PatientProcedureChecklist.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_PatientProcedureChecklist());

    ims.core.admin.domain.objects.ClinicalContact value1 = null;
    if (null != valueObject.getClinicalContact()) {
      if (valueObject.getClinicalContact().getBoId() == null) {
        if (domMap.get(valueObject.getClinicalContact()) != null) {
          value1 =
              (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
      {
        value1 = domainObject.getClinicalContact();
      } else {
        value1 =
            (ims.core.admin.domain.objects.ClinicalContact)
                domainFactory.getDomainObject(
                    ims.core.admin.domain.objects.ClinicalContact.class,
                    valueObject.getClinicalContact().getBoId());
      }
    }
    domainObject.setClinicalContact(value1);
    domainObject.setPresentProcedures(
        ims.core.vo.domain.PatientProcedureVoAssembler.extractPatientProcedureSet(
            domainFactory,
            valueObject.getPresentProcedures(),
            domainObject.getPresentProcedures(),
            domMap));
    domainObject.setOtherProcedures(
        ims.clinical.vo.domain.PatientAbsentProcedureVoAssembler.extractPatientAbsentProcedureSet(
            domainFactory,
            valueObject.getOtherProcedures(),
            domainObject.getOtherProcedures(),
            domMap));
    domainObject.setAuthoringInformation(
        ims.core.vo.domain.AuthoringInformationVoAssembler.extractAuthoringInformation(
            domainFactory, valueObject.getAuthoringInformation(), domMap));

    return domainObject;
  }