Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Unindex;

import java.util.Date;
Expand All @@ -12,17 +13,29 @@ public class EmailNotification extends BaseEntity<Long> {

@Id
private Long id;

@Unindex
private List<String> recipients;

@Unindex
private String rule;
private String subject;

@Unindex
private String body;

@Unindex
private String reason;

@Unindex
private String emailStatus;
@Unindex

@Index
private Date timestamp;

@Index
private Date timestampSend;


public Long getId() {
return id;
}
Expand All @@ -39,12 +52,20 @@ public void setRecipients(List<String> recipients) {
this.recipients = recipients;
}

public String getRule() {
return rule;
public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public String getBody() {
return body;
}

public void setRule(String rule) {
this.rule = rule;
public void setBody(String body) {
this.body = body;
}

public String getReason() {
Expand All @@ -63,6 +84,14 @@ public void setEmailStatus(String emailStatus) {
this.emailStatus = emailStatus;
}

public Date getTimestamp() {
return timestamp;
}

public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}

public Date getTimestampSend() {
return timestampSend;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public interface EmailService {

void push(EmailConfig email);

void execute(String subject, String body, String reason, String to);
void execute(Long emailNotificationId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;

import com.googlecode.objectify.Key;

import com.ciandt.techgallery.Constants;
import com.ciandt.techgallery.persistence.dao.EmailNotificationDAO;
import com.ciandt.techgallery.persistence.dao.impl.EmailNotificationDAOImpl;
Expand Down Expand Up @@ -69,52 +67,63 @@ public static EmailServiceImpl getInstance() {
* Push email to queue.
*/
public void push(EmailConfig email) {
Long emailNotificationId = addEmailNotification(email);

QueueFactory.getQueue(queueName).add(
TaskOptions.Builder.withUrl(queueUrl).param("subject", email.getSubject())
.param("body", email.getBody()).param("reason", email.getReason())
.param("to", email.getTo()[0]));
TaskOptions.Builder
.withUrl(queueUrl)
.param("emailNotificationId", emailNotificationId.toString())
);
}

public void execute(String subject, String body, String reason, String to) {
sendEmail(new EmailConfig(subject, body, reason, to));
public void execute(Long emailNotificationId) {
final EmailNotification emailNotification = emailNotificationDao.findById(emailNotificationId);
sendEmail(emailNotification);
}

private void sendEmail(EmailConfig email) {
private void sendEmail(EmailNotification emailNotification) {
try {
Message msg = prepareMessage(email);
Message msg = prepareMessage(emailNotification);
Transport.send(msg);
registerEmailNotification(email, true);
updateEmailNotification(emailNotification, true);

} catch (Throwable err) {
Long notificationId = registerEmailNotification(email, false);
log.log(Level.SEVERE, "Error when attempting to send email " + notificationId, err);
updateEmailNotification(emailNotification, false);
log.log(Level.SEVERE, "Error when attempting to send email " + emailNotification.getId(), err);
}
}

private Message prepareMessage(EmailConfig email) throws UnsupportedEncodingException,
private Message prepareMessage(EmailNotification emailNotification) throws UnsupportedEncodingException,
MessagingException {

Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(getFrom());
for (String to : email.getTo()) {
for (String to : emailNotification.getRecipients()) {
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
}
msg.setContent(email.getBody(), "text/html");
msg.setSubject(email.getSubject(), "UTF-8");
msg.setContent(emailNotification.getBody(), "text/html");
msg.setSubject(emailNotification.getSubject(), "UTF-8");
return msg;
}

private long registerEmailNotification(EmailConfig email, boolean success) {
private long addEmailNotification(EmailConfig email) {
EmailNotification emailNotification = new EmailNotification();
emailNotification.setRecipients(Arrays.asList(email.getTo()));
emailNotification.setSubject(email.getSubject());
emailNotification.setBody(email.getBody());
emailNotification.setReason(email.getReason());
emailNotification.setTimestamp(new Date());

return emailNotificationDao.add(emailNotification).getId();
}

private void updateEmailNotification(EmailNotification emailNotification, boolean success) {
emailNotification.setTimestampSend(new Date());
emailNotification.setEmailStatus(success ? "SUCCESS" : "FAILURE");

Key<EmailNotification> key = emailNotificationDao.add(emailNotification);
return key.getId();
emailNotificationDao.update(emailNotification);
}

private InternetAddress getFrom() throws UnsupportedEncodingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class EmailServlet extends HttpServlet {

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) {
emailService.execute(request.getParameter("subject"), request.getParameter("body"),
request.getParameter("reason"), request.getParameter("to"));
emailService.execute(Long.parseLong(request.getParameter("emailNotificationId")));
}
}