private List <OrderNoteboxItem> BuildOrderNoteboxItems(List <NoteboxItem> inboxItems) { if (inboxItems.Count == 0) { return(new List <OrderNoteboxItem>()); } // Get all the patients for all the items var patients = CollectionUtils.Unique(CollectionUtils.Map <NoteboxItem, Patient>(inboxItems, item => item.Patient)); var patientQuery = new HqlProjectionQuery(new HqlFrom(typeof(PatientProfile).Name, "pp")); var patientCriteria = new PatientProfileSearchCriteria(); patientCriteria.Patient.In(patients); patientQuery.Conditions.AddRange(HqlCondition.FromSearchCriteria("pp", patientCriteria)); var profiles = ExecuteHql <PatientProfile>(patientQuery); // Have to manually get the postings (and later their recipients) to work around a Hibernate fetch="subselect" issue. // The subselect somehow removed the "top(100)" and "order by" clause. Making the query slow. // Load all the postings for all the notes. There may be more than one postings per orderNote. // Therefore it is inappropriate to just use the postings in the base query. var notes = CollectionUtils.Unique(CollectionUtils.Map <NoteboxItem, Note>(inboxItems, item => item.Note)); var postingQuery = new HqlProjectionQuery(new HqlFrom(typeof(NotePosting).Name, "np")); var postingCriteria = new NotePostingSearchCriteria(); postingCriteria.Note.In(notes); postingQuery.Conditions.AddRange(HqlCondition.FromSearchCriteria("np", postingCriteria)); postingQuery.Froms[0].Joins.Add(new HqlJoin("np.Recipient", null, HqlJoinMode.Left, true)); var postings = ExecuteHql <NotePosting>(postingQuery); // Build order notebox items var orderNoteboxItems = CollectionUtils.Map(inboxItems, delegate(NoteboxItem item) { // Find the appropriate patient profile based on OrderingFacility var profile = CollectionUtils.SelectFirst(profiles, pp => pp.Patient.Equals(item.Patient) && pp.Mrn.AssigningAuthority.Code.Equals(item.OrderingFacilityInformationAuthority.Code)); // Find all the recipients var postingsForThisNote = CollectionUtils.Select(postings, np => np.Note.Equals(item.Note)); var recipients = CollectionUtils.Map <NotePosting, object>(postingsForThisNote, posting => posting is StaffNotePosting ? (object)((StaffNotePosting)posting).Recipient : (object)((GroupNotePosting)posting).Recipient); return(new OrderNoteboxItem(item.Note, item.Order, item.Patient, profile, item.Author, recipients, item.DiagnosticServiceName, item.NotePostingAcknowledged)); }); return(orderNoteboxItems); }
private List<OrderNoteboxItem> BuildOrderNoteboxItems(List<NoteboxItem> inboxItems) { if (inboxItems.Count == 0) return new List<OrderNoteboxItem>(); // Get all the patients for all the items var patients = CollectionUtils.Unique(CollectionUtils.Map<NoteboxItem, Patient>(inboxItems, item => item.Patient)); var patientQuery = new HqlProjectionQuery(new HqlFrom(typeof(PatientProfile).Name, "pp")); var patientCriteria = new PatientProfileSearchCriteria(); patientCriteria.Patient.In(patients); patientQuery.Conditions.AddRange(HqlCondition.FromSearchCriteria("pp", patientCriteria)); var profiles = ExecuteHql<PatientProfile>(patientQuery); // Have to manually get the postings (and later their recipients) to work around a Hibernate fetch="subselect" issue. // The subselect somehow removed the "top(100)" and "order by" clause. Making the query slow. // Load all the postings for all the notes. There may be more than one postings per orderNote. // Therefore it is inappropriate to just use the postings in the base query. var notes = CollectionUtils.Unique(CollectionUtils.Map<NoteboxItem, Note>(inboxItems, item => item.Note)); var postingQuery = new HqlProjectionQuery(new HqlFrom(typeof(NotePosting).Name, "np")); var postingCriteria = new NotePostingSearchCriteria(); postingCriteria.Note.In(notes); postingQuery.Conditions.AddRange(HqlCondition.FromSearchCriteria("np", postingCriteria)); postingQuery.Froms[0].Joins.Add(new HqlJoin("np.Recipient", null, HqlJoinMode.Left, true)); var postings = ExecuteHql<NotePosting>(postingQuery); // Build order notebox items var orderNoteboxItems = CollectionUtils.Map(inboxItems, delegate(NoteboxItem item) { // Find the appropriate patient profile based on OrderingFacility var profile = CollectionUtils.SelectFirst(profiles, pp => pp.Patient.Equals(item.Patient) && pp.Mrn.AssigningAuthority.Code.Equals(item.OrderingFacilityInformationAuthority.Code)); // Find all the recipients var postingsForThisNote = CollectionUtils.Select(postings, np => np.Note.Equals(item.Note)); var recipients = CollectionUtils.Map<NotePosting, object>(postingsForThisNote, posting => posting is StaffNotePosting ? (object)((StaffNotePosting)posting).Recipient : (object)((GroupNotePosting)posting).Recipient); return new OrderNoteboxItem(item.Note, item.Order, item.Patient, profile, item.Author, recipients, item.DiagnosticServiceName, item.NotePostingAcknowledged); }); return orderNoteboxItems; }