Implementovaný parser na šablony e-mailů. Rozchozeny unit testy.

multitenant
Josef Rokos 11 years ago
parent 9905cc769d
commit a04de6d928

@ -80,6 +80,11 @@
<artifactId>spring-security-ldap</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- testing LDAP server
<dependency>

@ -0,0 +1,59 @@
package info.bukova.isspst.mail;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EntityMessageBuilder implements MessageBuilder {
private final static Logger logger = LoggerFactory.getLogger(EntityMessageBuilder.class);
@Override
public MailMessage buildMessage(MailMessage template, Object data) {
MailMessage msg = new MailMessage(template);
msg.setSubject(replacaProperties(msg.getSubject(), data));
msg.setText(replacaProperties(msg.getText(), data));
return msg;
}
private String replacaProperties(String src, Object data) {
int current = 0;
List<String> properties = new ArrayList<String>();
boolean found = false;
String ret = src;
do {
int openIndex = src.indexOf("[", current);
int closeIndex = src.indexOf("]", current);
if (openIndex != -1) {
String property = src.substring(openIndex + 1, closeIndex);
if (!properties.contains(property)) {
properties.add(property);
}
found = true;
} else {
found = false;
}
current = closeIndex + 1;
} while (found);
for (String p : properties) {
try {
ret = ret.replaceAll("\\[" + p + "\\]", BeanUtils.getProperty(data, p));
} catch (IllegalAccessException e) {
logger.error(e.getMessage());
} catch (InvocationTargetException e) {
logger.error(e.getMessage());
} catch (NoSuchMethodException e) {
logger.error(e.getMessage());
}
}
return ret;
}
}

@ -14,6 +14,18 @@ public class MailMessage extends SimpleMailMessage {
private byte[] attachementData;
private String contentType;
public MailMessage() {
super();
}
public MailMessage(MailMessage message) {
super(message);
this.html = message.isHtml();
this.attachementName = message.getAttachementName();
this.attachementData = message.getAttachementData();
this.contentType = message.getContentType();
}
public boolean isHtml() {
return html;
}

@ -0,0 +1,8 @@
package info.bukova.isspst.mail;
public interface MessageBuilder {
MailMessage buildMessage(MailMessage template, Object data);
}

@ -54,7 +54,7 @@ public class GlobalSettingsVM {
@Command
@NotifyChange("settings")
public void insertField(@BindingParam("field") String field, @BindingParam("message") MailMessage message) {
message.setText(message.getText() + ":" + field + ":");
message.setText(message.getText() + "[" + field + "]");
}
}

@ -27,6 +27,8 @@
<property name="from" value="${mail.from}"/>
</bean>
<bean id="messageBuilder" class="info.bukova.isspst.mail.EntityMessageBuilder"/>
<!-- <bean id="mailer" class="info.bukova.rsfaktura.services.mail.ThreadMailer"> -->
<!-- <constructor-arg ref="attachementMailer"/> -->
<!-- </bean> -->

@ -0,0 +1,5 @@
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseurl=jdbc:mysql://127.0.0.1:3306/isspst?characterEncoding=latin2&autoReconnect=true
jdbc.username=isspst
jdbc.password=xsacfgd

@ -0,0 +1,2 @@
ldap.server=ldap://localhost:3089
ldap.userDNPattern=uid=\{0\},OU=people,DC=bukova,DC=info

@ -0,0 +1,7 @@
mail.from=josef.rokos@gmail.com
mail.host=smtp.gmail.com
mail.port=587
mail.useauth=true
mail.usessl=true
mail.username=josef.rokos@gmail.com
mail.password=XXXXX

@ -0,0 +1,35 @@
package info.bukova.isspst.mail;
import info.bukova.isspst.data.Material;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/test-context.xml"})
public class MessageBuilderTest {
@Autowired
private MessageBuilder messageBuilder;
@Test
public void buildTest() {
MailMessage template = new MailMessage();
Material data = new Material();
data.setCode("pokus");
data.setDescription("popisek");
template.setSubject("predmet");
template.setText("pokusnej text [code] s popiskem [description]");
MailMessage replaced = messageBuilder.buildMessage(template, data);
assertEquals("pokusnej text pokus s popiskem popisek", replaced.getText());
}
}

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.mock.web.MockServletContext">
</bean>
<import resource="file:src/main/webapp/WEB-INF/spring/root-context.xml"/>
</beans>
Loading…
Cancel
Save