Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,28 @@
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.12.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<sourceDirectory>src</sourceDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
87 changes: 0 additions & 87 deletions src/main/java/VTMain.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.opensaber.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;
}

}
34 changes: 34 additions & 0 deletions src/main/java/io/opensaber/views/EvaluatorFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.opensaber.views;

public class EvaluatorFactory {

public static final String concat = "concat";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MANDATORY: Why are these hardcoded here? We are creating a library and this can't be in a Factory class for sure.

public static final String userDefinedConcat = "userDefinedConcat";
public static final String customDefinedConcat = "customDefinedConcat";
/**
* returns the instance of IEvaluator implementations (like:FunctionEvaluator, ProviderEvaluator
* @param functionName
* @param function
* @return
*/
public static IEvaluator<Object> getInstance(String functionName, FieldFunction function){
IEvaluator<Object> evaluator = null;
switch(functionName){

case concat :
evaluator = new FunctionEvaluator(function);
break;
case userDefinedConcat :
evaluator = new ProviderEvaluator<Object>(function);
break;
case customDefinedConcat :
break;
default :
evaluator = new FunctionEvaluator(function);
break;
}

return evaluator;
}

}
25 changes: 25 additions & 0 deletions src/main/java/io/opensaber/views/Field.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package io.opensaber.views;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Field {
private static Logger logger = LoggerFactory.getLogger(Field.class);

private String name;
private String title;
Expand Down Expand Up @@ -63,5 +67,26 @@ public String getTitle() {
return this.title;
}
}
/**
* parse function to get the function name
*
* @return function name(like: concat)
*/
public String getFunctioName(){
String fdName = StringUtils.substring(this.function, this.function.lastIndexOf("/")+1, this.function.indexOf("("));
if(fdName.isEmpty()){
logger.error("$function reference is not valid! for " + this.function);
throw new IllegalArgumentException("$function reference is not valid! ");
}
return fdName;
}
/**
*
* @return array of args name
*/
public String[] getArgNames(){
String argNames = this.function.substring(this.function.indexOf("(") + 1, this.function.lastIndexOf(")"));
return argNames.split(", ");
}

}
23 changes: 7 additions & 16 deletions src/main/java/io/opensaber/views/FunctionEvaluator.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package io.opensaber.views;



import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.JexlEngine;
import org.apache.commons.jexl2.MapContext;

import java.util.List;

public class FunctionEvaluator<T> {
public class FunctionEvaluator implements IEvaluator<Object>{
private static final JexlEngine jexl = new JexlEngine();
private JexlContext jexlContext = new MapContext();
private FieldFunction function;
private Expression jexlExpression;

private static final String ARG = "arg";

public FunctionEvaluator(FieldFunction function) {
this.function = function;
Expand All @@ -22,7 +20,7 @@ public FunctionEvaluator(FieldFunction function) {
public void setContextArgs() {
int itr = 1;
for(Object val : function.getArgValues()) {
String arg = "arg" + itr++;
String arg = ARG + itr++;
jexlContext.set(arg, val);
}
}
Expand All @@ -32,19 +30,12 @@ private void prepare() {
setContextArgs();
}

public T evaluate() {
@Override
public Object evaluate() {
prepare();
T result = (T) jexlExpression.evaluate(jexlContext);
Object result = jexlExpression.evaluate(jexlContext);

return result;
}

public String concat(List<String> args) {
String res = "";
for (String arg : args) {
res = res.isEmpty() ? arg : (res + ":" + arg);
}
return res;
}

}
10 changes: 10 additions & 0 deletions src/main/java/io/opensaber/views/IEvaluator.java
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();
}
47 changes: 47 additions & 0 deletions src/main/java/io/opensaber/views/ProviderEvaluator.java
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;
}

}
13 changes: 0 additions & 13 deletions src/main/java/io/opensaber/views/SampleViewFunctionProvider.java

This file was deleted.

Loading