Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- issues #1 and #2 fixed #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
35 changes: 18 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#Gradle
.gradle/
build/
#Idea
.idea/
*.iml
*.ipr
*.iws
#Eclipse
.classpath
.project
.settings/
bin/
test-output
#Other
*.log
*~
#Gradle
.gradle/
build/
#Idea
.idea/
*.iml
*.ipr
*.iws
#Eclipse
.classpath
.project
.settings/
bin/
test-output
#Other
*.log
*~
/.nb-gradle/private/
120 changes: 61 additions & 59 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,59 +1,61 @@
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'tomcat'
apply plugin: 'pitest'

ext {
jettyVersion = '7.6.0.v20120127'
}

buildscript {
repositories {
jcenter()
}

dependencies {
classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:1.2.4'
classpath 'info.solidsoft.gradle.pitest:gradle-pitest-plugin:1.0.0'
}
}

dependencies {
repositories {
mavenCentral()
mavenLocal()
}

providedCompile 'javax.servlet:servlet-api:2.5'

compile 'com.h2database:h2:1.4.181'
compile 'javax.mail:mail:1.4'
compile 'com.icegreen:greenmail:1.3.1b'
compile 'com.google.code.gson:gson:2.2.4'
compile 'commons-codec:commons-codec:1.4'
compile 'com.google.guava:guava:18.0'

testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile 'org.assertj:assertj-core:1.6.1'
testCompile 'pl.pragmatists:JUnitParams:1.0.2'
testCompile 'com.googlecode.catch-exception:catch-exception:1.2.0'

testCompile "org.eclipse.jetty:jetty-servlet:$jettyVersion"
testCompile "org.eclipse.jetty:jetty-webapp:$jettyVersion"

def tomcatVersion = '7.0.55'
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
}
}

sourceCompatibility = 1.8

task wrapper(type: org.gradle.api.tasks.wrapper.Wrapper) {
gradleVersion = '1.12'
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'tomcat'
apply plugin: 'pitest'

ext {
jettyVersion = '7.6.0.v20120127'
}

buildscript {
repositories {
jcenter()
}

dependencies {
classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:1.2.4'
classpath 'info.solidsoft.gradle.pitest:gradle-pitest-plugin:1.0.0'
}
}

dependencies {
repositories {
mavenCentral()
mavenLocal()
}

providedCompile 'javax.servlet:servlet-api:2.5'

compile 'com.h2database:h2:1.4.181'
compile 'javax.mail:mail:1.4'
compile 'com.icegreen:greenmail:1.3.1b'
compile 'com.google.code.gson:gson:2.2.4'
compile 'commons-codec:commons-codec:1.4'
compile 'com.google.guava:guava:18.0'
compile 'org.apache.velocity:velocity:1.6.2'

testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile 'org.assertj:assertj-core:1.6.1'
testCompile 'pl.pragmatists:JUnitParams:1.0.2'
testCompile 'com.googlecode.catch-exception:catch-exception:1.2.0'
testCompile 'pl.wkr:fluent-exception-rule:1.0.1'

testCompile "org.eclipse.jetty:jetty-servlet:$jettyVersion"
testCompile "org.eclipse.jetty:jetty-webapp:$jettyVersion"

def tomcatVersion = '7.0.55'
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
}
}

sourceCompatibility = 1.8

task wrapper(type: org.gradle.api.tasks.wrapper.Wrapper) {
gradleVersion = '1.12'
}
21 changes: 0 additions & 21 deletions src/main/java/legacycode/CustomerData.java

This file was deleted.

155 changes: 155 additions & 0 deletions src/main/java/legacycode/EMailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package legacycode;

import java.io.StringWriter;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.log.NullLogChute;

import legacycode.order.Order;
import legacycode.payment.Payment;
import legacycode.transaction.Transaction;

