Advertisement
k-joseph

Untitled

Jun 25th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. package org.openmrs.module.chartsearch.page.controller;
  2.  
  3. /**
  4. * Created by Eli on 10/03/14.
  5. */
  6.  
  7. import org.openmrs.Patient;
  8. import org.openmrs.api.context.Context;
  9. import org.openmrs.module.appui.UiSessionContext;
  10. import org.openmrs.module.chartsearch.*;
  11. import org.openmrs.module.chartsearch.solr.ChartSearchIndexer;
  12. import org.openmrs.module.chartsearch.solr.ChartSearchSearcher;
  13. import org.openmrs.module.chartsearch.web.dwr.DWRChartSearchService;
  14. import org.openmrs.module.emrapi.patient.PatientDomainWrapper;
  15. import org.openmrs.ui.framework.annotation.BindParams;
  16. import org.openmrs.ui.framework.annotation.InjectBeans;
  17. import org.openmrs.ui.framework.page.PageModel;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.web.bind.annotation.RequestParam;
  21.  
  22. import java.lang.reflect.Array;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.List;
  26.  
  27. import javax.servlet.http.HttpServletRequest;
  28.  
  29. public class ChartsearchPageController {
  30.  
  31. private static final Logger log = LoggerFactory.getLogger(ChartsearchPageController.class);
  32.  
  33. private ChartSearchIndexer chartSearchIndexer = getComponent(ChartSearchIndexer.class);
  34.  
  35. private ChartSearchSearcher searcher = getComponent(ChartSearchSearcher.class);
  36.  
  37. public void controller(PageModel model, @BindParams SearchPhrase search_phrase, UiSessionContext sessionContext, @RequestParam("patientId") Patient patient,
  38. @InjectBeans PatientDomainWrapper patientDomainWrapper, HttpServletRequest request) {
  39.  
  40. //String[] cats = {"Test", "Finding", "Misc"} ;categories.setCategories(cats);
  41. String[] categories = request.getParameterValues("categories");
  42. System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + categories);
  43. System.out.println(categories.toString());
  44. List<String> selectedCategories = Arrays.asList(categories);
  45.  
  46.  
  47. patientDomainWrapper.setPatient(patient);
  48. model.addAttribute("patient", patientDomainWrapper);
  49.  
  50. SearchAPI searchAPIInstance = SearchAPI.getInstance();
  51.  
  52. log.info("getting patient ID :" + patient);
  53. log.info("trying to index a patient");
  54.  
  55. if (chartSearchIndexer != null && patient != null) {
  56. //chartSearchIndexer.clearIndex(IndexClearStrategies.IDS.toString(), patient.getPatientId()+"", 0, 0);
  57. chartSearchIndexer.indexPatientData(patient.getPatientId());
  58. }
  59. log.info("indexed patient");
  60. //Searching an empty phrase to get all results to show at start
  61. SearchPhrase emptyPhrase = new SearchPhrase("");
  62. List<ChartListItem> items = searchAPIInstance.search(patient.getPatientId(), search_phrase, selectedCategories);
  63. List<ChartListItem> updatedItems = new ArrayList<ChartListItem>();
  64. //loop to get full details about observations.
  65. for (ChartListItem chartListItem : items) {
  66. if (chartListItem instanceof ObsItem) {
  67. int itemObsId = ((ObsItem) chartListItem).getObsId();
  68. ChartListItem updatedObservation = DWRChartSearchService.getObservationDetails(itemObsId);
  69. updatedItems.add(updatedObservation);
  70. }
  71. if (chartListItem instanceof FormItem) {
  72. updatedItems.add(chartListItem);
  73. }
  74.  
  75. if (chartListItem instanceof EncounterItem) {
  76. updatedItems.add(chartListItem);
  77. }
  78.  
  79. }
  80.  
  81. //setting results to show.
  82. searchAPIInstance.setResults(updatedItems);
  83.  
  84. }
  85.  
  86. private <T> T getComponent(Class<T> clazz) {
  87. List<T> list = Context.getRegisteredComponents(clazz);
  88. if (list == null || list.size() == 0)
  89. throw new RuntimeException("Cannot find component of " + clazz);
  90. return list.get(0);
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement