Advertisement
k-joseph

Untitled

Jun 25th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 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.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.List;
  25.  
  26. public class ChartsearchPageController {
  27.  
  28. private static final Logger log = LoggerFactory.getLogger(ChartsearchPageController.class);
  29.  
  30. private ChartSearchIndexer chartSearchIndexer = getComponent(ChartSearchIndexer.class);
  31.  
  32. private ChartSearchSearcher searcher = getComponent(ChartSearchSearcher.class);
  33.  
  34. public void controller(PageModel model, @BindParams SearchPhrase search_phrase, UiSessionContext sessionContext, @RequestParam("patientId") Patient patient,
  35. @InjectBeans PatientDomainWrapper patientDomainWrapper, @BindParams SearchCategory categories) {
  36.  
  37. List<String>selectedCategories = Arrays.asList(categories.getCategories());
  38.  
  39. System.out.println(categories);
  40. System.out.println(categories.getCategories().toString());
  41.  
  42. patientDomainWrapper.setPatient(patient);
  43. model.addAttribute("patient", patientDomainWrapper);
  44.  
  45. SearchAPI searchAPIInstance = SearchAPI.getInstance();
  46.  
  47. log.info("getting patient ID :" + patient);
  48. log.info("trying to index a patient");
  49.  
  50. if (chartSearchIndexer != null && patient != null) {
  51. //chartSearchIndexer.clearIndex(IndexClearStrategies.IDS.toString(), patient.getPatientId()+"", 0, 0);
  52. chartSearchIndexer.indexPatientData(patient.getPatientId());
  53. }
  54. log.info("indexed patient");
  55. //Searching an empty phrase to get all results to show at start
  56. SearchPhrase emptyPhrase = new SearchPhrase("");
  57. List<ChartListItem> items = searchAPIInstance.search(patient.getPatientId(), search_phrase, selectedCategories);
  58. List<ChartListItem> updatedItems = new ArrayList<ChartListItem>();
  59. //loop to get full details about observations.
  60. for (ChartListItem chartListItem : items) {
  61. if (chartListItem instanceof ObsItem) {
  62. int itemObsId = ((ObsItem) chartListItem).getObsId();
  63. ChartListItem updatedObservation = DWRChartSearchService.getObservationDetails(itemObsId);
  64. updatedItems.add(updatedObservation);
  65. }
  66. if (chartListItem instanceof FormItem) {
  67. updatedItems.add(chartListItem);
  68. }
  69.  
  70. if (chartListItem instanceof EncounterItem) {
  71. updatedItems.add(chartListItem);
  72. }
  73.  
  74. }
  75.  
  76. //setting results to show.
  77. searchAPIInstance.setResults(updatedItems);
  78.  
  79. }
  80.  
  81. private <T> T getComponent(Class<T> clazz) {
  82. List<T> list = Context.getRegisteredComponents(clazz);
  83. if (list == null || list.size() == 0)
  84. throw new RuntimeException("Cannot find component of " + clazz);
  85. return list.get(0);
  86. }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement