-
Notifications
You must be signed in to change notification settings - Fork 0
Os VT 2 - Providers implemented #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
8f470ec
42b2150
8a8738b
ef78283
553d09b
89fd558
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package io.opensaber.views; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class EvaluatorFactory { | ||
|
|
||
| /** | ||
| * returns the instance of IEvaluator implementations (like:FunctionEvaluator, ProviderEvaluator | ||
| * | ||
| * @param actualValues | ||
| * @param functiondef | ||
| * @return | ||
| */ | ||
| public static IEvaluator<Object> getInstance(FunctionDefinition functiondef, List<Object> actualValues) { | ||
| IEvaluator<Object> evaluator = null; | ||
| FieldFunction function = null; | ||
|
|
||
| if (functiondef.getResult() != null) { | ||
| function = getFieldFunction(functiondef.getResult(), actualValues); | ||
| evaluator = new FunctionEvaluator(function); | ||
|
|
||
| } else if (functiondef.getProvider() != null) { | ||
| function = getFieldFunction(functiondef.getProvider(), actualValues); | ||
| evaluator = new ProviderEvaluator(function); | ||
| } | ||
|
|
||
| return evaluator; | ||
| } | ||
| /** | ||
| * Creates FieldFunction and sets argValues | ||
| * @param expression | ||
| * @param actualValues | ||
| * @return | ||
| */ | ||
| private static FieldFunction getFieldFunction(String expression, List<Object> actualValues) { | ||
| FieldFunction function = new FieldFunction(expression); | ||
| function.setArgValues(actualValues); | ||
| return function; | ||
|
|
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package io.opensaber.views; | ||
|
|
||
| public interface IEvaluator<T> { | ||
| /** | ||
| * evaluates to provide result | ||
| * From a given expression, a provider class, a reference template of a sview template | ||
| * @return | ||
| */ | ||
| public T evaluate(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package io.opensaber.views; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class ProviderEvaluator implements IEvaluator<Object> { | ||
|
|
||
| private static Logger logger = LoggerFactory.getLogger(ProviderEvaluator.class); | ||
|
|
||
| private FieldFunction function; | ||
|
|
||
| public ProviderEvaluator(FieldFunction function) { | ||
| this.function = function; | ||
| } | ||
|
|
||
| @Override | ||
| public Object evaluate() { | ||
| IViewFunctionProvider<Object> viewFuntionProvider = getInstance(function.getExpression()); | ||
| Object result = viewFuntionProvider.doAction(function.getArgValues()); | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * invokes instance for given providerName | ||
| * @param providerName full qualified name of class | ||
| * @return | ||
| */ | ||
| public IViewFunctionProvider<Object> getInstance(String providerName) { | ||
|
|
||
| IViewFunctionProvider<Object> viewFunctionProvider = null; | ||
| try { | ||
| if (providerName == null || providerName.isEmpty()) { | ||
| throw new IllegalArgumentException( | ||
| "view function provider class {} cannot be instantiate with empty value"); | ||
| } | ||
| Class<?> advisorClass = Class.forName(providerName); | ||
| viewFunctionProvider = (IViewFunctionProvider) advisorClass.newInstance(); | ||
| logger.info("Invoked view function provider class with classname: " + providerName); | ||
|
|
||
| } catch (ClassNotFoundException | SecurityException | InstantiationException | IllegalAccessException | ||
| | IllegalArgumentException e) { | ||
| logger.error("view function provider class {} cannot be instantiate with exception:", providerName, e); | ||
| } | ||
| return viewFunctionProvider; | ||
| } | ||
|
|
||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,21 +32,22 @@ public JsonNode transform(ViewTemplate viewTemplate, ObjectNode node) { | |
| if (functionStr != null) { | ||
|
|
||
| String fdName = field.getFunctioName(); | ||
| String expression = viewTemplate.getExpression(fdName); | ||
|
|
||
| FieldFunction function = new FieldFunction(expression); | ||
| FunctionDefinition funcDef = viewTemplate.getFunctionDefinition(fdName); | ||
|
|
||
| List<Object> actualValues = new ArrayList<>(); | ||
| for (String oneArg : field.getArgNames()) { | ||
| // Cut off the $ | ||
| actualValues.add(ValueType.getValue(nodeAttrs.get(oneArg.substring(1)))); | ||
| } | ||
| function.setArgValues(actualValues); | ||
|
|
||
| FunctionEvaluator evaluator = new FunctionEvaluator(function); | ||
|
|
||
|
|
||
| IEvaluator<Object> evaluator = EvaluatorFactory.getInstance(funcDef, actualValues); | ||
| if (field.getDisplay()) { | ||
| result.put(field.getTitle(), evaluator.evaluate().toString()); | ||
|
|
||
| Object evaluatedValue = evaluator.evaluate(); | ||
| if(evaluatedValue instanceof String){ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you test that
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have tested this, does not work for String. Hence I preferred a check. |
||
| result.put(field.getTitle(), evaluatedValue.toString()); | ||
| } else { | ||
| result.set(field.getTitle(), JsonNodeFactory.instance.pojoNode(evaluatedValue)); | ||
| } | ||
| } | ||
| } else if (field.getDisplay()) { | ||
| result.set(field.getTitle(), nodeAttrs.get(field.getName())); | ||
|
|
@@ -56,5 +57,6 @@ public JsonNode transform(ViewTemplate viewTemplate, ObjectNode node) { | |
| logger.debug("Node transformation result: " + result); | ||
| return JsonNodeFactory.instance.objectNode().set(subjectType, result); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package org.example.provider; | ||
|
|
||
| import io.opensaber.views.IViewFunctionProvider; | ||
| import java.util.List; | ||
| /** | ||
| * This class is a sample implementation class of IViewFunctionProvider<T> | ||
| * | ||
| */ | ||
| public class SampleViewFunctionProvider implements IViewFunctionProvider<String> { | ||
|
|
||
| @Override | ||
| public String doAction(List<Object> values) { | ||
| // doing a simple concat for the values | ||
| return concat(values); | ||
| } | ||
|
|
||
| /** | ||
| * simple concat for the values as string and comma(',') as seperator | ||
| * | ||
| * @param args | ||
| * @return | ||
| */ | ||
| public String concat(List<Object> args) { | ||
| String res = ""; | ||
| for (Object arg : args) { | ||
| res = res.toString().isEmpty() ? arg.toString() : (res + " : " + arg.toString()); | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.