Commit d1b12530bdbd5c832e7f54715d2213aa8b2b6fda

Authored by hausdoerfer
1 parent 5d016ca2

added persistence to scheduler and corrected errors in the user

services/Common/src/main/java/de/bht/beuthbot/daos/ReminderDAO.java 0 → 100644
  1 +package de.bht.beuthbot.daos;
  2 +
  3 +import de.bht.beuthbot.model.entities.Reminder;
  4 +
  5 +import javax.ejb.Remote;
  6 +
  7 +/**
  8 + * Created by Stephan Hausdörfer on 16.01.2018.
  9 + */
  10 +@Remote
  11 +public interface ReminderDAO extends GenericDAO<Reminder, Long> {
  12 +
  13 + Reminder createReminder();
  14 +}
... ...
services/Common/src/main/java/de/bht/beuthbot/daos/ReminderDAOImpl.java 0 → 100644
  1 +package de.bht.beuthbot.daos;
  2 +
  3 +import de.bht.beuthbot.model.entities.Reminder;
  4 +import de.bht.beuthbot.model.entities.ReminderImpl;
  5 +
  6 +import javax.ejb.Stateless;
  7 +
  8 +/**
  9 + * Created by Stephan Hausdörfer on 16.01.2018.
  10 + */
  11 +@Stateless
  12 +public class ReminderDAOImpl extends GenericHibernateDAO<Reminder, ReminderImpl, Long> implements ReminderDAO {
  13 +
  14 + @Override
  15 + public Reminder createReminder() {
  16 + return new ReminderImpl();
  17 + }
  18 +}
