Implementovaný parser na šablony e-mailů. Rozchozeny unit testy.
parent
9905cc769d
commit
a04de6d928
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package info.bukova.isspst.mail;
|
||||
|
||||
|
||||
public interface MessageBuilder {
|
||||
|
||||
MailMessage buildMessage(MailMessage template, Object data);
|
||||
|
||||
}
|
@ -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…
Reference in New Issue