Advertisement
k-joseph

Untitled

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