Skip to content
Permalink
Browse files

Added allergies and appointments category filters

  • Loading branch information
k-joseph committed Jul 7, 2015
1 parent 12108ca commit 0ec93e884ab111905877acef708e958283fcc2d7
@@ -0,0 +1,88 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.chartsearch;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.openmrs.module.chartsearch.solr.ChartSearchSearcher;

/**
* Handles all the logic needed to support Allergies and Appointments etc category filtering which
* is not supported using solr faceting
*/
public class ChartSearchNonFacetFiltering {

/**
* Checks if categories collection contains a specified categgory such as allergies and
* appointments
*
* @param categories
* @param categoryName
* @return
*/
public boolean checkIfCategoriesContainNonFacetCategory(List<String> categories, String categoryName) {
boolean contains = false;
if (categories != null && !categories.isEmpty() && categories.size() > 1 && StringUtils.isNotBlank(categoryName)) {
for (int i = 0; i < categories.size(); i++) {
if (categories.get(i).equals(categoryName)) {
contains = true;
break;
}
}
}
return contains;
}

public boolean checkifCategoriesContainsOnlyOneNonFacetCategory(List<String> categories, String categoryName) {
boolean contains = false;
if (categories != null && !categories.isEmpty() && StringUtils.isNotBlank(categoryName)) {
if (categories.size() == 1 && categories.get(0).equals(categoryName)) {
contains = true;
}
}

return contains;
}

public void applyNonFacetingLogicWhileSearching(Integer patientId, String searchText, List<String> selectedCategories,

This comment has been minimized.

Copy link
@k-joseph

k-joseph Jul 7, 2015

Author Member

Still has some small issues

SolrServer solrServer, SolrQuery query, List<ChartListItem> list)
throws SolrServerException {
ChartSearchSearcher searcher = new ChartSearchSearcher();

if (checkifCategoriesContainsOnlyOneNonFacetCategory(selectedCategories, "allergies")) {
searcher.searchAllergiesAndGenerateSolrDoc(patientId, searchText, solrServer, list);
} else if (checkifCategoriesContainsOnlyOneNonFacetCategory(selectedCategories, "appointments")) {
searcher.searchAppointmentsAndGenerateSolrDoc(patientId, searchText, solrServer, list);
} else if (checkIfCategoriesContainNonFacetCategory(selectedCategories, "allergies")
&& !checkIfCategoriesContainNonFacetCategory(selectedCategories, "appointments")) {
searcher.searchObservationsAndGenerateSolrDoc(solrServer, query, list);
searcher.searchAllergiesAndGenerateSolrDoc(patientId, searchText, solrServer, list);
searcher.searchEncounterTypesAndGenerateSolrDoc(patientId, searchText, solrServer, list);
searcher.searchEFormsAndGenerateSolrDoc(searchText, solrServer, list);
} else if (!checkIfCategoriesContainNonFacetCategory(selectedCategories, "allergies")
&& checkIfCategoriesContainNonFacetCategory(selectedCategories, "appointments")) {
searcher.searchObservationsAndGenerateSolrDoc(solrServer, query, list);
searcher.searchAppointmentsAndGenerateSolrDoc(patientId, searchText, solrServer, list);
searcher.searchEncounterTypesAndGenerateSolrDoc(patientId, searchText, solrServer, list);
searcher.searchEFormsAndGenerateSolrDoc(searchText, solrServer, list);
} else {
searcher.searchObservationsAndGenerateSolrDoc(solrServer, query, list);
searcher.searchAllergiesAndGenerateSolrDoc(patientId, searchText, solrServer, list);
searcher.searchAppointmentsAndGenerateSolrDoc(patientId, searchText, solrServer, list);
searcher.searchEncounterTypesAndGenerateSolrDoc(patientId, searchText, solrServer, list);
searcher.searchEFormsAndGenerateSolrDoc(searchText, solrServer, list);
}
}

}
@@ -89,7 +89,6 @@ public static String generateJson(boolean wholePageIsToBeLoaded) {
JSONArray history = getAllSearchHistoriesToSendToTheUI(wholePageIsToBeLoaded);
JSONArray bookmarks = getAllSearchBookmarksToReturnToUI(wholePageIsToBeLoaded);
List<String> catNms = SearchAPI.getSelectedCategoryNames();
String[] appliedCats = new String[catNms.size()];
JSONArray allergies = generateAllergiesJSONFromResults(returnedResults);
JSONArray appointments = generateAppointmentsJSONFromResults(returnedResults);

@@ -98,7 +97,7 @@ public static String generateJson(boolean wholePageIsToBeLoaded) {
jsonToReturn.put("searchSuggestions", searchSuggestions);
jsonToReturn.put("searchHistory", history);
jsonToReturn.put("searchBookmarks", bookmarks);
jsonToReturn.put("appliedFacets", (String[]) catNms.toArray(appliedCats));
jsonToReturn.put("appliedCategories", (String[]) catNms.toArray(new String[catNms.size()]));
jsonToReturn.put("patientAllergies", allergies);
jsonToReturn.put("patientAppointments", appointments);

@@ -34,6 +34,7 @@
import org.openmrs.module.chartsearch.AllergyItem;
import org.openmrs.module.chartsearch.AppointmentItem;
import org.openmrs.module.chartsearch.ChartListItem;
import org.openmrs.module.chartsearch.ChartSearchNonFacetFiltering;
import org.openmrs.module.chartsearch.EncounterItem;
import org.openmrs.module.chartsearch.FormItem;
import org.openmrs.module.chartsearch.ObsItem;
@@ -91,6 +92,9 @@ public Long getDocumentListCount(Integer patientId, String searchText) throws Ex
List<String> selectedCategories) throws Exception {
SolrServer solrServer = SolrSingleton.getInstance().getServer();
SolrQuery query = new SolrQuery();
List<ChartListItem> list = new ArrayList<ChartListItem>();
ChartSearchNonFacetFiltering nonFaceting = new ChartSearchNonFacetFiltering();

searchText = StringUtils.isNotBlank(searchText) ? searchText : "*";
//check for existence of characters such as ", and : in the search text and submit as it is if so
if (searchText.contains("\"")) {
@@ -125,18 +129,13 @@ public Long getDocumentListCount(Integer patientId, String searchText) throws Ex
//adding facet field for concept_class
query.addFacetField("concept_class_name");

List<ChartListItem> list = searchObservationsAndGenerateSolrDoc(solrServer, query);

searchAllergiesAndGenerateSolrDoc(patientId, searchText, solrServer, list);
searchAppointmentsAndGenerateSolrDoc(patientId, searchText, solrServer, list);
searchEncounterTypesAndGenerateSolrDoc(patientId, searchText, solrServer, list);
searchEFormsAndGenerateSolrDoc(searchText, solrServer, list);
nonFaceting.applyNonFacetingLogicWhileSearching(patientId, searchText, selectedCategories, solrServer, query, list);

return list;
}

private void searchAppointmentsAndGenerateSolrDoc(Integer patientId, String searchText, SolrServer solrServer,
List<ChartListItem> list) throws SolrServerException {
public void searchAppointmentsAndGenerateSolrDoc(Integer patientId, String searchText, SolrServer solrServer,
List<ChartListItem> list) throws SolrServerException {
SolrQuery query5 = new SolrQuery(String.format("appointment_text:(%s)", searchText));

query5.addFilterQuery(String.format("patient_id:%d", patientId));
@@ -163,7 +162,7 @@ private void searchAppointmentsAndGenerateSolrDoc(Integer patientId, String sear
}
}

private void searchEFormsAndGenerateSolrDoc(String searchText, SolrServer solrServer, List<ChartListItem> list)
public void searchEFormsAndGenerateSolrDoc(String searchText, SolrServer solrServer, List<ChartListItem> list)
throws SolrServerException {
// forms
System.out.println("Forms:");
@@ -186,7 +185,7 @@ private void searchEFormsAndGenerateSolrDoc(String searchText, SolrServer solrSe
}

@SuppressWarnings("unchecked")
private List<ChartListItem> searchObservationsAndGenerateSolrDoc(SolrServer solrServer, SolrQuery query)
public void searchObservationsAndGenerateSolrDoc(SolrServer solrServer, SolrQuery query, List<ChartListItem> list)
throws SolrServerException {
System.out.println("Observations:");
QueryResponse response = solrServer.query(query);
@@ -195,7 +194,6 @@ private void searchEFormsAndGenerateSolrDoc(String searchText, SolrServer solrSe

Iterator<SolrDocument> iter = response.getResults().iterator();

List<ChartListItem> list = new ArrayList<ChartListItem>();
while (iter.hasNext()) {
SolrDocument document = iter.next();

@@ -230,11 +228,10 @@ private void searchEFormsAndGenerateSolrDoc(String searchText, SolrServer solrSe
System.out.println(document.get("obs_id") + ", " + document.get("concept_name") + ", "
+ document.get("obs_datetime"));
}
return list;
}

private void searchEncounterTypesAndGenerateSolrDoc(Integer patientId, String searchText, SolrServer solrServer,
List<ChartListItem> list) throws SolrServerException {
public void searchEncounterTypesAndGenerateSolrDoc(Integer patientId, String searchText, SolrServer solrServer,
List<ChartListItem> list) throws SolrServerException {
// Encounters
System.out.println("Encounters:");
SolrQuery query3 = new SolrQuery(String.format("encounter_type:(%s)", searchText));
@@ -255,8 +252,8 @@ private void searchEncounterTypesAndGenerateSolrDoc(Integer patientId, String se
}
}

private void searchAllergiesAndGenerateSolrDoc(Integer patientId, String searchText, SolrServer solrServer,
List<ChartListItem> list) throws SolrServerException {
public void searchAllergiesAndGenerateSolrDoc(Integer patientId, String searchText, SolrServer solrServer,
List<ChartListItem> list) throws SolrServerException {
SolrQuery query4 = new SolrQuery(String.format("allergy_text:(%s)", searchText));

query4.addFilterQuery(String.format("patient_id:%d", patientId));
@@ -78,6 +78,7 @@ public static void searchAndReturnResults(SearchPhrase search_phrase, Patient pa
List<ChartListItem> items = searchAPIInstance.search(patient.getPatientId(), search_phrase, selectedCategories,
reloadWholePage);
List<ChartListItem> updatedItems = new ArrayList<ChartListItem>();

for (ChartListItem chartListItem : items) {
if (chartListItem instanceof ObsItem) {
int itemObsId = ((ObsItem) chartListItem).getObsId();
@@ -214,7 +214,7 @@ div.ui-datepicker{
.display_filter_onclick {
left:-1px;
margin-left:0px;
width: 140px;
width: 152px;
}
.dropdown .insider_categories:active {
@@ -555,7 +555,7 @@
updateBookmarksAndNotesUI();
displayQuickSearches();
updateCategeriesAtUIGlobally(jsonAfterParse.appliedFacets);
updateCategeriesAtUIGlobally(jsonAfterParse.appliedCategories);
},
error: function(e) {}
});
@@ -13,7 +13,7 @@
showHistorySuggestionsOnLoad();
displayBothPersonalAndGlobalNotes();
updateCategeriesAtUIGlobally(jsonAfterParse.appliedFacets);
updateCategeriesAtUIGlobally(jsonAfterParse.appliedCategories);
jq( "#date_filter_title" ).click(function() {
jq( "#date_filter_options" ).toggle();
@@ -250,7 +250,7 @@
displayBothPersonalAndGlobalNotes();
displayQuickSearches();
updateBookmarksAndNotesUI();
updateCategeriesAtUIGlobally(jsonAfterParse.appliedFacets);
updateCategeriesAtUIGlobally(jsonAfterParse.appliedCategories);
},
error: function(e) {
//alert("Error occurred!!! " + e);
@@ -701,15 +701,15 @@
<i class="icon-sort-down" id="icon-arrow-dropdown"></i>
</span>
<div class="filter_categories" id="filter_categories_categories">
<a href="" id="selectAll_categories" class="disabled_link">Select All</a>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<a href="" id="deselectAll_categories" class="disabled_link">Clear</a>
<a href="" id="selectAll_categories" class="disabled_link">Select All</a>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<a href="" id="deselectAll_categories" class="disabled_link">Clear</a>
<br /><hr />
<div id="inside_filter_categories">
<script type="text/javascript">
displayCategories(jsonAfterParse);
</script>
</div>
<hr />
<input id="submit_selected_categories" type="submit" value="OK" />&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<a href="" id="hide_categories">Cancel</a>
<input id="submit_selected_categories" type="submit" value="OK" />&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<a href="" id="hide_categories">Cancel</a>
</div>
</div>
</div>
@@ -245,8 +245,7 @@ function addAppointmentsToResults(app) {
resultText += type;
resultText += '</h3>';
resultText += '<br><span class="obsgroup_date">';
resultText += start.toTimeString() + ' - '
+ end.toTimeString();
resultText += start.toTimeString() + ' - ' + end.toTimeString();
resultText += '</span></div>';
if (typeDesc) {
resultText += '<span class="obsgroup_value">';
@@ -589,7 +588,7 @@ function load_appointment(appId) {
var cancelReason = app.cancelReason;
var provider = app.provider;
var date;

typeDesc = !typeDesc ? "" : typeDesc;
provider = !provider ? "" : provider;
reason = !reason ? "" : reason;
@@ -607,7 +606,8 @@ function load_appointment(appId) {
resultText += type;
resultText += '</h3>';
resultText += '<table>';
resultText += '<tr><th>Appointment Service Type:</th><td>' + type + '</td></tr>';
resultText += '<tr><th>Appointment Service Type:</th><td>' + type
+ '</td></tr>';
resultText += '<tr><th>Description:</th><td>' + typeDesc + '</td></tr>';
resultText += '<tr><th>Provider:</th><td>' + provider + '</td></tr>';
resultText += '<tr><th>Reason:</th><td>' + reason + '</td></tr>';
@@ -1203,7 +1203,8 @@ function refresh_data() {
searchText.value = jsonAfterParse.search_phrase;
var numberOfResults = jsonAfterParse.obs_groups.length
+ jsonAfterParse.obs_singles.length
+ jsonAfterParse.patientAllergies.length + jsonAfterParse.patientAppointments.length;
+ jsonAfterParse.patientAllergies.length
+ jsonAfterParse.patientAppointments.length;
document.getElementById('found-results-summary').innerHTML = "<b>"
+ numberOfResults + "</b> Results (<b>"
+ jsonAfterParse.retrievalTime + "</b> seconds)";
@@ -1223,6 +1224,8 @@ function refresh_data() {
function displayCategories(jsonAfterParse) {
var categories = document.getElementsByClassName('category_check');
var checkedCategories = new Object();
var allergies = jsonAfterParse.patientAllergies;
var appointments = jsonAfterParse.patientAppointments;

if (jsonAfterParse.noResults.foundNoResults) {
document.getElementById('inside_filter_categories').innerHTML = "";
@@ -1239,27 +1242,32 @@ function displayCategories(jsonAfterParse) {
document.getElementById('inside_filter_categories').innerHTML = "";

// now fetch and display new categories from the server

displayNonFacetCategories(allergies, "allergies");
displayNonFacetCategories(appointments, "appointments");

for ( var i = 0; i < jsonAfterParse.facets.length; i++) {
var name = jsonAfterParse.facets[i].facet.name;
var count = jsonAfterParse.facets[i].facet.count;
var displaycheckBox;
var displayDetail;

if (count != 0) {
var displaycheckBox = "<div class='category_filter_item'><input class='category_check' id='"
displaycheckBox = "<div class='category_filter_item'><input class='category_check' id='"
+ name
+ "_category' type='checkbox' name='categories' value='"
+ name;
var displayDetail = "<a href='' class='select_one_category' id='select_"
displayDetail = "<a href='' class='select_one_category' id='select_"
+ name
+ "_category'>"
+ capitalizeFirstLetter(name)
+ "</a> (" + count + ") </div>";

} else {
var displaycheckBox = "<div class='category_filter_item-disabled'><input class='category_check' id='"
displaycheckBox = "<div class='category_filter_item-disabled'><input class='category_check' id='"
+ name
+ "_category' type='checkbox' name='categories' value='"
+ name;
var displayDetail = "<a href='' class='select_one_category' id='select_"
displayDetail = "<a href='' class='select_one_category' id='select_"
+ name
+ "_category'>"
+ capitalizeFirstLetter(name)
@@ -1277,6 +1285,35 @@ function displayCategories(jsonAfterParse) {
}
}

function displayNonFacetCategories(cat, catName) {
var displayNonFacetcheckBox;
var displayNonFacetDetail;

if (cat.length !== 0) {
displayNonFacetcheckBox = "<div class='category_filter_item'><input class='category_check' id='"
+ catName
+ "_category' type='checkbox' name='categories' value='"
+ catName + "' />";
displayNonFacetDetail = "<a href='' class='select_one_category' id='select_"
+ catName
+ "_category'>"
+ capitalizeFirstLetter(catName)
+ "</a> (" + cat.length + ") </div>";
} else {
displayNonFacetcheckBox = "<div class='category_filter_item-disabled'><input class='category_check' id='"
+ catName
+ "_category' type='checkbox' name='categories' value='"
+ catName + "' />";
displayNonFacetDetail = "<a href='' class='select_one_category' id='select_"
+ catName
+ "_category'>"
+ capitalizeFirstLetter(catName)
+ "</a> (" + 0 + ") </div>";
}
document.getElementById('inside_filter_categories').innerHTML += displayNonFacetcheckBox
+ displayNonFacetDetail;
}

/*
* In-case the has doesn't have privileges to view some results, they are not
* returned and a message is instead displayed

0 comments on commit 0ec93e8

Please sign in to comment.
You can’t perform that action at this time.