@Test public void testSomeUtilityFunctionForRequireExpression() throws Exception { List<Map<String,Map>> test = new ArrayList<Map<String, Map>>(); test.add(new HashMap<String, Map>()); test.add(new HashMap<String, Map>()); test.get(0).put("encounter", new HashMap<String, Integer>()); test.get(0).get("encounter").put("encounterType", 1); test.get(1).put("encounter", new HashMap<String, Integer>()); test.get(1).get("encounter").put("encounterType", 2); AppContextModel contextModel = new AppContextModel(); contextModel.put("test", test); assertTrue(service.checkRequireExpression(extensionRequiring("some(test, function(it) { return it.encounter.encounterType === 1})"), contextModel)); }
@Override public boolean checkRequireExpression(Requireable candidate, AppContextModel contextModel) { try { String requireExpression = candidate.getRequire(); if (StringUtils.isBlank(requireExpression)) { return true; } else { Bindings bindings = new SimpleBindings(); Map<String, Object> mapProperties = new HashMap<String, Object>(); for (Map.Entry<String, Object> e : contextModel.entrySet()) { if (e.getValue() instanceof Map) { mapProperties.put(e.getKey(), e.getValue()); } else { bindings.put(e.getKey(), e.getValue()); } } javascriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); ObjectMapper jackson = new ObjectMapper(); for (Map.Entry<String, Object> e : mapProperties.entrySet()) { try { javascriptEngine.eval("var " + e.getKey() + " = " + jackson.writeValueAsString(e.getValue()) + ";"); } catch (Exception ex) { StringBuilder extraInfo = new StringBuilder(); extraInfo.append("type:").append(e.getValue().getClass().getName()); if (e.getValue() instanceof Map) { Map<?, ?> map = (Map<?, ?>) e.getValue(); extraInfo.append(" properties:"); for (Map.Entry entry : map.entrySet()) { extraInfo.append(" ").append(entry.getKey().toString()).append(":") .append(entry.getValue() == null ? "null" : entry.getValue().getClass().toString()); } } log.error("Failed to set '" + e.getKey() + "' scope variable (" + extraInfo + ") while evaluating require check for " + candidate.getClass().getSimpleName() .toLowerCase() + " " + candidate.getId(), ex); return false; } } return javascriptEngine.eval("(" + requireExpression + ") == true").equals(Boolean.TRUE); } } catch (Exception e) { log.error("Failed to evaluate 'require' check for extension " + candidate.getId(), e); return false; } }
AppFrameworkServiceImpl extends BaseOpenmrsService implements AppFrameworkService { @Override public boolean checkRequireExpression(Requireable candidate, AppContextModel contextModel) { try { String requireExpression = candidate.getRequire(); if (StringUtils.isBlank(requireExpression)) { return true; } else { Bindings bindings = new SimpleBindings(); Map<String, Object> mapProperties = new HashMap<String, Object>(); for (Map.Entry<String, Object> e : contextModel.entrySet()) { if (e.getValue() instanceof Map) { mapProperties.put(e.getKey(), e.getValue()); } else { bindings.put(e.getKey(), e.getValue()); } } javascriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); ObjectMapper jackson = new ObjectMapper(); for (Map.Entry<String, Object> e : mapProperties.entrySet()) { try { javascriptEngine.eval("var " + e.getKey() + " = " + jackson.writeValueAsString(e.getValue()) + ";"); } catch (Exception ex) { StringBuilder extraInfo = new StringBuilder(); extraInfo.append("type:").append(e.getValue().getClass().getName()); if (e.getValue() instanceof Map) { Map<?, ?> map = (Map<?, ?>) e.getValue(); extraInfo.append(" properties:"); for (Map.Entry entry : map.entrySet()) { extraInfo.append(" ").append(entry.getKey().toString()).append(":") .append(entry.getValue() == null ? "null" : entry.getValue().getClass().toString()); } } log.error("Failed to set '" + e.getKey() + "' scope variable (" + extraInfo + ") while evaluating require check for " + candidate.getClass().getSimpleName() .toLowerCase() + " " + candidate.getId(), ex); return false; } } return javascriptEngine.eval("(" + requireExpression + ") == true").equals(Boolean.TRUE); } } catch (Exception e) { log.error("Failed to evaluate 'require' check for extension " + candidate.getId(), e); return false; } } }
AppFrameworkServiceImpl extends BaseOpenmrsService implements AppFrameworkService { @Override public boolean checkRequireExpression(Requireable candidate, AppContextModel contextModel) { try { String requireExpression = candidate.getRequire(); if (StringUtils.isBlank(requireExpression)) { return true; } else { Bindings bindings = new SimpleBindings(); Map<String, Object> mapProperties = new HashMap<String, Object>(); for (Map.Entry<String, Object> e : contextModel.entrySet()) { if (e.getValue() instanceof Map) { mapProperties.put(e.getKey(), e.getValue()); } else { bindings.put(e.getKey(), e.getValue()); } } javascriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); ObjectMapper jackson = new ObjectMapper(); for (Map.Entry<String, Object> e : mapProperties.entrySet()) { try { javascriptEngine.eval("var " + e.getKey() + " = " + jackson.writeValueAsString(e.getValue()) + ";"); } catch (Exception ex) { StringBuilder extraInfo = new StringBuilder(); extraInfo.append("type:").append(e.getValue().getClass().getName()); if (e.getValue() instanceof Map) { Map<?, ?> map = (Map<?, ?>) e.getValue(); extraInfo.append(" properties:"); for (Map.Entry entry : map.entrySet()) { extraInfo.append(" ").append(entry.getKey().toString()).append(":") .append(entry.getValue() == null ? "null" : entry.getValue().getClass().toString()); } } log.error("Failed to set '" + e.getKey() + "' scope variable (" + extraInfo + ") while evaluating require check for " + candidate.getClass().getSimpleName() .toLowerCase() + " " + candidate.getId(), ex); return false; } } return javascriptEngine.eval("(" + requireExpression + ") == true").equals(Boolean.TRUE); } } catch (Exception e) { log.error("Failed to evaluate 'require' check for extension " + candidate.getId(), e); return false; } } AppFrameworkServiceImpl(AllAppTemplates allAppTemplates, AllAppDescriptors allAppDescriptors,
	    AllFreeStandingExtensions allFreeStandingExtensions, AllComponentsState allComponentsState,
	    LocationService locationService, FeatureToggleProperties featureToggles, AppFrameworkConfig appFrameworkConfig,
	    AllUserApps allUserApps); }
