public List<String> search(UiSessionContext context, @RequestParam("id") String query) throws Exception { Locale locale = context.getLocale(); List<ConceptSearchResult> hits = Context.getConceptService().getConcepts(query, locale, true); if (hits.size() > 100) { hits = hits.subList(0, 100); } List<String> list = new ArrayList<String>(); Gson gson = new Gson(); for (ConceptSearchResult hit : hits) { list.add(gson.toJson(simplifyConcept(hit.getConcept(), locale))); } return list; }
/** Concept Search */ @RequestMapping("/module/htmlformentry/conceptSearch") public void conceptSearch( ModelMap model, HttpServletRequest request, HttpServletResponse response, @RequestParam(required = true, value = "term") String query, @RequestParam(required = false, value = "answerids") String allowedconceptids, @RequestParam(required = false, value = "answerclasses") String answerclasses) throws Exception { response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); List<Locale> l = new Vector<Locale>(); l.add(Context.getLocale()); List<ConceptClass> cptClassList = new ArrayList<ConceptClass>(); HashSet<Integer> set = new HashSet<Integer>(); if (!"null".equals(allowedconceptids) && !"".equals(allowedconceptids)) { // we filter this by conceptids for (StringTokenizer st = new StringTokenizer(allowedconceptids, ","); st.hasMoreTokens(); ) { set.add(Integer.parseInt(st.nextToken())); } } else if (!"null".equals(answerclasses) && !"".equals(answerclasses)) { for (StringTokenizer st = new StringTokenizer(answerclasses, ","); st.hasMoreTokens(); ) { cptClassList.add(conceptService.getConceptClassByName(st.nextToken())); } } else { throw new Exception("answerconceptids set and answerclasses are both empty."); } List<ConceptSearchResult> results = conceptService.getConcepts( query, l, false, cptClassList, null, null, null, null, null, null); if (!set.isEmpty()) { for (Iterator<ConceptSearchResult> it = results.iterator(); it.hasNext(); ) { if (!set.contains(it.next().getConcept().getConceptId())) { it.remove(); } } } // return in JSON object list format // [ { "id": "Dromas ardeola", "label": "Crab-Plover", "value":"Crab-Plover" }, out.print("["); for (Iterator<ConceptSearchResult> i = results.iterator(); i.hasNext(); ) { ConceptSearchResult res = i.next(); String ds = res.getConcept().getDisplayString(); out.print("{ \"value\":\""); if (res.getConceptName().isPreferred() || res.getConceptName().getName().equalsIgnoreCase(ds)) { out.print(WebUtil.escapeQuotes(res.getConceptName().getName())); } else { out.print(WebUtil.escapeQuotes(res.getConcept().getDisplayString())); } out.print("\",\"id\""); out.print(":\"" + res.getConcept().getId()); out.print("\"}"); if (i.hasNext()) out.print(","); } out.print("]"); }