Advertisement
k-joseph

Untitled

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