AppFrameworkServiceImpl extends BaseOpenmrsService implements AppFrameworkService { @Override public boolean checkRequireExpression(Requireable candidate, AppContextModel contextModel) { try { String requireExpression = candidate.getRequire(); if (StringUtils.isBlank(requireExpression)) { return true; } else { Bindings bindings = new SimpleBindings(); Map<String, Object> mapProperties = new HashMap<String, Object>(); for (Map.Entry<String, Object> e : contextModel.entrySet()) { if (e.getValue() instanceof Map) { mapProperties.put(e.getKey(), e.getValue()); } else { bindings.put(e.getKey(), e.getValue()); } } javascriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); ObjectMapper jackson = new ObjectMapper(); for (Map.Entry<String, Object> e : mapProperties.entrySet()) { try { javascriptEngine.eval("var " + e.getKey() + " = " + jackson.writeValueAsString(e.getValue()) + ";"); } catch (Exception ex) { StringBuilder extraInfo = new StringBuilder(); extraInfo.append("type:").append(e.getValue().getClass().getName()); if (e.getValue() instanceof Map) { Map<?, ?> map = (Map<?, ?>) e.getValue(); extraInfo.append(" properties:"); for (Map.Entry entry : map.entrySet()) { extraInfo.append(" ").append(entry.getKey().toString()).append(":") .append(entry.getValue() == null ? "null" : entry.getValue().getClass().toString()); } } log.error("Failed to set '" + e.getKey() + "' scope variable (" + extraInfo + ") while evaluating require check for " + candidate.getClass().getSimpleName() .toLowerCase() + " " + candidate.getId(), ex); return false; } } return javascriptEngine.eval("(" + requireExpression + ") == true").equals(Boolean.TRUE); } } catch (Exception e) { log.error("Failed to evaluate 'require' check for extension " + candidate.getId(), e); return false; } } AppFrameworkServiceImpl(AllAppTemplates allAppTemplates, AllAppDescriptors allAppDescriptors,
	    AllFreeStandingExtensions allFreeStandingExtensions, AllComponentsState allComponentsState,
	    LocationService locationService, FeatureToggleProperties featureToggles, AppFrameworkConfig appFrameworkConfig,
	    AllUserApps allUserApps); @Override List<AppDescriptor> getAllApps(); @Override List<Extension> getExtensionsById(String extensionPointId, String id); @Override // TODO shouldn't this be getAllFreeStandingExtensions? List<Extension> getAllExtensions(String extensionPointId); @Override List<AppDescriptor> getAllEnabledApps(); @Override List<Extension> getAllEnabledExtensions(); @Override List<Extension> getAllEnabledExtensions(String extensionPointId); @Override void enableApp(String appId); @Override void disableApp(String appId); @Override void enableExtension(String extensionId); @Override void disableExtension(String extensionId); @Override List<Extension> getExtensionsForCurrentUser(); @Override List<Extension> getExtensionsForCurrentUser(String extensionPointId); @Override List<Extension> getExtensionsForCurrentUser(String extensionPointId, AppContextModel contextModel); @Override boolean checkRequireExpression(Requireable candidate, AppContextModel contextModel); @Override List<AppDescriptor> getAppsForCurrentUser(); @Override @Transactional(readOnly = true) List<Location> getLoginLocations(); @Override List<AppTemplate> getAllAppTemplates(); @Override AppTemplate getAppTemplate(String id); @Override AppDescriptor getApp(String id); @Override UserApp getUserApp(String appId); @Override List<UserApp> getUserApps(); @Override UserApp saveUserApp(UserApp userApp); @Override void purgeUserApp(UserApp userApp); }
