@RequestMapping("/module/htmlformentry/drugSearch") public void localizedMessage(@RequestParam("term") String query, HttpServletResponse response) throws IOException { List<Drug> drugs; // we want to use a later API method from 1.8+ if it is available, so we need to access it via // reflection if (OpenmrsConstants.OPENMRS_VERSION_SHORT.startsWith("1.6") || OpenmrsConstants.OPENMRS_VERSION_SHORT.startsWith("1.7")) { drugs = conceptService.getDrugs(query); // this method returns retired drugs, so it is not ideal } else { try { Object conceptService = Context.getService(Context.loadClass("org.openmrs.api.ConceptService")); Method getDrugsMethod = conceptService .getClass() .getMethod( "getDrugs", String.class, Concept.class, boolean.class, boolean.class, boolean.class, Integer.class, Integer.class); drugs = (List<Drug>) getDrugsMethod.invoke( conceptService, query, null, true, false, false, 0, 100); // this method excludes retired drugs } catch (Exception ex) { throw new RuntimeException( "Unable to access ConceptService getDrugs method via reflection", ex); } } List<Map<String, Object>> simplified = drugCompatibility.simplify(drugs); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); new ObjectMapper().writeValue(out, simplified); }
/** * This method exists to allow us to quickly support providers as introduce in OpenMRS 1.9.x, * without having to branch the module. We should remove this method when do a proper * implementation. * * @return */ private boolean openmrsVersionDoesNotSupportProviders() { return OpenmrsConstants.OPENMRS_VERSION_SHORT.startsWith("1.6") || OpenmrsConstants.OPENMRS_VERSION_SHORT.startsWith("1.7") || OpenmrsConstants.OPENMRS_VERSION_SHORT.startsWith("1.8"); }