/**
*
* @author mcendrowicz
*/
public class EMailService {

private static final VelocityEngine velocityEngine = new VelocityEngine();

static {
try {
Properties velocityProperties = new Properties();
velocityProperties.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
velocityEngine.init(velocityProperties);
} catch (Exception ex) {
throw new ExceptionInInitializerError(ex);
}
}

public void cancelledOrExpiredOrderNotification(Order order, String status) {
try {
VelocityContext context = getVelocityContext(order, 0, status);
final String subject = getOrderNotificationSubject(context, false);
final String body = getOrderNotificationBody(context, false);

sendEmail(order.getCustomerData().getEmail(), subject, body);
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void paidOrderNotification(Order order, int surplus) {
try {
VelocityContext context = getVelocityContext(order, surplus, null);
final String subject = getOrderNotificationSubject(context, true);
final String body = getOrderNotificationBody(context, true);

sendEmail(order.getCustomerData().getEmail(), subject, body);

if (surplus > 0) {
sendEmail("[email protected]", "Order #" + order.getId() + " has surplus of " + surplus, "");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

private String getOrderNotificationSubject(VelocityContext context, boolean isPaid) throws Exception {
Template subjectTemplate = null;
if (isPaid) {
subjectTemplate = velocityEngine.getTemplate("templates/order-paid-subject.vm");
} else {
subjectTemplate = velocityEngine.getTemplate("templates/order-not-paid-subject.vm");
}
StringWriter subjectWriter = new StringWriter();
subjectTemplate.merge(context, subjectWriter);

return subjectWriter.toString();
}

private String getOrderNotificationBody(VelocityContext context, boolean isPaid) throws Exception {
Template bodyTemplate = null;
if (isPaid) {
bodyTemplate = velocityEngine.getTemplate("templates/order-paid-body.vm");
} else {
bodyTemplate = velocityEngine.getTemplate("templates/order-not-paid-body.vm");
}
StringWriter bodyWriter = new StringWriter();
bodyTemplate.merge(context, bodyWriter);

return bodyWriter.toString();
}

private VelocityContext getVelocityContext(Order order, int surplus, String status) {
VelocityContext context = new VelocityContext();
context.put("orderId", order.getId());
context.put("userName", order.getCustomerData().getFullName());
context.put("surplus", surplus);
context.put("status", status);

return context;
}

public void cancelledTransactionNotification(Transaction transaction, Payment payment) {
final String paymentId = payment.getId();
final String contactEmail = transaction.getContactEmail();
final String contactPerson = transaction.getContactPerson();

StringBuilder subject = new StringBuilder();
subject.append("Payment #").append(paymentId).append(" has been cancelled!");

StringBuilder body = new StringBuilder();
body.append("Hello ").append(contactPerson).append(",\n");
body.append("your payment #").append(paymentId).append(" has been cancelled!");

sendEmail(contactEmail, subject.toString(), body.toString());
}

public void completedTransactionNotification(Transaction transaction, Payment payment) {
final String paymentId = payment.getId();
final String contactEmail = transaction.getContactEmail();
final String contactPerson = transaction.getContactPerson();

StringBuilder subject = new StringBuilder();
subject.append("Payment #").append(paymentId).append(" has been successfully processed!");

StringBuilder body = new StringBuilder();
body.append("Hello ").append(contactPerson).append(",\n");
body.append("your payment #").append(paymentId).append(" has been successfully processed!").append("\n").append("Thanks!");

sendEmail(contactEmail, subject.toString(), body.toString());
}

private void sendEmail(String address, String subject, String body) {
System.out.println("SUBJECT:");
System.out.println(subject);
System.out.println("BODY:");
System.out.println(body);
try {
Session session = Session.getInstance(new Properties());
Message msg = new MimeMessage(session);
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(address));
msg.setSubject(subject);
msg.setText(body);
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect("127.0.0.1", 9988, "", "");
transport.sendMessage(msg, msg.getAllRecipients());

} catch (Exception e) {
throw new RuntimeException(e);
}
}

}
42 changes: 0 additions & 42 deletions src/main/java/legacycode/Order.java

This file was deleted.

Loading