Skip to content

Commit

Permalink
#288 implemented email delayed notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Titogelo committed Mar 24, 2017
1 parent a94ea39 commit b7750f9
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 45 deletions.
30 changes: 21 additions & 9 deletions src/main/java/org/celstec/arlearn2/delegators/MailDelegator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
******************************************************************************/
package org.celstec.arlearn2.delegators;

import org.celstec.arlearn2.beans.account.Account;
import org.celstec.arlearn2.beans.run.Run;

import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
Expand All @@ -37,7 +40,8 @@
public class MailDelegator extends GoogleDelegator {

private static final Logger logger = Logger.getLogger(ActionDelegator.class.getName());
private static final long MILLIS_PER_DAY = 1 * 5 * 60 * 1000L; // 5 minutes
private static final long MINUTES = 1; // 1 minute
private static final long MILLIS_PER_DAY = 1 * MINUTES * 60 * 1000L;

public MailDelegator(String authtoken) {
super(authtoken);
Expand Down Expand Up @@ -121,12 +125,20 @@ public void sendReminders(org.celstec.arlearn2.beans.run.Message message, String
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);

String content = "You have missed <strong>a message</strong> from "+message.getSenderId()+" in the last week.";
String message_missed = message.getBody();
AccountDelegator ad = new AccountDelegator(this);
RunDelegator rd = new RunDelegator(this);

Account account = ad.getContactDetails(message.getSenderProviderId() + ":" + message.getSenderId());
Run run = rd.getRun(message.getRunId());

String content = "You have missed <strong>a message</strong> in "+run.getTitle()+" in the last "+MINUTES+" minutes.";
String message_missed = "<strong>"+account.getName()+"</strong>: "+message.getBody();
String button = "<a href=\"http://dojo-ibl.appspot.com/main.html#/inquiry/"+run.getRunId()+"\" class=\"btn-primary\">Catch up now!</a>";
String email_header = "Message missed - DojoIBL";
String subject = "("+run.getTitle()+") Someone sent a message in DojoIBL";

String from = "[email protected]";
String fromName = "DojoIBL board";
// String toMail = account.getEmail();
String fromName = account.getGivenName()+" (DojoIBL)";

String msgBody = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
msgBody += "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
Expand Down Expand Up @@ -413,7 +425,7 @@ public void sendReminders(org.celstec.arlearn2.beans.run.Message message, String
msgBody += "<table class=\"main\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">";
msgBody += "<tr>";
msgBody += "<td class=\"alert alert-good\">";
msgBody += "Weekly digest - DojoIBL";
msgBody += email_header;
msgBody += "</td>";
msgBody += "</tr>";
msgBody += "<tr>";
Expand All @@ -439,7 +451,7 @@ public void sendReminders(org.celstec.arlearn2.beans.run.Message message, String
msgBody += "</tr>";
msgBody += "<tr>";
msgBody += "<td class=\"content-block\">";
msgBody += "<a href=\"http://dojo-ibl.appspot.com/main.html#/home\" class=\"btn-primary\">Catch up now!</a>";
msgBody += button;
msgBody += "</td>";
msgBody += "</tr>";
msgBody += "<tr>";
Expand All @@ -454,7 +466,7 @@ public void sendReminders(org.celstec.arlearn2.beans.run.Message message, String
msgBody += "<div class=\"footer\">";
msgBody += "<table width=\"100%\">";
msgBody += "<tr>";
msgBody += "<td class=\"aligncenter content-block\"><a href=\"http://dojo-ibl.appspot.com/main.html#/home\">http://dojo-ibl.appspot.com/main.html#/home</a> by the Open Universiteit</td>";
msgBody += "<td class=\"aligncenter content-block\"><a href=\"http://dojo-ibl.appspot.com/main.html#/home\">http://dojo-ibl.appspot.com</a> - Welten Institute</td>";
msgBody += "</tr>";
msgBody += "</table>";
msgBody += "</div></div>";
Expand Down Expand Up @@ -496,7 +508,7 @@ public void sendReminders(org.celstec.arlearn2.beans.run.Message message, String

msg.addRecipient(Message.RecipientType.TO, new InternetAddress(from));
msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(from));
msg.setSubject("Someone sent a message in DojoIBL");
msg.setSubject(subject);

final MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(msgBody, "text/html");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.celstec.arlearn2.delegators;

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import org.celstec.arlearn2.api.Service;
import org.celstec.arlearn2.beans.account.Account;
import org.celstec.arlearn2.beans.run.Message;
import org.celstec.arlearn2.beans.run.MessageList;
import org.celstec.arlearn2.beans.run.Run;
import org.celstec.arlearn2.beans.run.RunAccess;
import org.celstec.arlearn2.jdo.manager.MessageManager;
import org.celstec.arlearn2.jdo.manager.ThreadManager;
Expand All @@ -13,7 +17,9 @@

public class MessageDelegator extends GoogleDelegator {
private static final Logger logger = Logger.getLogger(MessageDelegator.class.getName());
private static final long MILLIS_PER_DAY = 1 * 5 * 60 * 1000L; // 1 minute
private static final long HOURS = 1; // hours
private static final long MINUTES = 60; // minutes
private static final long MILLIS_PER_DAY = HOURS * MINUTES * 60 * 1000L;

public MessageDelegator(Service service) {
super(service);
Expand Down Expand Up @@ -41,19 +47,14 @@ public Message createMessage(Message message) {
* last 1 minutes.
* */

// Queue q = QueueFactory.getDefaultQueue();
Queue q = QueueFactory.getDefaultQueue();
//// Queue q = QueueFactory.getQueue(String.valueOf(message.getRunId()));
// q.purge();
//
//// q.deleteTask(String.valueOf(message.getRunId()));
//
//
//
// q.add(TaskOptions.Builder.withUrl("/setTimerEmailNotification").countdownMillis(MILLIS_PER_DAY)
// .param("token", this.getAuthToken())
// .param("name", String.valueOf(message.getRunId())));

RunAccessDelegator rad = new RunAccessDelegator(this);
RunDelegator rd = new RunDelegator(this);
NotificationDelegator nd = new NotificationDelegator(this);
MailDelegator md = new MailDelegator(this);
AccountDelegator ad = new AccountDelegator(this);
Expand All @@ -77,7 +78,27 @@ public Message createMessage(Message message) {
nd.broadcast(returnMessage, ra.getAccount());
}

md.sendReminders(returnMessage, email_list);
Run run = rd.getRun(message.getRunId());


if(run.getAvoidNotification() == null){
run.setAvoidNotification(0l);
rd.updateRun(run, run.getRunId());
}

// 0l = false
// 1l = true

if (run.getAvoidNotification() != 1l){
md.sendReminders(returnMessage, email_list);

q.add(TaskOptions.Builder.withUrl("/setTimerEmailNotification").countdownMillis(MILLIS_PER_DAY)
.param("token", this.getAuthToken())
.param("name", String.valueOf(message.getRunId())));
run.setAvoidNotification(1l);
rd.updateRun(run, run.getRunId());
}


return returnMessage;
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/celstec/arlearn2/jdo/classes/RunJDO.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public class RunJDO extends RunClass{
@Persistent
private String code;

@Persistent
private Long avoidNotification;

public Long getRunId() {
return id.getId();
}
Expand Down Expand Up @@ -140,6 +143,11 @@ public void setCode(String code) {
this.code = code;
}

public Long getAvoidNotification() {
return avoidNotification;
}


public void setAvoidNotification(Long avoidNotification) {
this.avoidNotification = avoidNotification;
}
}
11 changes: 11 additions & 0 deletions src/main/java/org/celstec/arlearn2/jdo/manager/RunManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public static Long addRun(String title, String owner, Long gameId, Long runId, L
runJdo.setServerCreationTime(serverCreationTime);
runJdo.setLastModificationDate(serverCreationTime);
runJdo.setPayload(new Text(run.toString()));

runJdo.setCode(run.getCode());
runJdo.setAvoidNotification(run.getAvoidNotification());

if (run.getRunConfig() != null) {
runJdo.setTagId(run.getRunConfig().getNfcTag());
}
Expand All @@ -70,6 +74,8 @@ public static Long addRun(Run run) {
runJdo.setLastModificationDate(run.getServerCreationTime());
runJdo.setPayload(new Text(run.toString()));
runJdo.setCode(run.getCode());
runJdo.setAvoidNotification(run.getAvoidNotification());

if (run.getRunConfig() != null) {
runJdo.setTagId(run.getRunConfig().getNfcTag());
}
Expand Down Expand Up @@ -166,6 +172,10 @@ public static void updateRun(long runId, Run run) {
jdo.setServerCreationTime(run.getServerCreationTime());
jdo.setDeleted(false);
jdo.setCode(run.getCode());

jdo.setAvoidNotification(run.getAvoidNotification());


if (run.getRunConfig() != null) {
jdo.setTagId(run.getRunConfig().getNfcTag());
}
Expand Down Expand Up @@ -198,6 +208,7 @@ private static Run toBean(RunJDO jdo) {
run.setServerCreationTime(jdo.getServerCreationTime());
run.setLastModificationDate(jdo.getLastModificationDate());
run.setCode(jdo.getCode());
run.setAvoidNotification(jdo.getAvoidNotification());
return run;
}

Expand Down
54 changes: 29 additions & 25 deletions src/main/java/org/celstec/arlearn2/tasks/TimerSendEmail.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@
******************************************************************************/
package org.celstec.arlearn2.tasks;

import org.celstec.arlearn2.beans.account.Account;
import org.celstec.arlearn2.beans.run.RunAccess;
import org.celstec.arlearn2.delegators.*;
import org.celstec.arlearn2.beans.run.Run;
import org.celstec.arlearn2.delegators.RunDelegator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -48,27 +44,35 @@ protected void doGet(HttpServletRequest request, HttpServletResponse resp) throw
token = request.getParameter("token");
runId = Long.parseLong(request.getParameter("name"));

RunAccessDelegator rad = new RunAccessDelegator(token);
AccountDelegator ad = new AccountDelegator(token);
NotificationDelegator nd = new NotificationDelegator(token);
MailDelegator md = new MailDelegator(token);

for(RunAccess ra: rad.getRunAccess(runId).getRunAccess()) {

Account account1 = ad.getContactDetails(ra.getAccount());

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); //2016/11/16 12:08:43

// md.sendReminders("HOLA", ra.getAccount());

// if(Math.abs(account1.getLastModificationDate() - date.getTime()) < MILLIS_PER_DAY){
// System.out.print("["+account1.getEmail()+"] "+account1.getName()+" login less than 1 minutes ago");
// }else{
// System.out.print("["+account1.getEmail()+"] "+account1.getName()+" login more than 1 minutes ago");
// }
}
RunDelegator rd = new RunDelegator(token);

Run run = rd.getRun(runId);
run.setAvoidNotification(0l);
rd.updateRun(run, run.getRunId());


// AccountDelegator ad = new AccountDelegator(token);
// NotificationDelegator nd = new NotificationDelegator(token);
// MailDelegator md = new MailDelegator(token);
//
// for(RunAccess ra: rad.getRunAccess(runId).getRunAccess()) {
//
// Account account1 = ad.getContactDetails(ra.getAccount());
//
// DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
// Date date = new Date();
// System.out.println(dateFormat.format(date)); //2016/11/16 12:08:43
//
//// md.sendReminders("HOLA", ra.getAccount());
//
// // if(Math.abs(account1.getLastModificationDate() - date.getTime()) < MILLIS_PER_DAY){
// // System.out.print("["+account1.getEmail()+"] "+account1.getName()+" login less than 1 minutes ago");
// // }else{
// // System.out.print("["+account1.getEmail()+"] "+account1.getName()+" login more than 1 minutes ago");
// // }
// }


} catch (Exception e) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/src/components/home/home.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<td class="project-actions footable-visible footable-last-column">
<!--<a class="btn btn-danger btn-sm" ng-click="deleteInquiry(game.gameId)" ng-show="game.access[myAccount.accountType+':'+myAccount.localId].accessRights == 1"><i class="fa fa-trash"></i> Delete </a>-->
<a class="btn btn-success btn-sm" ng-click="cloneInquiry(game.gameId)" ng-show="game.access[myAccount.accountType+':'+myAccount.localId].accessRights == 1"><i class="fa fa-copy"></i> Clone </a>
<a class="btn btn-warning btn-sm" href="#inquiry/{{ game.gameId }}/edit" ng-show="game.access[myAccount.accountType+':'+myAccount.localId].accessRights == 1"><i class="fa fa-pencil-square"></i> Teacher view</a>
<a class="btn btn-warning btn-sm" href="#inquiry/{{ game.gameId }}/edit" ng-click="ga('send', { hitType: 'event', eventCategory: 'Inquiry', eventAction: 'access', eventLabel: '{{ game.title }}' });" ng-show="game.access[myAccount.accountType+':'+myAccount.localId].accessRights == 1"><i class="fa fa-pencil-square"></i> Teacher view</a>
</td>
</tr>
<tr ng-if="game.gameId == run.gameId" ng-repeat="run in runs[gameSelected]">
Expand Down
3 changes: 3 additions & 0 deletions src/main/webapp/src/components/run/run.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ angular.module('DojoIBL')
////////////////////////////////////////
if(runAsJson.runId)
dataCache.put(runAsJson.runId, runAsJson);

runAsJson.avoidNotification = 0;

return newrun.$save();
},
updateRun: function(runAsJson){
Expand Down

0 comments on commit b7750f9

Please sign in to comment.