HL7QueryServiceImpl extends BaseOpenmrsService implements HL7QueryService { @Override public String evaluateTemplate(HL7Template template, Map<String, Object> bindings) { PreparedTemplate prepared = templateCache.get(template.getName()); if (prepared == null) { TemplateFactory<?> factory = templateFactories.get(template.getLanguage()); if (factory == null) { throw new APIException("Unknown template language: " + template.getLanguage()); } prepared = factory.prepareTemplate(template.getTemplate()); templateCache.put(template.getName(), prepared); } if (bindings == null) bindings = new HashMap<String, Object>(); bindings.put("func", templateFunctions); return prepared.evaluate(bindings); } HL7QueryServiceImpl(); void setDao(HL7QueryDAO dao); HL7QueryDAO getDao(); @Override String evaluateTemplate(HL7Template template, Map<String, Object> bindings); void clearTemplateCache(); @Override HL7Template getHL7Template(Integer id); @Override HL7Template getHL7TemplateByUuid(String uuid); @Override HL7Template getHL7TemplateByName(String name); @Override List<HL7Template> getHL7TemplatesByName(String name); @Override List<HL7Template> getHL7TemplatesByEntity(String entity); @Override HL7Template saveHL7Template(HL7Template hl7Template); @Override HL7Template retireHL7Template(HL7Template hl7Template, String reason); @Override HL7Template unretireHL7Template(HL7Template hl7Template); @Override void purgeHL7Template(HL7Template hl7Template); @Override List<HL7Template> getHL7Templates(boolean includeRetired); String renderPipeDelimitedHl7(String xml);  }
@Test public void evaluateTemplate_shouldEvaluateAGroovyTemplate() throws Exception { HL7Template t = new HL7Template(); t.setLanguage(HL7QueryService.LANGUAGE_GROOVY); t.setTemplate("Easy as ${ list.join(', ') }"); Map<String, Object> bindings = new HashMap<String, Object>(); bindings.put("list", Arrays.asList(1, 2, 3)); String evaluated = service.evaluateTemplate(t, bindings); Assert.assertEquals("Easy as 1, 2, 3", evaluated); }
@Test(expected = HL7TemplateException.class) public void evaluateTemplate_shouldFailToEvaluateAGroovyTemplateAgainstBadInput() throws Exception { HL7Template t = new HL7Template(); t.setLanguage(HL7QueryService.LANGUAGE_GROOVY); t.setTemplate("Easy as ${ list.join(', ') }"); Map<String, Object> bindings = new HashMap<String, Object>(); service.evaluateTemplate(t, bindings); }
@Test(expected = APIException.class) public void evaluateTemplate_shouldFailToEvaluateATemplateOfAnUnknownLanguage() throws Exception { HL7Template t = new HL7Template(); t.setLanguage("C++"); t.setTemplate("Easy as 1, 2, 3"); service.evaluateTemplate(t, null); }