/**
   * Impl for TTO HL7 messages you finds the current I/P care context for the patient in the message
   * (Note there will always be one open(no end/discharge date) I/P care context), then the
   * MedicationDetails record for the care context and replace the TTO coll
   */
  public void setPatientCareContextTTORecord(ims.clinical.vo.PatientTTOIfVo patientTTOs) {
    if (patientTTOs == null)
      throw new DomainRuntimeException("Message details are null, cannot process message");
    if (patientTTOs.getPatient() == null)
      throw new DomainRuntimeException("Patient details are null, cannot process message");

    DomainFactory factory = getDomainFactory();
    CareContext careContext = getOpenInPatientCareContext(patientTTOs.getPatient());
    if (careContext == null)
      throw new DomainRuntimeException("Open inpatient care context cannot be found for patient");
    MedicationDetails md = getMedicationDetailsFromCareContext(careContext);
    if (md == null)
      throw new DomainRuntimeException("Failed to get or create Medication Details record");

    md.getTTAs().clear();
    md.getAdmissionMedicationChanges().clear();
    Set<?> ttoFromMessage =
        TTODetailsIFVoAssembler.extractTTAMedicationSet(factory, patientTTOs.getTTOs());
    md.getTTAs().addAll(ttoFromMessage);

    Set<?> medChanges =
        AdmissionMedicationChangesVoAssembler.extractAdmissionMedicationChangesSet(
            factory, patientTTOs.getMedicationChanges());
    md.getAdmissionMedicationChanges().addAll(medChanges);
    try {
      factory.save(md);
    } catch (StaleObjectException soe) {
      throw new DomainRuntimeException(soe);
    }
  }
 private MedicationDetails getMedicationDetailsFromCareContext(CareContext careContext) {
   DomainFactory factory = getDomainFactory();
   MedicationDetails md;
   List<?> medicationDetailsList =
       factory.find(
           "select md from MedicationDetails md left join md.careContext cc where cc.id = :CC",
           new String[] {"CC"},
           new Object[] {careContext.getId()});
   if (medicationDetailsList == null
       || medicationDetailsList.size() == 0
       || medicationDetailsList.get(0) == null) {
     // Create one
     md = new MedicationDetails();
     md.setCareContext(careContext);
     md.setTTARequired(getDomLookup(TTORequired.REQUIRED));
     return md;
   }
   return (MedicationDetails) medicationDetailsList.get(0);
 }