Skip to content

Commit

Permalink
Supported selecting a default search from saved bookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
k-joseph committed Jun 13, 2015
1 parent cee6246 commit a2f5b83
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 61 deletions.
Expand Up @@ -190,7 +190,7 @@ public JSONObject getSearchBookmarkSearchDetailsByUuid(String uuid) {
json.put("bookmarkName", bookmark.getBookmarkName());
json.put("commaCategories", bookmark.getSelectedCategories());
json.put("categories", categories);
//TODO json.put("isDefaultSearch", bookmark.isDefaultSearch());
json.put("isDefaultSearch", bookmark.isDefaultSearch());

return json;
} else
Expand All @@ -201,7 +201,7 @@ public JSONArray saveBookmarkProperties(String uuid, String bookmarkName, String
ChartSearchBookmark bookmark = chartSearchService.getSearchBookmarkByUuid(uuid);
if (bookmark != null) {
bookmark.setBookmarkName(bookmarkName);
bookmark.setSearchPhrase(searchPhrase);
//bookmark.setSearchPhrase(searchPhrase); search phrase needs not to be modified
bookmark.setSelectedCategories(selectedCategories);

chartSearchService.saveSearchBookmark(bookmark);
Expand Down Expand Up @@ -301,6 +301,72 @@ public JSONArray deleteBookmarkOfSelectedUuids(String[] uuids) {
return GeneratingJson.getAllSearchBookmarksToReturnTomanagerUI(false);
}

/**
* Supports a search for everything as initial default search, which is overwritten by most
* recent search history and also bound to be overwritten if the user has set up a default
* search from amoung his/her saved bookmarks
*
* @return searchPhrase and selectedCategories if bookmark is default
*/
public JSONObject returnDefaultSearchPhrase(String currentSPhrase, Integer patientId) {
JSONObject json = new JSONObject();
if (StringUtils.isBlank(currentSPhrase)) {
ChartSearchCache cache = new ChartSearchCache();
String lastSearchPhraseFromHistory = cache.fetchLastHistorySearchPhrase(patientId);
List<ChartSearchBookmark> allBookmarks = chartSearchService.getAllSearchBookmarks();
ChartSearchBookmark defaultBookmark = null;

for (ChartSearchBookmark bookmark : allBookmarks) {
if (bookmark.isDefaultSearch()) {
defaultBookmark = bookmark;
} else getDefaultSearchFromHistoryIfItExists(currentSPhrase, json, lastSearchPhraseFromHistory);
}

if (defaultBookmark != null) {
List<String> categories = defaultBookmark.getSelectedCategoriesAsList();

json.put("searchPhrase", defaultBookmark.getSearchPhrase());
if (categories != null && !categories.isEmpty()) {
json.put("selectedCategories", categories);
}
} else {
getDefaultSearchFromHistoryIfItExists(currentSPhrase, json, lastSearchPhraseFromHistory);
}
}

return json;
}

private void getDefaultSearchFromHistoryIfItExists(String currentSPhrase, JSONObject json, String lastSearchPhraseFromHistory) {
if (StringUtils.isNotBlank(lastSearchPhraseFromHistory)) {
json.put("searchPhrase", lastSearchPhraseFromHistory);
} else
json.put("searchPhrase", currentSPhrase);
}

/**
* Sets only one, selected as bookmark and sets all others to false since default search from
* bookmarks can only be one
*
* @param uuid
* @return
*/
public JSONArray setBookmarkAsDefaultSearch(String uuid) {
if (StringUtils.isNotBlank(uuid)) {
List<ChartSearchBookmark> allBookmarks = chartSearchService.getAllSearchBookmarks();

for (ChartSearchBookmark bookmark : allBookmarks) {
if (bookmark.getUuid().equals(uuid)) {
bookmark.setIsDefaultSearch(true);
} else {
bookmark.setIsDefaultSearch(false);
}
chartSearchService.saveSearchBookmark(bookmark);
}
}
return GeneratingJson.getAllSearchBookmarksToReturnTomanagerUI(false);
}

private <T> T getComponent(Class<T> clazz) {
List<T> list = Context.getRegisteredComponents(clazz);
if (list == null || list.size() == 0)
Expand Down
Expand Up @@ -296,6 +296,7 @@ private static JSONObject generateBookmarksJSON(boolean wholePageIsToBeLoaded, C
json.put("categories", curBookmark.getSelectedCategories());
json.put("uuid", curBookmark.getUuid());
json.put("patientId", curBookmark.getPatient().getPatientId());
json.put("isDefaultSearch", curBookmark.isDefaultSearch());

return json;
}
Expand Down
54 changes: 28 additions & 26 deletions api/src/main/java/org/openmrs/module/chartsearch/SearchAPI.java
Expand Up @@ -17,7 +17,8 @@
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import net.sf.json.JSONObject;

import org.openmrs.api.context.Context;
import org.openmrs.module.chartsearch.solr.ChartSearchSearcher;
import org.openmrs.module.chartsearch.synonyms.SynonymsAPI;
Expand Down Expand Up @@ -66,16 +67,35 @@ public void clearResults() {
SearchAPI.resultList.clear();
}

public List<ChartListItem> search(Integer patientId, SearchPhrase searchPhrase, List<String> selectedCategoryNames) {
SearchAPI.searchPhrase = searchPhrase;
SearchAPI.setSelectedCategoryNames(selectedCategoryNames);
public List<ChartListItem> search(Integer patientId, SearchPhrase searchPhrase, List<String> selectedCategoryNames,
boolean reloadWholePage) {
SearchAPI.patientId = patientId;
System.out.println("phrase :" + searchPhrase.getPhrase());
if (searchPhrase.getPhrase().equals(",")) {
searchPhrase.setPhrase("");
List<String> categories = null;

if (reloadWholePage) {
ChartSearchCache cache = new ChartSearchCache();
JSONObject defaultSearchProps = cache.returnDefaultSearchPhrase(searchPhrase.getPhrase(),
SearchAPI.getPatientId());
String phrase = (String) defaultSearchProps.get("searchPhrase");
List<String> cats = (List<String>) defaultSearchProps.get("selectedCategories");
SearchAPI.searchPhrase = new SearchPhrase(phrase);

searchPhrase.setPhrase(phrase);
if (cats != null && !cats.isEmpty()) {
categories = cats;
} else {
categories = selectedCategoryNames;
}
} else {
SearchAPI.searchPhrase = searchPhrase;
categories = selectedCategoryNames;
if (searchPhrase.getPhrase().equals(",")) {
searchPhrase.setPhrase("");
}
}
SearchAPI.selectedCategoryNames = categories;

searchPhrase.setPhrase(returnDefaultSearchPhrase(searchPhrase.getPhrase(), SearchAPI.getPatientId()));
System.out.println("phrase :" + searchPhrase.getPhrase());

String searchPhraseStr = searchPhrase.getPhrase();

Expand Down Expand Up @@ -103,24 +123,6 @@ public List<ChartListItem> search(Integer patientId, SearchPhrase searchPhrase,
return items;
}

/**
* TODO Support defining a default search from a user's configured bookmark as default
*
* @return searchPhrase
*/
public String returnDefaultSearchPhrase(String currentSPhrase, Integer patientId) {
if (StringUtils.isBlank(currentSPhrase)) {
ChartSearchCache cache = new ChartSearchCache();
String lastSearchPhraseFromHistory = cache.fetchLastHistorySearchPhrase(patientId);

if (StringUtils.isNotBlank(lastSearchPhraseFromHistory)) {
return lastSearchPhraseFromHistory;
} else
return currentSPhrase;
} else
return currentSPhrase;
}

public SearchPhrase getSearchPhrase() {
return searchPhrase;
}
Expand Down
Expand Up @@ -13,6 +13,8 @@
*/
package org.openmrs.module.chartsearch;

import org.apache.commons.lang3.StringUtils;

public class SearchPhrase {

String phrase;
Expand All @@ -22,7 +24,7 @@ public SearchPhrase() {
}

public SearchPhrase(String phrase) {
if (phrase == ",") {
if (phrase == "," || StringUtils.isBlank(phrase)) {
this.phrase = "";
} else {
this.phrase = phrase;
Expand Down
Expand Up @@ -44,4 +44,8 @@ public JSONArray saveBookmarkProperties(@RequestParam("bookmarkUuid") String uui
public JSONArray deleteBookmarkInTheDialog(@RequestParam("bookmarkUuid") String uuid) {
return cache.deleteBookmarkInTheDialog(uuid);
}

public JSONArray setBookmarkAsDefaultSearch(@RequestParam("selectedUuid") String uuid) {
return cache.setBookmarkAsDefaultSearch(uuid);
}
}
Expand Up @@ -28,4 +28,8 @@ public void controller(FragmentModel fragmentModel) {
public JSONArray deleteSelectedHistory(@RequestParam("selectedUuids[]") String[] uuids) {
return cache.deleteHistoryOfSelectedUuids(uuids);
}

public JSONArray setBookmarkAsDefaultSearch(@RequestParam("selectedBookmarkUuid") String uuid) {
return cache.setBookmarkAsDefaultSearch(uuid);
}
}
Expand Up @@ -78,7 +78,7 @@ public String getResultsFromTheServer(FragmentModel model, @RequestParam("phrase
SearchAPI searchAPIInstance = SearchAPI.getInstance();
searchAPIInstance.clearResults();

ChartsearchPageController.searchAndReturnResults(search_phrase, patient, categories, searchAPIInstance);
ChartsearchPageController.searchAndReturnResults(search_phrase, patient, categories, searchAPIInstance, false);
return GeneratingJson.generateJson(false);
}

Expand Down
Expand Up @@ -30,13 +30,13 @@ public void controller(FragmentModel model, @RequestParam("patientId") Patient p
model.addAttribute("patientId", patient);
}

public String getResultsFromTheServer(FragmentModel model, @RequestParam("phrase") SearchPhrase search_phrase,
public String getResultsFromTheServer(FragmentModel model, @RequestParam(value = "phrase", required = false) SearchPhrase search_phrase,
@RequestParam("patientId") Patient patient,
@RequestParam(value = "categories[]", required = false) String[] categories) {
SearchAPI searchAPIInstance = SearchAPI.getInstance();
searchAPIInstance.clearResults();

ChartsearchPageController.searchAndReturnResults(search_phrase, patient, categories, searchAPIInstance);
ChartsearchPageController.searchAndReturnResults(search_phrase, patient, categories, searchAPIInstance, false);
return GeneratingJson.generateJson(false);
}

Expand Down
Expand Up @@ -52,7 +52,7 @@ public void controller(@RequestParam("patientId") Patient patient, PageModel mod
model.addAttribute("patient", patientDomainWrapper);
SearchAPI searchAPIInstance = SearchAPI.getInstance();
indexPatientData(patient);
searchAndReturnResults(search_phrase, patient, categories, searchAPIInstance);
searchAndReturnResults(search_phrase, patient, categories, searchAPIInstance, true);
}
}

Expand All @@ -66,12 +66,15 @@ private void indexPatientData(Patient patient) {
}

public static void searchAndReturnResults(SearchPhrase search_phrase, Patient patient, String[] categories,
SearchAPI searchAPIInstance) {
SearchAPI searchAPIInstance, boolean reloadWholePage) {
if (search_phrase == null) {
search_phrase = new SearchPhrase();
}
if (categories == null) {
categories = new String[0];
}
List<String> selectedCategories = Arrays.asList(categories);
List<ChartListItem> items = searchAPIInstance.search(patient.getPatientId(), search_phrase, selectedCategories);
List<ChartListItem> items = searchAPIInstance.search(patient.getPatientId(), search_phrase, selectedCategories, reloadWholePage);
List<ChartListItem> updatedItems = new ArrayList<ChartListItem>();
//loop to get full details about observations.
for (ChartListItem chartListItem : items) {
Expand All @@ -92,7 +95,7 @@ public static void searchAndReturnResults(SearchPhrase search_phrase, Patient pa
//setting results to show.
searchAPIInstance.setResults(updatedItems);

//saving search record where necessary.
//saving search record where necessary every after a search.
ChartSearchCache csCache = new ChartSearchCache();
csCache.saveOrUpdateSearchHistory(search_phrase.getPhrase(), patient.getPatientId());
}
Expand Down
60 changes: 53 additions & 7 deletions omod/src/main/webapp/fragments/manageBookmarks.gsp
Expand Up @@ -36,7 +36,7 @@
});
jq("table").on('mouseenter', 'tr', function(event) {
if(event.target.localName !== "th") {
if(event.target.localName !== "th" && event.target.offsetParent.id !== "todays-history" && event.target.offsetParent.id !== "this-weeks-history" && event.target.offsetParent.id !== "this-month-history" && event.target.offsetParent.id !== "other-history") {
jq(this).css("cursor", "pointer");
jq(this).css('background', '#F0EAEA');
}
Expand All @@ -56,8 +56,8 @@
deleteAllSelectedBookmarks();
});
jq("body").on("each", "#dialog-bookmark-categories option:selected", function (event) {
//categories.push(jq(this).text());
jq("#save-default-search").click(function(event) {
setSelectedBookmarkAsDefaultSearch();
});
jq("#dialog-bookmark-save").click(function(event) {
Expand Down Expand Up @@ -121,13 +121,23 @@
if(bookmarksAfterparse.length != 0) {
for(i = 0; i < bookmarksAfterparse.length; i++) {
var bookmark = bookmarksAfterparse[i];
var displayDefaultSearch = "";
if(bookmark.isDefaultSearch) {
displayDefaultSearch = "<input name='radiogroup' type='radio' id='" + bookmark.uuid + "' checked >";
} else {
displayDefaultSearch = "<input name='radiogroup' type='radio' id='" + bookmark.uuid + "' >";
}
trBookmarkEntries += "<tr id='" + bookmark.uuid + "'><td><label><input type='checkbox' class='bookmark-check' id='" + bookmark.uuid + "' > (" + bookmark.patientId + ")</label></td><td><input name='radiogroup' type='radio'></td><td>" + bookmark.bookmarkName + "</td><td>" + bookmark.searchPhrase + "</td><td>" + bookmark.categories + "</td></tr>";
trBookmarkEntries += "<tr id='" + bookmark.uuid + "'><td><label><input type='checkbox' class='bookmark-check' id='" + bookmark.uuid + "' > (" + bookmark.patientId + ")</label></td><td>" + displayDefaultSearch + "</td><td>" + bookmark.bookmarkName + "</td><td>" + bookmark.searchPhrase + "</td><td>" + bookmark.categories + "</td></tr>";
}
}
if(trBookmarkEntries !== "") {
jq("#returned-search-bookmarks").html(thBookmarks + trBookmarkEntries);
jq("#grouped-functionality").show();
} else {
jq("#returned-search-bookmarks").html("You currently have no saved bookmarks to manage");
jq("#grouped-functionality").hide();
}
}
Expand Down Expand Up @@ -158,19 +168,55 @@
}
}
function setSelectedBookmarkAsDefaultSearch() {
var selectedDefaultBkuuid = returnUuidOfSelectedDefaultBookmark();
if(selectedDefaultBkuuid) {
jq.ajax({
type: "POST",
url: "${ ui.actionLink('setBookmarkAsDefaultSearch') }",
data: {"selectedUuid":selectedDefaultBkuuid},
dataType: "json",
success: function(bookmarks) {
bookmarksAfterparse = bookmarks.reverse();
displayExistingBookmarks();
alert("Successfully saved default search :-)");
},
error: function(e) {
}
});
} else {
alert("Please select a bookmark to set as a default search and try again!");
}
}
function returnUuidsOfSeletedBookmarks() {
var selectedBookmarkUuids = [];
jq('#bookmarks-section input:checked').each(function() {
var selectedId = jq(this).attr("id");
if(selectedId !== "bookmark-check-all" && jq(this).attr("type") !== "radio") {
if(selectedId !== "bookmark-check-all" && jq(this).attr("type") !== "radio" && jq(this).attr("type") === "checkbox" && jq(this).attr("class") === "bookmark-check") {
selectedBookmarkUuids.push(selectedId);
}
});
return selectedBookmarkUuids;
}
function returnUuidOfSelectedDefaultBookmark() {
var selecteduuid;
jq('#bookmarks-section input:checked').each(function() {
var selectedId = jq(this).attr("id");
if(selectedId !== "bookmark-check-all" && jq(this).attr("name") === "radiogroup" && jq(this).attr("type") === "radio") {
selecteduuid = selectedId;
}
});
return selecteduuid;
}
function saveBookmarkProperties(bookmarkUuid, bkName, phrase, categories) {
jq('#selected-bookmark-dialog-content').dialog('close');
if(bookmarkUuid !== "" && bkName !== "" && phrase !== "") {
Expand Down Expand Up @@ -217,8 +263,8 @@

<div id="selected-bookmark-dialog-content">
<input type="hidden" id="dialog-bookmark-uuid" value="">
Bookmark Name: <input type="textbox" id="dialog-bookmark-name" value=""><br /><br />
Search Phrase: <input type="textbox" id="dialog-bookmark-phrase" value=""><br /><br />
Bookmark Name: <input type="textbox" id="dialog-bookmark-name" value="" /><br /><br />
Search Phrase: <input type="textbox" id="dialog-bookmark-phrase" disabled value="" /><br /><br />
Categories:
<select multiple id="dialog-bookmark-categories">
</select> <b>Tip:</b> Use Ctrl + Left Click<br /><br />
Expand Down

0 comments on commit a2f5b83

Please sign in to comment.