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

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

    // EpisodeOfCare
    valueObject.setEpisodeOfCare(
        ims.core.vo.domain.EpisodeOfCareForPatientDocumentsVoAssembler.create(
            map, domainObject.getEpisodeOfCare()));
    // StartDateTime
    java.util.Date StartDateTime = domainObject.getStartDateTime();
    if (null != StartDateTime) {
      valueObject.setStartDateTime(new ims.framework.utils.DateTime(StartDateTime));
    }
    // ResponsibleHCP
    if (domainObject.getResponsibleHCP() != null) {
      if (domainObject.getResponsibleHCP()
          instanceof
          HibernateProxy) // If the proxy is set, there is no need to lazy load, the proxy knows the
      // id already.
      {
        HibernateProxy p = (HibernateProxy) domainObject.getResponsibleHCP();
        int id = Integer.parseInt(p.getHibernateLazyInitializer().getIdentifier().toString());
        valueObject.setResponsibleHCP(new ims.core.resource.people.vo.HcpRefVo(id, -1));
      } else {
        valueObject.setResponsibleHCP(
            new ims.core.resource.people.vo.HcpRefVo(
                domainObject.getResponsibleHCP().getId(),
                domainObject.getResponsibleHCP().getVersion()));
      }
    }
    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.core.admin.domain.objects.CareContext
   */
  public static ims.core.vo.CareContextSmallVo insert(
      DomainObjectMap map,
      ims.core.vo.CareContextSmallVo valueObject,
      ims.core.admin.domain.objects.CareContext domainObject) {
    if (null == domainObject) {
      return valueObject;
    }
    if (null == map) {
      map = new DomainObjectMap();
    }

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

    // Context
    ims.domain.lookups.LookupInstance instance1 = domainObject.getContext();
    if (null != instance1) {
      ims.framework.utils.ImagePath img = null;
      ims.framework.utils.Color color = null;
      img = null;
      if (instance1.getImage() != null) {
        img =
            new ims.framework.utils.ImagePath(
                instance1.getImage().getImageId(), instance1.getImage().getImagePath());
      }
      color = instance1.getColor();
      if (color != null) color.getValue();

      ims.core.vo.lookups.ContextType voLookup1 =
          new ims.core.vo.lookups.ContextType(
              instance1.getId(), instance1.getText(), instance1.isActive(), null, img, color);
      ims.core.vo.lookups.ContextType parentVoLookup1 = voLookup1;
      ims.domain.lookups.LookupInstance parent1 = instance1.getParent();
      while (parent1 != null) {
        if (parent1.getImage() != null) {
          img =
              new ims.framework.utils.ImagePath(
                  parent1.getImage().getImageId(), parent1.getImage().getImagePath());
        } else {
          img = null;
        }
        color = parent1.getColor();
        if (color != null) color.getValue();
        parentVoLookup1.setParent(
            new ims.core.vo.lookups.ContextType(
                parent1.getId(), parent1.getText(), parent1.isActive(), null, img, color));
        parentVoLookup1 = parentVoLookup1.getParent();
        parent1 = parent1.getParent();
      }
      valueObject.setContext(voLookup1);
    }
    // StartDateTime
    java.util.Date StartDateTime = domainObject.getStartDateTime();
    if (null != StartDateTime) {
      valueObject.setStartDateTime(new ims.framework.utils.DateTime(StartDateTime));
    }
    // EndDateTime
    java.util.Date EndDateTime = domainObject.getEndDateTime();
    if (null != EndDateTime) {
      valueObject.setEndDateTime(new ims.framework.utils.DateTime(EndDateTime));
    }
    // CareContextHistory
    valueObject.setCareContextHistory(
        ims.core.vo.domain.CareContextHistVoAssembler
            .createCareContextHistVoCollectionFromCareContextHistory(
                map, domainObject.getCareContextHistory()));
    return valueObject;
  }
  public OrderInvestigationForAttendenceNotesCcVoCollection listOrderInvestigations(
      PatientRefVo patient, CareContextRefVo careContext, String investigationsAlreadyAdded) {
    if (patient == null || patient.getID_Patient() == null)
      throw new CodingRuntimeException(
          "Patient parameter can not be null when listing OrderInvestigations.");

    DomainFactory factory = getDomainFactory();
    java.util.Date fromDate = null;
    java.util.Date dateTo = null;

    if (careContext != null && careContext.getID_CareContext() != null) {
      CareContext doCareContext =
          (CareContext) factory.getDomainObject(CareContext.class, careContext.getID_CareContext());
      if (doCareContext != null) {
        if (doCareContext.getStartDateTime() != null) {
          fromDate = doCareContext.getStartDateTime();
        }

        if (doCareContext.getEndDateTime() != null) {
          return null;
        }

        dateTo = new java.util.Date();
      }
    }

    StringBuilder query = new StringBuilder();

    ArrayList<String> paramNames = new ArrayList<String>();
    ArrayList<Object> paramValues = new ArrayList<Object>();

    query.append("SELECT orderInv FROM OrderInvestigation AS orderInv ");
    query.append(
        " LEFT JOIN FETCH orderInv.orderDetails AS details LEFT JOIN details.patient AS patient left join orderInv.investigation as inv left join inv.investigationIndex as invIndex left join orderInv.ordInvCurrentStatus.ordInvStatus as ordStatus ");

    query.append(
        "WHERE patient.id = :PATIENT_ID and ordStatus.id <> :cancelledStatusId and ordStatus.id <> :cancelRequestStatusId");
    paramNames.add("PATIENT_ID");
    paramValues.add(patient.getID_Patient());

    // WDEV-17303
    paramNames.add("cancelledStatusId");
    paramValues.add(OrderInvStatus.CANCELLED.getID());
    paramNames.add("cancelRequestStatusId");
    paramValues.add(OrderInvStatus.CANCEL_REQUEST.getID());

    if (fromDate != null && dateTo != null) {
      query.append(
          " AND orderInv.systemInformation.creationDateTime BETWEEN :FROM_DATE AND :END_DATE ");

      paramNames.add("FROM_DATE");
      paramValues.add(fromDate);
      paramNames.add("END_DATE");
      paramValues.add(dateTo);
    }

    if (investigationsAlreadyAdded != null && investigationsAlreadyAdded.length() > 0) {
      query.append(" AND orderInv.id not in (");
      query.append(investigationsAlreadyAdded);
      query.append(") ");
    }

    query.append(" order by UPPER(invIndex.name) asc");

    return OrderInvestigationForAttendenceNotesCcVoAssembler
        .createOrderInvestigationForAttendenceNotesCcVoCollectionFromOrderInvestigation(
            factory.find(query.toString(), paramNames, paramValues));
  }