AppFrameworkServiceImpl extends BaseOpenmrsService implements AppFrameworkService { @Override public boolean checkRequireExpression(Requireable candidate, AppContextModel contextModel) { try { String requireExpression = candidate.getRequire(); if (StringUtils.isBlank(requireExpression)) { return true; } else { Bindings bindings = new SimpleBindings(); Map<String, Object> mapProperties = new HashMap<String, Object>(); for (Map.Entry<String, Object> e : contextModel.entrySet()) { if (e.getValue() instanceof Map) { mapProperties.put(e.getKey(), e.getValue()); } else { bindings.put(e.getKey(), e.getValue()); } } javascriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); ObjectMapper jackson = new ObjectMapper(); for (Map.Entry<String, Object> e : mapProperties.entrySet()) { try { javascriptEngine.eval("var " + e.getKey() + " = " + jackson.writeValueAsString(e.getValue()) + ";"); } catch (Exception ex) { StringBuilder extraInfo = new StringBuilder(); extraInfo.append("type:").append(e.getValue().getClass().getName()); if (e.getValue() instanceof Map) { Map<?, ?> map = (Map<?, ?>) e.getValue(); extraInfo.append(" properties:"); for (Map.Entry entry : map.entrySet()) { extraInfo.append(" ").append(entry.getKey().toString()).append(":") .append(entry.getValue() == null ? "null" : entry.getValue().getClass().toString()); } } log.error("Failed to set '" + e.getKey() + "' scope variable (" + extraInfo + ") while evaluating require check for " + candidate.getClass().getSimpleName() .toLowerCase() + " " + candidate.getId(), ex); return false; } } return javascriptEngine.eval("(" + requireExpression + ") == true").equals(Boolean.TRUE); } } catch (Exception e) { log.error("Failed to evaluate 'require' check for extension " + candidate.getId(), e); return false; } } AppFrameworkServiceImpl(AllAppTemplates allAppTemplates, AllAppDescriptors allAppDescriptors,
	    AllFreeStandingExtensions allFreeStandingExtensions, AllComponentsState allComponentsState,
	    LocationService locationService, FeatureToggleProperties featureToggles, AppFrameworkConfig appFrameworkConfig,
	    AllUserApps allUserApps); @Override List<AppDescriptor> getAllApps(); @Override List<Extension> getExtensionsById(String extensionPointId, String id); @Override // TODO shouldn't this be getAllFreeStandingExtensions? List<Extension> getAllExtensions(String extensionPointId); @Override List<AppDescriptor> getAllEnabledApps(); @Override List<Extension> getAllEnabledExtensions(); @Override List<Extension> getAllEnabledExtensions(String extensionPointId); @Override void enableApp(String appId); @Override void disableApp(String appId); @Override void enableExtension(String extensionId); @Override void disableExtension(String extensionId); @Override List<Extension> getExtensionsForCurrentUser(); @Override List<Extension> getExtensionsForCurrentUser(String extensionPointId); @Override List<Extension> getExtensionsForCurrentUser(String extensionPointId, AppContextModel contextModel); @Override boolean checkRequireExpression(Requireable candidate, AppContextModel contextModel); @Override List<AppDescriptor> getAppsForCurrentUser(); @Override @Transactional(readOnly = true) List<Location> getLoginLocations(); @Override List<AppTemplate> getAllAppTemplates(); @Override AppTemplate getAppTemplate(String id); @Override AppDescriptor getApp(String id); @Override UserApp getUserApp(String appId); @Override List<UserApp> getUserApps(); @Override UserApp saveUserApp(UserApp userApp); @Override void purgeUserApp(UserApp userApp);  }