Commit e2aff233373b9b406a65cdb65ba6b96afac44101

Authored by hausdoerfer
1 parent 1287623a

built-in logging for inbound messages and customized table names

services/Common/src/main/java/de/bht/beuthbot/daos/LogMessageDAO.java 0 → 100644
  1 +package de.bht.beuthbot.daos;
  2 +
  3 +import de.bht.beuthbot.model.entities.LogMessage;
  4 +
  5 +import javax.ejb.Remote;
  6 +
  7 +/**
  8 + * Created by Stephan Hausdörfer on 19.01.2018.
  9 + */
  10 +@Remote
  11 +public interface LogMessageDAO extends GenericDAO<LogMessage, Long> {
  12 +
  13 + LogMessage createMessage();
  14 +}
services/Common/src/main/java/de/bht/beuthbot/daos/LogMessageDAOImpl.java 0 → 100644
  1 +package de.bht.beuthbot.daos;
  2 +
  3 +import de.bht.beuthbot.model.entities.LogMessage;
  4 +import de.bht.beuthbot.model.entities.LogMessageImpl;
  5 +
  6 +import javax.ejb.Stateless;
  7 +
  8 +/**
  9 + * Created by Stephan Hausdörfer on 19.01.2018.
  10 + */
  11 +@Stateless
  12 +public class LogMessageDAOImpl extends GenericHibernateDAO<LogMessage, LogMessageImpl, Long> implements LogMessageDAO {
  13 +
  14 + @Override
  15 + public LogMessage createMessage() {
  16 + return new LogMessageImpl();
  17 + }
  18 +}
