Commit 0f38e630aa386b70a2425014fc7e8bcf48a1d8ad

Authored by mfcb
1 parent 7d394baa

Added informalDate-functionality. Added basic DeleteReminder-functionality

services/Common/src/main/java/de/bht/beuthbot/model/Intent.java
... ... @@ -7,7 +7,7 @@ public enum Intent {
7 7  
8 8 SHOW_FOOD("showFood"), HELLO("Hello"), GREET("greet"), BYE("Bye"),
9 9 GOODBYE("goodbye"), RESTAURANT_SEARCH("restaurant_search"), START("Start"),
10   - SHOW_PRICE("showPrice"), REMIND("remind"), CREATE_REMINDER("createReminder"), FALLBACK("Fallback");
  10 + SHOW_PRICE("showPrice"), REMIND("remind"), CREATE_REMINDER("createReminder"), DELETE_REMINDER("deleteReminder"), FALLBACK("Fallback");
11 11  
12 12 private String text;
13 13  
... ...
services/Common/src/main/java/de/bht/beuthbot/utils/DateStringExtractor.java
... ... @@ -3,6 +3,7 @@ package de.bht.beuthbot.utils;
3 3 import javax.ejb.Local;
4 4 import java.time.DayOfWeek;
5 5 import java.time.LocalDate;
  6 +import java.time.LocalDateTime;
6 7 import java.time.format.TextStyle;
7 8 import java.util.Locale;
8 9  
... ...
services/MainBot/src/main/java/de/bht/beuthbot/canteen/model/CanteenData.java
... ... @@ -5,6 +5,7 @@ import org.slf4j.Logger;
5 5 import org.slf4j.LoggerFactory;
6 6  
7 7 import java.time.LocalDate;
  8 +import java.time.LocalDateTime;
8 9 import java.util.List;
9 10 import java.util.stream.Collectors;
10 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;
4 5 import de.bht.beuthbot.jms.Target;
5 6 import de.bht.beuthbot.model.Attachment;
6 7 import de.bht.beuthbot.model.EntityName;
... ... @@ -21,6 +22,10 @@ import de.bht.beuthbot.jms.TaskMessage;
21 22 import java.text.ParseException;
22 23 import java.text.SimpleDateFormat;
23 24 import java.time.LocalDate;
  25 +import java.time.LocalDateTime;
  26 +import java.time.LocalTime;
  27 +import java.time.format.DateTimeFormatter;
  28 +import java.time.temporal.TemporalAccessor;
24 29 import java.util.*;
25 30 import javax.annotation.PostConstruct;
26 31 import javax.annotation.Resource;
... ... @@ -51,8 +56,14 @@ public class SchedulerBean {
51 56 @Resource(lookup = "java:global/global/ApplicationBean")
52 57 private Application application;
53 58  
54   - //list of reminders to be populated with data from the DB
55   - private static ArrayList<Reminder> _reminders = new ArrayList<>();
  59 + /** Injected AppUserDAO */
  60 + @Resource(lookup = "java:global/global/AppUserDAOImpl")
  61 + private AppUserDAO userDAO;
  62 +
  63 + /** Injected ReminderDAO */
  64 + @Resource(lookup = "java:global/global/ReminderDAOImpl")
  65 + private ReminderDAO reminderDAO;
  66 +
56 67  
57 68 boolean isActive = true;
58 69  
... ... @@ -73,16 +84,19 @@ public class SchedulerBean {
73 84  
74 85 if (!isActive) return;
75 86  
76   - SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy KK:mm");
77   - String dateNow = formatter.format(new Date());
  87 + List<Reminder> reminders = reminderDAO.findAll();
78 88  
79   - for (Reminder reminder : _reminders) {
  89 + LocalDateTime dateNow = LocalDateTime.now();
  90 + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd HH:mm");
  91 + for (Reminder reminder : reminders) {
  92 + LocalDateTime reminderDate = reminder.getCycleDate();
  93 + logger.debug("Date now: " + dateNow.format(formatter) + " reminder date: " + reminder.getCycleDate().format(formatter));
80 94 //Check if date time now equals reminder date time
81   - if(dateNow.equals(formatter.format(reminder.getCycleTime()))) {
  95 + if(dateNow.format(formatter).equals(reminderDate.format(formatter))) {
82 96 Long messageID = 1L;
83   - AppUser user = reminder.getUser(); //TODO: Fill with correct value from DB
  97 + AppUser user = userDAO.findById(reminder.getUserId());
84 98 Long messengerId = Long.valueOf(user.getFacebookUserId() == null ? user.getTelegramUserId() : user.getFacebookUserId());
85   - Messenger messenger = reminder.getMessenger(); //TODO: Fill with correct value from DB
  99 + Messenger messenger = reminder.getMessenger();
86 100 String text = "";
87 101 String intent = reminder.getIntent();
88 102 Map entities = reminder.getEntities();
... ... @@ -96,6 +110,14 @@ public class SchedulerBean {
96 110 }
97 111 }
98 112  
  113 + //---------------------
  114 + //CreateReminder function
  115 + //The way this function works is by extracting the date and time from the entities of a TaskMessage.
  116 + //First a LocalDate variable is instantiated, storing the desired reminder date.
  117 + //If the message contains a cycle entity, the cycle is set accordingly, otherwise it defaults to ONCE.
  118 + //Then a LocalTime variable is created in order to extract the time at which the user desires to be reminded.
  119 + //Should there be no time-entity present, a sensible value is chosen instead.
  120 + //---------------------
99 121 public Reminder createReminder(TaskMessage message, AppUser user) {
100 122 logger.debug("Creating reminder");
101 123  
... ... @@ -121,53 +143,77 @@ public class SchedulerBean {
121 143 break;
122 144 }
123 145  
124   - LocalDate cycleDate = DateStringExtractor.getLocalDateFromString(messageDay);
125   -
126   - Date cycleTime;
127   - try {
128   - //Read out time for Reminder
129   -
130   - String messageTime = message.getEntities().getOrDefault("time","");
131   - //time should not be empty
132   - if(!messageTime.equals("")) {
133   - //get Date new for day, month, year
134   - String dateNow = new SimpleDateFormat("dd.MM.yyyy").format(new Date());
135   - //remove all chars from time String (e.g. "10 Uhr")
136   - String reminderTime = messageTime.replaceAll("[^0-9:]", "");
137   - //trim spaces
138   - reminderTime = reminderTime.trim();
139   - //check if given time is e.g. "11:15"
140   - if(reminderTime.contains(":")) {
141   - //split for hours and minutes
142   - String[] splittedTime = reminderTime.split(":");
143   - //add hours and minutes to date
144   - if(splittedTime.length > 1)
145   - dateNow = dateNow + " " + splittedTime[0] + ":" + splittedTime[1];
146   - } else {
147   - //add hours from time and set to "00"
148   - dateNow = dateNow + " " + reminderTime + ":" + "00";
149   - }
  146 + LocalDate cycleDay = DateStringExtractor.getLocalDateFromString(messageDay);
  147 +
  148 + LocalTime cycleTime = LocalTime.now();
150 149  
151   - SimpleDateFormat f = new SimpleDateFormat("dd.MM.yyyy hh:mm");
152   - Date d = f.parse(dateNow);
153   - long milliseconds = d.getTime();
154   - cycleTime = new Date(milliseconds);
  150 + String messageTime = message.getEntities().getOrDefault("time","");
155 151  
156   - } else
157   - cycleTime = new Date();
  152 + if(!messageTime.equals("")) {
  153 + String dateNow = new SimpleDateFormat("dd.MM.yyyy").format(new Date());
  154 + logger.debug("Date now: " + dateNow);
  155 +
  156 + String reminderTime = messageTime.replaceAll("[^0-9:]", "");
  157 + //trim spaces
  158 + reminderTime = reminderTime.trim();
  159 + //check if given time is e.g. "11:15"
  160 + if (reminderTime.contains(":")) {
  161 + //split for hours and minutes
  162 + String[] splittedTime = reminderTime.split(":");
  163 + //add hours and minutes to date
  164 + if (splittedTime.length > 1)
  165 + dateNow = splittedTime[0] + ":" + splittedTime[1];
  166 + } else {
  167 + //add hours from time and set to "00"
  168 + dateNow = reminderTime + ":" + "00";
  169 + }
  170 + logger.debug("DateNow:" + dateNow);
  171 + cycleTime = LocalTime.parse(dateNow);
  172 + } else if((messageTime = message.getEntities().getOrDefault("informalTime", "")) != "") {
  173 + //the user might have given an informal time, such as "morning" or "evening"
  174 + switch(messageTime) {
  175 + case "MORNING":
  176 + cycleTime = LocalTime.of(9,0);
  177 + break;
  178 + case "NOON":
  179 + cycleTime = LocalTime.of(12,0);
  180 + break;
  181 + case "EVENING":
  182 + cycleTime = LocalTime.of(20,0);
  183 + break;
  184 + default:
  185 + cycleTime = LocalTime.of(9,0);
  186 + break;
  187 + }
  188 + } else {
  189 + //if no time entity is given, we will decide ourselves when it makes sense
  190 + if(cycleDay.isEqual(LocalDate.now())) {
  191 + //if the desired day is today, we'll remind the user in a few hours
  192 + //...unless that would make it tomorrow
  193 + if(cycleTime.plusHours(4).getHour() < cycleTime.getHour()) {
  194 + cycleTime = LocalTime.of(23,59);
  195 + } else {
  196 + cycleTime = LocalTime.of(cycleTime.plusHours(4).getHour(),0);
  197 + }
158 198  
159   - } catch (Exception e) {
160   - return null;
  199 + } else {
  200 + //if the desired day is sometime in the future, remind the user at 9 in the morning
  201 + cycleTime = LocalTime.of(9,0);
  202 + }
161 203 }
  204 + logger.debug("Resulting cycleTime: " + cycleTime.toString());
  205 + LocalDateTime cycleDate = LocalDateTime.of(cycleDay, cycleTime);
  206 +
162 207 //Figure out what we're reminding the user of
163 208 String intent = message.getEntities().getOrDefault("event","FALLBACK");
164 209  
  210 +
165 211 Map<String, String> entities = new HashMap<>();
166 212 switch(intent) {
167 213 case "showFood":
168 214 intent = Intent.SHOW_FOOD.getText();
169 215 logger.debug("Intent is show food");
170   - entities.put(EntityName.DATE.getText(), cycleDate.toString());
  216 + entities.put(EntityName.DATE.getText(), cycleDate.toLocalDate().toString());
171 217 entities.put(EntityName.TIME.getText(), cycleTime.toString());
172 218 logger.debug("Entities added: " + entities);
173 219 break;
... ... @@ -179,13 +225,39 @@ public class SchedulerBean {
179 225 break;
180 226 }
181 227  
182   - 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);
  228 + logger.debug("Creating reminder with date " + cycleDate.toString() + " and time " + cycleTime.minusHours(1).toString()); //For some reason the server time is off by an hour, so we need to subtract it from the cycleTime
185 229  
186   - return reminder;
  230 + Reminder reminder = reminderDAO.createReminder();
  231 + reminder.setUserId(user.getId());
  232 + reminder.setReminderCycle(cycle);
  233 + reminder.setCycleTime(cycleDate);
  234 + reminder.setMessenger(messenger);
  235 + reminder.setIntent(intent);
  236 + reminder.setEntities(entities);
  237 +
  238 + return reminderDAO.saveOrUpdate(reminder);
187 239  
188 240 //TODO: Insert the new Reminder into Database
189 241 }
190 242  
  243 + public boolean deleteReminder(TaskMessage message, AppUser user) {
  244 + int deletedReminders = 0;
  245 + logger.debug("Delete reminders for user:" + user.getTelegramUserId());
  246 + List<Reminder> reminders = reminderDAO.findAll();
  247 + logger.debug("Number of reminders: " + reminders.size());
  248 + for(Reminder reminder : reminders) {
  249 + logger.debug("Found reminder for this user:" + reminder.getUser().getTelegramUserId());
  250 + if(reminder.getUser().getTelegramUserId().equals(user.getTelegramUserId()) || reminder.getUser().getFacebookUserId().equals(user.getFacebookUserId())) {
  251 + logger.debug("Found reminder of this user");
  252 + reminders.remove(reminder);
  253 + deletedReminders++;
  254 + }
  255 + }
  256 + if(deletedReminders > 0) {
  257 + return true;
  258 + } else {
  259 + return false;
  260 + }
  261 + }
  262 +
191 263 }
... ...
services/MainBot/src/main/java/de/bht/beuthbot/scheduler/model/Reminder.java
... ... @@ -5,6 +5,7 @@ import de.bht.beuthbot.model.Messenger;
5 5 import de.bht.beuthbot.model.entities.AppUser;
6 6  
7 7 import java.time.LocalDate;
  8 +import java.time.LocalDateTime;
8 9 import java.util.Date;
9 10 import java.util.HashMap;
10 11 import java.util.Map;
... ... @@ -15,9 +16,7 @@ public class Reminder {
15 16 //Reminder cycle
16 17 private ReminderCycle _reminderCycle;
17 18 //Date of Cycle
18   - private LocalDate _cycleDate;
19   - //Time of cycle
20   - private Date _cycleTime;
  19 + private LocalDateTime _cycleDate;
21 20 //private
22 21 //Used Messenger
23 22 private Messenger _messenger;
... ... @@ -26,11 +25,10 @@ public class Reminder {
26 25 //Entities
27 26 private final Map<String, String> _entities;
28 27  
29   - public Reminder(AppUser user, ReminderCycle reminderCycle, LocalDate cycleDate, Date cycleTime, Messenger messenger, String intent, Map<String, String> entities) {
  28 + public Reminder(AppUser user, ReminderCycle reminderCycle, LocalDateTime cycleDate, Messenger messenger, String intent, Map<String, String> entities) {
30 29 _user = user;
31 30 _reminderCycle = reminderCycle;
32 31 _cycleDate = cycleDate;
33   - _cycleTime = cycleTime;
34 32 _messenger = messenger;
35 33 _intent = intent;
36 34 _entities = entities;
... ... @@ -68,17 +66,13 @@ public class Reminder {
68 66 _reminderCycle = reminderCycle;
69 67 }
70 68  
71   - public LocalDate getCycleDate() {
  69 + public LocalDateTime getCycleDate() {
72 70 return _cycleDate;
73 71 }
74 72  
75   - public void setCycleDate(LocalDate cycleDate) {
  73 + public void setCycleDate(LocalDateTime cycleDate) {
76 74 _cycleDate = cycleDate;
77 75 }
78 76  
79   - public Date getCycleTime() { return _cycleTime; }
80   -
81   - public void setCycleTime(Date cycleTime) { _cycleTime = cycleTime; }
82   -
83 77 public Map<String, String> getEntities() { return _entities; }
84 78 }
... ...
services/MainBot/src/main/resources/de/bht/beuthbot/drools/Canteen.drl
... ... @@ -16,32 +16,33 @@
16 16  
17 17 package de.bht.beuthbot.drools
18 18  
19   -import java.util.List
  19 +import java.util.List;
20 20 import java.util.ArrayList;
21 21 import java.util.Arrays;
22 22 import java.util.HashMap;
23 23  
24 24 import java.time.LocalDate;
25 25  
26   -import de.bht.beuthbot.canteen.model.Dish
  26 +import de.bht.beuthbot.canteen.model.Dish;
27 27 import de.bht.beuthbot.canteen.model.DishType;
28 28 import de.bht.beuthbot.canteen.model.CanteenData;
29 29 import de.bht.beuthbot.model.Intent;
30 30 import de.bht.beuthbot.model.EntityName;
31 31 import de.bht.beuthbot.drools.model.DroolsMessage;
32 32  
33   -import de.bht.beuthbot.scheduler.SchedulerBean
34   -import de.bht.beuthbot.jms.TaskMessage
35   -import de.bht.beuthbot.scheduler.model.ReminderCycle
36   -import java.util.Date
37   -import java.text.SimpleDateFormat
38   -import java.text.ParseException
39   -import de.bht.beuthbot.model.Messenger
  33 +import de.bht.beuthbot.scheduler.SchedulerBean;
  34 +import de.bht.beuthbot.jms.TaskMessage;
  35 +import de.bht.beuthbot.scheduler.model.ReminderCycle;
  36 +import java.util.Date;
  37 +import java.text.SimpleDateFormat;
  38 +import java.text.ParseException;
  39 +import de.bht.beuthbot.model.Messenger;
40 40 import de.bht.beuthbot.scheduler.model.Reminder;
41 41  
42 42 import java.time.DayOfWeek;
43   -import java.time.LocalDate
  43 +import java.time.LocalDate;
44 44 import de.bht.beuthbot.utils.DateStringExtractor;
  45 +import java.time.format.DateTimeFormatter;
45 46  
46 47 global de.bht.beuthbot.canteen.model.CanteenData canteenData;
47 48 global de.bht.beuthbot.model.entities.AppUser user;
... ... @@ -139,7 +140,7 @@ rule &quot;Create reminder&quot;
139 140 Reminder reminder = s.createReminder(message, user);
140 141 String messageText = "";
141 142 if(reminder.getIntent() == Intent.SHOW_FOOD.getText()) {
142   - messageText = "Ich werde dir " + DateStringExtractor.getGermanWeekDayFromLocalDate(reminder.getCycleDate(), true) + " um " + new SimpleDateFormat("KK:mm").format(reminder.getCycleTime()) + " Uhr den Mensaplan mitteilen!";
  143 + messageText = "Ich werde dir " + DateStringExtractor.getGermanWeekDayFromLocalDate(reminder.getCycleDate().toLocalDate(), true) + " um " + reminder.getCycleDate().toLocalTime().format(DateTimeFormatter.ofPattern("HH:mm")) + " Uhr den Mensaplan mitteilen!";
143 144 } else {
144 145 messageText = "Es war mir leider nicht möglich eine Erinnerung für dich anzulegen. Versuche es bitte noch einmal.";
145 146 } //TODO: Add other intents
... ... @@ -147,6 +148,24 @@ rule &quot;Create reminder&quot;
147 148  
148 149 end
149 150  
  151 +rule "Delete reminder"
  152 + dialect "java"
  153 + when
  154 + m : DroolsMessage(getIntent().equals(Intent.DELETE_REMINDER.getText()))
  155 + then
  156 + SchedulerBean s = new SchedulerBean();
  157 + TaskMessage message = new TaskMessage(m);
  158 + boolean didDelete = s.deleteReminder(message, user);
  159 + String messageText = "";
  160 + if(didDelete) {
  161 + messageText = "Ich werde dir von nun an keine Erinnerungen mehr schicken!";
  162 + } else {
  163 + messageText = "Ich tu doch gar nichts!";
  164 + }
  165 + m.setText(messageText);
  166 +
  167 +end
  168 +
150 169  
151 170 // --------------------------- Fallback -------------------------
152 171 rule "Fallback"
... ...