public static JSONArray getAllSearchHistoriesToSendToTheUI() {
    JSONArray histories = new JSONArray();
    List<ChartSearchHistory> allHistory = chartSearchService.getAllSearchHistory();

    for (ChartSearchHistory history : allHistory) {
      JSONObject json = null;
      if (Context.getAuthenticatedUser().getUserId().equals(history.getHistoryOwner().getUserId())
          && history.getPatient().getPatientId().equals(SearchAPI.getInstance().getPatientId())) {
        json = generateHistoryJSON(history);
      }
      if (json != null) {
        histories.add(json);
      }
    }

    return histories;
  }
 private static JSONObject generateHistoryJSON(ChartSearchHistory history) {
   JSONObject json;
   json = new JSONObject();
   json.put("searchPhrase", history.getSearchPhrase());
   json.put(
       "lastSearchedAt",
       history
           .getLastSearchedAt()
           .getTime()); // passing timestamp from java to client js is a better practice
   json.put(
       "formattedLastSearchedAt", Context.getDateFormat().format(history.getLastSearchedAt()));
   json.put("uuid", history.getUuid());
   json.put("patientId", history.getPatient().getPatientId());
   json.put("patientFamilyName", history.getPatient().getPersonName().getFamilyName());
   return json;
 }