Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/metamug/mason into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ancode4 committed Mar 12, 2019
2 parents 9cebe5b + b335950 commit 27f30ac
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 49 deletions.
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.3.0</version>
<version>3.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand All @@ -120,7 +120,7 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
<version>8.0.15</version>
<exclusions>
<exclusion>
<groupId>com.google.protobuf</groupId>
Expand Down Expand Up @@ -153,6 +153,12 @@
<version>0.6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
Expand All @@ -171,7 +177,6 @@
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
Expand All @@ -188,6 +193,7 @@
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/metamug/mason/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ public void init(FilterConfig config) {
if (encoding == null) {
encoding = "UTF-8";
}

if (config.getInitParameter("datasource") != null) {
config.getServletContext().setAttribute(DATA_SOURCE, config.getInitParameter("datasource"));
} else {
Expand Down
34 changes: 18 additions & 16 deletions src/main/java/com/metamug/mason/service/ConvertService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,40 @@
* @author anishhirlekar
*/
public class ConvertService {

public void convertToMap(Object res, LinkedHashMap<String,Object> map, String propName) {
if(res instanceof ResultImpl)
convertResultToMap((ResultImpl)res,map,propName);
else if(res instanceof JSONObject)
convertJsonObjectToMap((JSONObject)res,map,propName);
else if(res instanceof String)
map.put(propName,(String)res);

public void convertToMap(Object res, LinkedHashMap<String, Object> map, String propName) {
if (res instanceof ResultImpl) {
convertResultToMap((ResultImpl) res, map, propName);
} else if (res instanceof JSONObject) {
convertJsonObjectToMap((JSONObject) res, map, propName);
} else if (res instanceof String) {
map.put(propName, (String) res);
}
}
private void convertResultToMap(ResultImpl resultImpl, LinkedHashMap<String,Object> map, String propName){

private void convertResultToMap(ResultImpl resultImpl, LinkedHashMap<String, Object> map, String propName) {
SortedMap[] rows = resultImpl.getRows();
String[] columnNames = resultImpl.getColumnNames();

if (rows.length > 0) {
for (SortedMap row : rows) {
for (int i = 0; i < columnNames.length; i++) {
String columnName = columnNames[i].isEmpty() || columnNames[i].equalsIgnoreCase("null") ? "col" + i : columnNames[i];
String rowValue = String.valueOf(row.get(columnName));

if (rowValue != null && !rowValue.trim().isEmpty() && !rowValue.trim().equalsIgnoreCase("null"))
map.put(propName+"."+columnName, String.valueOf((row.get(columnName))));
if (rowValue != null && !rowValue.trim().isEmpty() && !rowValue.trim().equalsIgnoreCase("null")) {
map.put(propName + "." + columnName, String.valueOf((row.get(columnName))));
}

}
}
}
}
private void convertJsonObjectToMap(JSONObject jsonObject, LinkedHashMap<String,Object> map, String propName){

private void convertJsonObjectToMap(JSONObject jsonObject, LinkedHashMap<String, Object> map, String propName) {
Map<String, Object> flatMap = JsonFlattener.flattenAsMap(jsonObject.toString());
flatMap.entrySet().forEach(entry -> {
map.put(propName+"."+entry.getKey(), entry.getValue().toString());
map.put(propName + "." + entry.getKey(), entry.getValue().toString());
});
}
}
11 changes: 6 additions & 5 deletions src/main/java/com/metamug/mason/tag/ConvertTagHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @author anishhirlekar
*/
public class ConvertTagHandler extends BodyTagSupport implements TryCatchFinally {

private Object value;
private Object target;
private String property;
Expand All @@ -27,15 +28,15 @@ public int doEndTag() throws JspException {
ConvertService cs = new ConvertService();

cs.convertToMap(value, targetMap, property);

return EVAL_PAGE;
}
public void setProperty(String p){

public void setProperty(String p) {
property = p;
}
public void setValue(Object v){

public void setValue(Object v) {
value = v;
}

Expand Down
32 changes: 17 additions & 15 deletions src/main/java/com/metamug/mason/tag/ExecuteTagHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@
* @author Kainix
*/
public class ExecuteTagHandler extends BodyTagSupport implements TryCatchFinally {

private String className;
private String onError;
private Object param;
Expand All @@ -545,49 +546,49 @@ public class ExecuteTagHandler extends BodyTagSupport implements TryCatchFinally
@Override
public int doEndTag() throws JspException {
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();

Object result = null;
try {
Class cls = Class.forName((String) className);
Object newInstance = cls.newInstance();
ResultProcessable resProcessable;
RequestProcessable reqProcessable;

if (ResultProcessable.class.isAssignableFrom(cls)) {
resProcessable = (ResultProcessable) newInstance;
if (param instanceof ResultImpl) {
ResultImpl ri = (ResultImpl) param;

result = resProcessable.process(ri.getRows(), ri.getColumnNames(), ri.getRowCount());
result = resProcessable.process(ri.getRows(), ri.getColumnNames(), ri.getRowCount());
}
} else if (RequestProcessable.class.isAssignableFrom(cls)) {
reqProcessable = (RequestProcessable) newInstance;
if (param instanceof MasonRequest) {
MasonRequest mtg = (MasonRequest) param;

Map<String, String> requestParameters = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
mtg.getParams().entrySet().forEach(entry -> {
String key = entry.getKey();
String value = entry.getValue();
requestParameters.put(key, value);
});
if(null != persistParam){
if (null != persistParam) {
LinkedHashMap<String, Object> pMap = (LinkedHashMap<String, Object>) persistParam;
pMap.entrySet().forEach(entry -> {
requestParameters.put(entry.getKey(), entry.getValue().toString());
});
}
}

Enumeration<String> headerNames = request.getHeaderNames();
Map<String, String> requestHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
while (headerNames.hasMoreElements()) {
String header = headerNames.nextElement();
requestHeaders.put(header, request.getHeader(header));
}

ds = ConnectionProvider.getMasonDatasource();
result = reqProcessable.process(requestParameters, ds, requestHeaders);

result = reqProcessable.process(requestParameters, ds, requestHeaders);
/*
/*if (isVerbose != null && isVerbose) {
if (isCollect != null && isCollect) {
Expand All @@ -598,16 +599,16 @@ public int doEndTag() throws JspException {
}*/
}
} else {
throw new JspException("", new MetamugException(MetamugError.CLASS_NOT_IMPLEMENTED,
throw new JspException("", new MetamugException(MetamugError.CLASS_NOT_IMPLEMENTED,
"Class " + cls + " isn't processable"));
}

pageContext.setAttribute(var, result);

} catch (Exception ex) {
throw new JspException("", new MetamugException(MetamugError.CODE_ERROR, ex, onError));
}

return EVAL_PAGE;
}

Expand All @@ -623,9 +624,10 @@ public void setParam(Object param) {
this.param = param;
}

public void setPersistParam(Object persistParam){
public void setPersistParam(Object persistParam) {
this.persistParam = persistParam;
}

/**
* Just re-throws the Throwable.
*
Expand Down
19 changes: 10 additions & 9 deletions src/test/java/com/metamug/mason/service/ConvertServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,28 @@
* @author anishhirlekar
*/
public class ConvertServiceTest {

private ConvertService cs;
LinkedHashMap<String,Object> map;
LinkedHashMap<String, Object> map;

@Before
public void init(){
map = new LinkedHashMap<>();
public void init() {
map = new LinkedHashMap<>();
}

@Test
public void convertJsonToMap(){
public void convertJsonToMap() {
cs = new ConvertService();
cs.convertToMap(TestData.TEST_JSON3, map, "testJson");
System.out.println(map);
}

@Test
public void convertStringToMap(){
public void convertStringToMap() {
cs = new ConvertService();
cs.convertToMap("Hello World!", map, "testStr");
System.out.println(map);
}

//todo: test resultimpl to map using mockito
}

0 comments on commit 27f30ac

Please sign in to comment.