Commit 5de3209ecf1b2363633e6a01000d83f67a3e0115

Authored by mfcb
2 parents 0f38e630 46466384

Merge remote-tracking branch 'origin/development' into development

# Conflicts:
#	services/MainBot/src/main/java/de/bht/beuthbot/scheduler/SchedulerBean.java
#	services/MainBot/src/main/java/de/bht/beuthbot/scheduler/model/Reminder.java
#	services/MainBot/src/main/resources/de/bht/beuthbot/drools/Canteen.drl
Showing 25 changed files with 699 additions and 95 deletions
docu/BeuthBot.postman_collection.json 0 → 100644
  1 +{
  2 + "info": {
  3 + "name": "BeuthBot",
  4 + "_postman_id": "fd29548f-6b95-81d0-cae3-1233324d3846",
  5 + "description": "",
  6 + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  7 + },
  8 + "item": [
  9 + {
  10 + "name": "Facebook",
  11 + "description": "",
  12 + "item": [
  13 + {
  14 + "name": "FB setWebhook",
  15 + "request": {
  16 + "method": "GET",
  17 + "header": [],
  18 + "body": {},
  19 + "url": {
  20 + "raw": "localhost:8080/facebook/setWebhook",
  21 + "host": [
  22 + "localhost"
  23 + ],
  24 + "port": "8080",
  25 + "path": [
  26 + "facebook",
  27 + "setWebhook"
  28 + ]
  29 + },
  30 + "description": ""
  31 + },
  32 + "response": []
  33 + },
  34 + {
  35 + "name": "FB getUpdates",
  36 + "request": {
  37 + "method": "POST",
  38 + "header": [
  39 + {
  40 + "key": "Content-Type",
  41 + "value": "application/json"
  42 + }
  43 + ],
  44 + "body": {
  45 + "mode": "raw",
  46 + "raw": "{\"object\":\"page\",\"entry\":[{\"id\":\"163783704220143\",\"time\":1513427045712,\"messaging\":[{\"sender\":{\"id\":\"1385529261556255\"},\"recipient\":{\"id\":\"163783704220143\"},\"timestamp\":1513427044846,\"message\":{\"mid\":\"mid.$cAADOButO2l5mkI1R7lgX0gFO8FR5\",\"seq\":4370,\"text\":\"Hallo\"}}]}]}"
  47 + },
  48 + "url": {
  49 + "raw": "localhost:8080/facebook/getUpdates",
  50 + "host": [
  51 + "localhost"
  52 + ],
  53 + "port": "8080",
  54 + "path": [
  55 + "facebook",
  56 + "getUpdates"
  57 + ]
  58 + },
  59 + "description": ""
  60 + },
  61 + "response": []
  62 + }
  63 + ]
  64 + },
  65 + {
  66 + "name": "Rasa",
  67 + "description": "",
  68 + "item": [
  69 + {
  70 + "name": "Rasa parse",
  71 + "request": {
  72 + "method": "GET",
  73 + "header": [],
  74 + "body": {},
  75 + "url": {
  76 + "raw": "localhost:5000/parse?q=Gerichte",
  77 + "host": [
  78 + "localhost"
  79 + ],
  80 + "port": "5000",
  81 + "path": [
  82 + "parse"
  83 + ],
  84 + "query": [
  85 + {
  86 + "key": "q",
  87 + "value": "Gerichte",
  88 + "equals": true
  89 + }
  90 + ]
  91 + },
  92 + "description": ""
  93 + },
  94 + "response": []
  95 + }
  96 + ]
  97 + },
  98 + {
  99 + "name": "Telegram",
  100 + "description": "",
  101 + "item": [
  102 + {
  103 + "name": "Telegram setWebhook",
  104 + "request": {
  105 + "method": "GET",
  106 + "header": [],
  107 + "body": {},
  108 + "url": {
  109 + "raw": ""
  110 + },
  111 + "description": ""
  112 + },
  113 + "response": []
  114 + },
  115 + {
  116 + "name": "Telegram setWebhook",
  117 + "request": {
  118 + "method": "POST",
  119 + "header": [],
  120 + "body": {},
  121 + "url": {
  122 + "raw": "localhost:8080/telegram/setWebhook",
  123 + "host": [
  124 + "localhost"
  125 + ],
  126 + "port": "8080",
  127 + "path": [
  128 + "telegram",
  129 + "setWebhook"
  130 + ]
  131 + },
  132 + "description": ""
  133 + },
  134 + "response": []
  135 + }
  136 + ]
  137 + },
  138 + {
  139 + "name": "Wildfly Home",
  140 + "request": {
  141 + "method": "GET",
  142 + "header": [],
  143 + "body": {},
  144 + "url": {
  145 + "raw": "localhost/8080",
  146 + "host": [
  147 + "localhost"
  148 + ],
  149 + "path": [
  150 + "8080"
  151 + ]
  152 + },
  153 + "description": ""
  154 + },
  155 + "response": []
  156 + }
  157 + ]
  158 +}