... ...
services/Common/src/main/java/de/bht/beuthbot/model/entities/EntityBaseImpl.java
... ... @@ -38,7 +38,7 @@ public class EntityBaseImpl implements EntityBase {
38 38  
39 39 @Override
40 40 public Long getId() {
41   - return null;
  41 + return id;
42 42 }
43 43  
44 44 @Override
... ...
services/Common/src/main/java/de/bht/beuthbot/model/entities/Reminder.java 0 → 100644
  1 +package de.bht.beuthbot.model.entities;
  2 +
  3 +import de.bht.beuthbot.model.Messenger;
  4 +import de.bht.beuthbot.model.enumeration.ReminderCycle;
  5 +
  6 +import java.time.LocalDate;
  7 +import java.util.Map;
  8 +
  9 +/**
  10 + * Created by Stephan Hausdörfer on 16.01.2018.
  11 + */
  12 +public interface Reminder extends EntityBase {
  13 +
  14 + long getUserId();
  15 +
  16 + void setUserId(long userId);
  17 +
  18 + ReminderCycle getReminderCycle();
  19 +
  20 + void setReminderCycle(ReminderCycle reminderCycle);
  21 +
  22 + LocalDate getCycleTime();
  23 +
  24 + void setCycleTime(LocalDate cycleTime);
  25 +
  26 + Messenger getMessenger();
  27 +
  28 + void setMessenger(Messenger messenger);
  29 +
  30 + String getIntent();
  31 +
  32 + void setIntent(String intent);
  33 +
  34 + Map<String, String> getEntities();
  35 +
  36 + void setEntities(Map<String, String> entities);
  37 +}
... ...
services/Common/src/main/java/de/bht/beuthbot/model/entities/ReminderImpl.java 0 → 100644
  1 +package de.bht.beuthbot.model.entities;
  2 +
  3 +import de.bht.beuthbot.model.Messenger;
  4 +import de.bht.beuthbot.model.enumeration.ReminderCycle;
  5 +
  6 +import javax.persistence.ElementCollection;
  7 +import javax.persistence.Entity;
  8 +import javax.persistence.Table;
  9 +import java.time.LocalDate;
  10 +import java.util.Map;
  11 +
  12 +/**
  13 + * Created by Stephan Hausdörfer on 16.01.2018.
  14 + */
  15 +@Entity
  16 +@Table
  17 +public class ReminderImpl extends EntityBaseImpl implements Reminder {
  18 +
  19 + private long userId;
  20 +
  21 + private ReminderCycle reminderCycle;
  22 +
  23 + private LocalDate cycleTime;
  24 +
  25 + private Messenger messenger;
  26 +
  27 + private String intent;
  28 +
  29 + @ElementCollection
  30 + private Map<String, String> entities;
  31 +
  32 + @Override
  33 + public long getUserId() {
  34 + return userId;
  35 + }
  36 +
  37 + @Override
  38 + public void setUserId(long userId) {
  39 + this.userId = userId;
  40 + }
  41 +
  42 + @Override
  43 + public ReminderCycle getReminderCycle() {
  44 + return reminderCycle;
  45 + }
  46 +
  47 + @Override
  48 + public void setReminderCycle(ReminderCycle reminderCycle) {
  49 + this.reminderCycle = reminderCycle;
  50 + }
  51 +
  52 + @Override
  53 + public LocalDate getCycleTime() {
  54 + return cycleTime;
  55 + }
  56 +
  57 + @Override
  58 + public void setCycleTime(LocalDate cycleTime) {
  59 + this.cycleTime = cycleTime;
  60 + }
  61 +
  62 + @Override
  63 + public Messenger getMessenger() {
  64 + return messenger;
  65 + }
  66 +
  67 + @Override
  68 + public void setMessenger(Messenger messenger) {
  69 + this.messenger = messenger;
  70 + }
  71 +
  72 + @Override
  73 + public String getIntent() {
  74 + return intent;
  75 + }
  76 +
  77 + @Override
  78 + public void setIntent(String intent) {
  79 + this.intent = intent;
  80 + }
  81 +
  82 + @Override
  83 + public Map<String, String> getEntities() {
  84 + return entities;
  85 + }
  86 +
  87 + @Override
  88 + public void setEntities(Map<String, String> entities) {
  89 + this.entities = entities;
  90 + }
  91 +}
... ...
services/Common/src/main/java/de/bht/beuthbot/model/enumeration/ReminderCycle.java 0 → 100644
  1 +package de.bht.beuthbot.model.enumeration;
  2 +
  3 +/**
  4 + * Created by Stephan Hausdörfer on 16.01.2018.
  5 + */
  6 +public enum ReminderCycle {
  7 + ONCE,
  8 + DAILY,
  9 + WEEKLY,
  10 + MONTHLY
  11 +}
... ...
services/MainBot/src/main/java/de/bht/beuthbot/scheduler/SchedulerBean.java
1 1 package de.bht.beuthbot.scheduler;
2 2  
3 3 import de.bht.beuthbot.attachments.AttachmentStore;
  4 +import de.bht.beuthbot.daos.AppUserDAO;
  5 +import de.bht.beuthbot.daos.ReminderDAO;
4 6 import de.bht.beuthbot.jms.Target;
5   -import de.bht.beuthbot.model.Attachment;
6 7 import de.bht.beuthbot.model.EntityName;
7 8 import de.bht.beuthbot.model.Intent;
8 9 import de.bht.beuthbot.model.Messenger;
9 10 import de.bht.beuthbot.model.entities.AppUser;
10   -import de.bht.beuthbot.scheduler.model.Reminder;
11   -import de.bht.beuthbot.scheduler.model.ReminderCycle;
  11 +import de.bht.beuthbot.model.entities.Reminder;
  12 +import de.bht.beuthbot.model.enumeration.ReminderCycle;
12 13 import de.bht.beuthbot.utils.DateStringExtractor;
  14 +
13 15 import org.slf4j.Logger;
14 16 import org.slf4j.LoggerFactory;
15 17  
16 18 import de.bht.beuthbot.conf.Application;
17   -import de.bht.beuthbot.conf.Configuration;
18 19 import de.bht.beuthbot.jms.ProcessQueue;
19 20 import de.bht.beuthbot.jms.TaskMessage;
20 21  
21   -import java.text.ParseException;
22 22 import java.text.SimpleDateFormat;
23 23 import java.time.LocalDate;
24 24 import java.util.*;
25 25 import javax.annotation.PostConstruct;
26 26 import javax.annotation.Resource;
27 27 import javax.ejb.*;
28   -import javax.ejb.Timer;
29   -import javax.ws.rs.GET;
30   -import javax.ws.rs.Path;
31 28  
32 29 @Singleton
33 30 @Startup
... ... @@ -51,8 +48,13 @@ public class SchedulerBean {
51 48 @Resource(lookup = "java:global/global/ApplicationBean")
52 49 private Application application;
53 50  
54   - //list of reminders to be populated with data from the DB
55   - private static ArrayList<Reminder> _reminders = new ArrayList<>();
  51 + /** Injected AppUserDAO */
  52 + @Resource(lookup = "java:global/global/AppUserDAOImpl")
  53 + private AppUserDAO userDAO;
  54 +
  55 + /** Injected ReminderDAO */
  56 + @Resource(lookup = "java:global/global/ReminderDAOImpl")
  57 + private ReminderDAO reminderDAO;
56 58  
57 59 boolean isActive = true;
58 60  
... ... @@ -68,7 +70,6 @@ public class SchedulerBean {
68 70  
69 71 @Schedule(hour="*", minute="*")
70 72 public void fetchReminders() {
71   - //TODO: Establish DB connection and retrieve list of reminders
72 73 logger.debug("SchedulerBean fired!");
73 74  
74 75 if (!isActive) return;
... ... @@ -76,13 +77,15 @@ public class SchedulerBean {
76 77 SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy KK:mm");
77 78 String dateNow = formatter.format(new Date());
78 79  
79   - for (Reminder reminder : _reminders) {
  80 + List<Reminder> reminders = reminderDAO.findAll();
  81 +
  82 + for (Reminder reminder : reminders) {
80 83 //Check if date time now equals reminder date time
81 84 if(dateNow.equals(formatter.format(reminder.getCycleTime()))) {
82 85 Long messageID = 1L;
83   - AppUser user = reminder.getUser(); //TODO: Fill with correct value from DB
  86 + AppUser user = userDAO.findById(reminder.getUserId());
84 87 Long messengerId = Long.valueOf(user.getFacebookUserId() == null ? user.getTelegramUserId() : user.getFacebookUserId());
85   - Messenger messenger = reminder.getMessenger(); //TODO: Fill with correct value from DB
  88 + Messenger messenger = reminder.getMessenger();
86 89 String text = "";
87 90 String intent = reminder.getIntent();
88 91 Map entities = reminder.getEntities();
... ... @@ -105,7 +108,7 @@ public class SchedulerBean {
105 108 //Figure out when to remind the user
106 109  
107 110 //Set reminder cycle to one shot as a default
108   - ReminderCycle cycle = ReminderCycle.ONCE;
  111 + ReminderCycle cycle = ReminderCycle.ONCE;
109 112 //Extract the Date from the message
110 113 String messageDay = message.getEntities().getOrDefault("date", "TODAY");
111 114 //If the date retrieved from the message contains a cycling parameter (e.g. "daily"), set the reminderCycle
... ... @@ -180,12 +183,16 @@ public class SchedulerBean {
180 183 }
181 184  
182 185 logger.debug("Creating reminder with date " + cycleDate.toString() + " and time " + cycleTime.toString());
183   - Reminder reminder = new Reminder(user, cycle, cycleDate, cycleTime, messenger, intent, entities);
184   - _reminders.add(reminder);
185 186  
186   - return reminder;
  187 + Reminder reminder = reminderDAO.createReminder();
  188 + reminder.setUserId(user.getId());
  189 + reminder.setReminderCycle(cycle);
  190 + reminder.setCycleTime(cycleDate);
  191 + reminder.setMessenger(messenger);
  192 + reminder.setIntent(intent);
  193 + reminder.setEntities(entities);
187 194  
188   - //TODO: Insert the new Reminder into Database
  195 + return reminderDAO.saveOrUpdate(reminder);
189 196 }
190 197  
191 198 }
... ...
services/MainBot/src/main/java/de/bht/beuthbot/scheduler/model/Reminder.java deleted 100644 → 0
1   -package de.bht.beuthbot.scheduler.model;
2   -
3   -import de.bht.beuthbot.model.Intent;
4   -import de.bht.beuthbot.model.Messenger;
5   -import de.bht.beuthbot.model.entities.AppUser;
6   -
7   -import java.time.LocalDate;
8   -import java.util.Date;
9   -import java.util.HashMap;
10   -import java.util.Map;
11   -
12   -public class Reminder {
13   - //TODO: Replace with User Object
14   - private AppUser _user;
15   - //Reminder cycle
16   - private ReminderCycle _reminderCycle;
17   - //Date of Cycle
18   - private LocalDate _cycleDate;
19   - //Time of cycle
20   - private Date _cycleTime;
21   - //private
22   - //Used Messenger
23   - private Messenger _messenger;
24   - //Intent after Reminder is triggered
25   - private String _intent;
26   - //Entities
27   - private final Map<String, String> _entities;
28   -
29   - public Reminder(AppUser user, ReminderCycle reminderCycle, LocalDate cycleDate, Date cycleTime, Messenger messenger, String intent, Map<String, String> entities) {
30   - _user = user;
31   - _reminderCycle = reminderCycle;
32   - _cycleDate = cycleDate;
33   - _cycleTime = cycleTime;
34   - _messenger = messenger;
35   - _intent = intent;
36   - _entities = entities;
37   - }
38   -
39   - public Messenger getMessenger() {
40   - return _messenger;
41   - }
42   -
43   - public void setMessenger(Messenger messenger) {
44   - _messenger = messenger;
45   - }
46   -
47   - public String getIntent() {
48   - return _intent;
49   - }
50   -
51   - public void setIntent(String intent) {
52   - _intent = intent;
53   - }
54   -
55   - public AppUser getUser() {
56   - return _user;
57   - }
58   -
59   - public void setUserID(AppUser user) {
60   - _user = user;
61   - }
62   -
63   - public ReminderCycle getReminderCycle() {
64   - return _reminderCycle;
65   - }
66   -
67   - public void setReminderCycle(ReminderCycle reminderCycle) {
68   - _reminderCycle = reminderCycle;
69   - }
70   -
71   - public LocalDate getCycleDate() {
72   - return _cycleDate;
73   - }
74   -
75   - public void setCycleDate(LocalDate cycleDate) {
76   - _cycleDate = cycleDate;
77   - }
78   -
79   - public Date getCycleTime() { return _cycleTime; }
80   -
81   - public void setCycleTime(Date cycleTime) { _cycleTime = cycleTime; }
82   -
83   - public Map<String, String> getEntities() { return _entities; }
84   -}
services/MainBot/src/main/java/de/bht/beuthbot/scheduler/model/ReminderCycle.java deleted 100644 → 0
1   -package de.bht.beuthbot.scheduler.model;
2   -
3   -public enum ReminderCycle {
4   - ONCE, DAILY, WEEKLY, MONTHLY
5   -}