0 \ No newline at end of file 19 \ No newline at end of file
services/Common/src/main/java/de/bht/beuthbot/model/entities/AppUserImpl.java
@@ -13,7 +13,7 @@ import javax.persistence.Table; @@ -13,7 +13,7 @@ import javax.persistence.Table;
13 * Postgres does not support creating a table named "user". 13 * Postgres does not support creating a table named "user".
14 */ 14 */
15 @Entity 15 @Entity
16 -@Table 16 +@Table(name = "app_user")
17 public class AppUserImpl extends EntityBaseImpl implements AppUser { 17 public class AppUserImpl extends EntityBaseImpl implements AppUser {
18 18
19 private String facebookUserId; 19 private String facebookUserId;
services/Common/src/main/java/de/bht/beuthbot/model/entities/GenericEntity.java
@@ -14,7 +14,7 @@ import java.util.stream.Collectors; @@ -14,7 +14,7 @@ import java.util.stream.Collectors;
14 * Follows the Entity-Attribute-Value model of database design. 14 * Follows the Entity-Attribute-Value model of database design.
15 */ 15 */
16 @Entity 16 @Entity
17 -@Table 17 +@Table(name = "generic_entity")
18 public class GenericEntity extends EntityBaseImpl { 18 public class GenericEntity extends EntityBaseImpl {
19 19
20 /** 20 /**
services/Common/src/main/java/de/bht/beuthbot/model/entities/GenericEntityAttribute.java
@@ -14,7 +14,7 @@ import java.util.stream.Collectors; @@ -14,7 +14,7 @@ import java.util.stream.Collectors;
14 * Holds a collection of generic values. 14 * Holds a collection of generic values.
15 */ 15 */
16 @Entity 16 @Entity
17 -@Table 17 +@Table(name = "generic_entity_attribute")
18 public class GenericEntityAttribute extends EntityBaseImpl { 18 public class GenericEntityAttribute extends EntityBaseImpl {
19 19
20 private String name; 20 private String name;
services/Common/src/main/java/de/bht/beuthbot/model/entities/GenericEntityAttributeValue.java
@@ -11,7 +11,7 @@ import javax.persistence.Table; @@ -11,7 +11,7 @@ import javax.persistence.Table;
11 * Supports complex nested types using a reference to a generic sub entity. 11 * Supports complex nested types using a reference to a generic sub entity.
12 */ 12 */
13 @Entity 13 @Entity
14 -@Table 14 +@Table(name = "generic_entity_attribute_value")
15 public class GenericEntityAttributeValue extends EntityBaseImpl { 15 public class GenericEntityAttributeValue extends EntityBaseImpl {
16 16
17 private Boolean valueAsBool; 17 private Boolean valueAsBool;
services/Common/src/main/java/de/bht/beuthbot/model/entities/LogMessage.java 0 → 100644
  1 +package de.bht.beuthbot.model.entities;
  2 +
  3 +/**
  4 + * Created by Stephan Hausdörfer on 19.01.2018.
  5 + */
  6 +public interface LogMessage extends EntityBase {
  7 +
  8 + long getUserId();
  9 +
  10 + void setUserId(long userId);
  11 +
  12 + String getMessage();
  13 +
  14 + void setMessage(String message);
  15 +}
services/Common/src/main/java/de/bht/beuthbot/model/entities/LogMessageImpl.java 0 → 100644
  1 +package de.bht.beuthbot.model.entities;
  2 +
  3 +import javax.persistence.Entity;
  4 +import javax.persistence.Table;
  5 +
  6 +/**
  7 + * Created by Stephan Hausdörfer on 19.01.2018.
  8 + */
  9 +@Entity
  10 +@Table(name = "log_message")
  11 +public class LogMessageImpl extends EntityBaseImpl implements LogMessage {
  12 +
  13 + private long userId;
  14 +
  15 + private String message;
  16 +
  17 +
  18 + @Override
  19 + public long getUserId() {
  20 + return userId;
  21 + }
  22 +
  23 + @Override
  24 + public void setUserId(long userId) {
  25 + this.userId = userId;
  26 + }
  27 +
  28 + @Override
  29 + public String getMessage() {
  30 + return message;
  31 + }
  32 +
  33 + @Override
  34 + public void setMessage(String message) {
  35 + this.message = message;
  36 + }
  37 +}
services/Common/src/main/java/de/bht/beuthbot/model/entities/ReminderImpl.java
@@ -3,6 +3,7 @@ package de.bht.beuthbot.model.entities; @@ -3,6 +3,7 @@ package de.bht.beuthbot.model.entities;
3 import de.bht.beuthbot.model.Messenger; 3 import de.bht.beuthbot.model.Messenger;
4 import de.bht.beuthbot.model.enumeration.ReminderCycle; 4 import de.bht.beuthbot.model.enumeration.ReminderCycle;
5 5
  6 +import javax.persistence.CollectionTable;
6 import javax.persistence.ElementCollection; 7 import javax.persistence.ElementCollection;
7 import javax.persistence.Entity; 8 import javax.persistence.Entity;
8 import javax.persistence.Table; 9 import javax.persistence.Table;
@@ -13,7 +14,7 @@ import java.util.Map; @@ -13,7 +14,7 @@ import java.util.Map;
13 * Created by Stephan Hausdörfer on 16.01.2018. 14 * Created by Stephan Hausdörfer on 16.01.2018.
14 */ 15 */
15 @Entity 16 @Entity
16 -@Table 17 +@Table(name = "reminder")
17 public class ReminderImpl extends EntityBaseImpl implements Reminder { 18 public class ReminderImpl extends EntityBaseImpl implements Reminder {
18 19
19 private long userId; 20 private long userId;
@@ -27,6 +28,7 @@ public class ReminderImpl extends EntityBaseImpl implements Reminder { @@ -27,6 +28,7 @@ public class ReminderImpl extends EntityBaseImpl implements Reminder {
27 private String intent; 28 private String intent;
28 29
29 @ElementCollection 30 @ElementCollection
  31 + @CollectionTable(name = "reminder_entities")
30 private Map<String, String> entities; 32 private Map<String, String> entities;
31 33
32 @Override 34 @Override
services/MainBot/src/main/java/de/bht/beuthbot/drools/DroolsService.java
1 package de.bht.beuthbot.drools; 1 package de.bht.beuthbot.drools;
2 2
3 -  
4 -  
5 -  
6 import com.google.gson.Gson; 3 import com.google.gson.Gson;
7 4
8 -  
9 import de.bht.beuthbot.attachments.AttachmentStore; 5 import de.bht.beuthbot.attachments.AttachmentStore;
10 import de.bht.beuthbot.canteen.Parser; 6 import de.bht.beuthbot.canteen.Parser;
11 import de.bht.beuthbot.canteen.model.CanteenData; 7 import de.bht.beuthbot.canteen.model.CanteenData;
12 import de.bht.beuthbot.conf.Application; 8 import de.bht.beuthbot.conf.Application;
13 import de.bht.beuthbot.daos.AppUserDAO; 9 import de.bht.beuthbot.daos.AppUserDAO;
  10 +import de.bht.beuthbot.daos.LogMessageDAO;
14 import de.bht.beuthbot.drools.model.DroolsMessage; 11 import de.bht.beuthbot.drools.model.DroolsMessage;
15 import de.bht.beuthbot.jms.ProcessQueue; 12 import de.bht.beuthbot.jms.ProcessQueue;
16 import de.bht.beuthbot.jms.ProcessQueueMessageProtocol; 13 import de.bht.beuthbot.jms.ProcessQueueMessageProtocol;
@@ -18,6 +15,8 @@ import de.bht.beuthbot.jms.Target; @@ -18,6 +15,8 @@ import de.bht.beuthbot.jms.Target;
18 import de.bht.beuthbot.jms.TaskMessage; 15 import de.bht.beuthbot.jms.TaskMessage;
19 import de.bht.beuthbot.model.Messenger; 16 import de.bht.beuthbot.model.Messenger;
20 import de.bht.beuthbot.model.entities.AppUser; 17 import de.bht.beuthbot.model.entities.AppUser;
  18 +
  19 +import de.bht.beuthbot.model.entities.LogMessage;
21 import org.kie.api.KieServices; 20 import org.kie.api.KieServices;
22 import org.kie.api.runtime.KieContainer; 21 import org.kie.api.runtime.KieContainer;
23 import org.kie.api.runtime.KieSession; 22 import org.kie.api.runtime.KieSession;
@@ -73,6 +72,9 @@ public class DroolsService implements MessageListener { @@ -73,6 +72,9 @@ public class DroolsService implements MessageListener {
73 @Resource(lookup = "java:global/global/AppUserDAOImpl") 72 @Resource(lookup = "java:global/global/AppUserDAOImpl")
74 private AppUserDAO userDAO; 73 private AppUserDAO userDAO;
75 74
  75 + @Resource(lookup = "java:global/global/LogMessageDAOImpl")
  76 + private LogMessageDAO logMessageDAO;
  77 +
76 @Inject 78 @Inject
77 private Parser parser; 79 private Parser parser;
78 80
@@ -143,7 +145,10 @@ public class DroolsService implements MessageListener { @@ -143,7 +145,10 @@ public class DroolsService implements MessageListener {
143 } 145 }
144 AppUser finishedUser = userDAO.saveOrUpdate(tempUser); 146 AppUser finishedUser = userDAO.saveOrUpdate(tempUser);
145 147
146 - 148 + LogMessage logMessage = logMessageDAO.createMessage();
  149 + logMessage.setUserId(finishedUser.getId());
  150 + logMessage.setMessage(botMessage.getText());
  151 + logMessageDAO.saveOrUpdate(logMessage);
147 152
148 ksession.setGlobal("canteenData", canteenData); 153 ksession.setGlobal("canteenData", canteenData);
149 ksession.setGlobal("user", finishedUser); 154 ksession.setGlobal("user", finishedUser);
services/Telegram/src/main/java/de/bht/beuthbot/messenger/telegram/TelegramReceiveAdapter.java
@@ -13,6 +13,9 @@ import com.pengrad.telegrambot.response.GetFileResponse; @@ -13,6 +13,9 @@ import com.pengrad.telegrambot.response.GetFileResponse;
13 import de.bht.beuthbot.attachments.AttachmentStore; 13 import de.bht.beuthbot.attachments.AttachmentStore;
14 import de.bht.beuthbot.conf.Application; 14 import de.bht.beuthbot.conf.Application;
15 import de.bht.beuthbot.conf.Configuration; 15 import de.bht.beuthbot.conf.Configuration;
  16 +import de.bht.beuthbot.daos.AppUserDAO;
  17 +import de.bht.beuthbot.daos.LogMessageDAO;
  18 +import de.bht.beuthbot.daos.ReminderDAO;
16 import de.bht.beuthbot.jms.ProcessQueue; 19 import de.bht.beuthbot.jms.ProcessQueue;
17 import de.bht.beuthbot.jms.TaskMessage; 20 import de.bht.beuthbot.jms.TaskMessage;
18 import de.bht.beuthbot.messenger.telegram.model.TelegramAttachment; 21 import de.bht.beuthbot.messenger.telegram.model.TelegramAttachment;
@@ -20,6 +23,11 @@ import de.bht.beuthbot.messenger.telegram.model.TelegramMessage; @@ -20,6 +23,11 @@ import de.bht.beuthbot.messenger.telegram.model.TelegramMessage;
20 import de.bht.beuthbot.messenger.telegram.model.TelegramWebhookURL; 23 import de.bht.beuthbot.messenger.telegram.model.TelegramWebhookURL;
21 import de.bht.beuthbot.model.Attachment; 24 import de.bht.beuthbot.model.Attachment;
22 import de.bht.beuthbot.model.AttachmentType; 25 import de.bht.beuthbot.model.AttachmentType;
  26 +import de.bht.beuthbot.model.Messenger;
  27 +import de.bht.beuthbot.model.entities.AppUser;
  28 +import de.bht.beuthbot.model.entities.LogMessage;
  29 +import de.bht.beuthbot.model.entities.Reminder;
  30 +import de.bht.beuthbot.model.enumeration.ReminderCycle;
23 import org.apache.http.HttpStatus; 31 import org.apache.http.HttpStatus;
24 import org.slf4j.Logger; 32 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory; 33 import org.slf4j.LoggerFactory;
@@ -27,10 +35,14 @@ import org.slf4j.LoggerFactory; @@ -27,10 +35,14 @@ import org.slf4j.LoggerFactory;
27 import javax.annotation.PostConstruct; 35 import javax.annotation.PostConstruct;
28 import javax.annotation.Resource; 36 import javax.annotation.Resource;
29 import javax.ws.rs.Consumes; 37 import javax.ws.rs.Consumes;
  38 +import javax.ws.rs.GET;
30 import javax.ws.rs.POST; 39 import javax.ws.rs.POST;
31 import javax.ws.rs.Path; 40 import javax.ws.rs.Path;
32 import javax.ws.rs.core.MediaType; 41 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response; 42 import javax.ws.rs.core.Response;
  43 +import java.time.LocalDate;
  44 +import java.util.HashMap;
  45 +import java.util.List;
34 46
35 /** 47 /**
36 * @Author: Christopher Kümmel on 5/14/2017. 48 * @Author: Christopher Kümmel on 5/14/2017.
@@ -56,6 +68,18 @@ public class TelegramReceiveAdapter { @@ -56,6 +68,18 @@ public class TelegramReceiveAdapter {
56 /** com.pengrad.telegrambot.TelegramBot; */ 68 /** com.pengrad.telegrambot.TelegramBot; */
57 private TelegramBot bot; 69 private TelegramBot bot;
58 70
  71 + /** Injected AppUserDAO */
  72 + @Resource(lookup = "java:global/global/AppUserDAOImpl")
  73 + private AppUserDAO userDAO;
  74 +
  75 + /** Injected AppUserDAO */
  76 + @Resource(lookup = "java:global/global/ReminderDAOImpl")
  77 + private ReminderDAO reminderDAO;
  78 +
  79 + /** Injected AppUserDAO */
  80 + @Resource(lookup = "java:global/global/LogMessageDAOImpl")
  81 + private LogMessageDAO logMessageDAO;
  82 +
59 /** 83 /**
60 * Initialize TelegramBot with Token 84 * Initialize TelegramBot with Token
61 */ 85 */
@@ -131,6 +155,52 @@ public class TelegramReceiveAdapter { @@ -131,6 +155,52 @@ public class TelegramReceiveAdapter {
131 } 155 }
132 } 156 }
133 157
  158 + /**
  159 + * Test method for database
  160 + * @return Http response
  161 + */
  162 + @GET
  163 + @Path("/testDatabase")
  164 + public Response testDatabase() {
  165 + logger.info("testDatabase: Start");
  166 + AppUser user = userDAO.createUser();
  167 + user.setFacebookUserId("88");
  168 + user.setTelegramUserId("99");
  169 + user.setProperty("test_key", "test_value");
  170 + userDAO.saveOrUpdate(user);
  171 + logger.info("testDatabase: Speichern durchgefuehrt");
  172 +
  173 + List<AppUser> userList = userDAO.findAll();
  174 + String result = userList.get(0).getTelegramUserId();
  175 + logger.info("TelegramUserId: " + result);
  176 + logger.info("testDatabase: Auslesen durchgefuehrt");
  177 +
  178 + Reminder reminder = reminderDAO.createReminder();
  179 + reminder.setMessenger(Messenger.TELEGRAM);
  180 + reminder.setReminderCycle(ReminderCycle.DAILY);
  181 + reminder.setCycleTime(LocalDate.now());
  182 + Long userId = userList.get(0).getId();
  183 + reminder.setUserId(userId);
  184 + reminder.setIntent("Intent");
  185 + HashMap entities = new HashMap();
  186 + entities.put("key1", "value2");
  187 + entities.put("key2", "value2");
  188 + reminder.setEntities(entities);
  189 + reminderDAO.saveOrUpdate(reminder);
  190 +
  191 + List<Reminder> reminders = reminderDAO.findAll();
  192 + Reminder reminderResult = reminders.get(0);
  193 + logger.info("Reminder Cycle: " + reminderResult.getReminderCycle().toString());
  194 + logger.info("Telegram ID via Reminder: " + userDAO.findById(reminderResult.getUserId()).getTelegramUserId());
  195 +
  196 + LogMessage logMessage = logMessageDAO.createMessage();
  197 + logMessage.setUserId(userList.get(0).getId());
  198 + logMessage.setMessage("Message");
  199 + logMessageDAO.saveOrUpdate(logMessage);
  200 +
  201 + return Response.status(HttpStatus.SC_OK).build();
  202 + }
  203 +
134 204
135 /** 205 /**
136 * RESTEasy HTTP Post to set Webhook 206 * RESTEasy HTTP Post to set Webhook