Commit b8560595 authored by Peter Harrison's avatar Peter Harrison

Merge branch 'master' of https://github.com/cheetah100/gravity

parents 4767b104 89cfcba0
......@@ -324,6 +324,12 @@
<version>2.3.20</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
......
/**
* GRAVITY WORKFLOW AUTOMATION
* (C) Copyright 2015 Orcon Limited
*
* This file is part of Gravity Workflow Automation.
*
* Gravity Workflow Automation is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Gravity Workflow Automation is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Gravity Workflow Automation.
* If not, see <http://www.gnu.org/licenses/>.
*/
package nz.net.orcon.kanban.automation.plugin;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Locale;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import freemarker.template.Configuration;
import freemarker.template.Template;
import nz.net.orcon.kanban.controllers.ResourceController;
import nz.net.orcon.kanban.model.Action;
public class FreemarkerPlugin implements Plugin {
@Autowired
private ResourceController resourceController;
@Override
public Map<String,Object> process( Action action, Map<String,Object> context ) throws Exception{
String resource = getResourceController().getResource(action.getResource());
Configuration configuration = new Configuration();
configuration.setEncoding(Locale.ENGLISH, "UTF-8");
Template template = new Template(action.getResource(), new StringReader(resource), configuration);
Writer output = new StringWriter();
template.process(context, output);
String result = output.toString();
context.put(action.getResponse(), result);
return context;
}
public ResourceController getResourceController() {
return resourceController;
}
public void setResourceController(ResourceController resourceController) {
this.resourceController = resourceController;
}
}
......@@ -21,42 +21,54 @@
package nz.net.orcon.kanban.automation.plugin;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Locale;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import freemarker.template.Configuration;
import freemarker.template.Template;
import nz.net.orcon.kanban.controllers.ResourceController;
import nz.net.orcon.kanban.model.Action;
/**
* This plugin is a simple text replacement plugin which replaces text in a resource
* with Strings or Objects from the Context. It is similar to velocity and freemarker,
* only it actually works ;-)
*
* @author peter
*
*/
public class TemplatePlugin implements Plugin {
@Autowired
private ResourceController resourceController;
@Override
public Map<String,Object> process( Action action, Map<String,Object> context ) throws Exception{
String resource = getResourceController().getResource(action.getResource());
Configuration configuration = new Configuration();
//configuration.setDefaultEncoding("UTF-8");
configuration.setEncoding(Locale.ENGLISH, "UTF-8");
Template template = new Template(action.getResource(), new StringReader(resource), configuration);
Writer output = new StringWriter();
template.process(context, output);
String result = output.toString();
context.put(action.getResponse(), result);
String resource = getResourceController().getResource(action.getResource());
StringBuilder builder = new StringBuilder(resource);
for( String field : context.keySet()){
String value = context.get(field).toString();
replaceAll( field, value, builder);
}
context.put(action.getResponse(), builder.toString());
return context;
}
private void replaceAll( String field, String value, StringBuilder builder){
int x = 0;
String toFind = "${" + field + "}";
while(x>=0){
x = builder.indexOf(toFind);
if(x>=0){
builder.replace(x, x+toFind.length(), value);
}
}
}
public ResourceController getResourceController() {
return resourceController;
}
......
/**
* GRAVITY WORKFLOW AUTOMATION
* (C) Copyright 2015 Orcon Limited
*
* This file is part of Gravity Workflow Automation.
*
* Gravity Workflow Automation is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Gravity Workflow Automation is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Gravity Workflow Automation.
* If not, see <http://www.gnu.org/licenses/>.
*/
package nz.net.orcon.kanban.automation.plugin;
import java.io.StringWriter;
import java.util.Map;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import nz.net.orcon.kanban.controllers.ResourceController;
import nz.net.orcon.kanban.model.Action;
public class VelocityPlugin implements Plugin {
@Autowired
private ResourceController resourceController;
@Autowired
private
VelocityEngine velocityEngine;
@Override
public Map<String,Object> process( Action action, Map<String,Object> context ) throws Exception{
String resource = getResourceController().getResource(action.getResource());
VelocityContext velocityContext = new VelocityContext(context);
StringWriter resultWriter = new StringWriter();
getVelocityEngine().evaluate(velocityContext, resultWriter, action.getResource(), resource);
String result = resultWriter.toString();
context.put(action.getResponse(), result);
return context;
}
public ResourceController getResourceController() {
return resourceController;
}
public void setResourceController(ResourceController resourceController) {
this.resourceController = resourceController;
}
public void setVelocityEngine(VelocityEngine velocityEngine) {
this.velocityEngine = velocityEngine;
this.velocityEngine.init();
}
public VelocityEngine getVelocityEngine() {
return velocityEngine;
}
}
/**
* GRAVITY WORKFLOW AUTOMATION
* (C) Copyright 2015 Orcon Limited
*
* This file is part of Gravity Workflow Automation.
*
* Gravity Workflow Automation is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Gravity Workflow Automation is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Gravity Workflow Automation.
* If not, see <http://www.gnu.org/licenses/>.
*/
package nz.net.orcon.kanban.automation.plugin;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import nz.net.orcon.kanban.controllers.ResourceController;
import nz.net.orcon.kanban.model.Action;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class FreemarkerPluginTest {
@Mock
private ResourceController resourceController;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
when(resourceController.getResource("test")).thenReturn("This is a test string that inserts here ->${replaceme}<- So do it!");
}
@Test
public void TestPlugin() throws Exception {
FreemarkerPlugin plugin = new FreemarkerPlugin();
plugin.setResourceController(resourceController);
Action action = new Action();
action.setResource("test");
action.setResponse("exampleResult");
Map<String,Object> context = new HashMap<String,Object>();
context.put("replaceme", "Hello World");
plugin.process(action, context);
Object result = context.get("exampleResult");
assertNotNull(result);
assertTrue( result instanceof String);
String resultString = (String) result;
assertEquals( "This is a test string that inserts here ->Hello World<- So do it!", resultString);
}
@Test
public void TestPluginWithArrays() throws Exception {
FreemarkerPlugin plugin = new FreemarkerPlugin();
plugin.setResourceController(resourceController);
// <#assign t=[\"boo\",\"bar\",\"doo\"]>
when(resourceController.getResource("test2")).thenReturn("<#list t as i>${i}</#list>");
Action action = new Action();
action.setResource("test2");
action.setResponse("exampleResult");
Map<String,Object> context = new HashMap<String,Object>();
ArrayList<String> t = new ArrayList<String>();
t.add("boo");
t.add("bar");
t.add("doo");
context.put("t", t);
plugin.process(action, context);
Object result = context.get("exampleResult");
assertNotNull(result);
assertTrue( result instanceof String);
String resultString = (String) result;
assertEquals( "boobardoo", resultString);
}
}
......@@ -24,7 +24,6 @@ package nz.net.orcon.kanban.automation.plugin;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
......@@ -66,35 +65,4 @@ public class TemplatePluginTest {
String resultString = (String) result;
assertEquals( "This is a test string that inserts here ->Hello World<- So do it!", resultString);
}
@Test
public void TestPluginWithArrays() throws Exception {
TemplatePlugin plugin = new TemplatePlugin();
plugin.setResourceController(resourceController);
// <#assign t=[\"boo\",\"bar\",\"doo\"]>
when(resourceController.getResource("test2")).thenReturn("<#list t as i>${i}</#list>");
Action action = new Action();
action.setResource("test2");
action.setResponse("exampleResult");
Map<String,Object> context = new HashMap<String,Object>();
ArrayList<String> t = new ArrayList<String>();
t.add("boo");
t.add("bar");
t.add("doo");
context.put("t", t);
plugin.process(action, context);
Object result = context.get("exampleResult");
assertNotNull(result);
assertTrue( result instanceof String);
String resultString = (String) result;
assertEquals( "boobardoo", resultString);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment