private void populateInstanceControls(SentToXrayVo selectedSendToXRay) {
    clearInstanceControls();

    if (selectedSendToXRay == null) {
      return;
    }

    selectedSendToXRay = domain.getSentToXRay(selectedSendToXRay);

    form.ccAllocatedBy().setValue(selectedSendToXRay.getSentBy());
    form.dtimSent().setValue(selectedSendToXRay.getSentDateTime());
    form.dtimReturned().setValue(selectedSendToXRay.getReturnedDateTime());

    if (selectedSendToXRay.getReturnedDateTime() != null) {
      form.chkReturned().setValue(true);
      form.dtimReturned().setValue(selectedSendToXRay.getReturnedDateTime());
      if (form.getLocalContext().getcurrentTracking().getCurrentStatus() != null) {
        form.cmbSetCurrentStatus()
            .setValue(form.getLocalContext().getcurrentTracking().getCurrentStatus().getStatus());
      }
    }
  }
  private SentToXrayVo populateInstanceDataFromScreen(SentToXrayVo sentToXRay) {
    if (sentToXRay == null) {
      sentToXRay = new SentToXrayVo();
    }

    sentToXRay.setPatient(form.getGlobalContext().Core.getPatientShort());
    sentToXRay.setEpisode(form.getGlobalContext().Core.getEpisodeofCareShort());
    sentToXRay.setAttendance(form.getGlobalContext().Core.getCurrentCareContext());
    sentToXRay.setTrackingArea(form.getLocalContext().getcurrentTracking().getCurrentArea());

    sentToXRay.setSentBy((MemberOfStaffLiteVo) form.ccAllocatedBy().getValue());
    sentToXRay.setSentDateTime(form.dtimSent().getValue());
    sentToXRay.setReturnedDateTime(form.dtimReturned().getValue());

    return sentToXRay;
  }
  private void populateRecordBrowser() {
    form.recbrSentToXRay().clear();

    SentToXrayVoCollection collSentToXRay =
        domain.getAllSentToXRay(form.getGlobalContext().Core.getCurrentCareContext());

    if (collSentToXRay == null) {
      return;
    }

    for (int i = 0; i < collSentToXRay.size(); i++) {
      SentToXrayVo sentToXRay = collSentToXRay.get(i);

      StringBuffer string = new StringBuffer();

      if (sentToXRay.getSentDateTimeIsNotNull()) {
        string.append("Sent on: " + sentToXRay.getSentDateTime());
      }

      if (sentToXRay.getSentByIsNotNull()) {
        string.append(" - " + sentToXRay.getSentBy().getName());
      }

      if (sentToXRay.getReturnedDateTimeIsNotNull()) {
        string.append(" - Returned on: " + sentToXRay.getReturnedDateTime());
      }

      form.recbrSentToXRay().newRow(sentToXRay, string.toString());
    }

    if (collSentToXRay.size() > 0 && form.getLocalContext().getselectedSendToXRay() == null) {
      form.getLocalContext().setselectedSendToXRay(collSentToXRay.get(0));
    }

    form.recbrSentToXRay().setValue(form.getLocalContext().getselectedSendToXRay());
    populateInstanceControls(form.getLocalContext().getselectedSendToXRay());
  }
  private boolean save() {
    // WDEV-17251
    SentToXrayVo XRayClone =
        (form.getLocalContext().getselectedSendToXRay() != null
            ? (SentToXrayVo) form.getLocalContext().getselectedSendToXRay().clone()
            : null);
    SentToXrayVo sentToXRayToSave = populateInstanceDataFromScreen(XRayClone);

    String[] errors = sentToXRayToSave.validate(getUIErrors()); // 	WDEV-15334

    if (errors != null && errors.length > 0) {
      engine.showErrors(errors);
      return false;
    }

    // Check SOE
    if (sentToXRayToSave.getID_SentToXrayIsNotNull() && domain.isStale(sentToXRayToSave)) {
      engine.showMessage(ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
      if (form.getLocalContext().getselectedSendToXRayIsNotNull()) {
        form.getLocalContext()
            .setselectedSendToXRay(
                domain.getSentToXRay(form.getLocalContext().getselectedSendToXRay()));
      }
      if (form.getLocalContext().getcurrentTracking() != null) {
        form.getLocalContext()
            .setcurrentTracking(domain.getTracking(form.getLocalContext().getcurrentTracking()));
      }
      open();
      return false;
    }

    TrackingForSendToXRayVo trackingToSave = null;
    if (form.getLocalContext().getcurrentTracking().getCurrentAreaIsNotNull()
        && !Boolean.TRUE.equals(form.getLocalContext().getcurrentTracking().getIsDischarged())) {
      trackingToSave = form.getLocalContext().getcurrentTracking();

      // WDEV-17251
      if (sentToXRayToSave.getReturnedDateTime() == null
          && (sentToXRayToSave.getID_SentToXray() == null
              || form.getLocalContext().getselectedSendToXRay() != null
                  && form.getLocalContext().getselectedSendToXRay().getReturnedDateTime()
                      != null)) {
        TrackingAttendanceStatusVo trackingStatusToSave =
            populateTrackingAttendanceStatus(ims.emergency.vo.lookups.TrackingStatus.SENT_TO_XRAY);
        trackingToSave.setIsCurrentlyInXray(true);
        trackingToSave.setCurrentStatus(trackingStatusToSave);

      } else if (sentToXRayToSave.getReturnedDateTimeIsNotNull()) {
        trackingToSave.setIsCurrentlyInXray(false);

        if (form.cmbSetCurrentStatus().getValue() != null) {
          TrackingAttendanceStatusVo trackingStatusToSave =
              populateTrackingAttendanceStatus(form.cmbSetCurrentStatus().getValue());
          trackingToSave.setCurrentStatus(trackingStatusToSave);
        }
      }
    }

    try {
      if (trackingToSave != null) {
        String[] errors1 = trackingToSave.validate();

        if (errors1 != null && errors1.length > 0) {
          engine.showErrors(errors1);
          return false;
        }
        form.getLocalContext().setselectedSendToXRay(domain.save(sentToXRayToSave, trackingToSave));
        form.getLocalContext().setcurrentTracking(domain.getTracking(trackingToSave));
      } else {
        if (!domain.isMostRecentRecordReturned(
            form.getGlobalContext().Core.getCurrentCareContext())) {
          engine.showMessage(
              "You cannot create a new record Send To XRay if the most recent record is not marked as returned!");
          open();
          return false;
        }
        form.getLocalContext().setselectedSendToXRay(domain.save(sentToXRayToSave, null));
      }
    } catch (StaleObjectException e) {
      engine.showMessage(ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
      if (form.getLocalContext().getselectedSendToXRay() != null) {
        form.getLocalContext()
            .setselectedSendToXRay(
                domain.getSentToXRay(form.getLocalContext().getselectedSendToXRay()));
      }
      if (trackingToSave != null) {
        form.getLocalContext().setcurrentTracking(domain.getTracking(trackingToSave));
      }
      open();
      return false;
    }

    return true;
  }