0 159 \ No newline at end of file
... ...
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 19 \ No newline at end of file
... ...
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/AppUserImpl.java
... ... @@ -13,7 +13,7 @@ import javax.persistence.Table;
13 13 * Postgres does not support creating a table named "user".
14 14 */
15 15 @Entity
16   -@Table
  16 +@Table(name = "app_user")
17 17 public class AppUserImpl extends EntityBaseImpl implements AppUser {
18 18  
19 19 private String facebookUserId;
... ...
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/GenericEntity.java
... ... @@ -14,7 +14,7 @@ import java.util.stream.Collectors;
14 14 * Follows the Entity-Attribute-Value model of database design.
15 15 */
16 16 @Entity
17   -@Table
  17 +@Table(name = "generic_entity")
18 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 14 * Holds a collection of generic values.
15 15 */
16 16 @Entity
17   -@Table
  17 +@Table(name = "generic_entity_attribute")
18 18 public class GenericEntityAttribute extends EntityBaseImpl {
19 19  
20 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 11 * Supports complex nested types using a reference to a generic sub entity.
12 12 */
13 13 @Entity
14   -@Table
  14 +@Table(name = "generic_entity_attribute_value")
15 15 public class GenericEntityAttributeValue extends EntityBaseImpl {
16 16  
17 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/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.CollectionTable;
  7 +import javax.persistence.ElementCollection;
  8 +import javax.persistence.Entity;
  9 +import javax.persistence.Table;
  10 +import java.time.LocalDate;
  11 +import java.util.Map;
  12 +
  13 +/**
  14 + * Created by Stephan Hausdörfer on 16.01.2018.
  15 + */
  16 +@Entity
  17 +@Table(name = "reminder")
  18 +public class ReminderImpl extends EntityBaseImpl implements Reminder {
  19 +
  20 + private long userId;
  21 +
  22 + private ReminderCycle reminderCycle;
  23 +
  24 + private LocalDate cycleTime;
  25 +
  26 + private Messenger messenger;
  27 +
  28 + private String intent;
  29 +
  30 + @ElementCollection
  31 + @CollectionTable(name = "reminder_entities")
  32 + private Map<String, String> entities;
  33 +
  34 + @Override
  35 + public long getUserId() {
  36 + return userId;
  37 + }
  38 +
  39 + @Override
  40 + public void setUserId(long userId) {
  41 + this.userId = userId;
  42 + }
  43 +
  44 + @Override
  45 + public ReminderCycle getReminderCycle() {
  46 + return reminderCycle;
  47 + }
  48 +
  49 + @Override
  50 + public void setReminderCycle(ReminderCycle reminderCycle) {
  51 + this.reminderCycle = reminderCycle;
  52 + }
  53 +
  54 + @Override
  55 + public LocalDate getCycleTime() {
  56 + return cycleTime;
  57 + }
  58 +
  59 + @Override
  60 + public void setCycleTime(LocalDate cycleTime) {
  61 + this.cycleTime = cycleTime;
  62 + }
  63 +
  64 + @Override
  65 + public Messenger getMessenger() {
  66 + return messenger;
  67 + }
  68 +
  69 + @Override
  70 + public void setMessenger(Messenger messenger) {
  71 + this.messenger = messenger;
  72 + }
  73 +
  74 + @Override
  75 + public String getIntent() {
  76 + return intent;
  77 + }
  78 +
  79 + @Override
  80 + public void setIntent(String intent) {
  81 + this.intent = intent;
  82 + }
  83 +
  84 + @Override
  85 + public Map<String, String> getEntities() {
  86 + return entities;
  87 + }
  88 +
  89 + @Override
  90 + public void setEntities(Map<String, String> entities) {
  91 + this.entities = entities;
  92 + }
  93 +}
... ...
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/Common/src/main/java/de/bht/beuthbot/persistence/GenericEntityAccessFacade.java
... ... @@ -8,6 +8,8 @@ import de.bht.beuthbot.model.entities.GenericEntityAttribute;
8 8 import de.bht.beuthbot.model.entities.GenericEntityAttributeValue;
9 9  
10 10 import java.io.IOException;
  11 +import java.io.StringWriter;
  12 +import java.io.Writer;
11 13 import java.util.List;
12 14 import java.util.stream.Collectors;
13 15  
... ... @@ -29,9 +31,9 @@ public class GenericEntityAccessFacade {
29 31 String propertyAsJson;
30 32  
31 33 if (isPrimitiveOrWrapperAttributeType(attributeType)) {
32   - propertyAsJson = GenericEntityJsonConverter.toJson(property.getFirstValueOrNull());
  34 + propertyAsJson = writeGenericHierarchyToJson(property.getFirstValueOrNull());
33 35 } else {
34   - propertyAsJson = GenericEntityJsonConverter.toJson(property);
  36 + propertyAsJson = writeGenericHierarchyToJson(property);
35 37 }
36 38  
37 39 return (T) JsonHelper.fromJson(propertyAsJson, attributeType);
... ... @@ -90,4 +92,17 @@ public class GenericEntityAccessFacade {
90 92  
91 93 return mapper.readValue(json, type);
92 94 }
  95 +
  96 + private static String writeGenericHierarchyToJson(Object value) throws IOException {
  97 + ObjectMapper mapper = new ObjectMapper();
  98 + SimpleModule module = new SimpleModule();
  99 + module.addSerializer(GenericEntity.class, new GenericEntitySerializer());
  100 + module.addSerializer(GenericEntityAttribute.class, new GenericEntityAttributeSerializer());
  101 + module.addSerializer(GenericEntityAttributeValue.class, new GenericEntityAttributeValueSerializer());
  102 + mapper.registerModule(module);
  103 +
  104 + Writer stringWriter = new StringWriter();
  105 + mapper.writeValue(stringWriter, value);
  106 + return stringWriter.toString();
  107 + }
93 108 }
... ...
services/Common/src/main/java/de/bht/beuthbot/persistence/GenericEntityAttributeSerializer.java 0 → 100644
  1 +package de.bht.beuthbot.persistence;
  2 +
  3 +import com.fasterxml.jackson.core.JsonGenerationException;
  4 +import com.fasterxml.jackson.core.JsonGenerator;
  5 +import com.fasterxml.jackson.databind.SerializerProvider;
  6 +import com.fasterxml.jackson.databind.ser.std.StdSerializer;
  7 +import de.bht.beuthbot.model.entities.GenericEntityAttribute;
  8 +import de.bht.beuthbot.model.entities.GenericEntityAttributeValue;
  9 +
  10 +import java.io.IOException;
  11 +import java.util.List;
  12 +
  13 +/**
  14 + * Created by Benjamin Rühl on 14.01.2018.
  15 + */
  16 +public class GenericEntityAttributeSerializer extends StdSerializer<GenericEntityAttribute> {
  17 +
  18 + private GenericEntityAttributeValueSerializer attributeValueSerializer = new GenericEntityAttributeValueSerializer();
  19 +
  20 + public GenericEntityAttributeSerializer() {
  21 + this(GenericEntityAttribute.class);
  22 + }
  23 +
  24 + public GenericEntityAttributeSerializer(Class<GenericEntityAttribute> t) {
  25 + super(t);
  26 + }
  27 +
  28 + @Override
  29 + public void serialize(GenericEntityAttribute value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
  30 + List<GenericEntityAttributeValue> attributeValues = value.getValues();
  31 +
  32 + jgen.writeStartObject();
  33 + jgen.writeFieldName(value.getName());
  34 +
  35 + if (attributeValues == null || attributeValues.isEmpty()) {
  36 + jgen.writeNullField(value.getName());
  37 + } else if (attributeValues.size() == 1) {
  38 + attributeValueSerializer.serialize(value.getValues().get(0), jgen, provider);
  39 + } else {
  40 + jgen.writeArrayFieldStart(value.getName());
  41 +
  42 + for (GenericEntityAttributeValue genericValue : value.getValues()) {
  43 + attributeValueSerializer.serialize(genericValue, jgen, provider);
  44 + }
  45 +
  46 + jgen.writeEndArray();
  47 + }
  48 +
  49 + jgen.writeEndObject();
  50 + }
  51 +}
... ...
services/Common/src/main/java/de/bht/beuthbot/persistence/GenericEntityAttributeValueSerializer.java 0 → 100644
  1 +package de.bht.beuthbot.persistence;
  2 +
  3 +import com.fasterxml.jackson.core.JsonGenerationException;
  4 +import com.fasterxml.jackson.core.JsonGenerator;
  5 +import com.fasterxml.jackson.databind.SerializerProvider;
  6 +import com.fasterxml.jackson.databind.ser.std.StdSerializer;
  7 +import de.bht.beuthbot.model.entities.GenericEntityAttributeValue;
  8 +
  9 +import java.io.IOException;
  10 +
  11 +/**
  12 + * Created by Benjamin Rühl on 14.01.2018.
  13 + */
  14 +public class GenericEntityAttributeValueSerializer extends StdSerializer<GenericEntityAttributeValue> {
  15 +
  16 + public GenericEntityAttributeValueSerializer() {
  17 + this(GenericEntityAttributeValue.class);
  18 + }
  19 +
  20 + public GenericEntityAttributeValueSerializer(Class<GenericEntityAttributeValue> t) {
  21 + super(t);
  22 + }
  23 +
  24 + @Override
  25 + public void serialize(GenericEntityAttributeValue value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
  26 + if (value.getValueAsBool() != null) {
  27 + jgen.writeBoolean(value.getValueAsBool());
  28 + } else if (value.getValueAsLong() != null) {
  29 + jgen.writeNumber(value.getValueAsLong());
  30 + } else if (value.getValueAsDouble() != null) {
  31 + jgen.writeNumber(value.getValueAsDouble());
  32 + } else if (value.getValueAsString() != null) {
  33 + jgen.writeString(value.getValueAsString());
  34 + } else if (value.getValueAsEntity() != null) {
  35 + new GenericEntitySerializer().serialize(value.getValueAsEntity(), jgen, provider);
  36 + }
  37 + }
  38 +}
... ...
services/Common/src/main/java/de/bht/beuthbot/persistence/GenericEntityJsonConverter.java
... ... @@ -18,6 +18,7 @@ import java.util.List;
18 18 /**
19 19 * Created by Benjamin Rühl on 22.12.2017.
20 20 * Helper class to serialize GenericEntity as if its generic attributes were normal fields
  21 + * @deprecated replaced by generic entity serializers
21 22 */
22 23 public class GenericEntityJsonConverter {
23 24  
... ...
services/Common/src/main/java/de/bht/beuthbot/persistence/GenericEntitySerializer.java 0 → 100644
  1 +package de.bht.beuthbot.persistence;
  2 +
  3 +import com.fasterxml.jackson.core.JsonGenerationException;
  4 +import com.fasterxml.jackson.core.JsonGenerator;
  5 +import com.fasterxml.jackson.databind.SerializerProvider;
  6 +import com.fasterxml.jackson.databind.ser.std.StdSerializer;
  7 +import de.bht.beuthbot.model.entities.GenericEntity;
  8 +import de.bht.beuthbot.model.entities.GenericEntityAttribute;
  9 +import de.bht.beuthbot.model.entities.GenericEntityAttributeValue;
  10 +
  11 +import java.io.IOException;
  12 +import java.lang.reflect.Field;
  13 +import java.lang.reflect.ParameterizedType;
  14 +import java.lang.reflect.Type;
  15 +import java.util.Arrays;
  16 +import java.util.List;
  17 +
  18 +/**
  19 + * Created by Benjamin Rühl on 14.01.2018.
  20 + */
  21 +public class GenericEntitySerializer extends StdSerializer<GenericEntity> {
  22 +
  23 + private GenericEntityAttributeSerializer attributeSerializer = new GenericEntityAttributeSerializer();
  24 +
  25 + public GenericEntitySerializer() {
  26 + this(GenericEntity.class);
  27 + }
  28 +
  29 + public GenericEntitySerializer(Class<GenericEntity> t) {
  30 + super(t);
  31 + }
  32 +
  33 + /**
  34 + * Uses a JsonGenerator to append a GenericEntity and its content to json.
  35 + * Does not open the json object because this part depends on whether the object stands for itself or is the value of a field.
  36 + * However it does close the object.
  37 + * @throws IOException
  38 + */
  39 + @Override
  40 + public void serialize(GenericEntity value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
  41 + jgen.writeStartObject();
  42 +
  43 + writeClassFieldsWithoutGenericEntityHierarchy(jgen, value);
  44 +
  45 + for (GenericEntityAttribute genericAttribute : value.getAttributes()) {
  46 + attributeSerializer.serialize(genericAttribute, jgen, provider);
  47 + }
  48 +
  49 + jgen.writeEndObject();
  50 + }
  51 +
  52 + /**
  53 + * Uses a JsonGenerator to append all fields and their values of the targetObject to json.
  54 + * Ignores fields that have GenericEntity, GenericEntityAttribute or GenericEntityAttributeValue as type or generic list parameter.
  55 + * @throws IOException
  56 + */
  57 + private void writeClassFieldsWithoutGenericEntityHierarchy(JsonGenerator jsonGenerator, Object targetObject) throws IOException {
  58 + for (Field field : targetObject.getClass().getFields()) {
  59 + if (isGenericEntityHierarchyType(field))
  60 + continue;
  61 +
  62 + try {
  63 + jsonGenerator.writeObjectField(field.getName(), field.get(targetObject));
  64 + } catch (Exception e) {
  65 + jsonGenerator.writeStringField(field.getName(), e.getClass().getName());
  66 + e.printStackTrace();
  67 + }
  68 + }
  69 + }
  70 +
  71 + /**
  72 + * Determines whether the field's type is GenericEntity, GenericEntityAttribute or GenericEntityAttributeValue
  73 + * or a list type with one of those types as generic type parameter.
  74 + */
  75 + private boolean isGenericEntityHierarchyType(Field field) {
  76 + List<Class> typesOfGenericEntityHierarchy = Arrays.asList(GenericEntity.class, GenericEntityAttribute.class, GenericEntityAttributeValue.class);
  77 +
  78 + // check for list types
  79 + try {
  80 + if (Iterable.class.isAssignableFrom(field.getType())) {
  81 + Type fieldFirstGenericType = ((ParameterizedType)field.getGenericType()).getActualTypeArguments()[0];
  82 + if (typesOfGenericEntityHierarchy.contains(fieldFirstGenericType.getClass()))
  83 + return true;
  84 + }
  85 + } catch (Exception e) {
  86 + // not a list or not generic
  87 + }
  88 +
  89 + return typesOfGenericEntityHierarchy.contains(field.getType());
  90 + }
  91 +}
... ...
services/Common/src/main/java/de/bht/beuthbot/persistence/JsonMapUserType.java
... ... @@ -18,6 +18,7 @@ import java.util.Map;
18 18 /**
19 19 * Created by Benjamin Rühl on 03.12.2017.
20 20 * Custom Hibernate UserType for mapping json typed columns in postgres to java maps
  21 + * @deprecated Currently not in use
21 22 * @see <a href="http://vojtechruzicka.com/postgresqls-jsonb-type-mapping-using-hibernate/">vojtechruzicka.com</a>
22 23 */
23 24 public class JsonMapUserType implements UserType {
... ...
services/MainBot/src/main/java/de/bht/beuthbot/drools/DroolsService.java
1 1 package de.bht.beuthbot.drools;
2 2  
3   -
4   -
5   -
6 3 import com.google.gson.Gson;
7 4  
8   -
9 5 import de.bht.beuthbot.attachments.AttachmentStore;
10 6 import de.bht.beuthbot.canteen.Parser;
11 7 import de.bht.beuthbot.canteen.model.CanteenData;
12 8 import de.bht.beuthbot.conf.Application;
13 9 import de.bht.beuthbot.daos.AppUserDAO;
  10 +import de.bht.beuthbot.daos.LogMessageDAO;
14 11 import de.bht.beuthbot.drools.model.DroolsMessage;
15 12 import de.bht.beuthbot.jms.ProcessQueue;
16 13 import de.bht.beuthbot.jms.ProcessQueueMessageProtocol;
... ... @@ -18,6 +15,8 @@ import de.bht.beuthbot.jms.Target;
18 15 import de.bht.beuthbot.jms.TaskMessage;
19 16 import de.bht.beuthbot.model.Messenger;
20 17 import de.bht.beuthbot.model.entities.AppUser;
  18 +
  19 +import de.bht.beuthbot.model.entities.LogMessage;
21 20 import org.kie.api.KieServices;
22 21 import org.kie.api.runtime.KieContainer;
23 22 import org.kie.api.runtime.KieSession;
... ... @@ -73,6 +72,9 @@ public class DroolsService implements MessageListener {
73 72 @Resource(lookup = "java:global/global/AppUserDAOImpl")
74 73 private AppUserDAO userDAO;
75 74  
  75 + @Resource(lookup = "java:global/global/LogMessageDAOImpl")
  76 + private LogMessageDAO logMessageDAO;
  77 +
76 78 @Inject
77 79 private Parser parser;
78 80  
... ... @@ -143,7 +145,10 @@ public class DroolsService implements MessageListener {
143 145 }
144 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 153 ksession.setGlobal("canteenData", canteenData);
149 154 ksession.setGlobal("user", finishedUser);
... ...
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.time.LocalDateTime;
9   -import java.util.Date;
10   -import java.util.HashMap;
11   -import java.util.Map;
12   -
13   -public class Reminder {
14   - //TODO: Replace with User Object
15   - private AppUser _user;
16   - //Reminder cycle
17   - private ReminderCycle _reminderCycle;
18   - //Date of Cycle
19   - private LocalDateTime _cycleDate;
20   - //private
21   - //Used Messenger
22   - private Messenger _messenger;
23   - //Intent after Reminder is triggered
24   - private String _intent;
25   - //Entities
26   - private final Map<String, String> _entities;
27   -
28   - public Reminder(AppUser user, ReminderCycle reminderCycle, LocalDateTime cycleDate, Messenger messenger, String intent, Map<String, String> entities) {
29   - _user = user;
30   - _reminderCycle = reminderCycle;
31   - _cycleDate = cycleDate;
32   - _messenger = messenger;
33   - _intent = intent;
34   - _entities = entities;
35   - }
36   -
37   - public Messenger getMessenger() {
38   - return _messenger;
39   - }
40   -
41   - public void setMessenger(Messenger messenger) {
42   - _messenger = messenger;
43   - }
44   -
45   - public String getIntent() {
46   - return _intent;
47   - }
48   -
49   - public void setIntent(String intent) {
50   - _intent = intent;
51   - }
52   -
53   - public AppUser getUser() {
54   - return _user;
55   - }
56   -
57   - public void setUserID(AppUser user) {
58   - _user = user;
59   - }
60   -
61   - public ReminderCycle getReminderCycle() {
62   - return _reminderCycle;
63   - }
64   -
65   - public void setReminderCycle(ReminderCycle reminderCycle) {
66   - _reminderCycle = reminderCycle;
67   - }
68   -
69   - public LocalDateTime getCycleDate() {
70   - return _cycleDate;
71   - }
72   -
73   - public void setCycleDate(LocalDateTime cycleDate) {
74   - _cycleDate = cycleDate;
75   - }
76   -
77   - public Map<String, String> getEntities() { return _entities; }
78   -}
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   -}
services/Telegram/src/main/java/de/bht/beuthbot/messenger/telegram/TelegramReceiveAdapter.java
... ... @@ -13,6 +13,9 @@ import com.pengrad.telegrambot.response.GetFileResponse;
13 13 import de.bht.beuthbot.attachments.AttachmentStore;
14 14 import de.bht.beuthbot.conf.Application;
15 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 19 import de.bht.beuthbot.jms.ProcessQueue;
17 20 import de.bht.beuthbot.jms.TaskMessage;
18 21 import de.bht.beuthbot.messenger.telegram.model.TelegramAttachment;
... ... @@ -20,6 +23,11 @@ import de.bht.beuthbot.messenger.telegram.model.TelegramMessage;
20 23 import de.bht.beuthbot.messenger.telegram.model.TelegramWebhookURL;
21 24 import de.bht.beuthbot.model.Attachment;
22 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 31 import org.apache.http.HttpStatus;
24 32 import org.slf4j.Logger;
25 33 import org.slf4j.LoggerFactory;
... ... @@ -27,10 +35,14 @@ import org.slf4j.LoggerFactory;
27 35 import javax.annotation.PostConstruct;
28 36 import javax.annotation.Resource;
29 37 import javax.ws.rs.Consumes;
  38 +import javax.ws.rs.GET;
30 39 import javax.ws.rs.POST;
31 40 import javax.ws.rs.Path;
32 41 import javax.ws.rs.core.MediaType;
33 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 48 * @Author: Christopher Kümmel on 5/14/2017.
... ... @@ -56,6 +68,18 @@ public class TelegramReceiveAdapter {
56 68 /** com.pengrad.telegrambot.TelegramBot; */
57 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 84 * Initialize TelegramBot with Token
61 85 */
... ... @@ -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 206 * RESTEasy HTTP Post to set Webhook
... ...