From 1bedd5fb7f5a1ff2d290e8721bf2102c2564ca74 Mon Sep 17 00:00:00 2001
From: DieGurke <55625494+DieGurke@users.noreply.github.com>
Date: Wed, 25 Mar 2020 16:34:55 +0100
Subject: [PATCH] Updated database implementation
*Added a contact abstract class that serves as a superclass for user and
group
* Added a group class
* Updated persistenceManager to fit the new contact system.
* Updated all classes that used methods, that were updated.
---
pom.xml | 2 +-
src/main/java/envoy/server/data/Contact.java | 62 ++++++++++++++++++
src/main/java/envoy/server/data/Group.java | 54 ++++++++++++++++
src/main/java/envoy/server/data/Message.java | 16 +++--
.../envoy/server/data/PersistenceManager.java | 63 +++++++++++++++----
src/main/java/envoy/server/data/User.java | 58 ++---------------
.../envoy/server/net/ConnectionManager.java | 6 +-
.../processors/ContactOperationProcessor.java | 4 +-
.../ContactsRequestEventProcessor.java | 2 +-
.../IDGeneratorRequestProcessor.java | 12 ++--
.../processors/LoginCredentialProcessor.java | 18 +++---
.../MessageStatusChangeProcessor.java | 2 +-
.../processors/UserStatusChangeProcessor.java | 6 +-
13 files changed, 215 insertions(+), 90 deletions(-)
create mode 100644 src/main/java/envoy/server/data/Contact.java
create mode 100644 src/main/java/envoy/server/data/Group.java
diff --git a/pom.xml b/pom.xml
index 046ec55..244170b 100755
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
com.github.informatik-ag-ngl
envoy-common
- develop-SNAPSHOT
+ f~groups-SNAPSHOT
com.github.informatik-ag-ngl
diff --git a/src/main/java/envoy/server/data/Contact.java b/src/main/java/envoy/server/data/Contact.java
new file mode 100644
index 0000000..9f1320a
--- /dev/null
+++ b/src/main/java/envoy/server/data/Contact.java
@@ -0,0 +1,62 @@
+package envoy.server.data;
+
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.MappedSuperclass;
+
+/**
+ * This class acts as a superclass for all contacts, being {@link User}s and
+ * {@link Group}s.
+ *
+ * Project: envoy-server-standalone
+ * File: Contact.java
+ * Created: 24.03.2020
+ *
+ * @author Maximilian Käfer
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+
+@MappedSuperclass
+// TODO add queries
+public abstract class Contact {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ protected long id;
+ protected String name;
+
+ /**
+ * @return a envoy.data.Contact object of this envoy.server.data.Contact object.
+ * @since Envoy Server Standalone v0.1-beta
+ */
+ public abstract envoy.data.Contact toCommon();
+
+ /**
+ * @return the ID of this contact.
+ * @since Envoy Server Standalone v0.1-beta
+ */
+ public long getID() { return id; }
+
+ /**
+ * Sets the ID of this contact.
+ *
+ * @param id to set for this contact
+ * @since Envoy Server Standalone v0.1-beta
+ */
+ public void setID(long id) { this.id = id; }
+
+ /**
+ * @return the name of this contact.
+ * @since Envoy Server Standalone v0.1-beta
+ */
+ public String getName() { return name; }
+
+ /**
+ * Sets the name of this contact.
+ *
+ * @param name to set for this contact
+ * @since Envoy Server Standalone v0.1-beta
+ */
+ public void setName(String name) { this.name = name; }
+}
diff --git a/src/main/java/envoy/server/data/Group.java b/src/main/java/envoy/server/data/Group.java
new file mode 100644
index 0000000..a98f2db
--- /dev/null
+++ b/src/main/java/envoy/server/data/Group.java
@@ -0,0 +1,54 @@
+package envoy.server.data;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.ManyToMany;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.Table;
+
+/**
+ * This class serves as a way to let Hibernate communicate with the server
+ * without bringing the dependency of JPA/Hibernate into the client.
+ * It will be referenced as "database group" to clarify between the different
+ * group objects.
+ *
+ * Project: envoy-server-standalone
+ * File: Group.java
+ * Created: 24.03.2020
+ *
+ * @author Maximilian Käfer
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+@Entity
+@Table(name = "groups")
+@NamedQueries({ @NamedQuery(query = "SELECT g FROM Group g WHERE g.name = :name", name = "getGroupByName") })
+public class Group extends Contact {
+
+ @ManyToMany(targetEntity = Group.class, cascade = CascadeType.ALL)
+ private List members;
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public envoy.data.Group toCommon() { return new envoy.data.Group(id, name, members.stream().map(User::getID).collect(Collectors.toList())); }
+
+ /**
+ * @return a list of all users that are a member of this group.
+ * @since Envoy Server Standalone v0.1-beta
+ */
+ public List getMembers() { return members; }
+
+ /**
+ * Sets the members of this group.
+ *
+ * @param Members a list of all users, that should be assigned to this group.
+ * @since Envoy Server Standalone v0.1-beta
+ */
+ public void setMembers(List members) { this.members = members; }
+
+}
diff --git a/src/main/java/envoy/server/data/Message.java b/src/main/java/envoy/server/data/Message.java
index e5f600c..3a6604c 100755
--- a/src/main/java/envoy/server/data/Message.java
+++ b/src/main/java/envoy/server/data/Message.java
@@ -2,7 +2,15 @@ package envoy.server.data;
import java.util.Date;
-import javax.persistence.*;
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.Table;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
import envoy.data.MessageBuilder;
@@ -89,7 +97,7 @@ public class Message {
*/
public envoy.data.Message toCommonMessage() {
// TODO: Attachment
- envoy.data.Message message = new MessageBuilder(sender.getId(), recipient.getId(), id).setText(text)
+ envoy.data.Message message = new MessageBuilder(sender.getID(), recipient.getID(), id).setText(text)
.setDate(creationDate)
.setStatus(status)
.setForwarded(forwarded)
@@ -103,12 +111,12 @@ public class Message {
* @return the id of a {link envoy.data.Message}
* @since Envoy Server Standalone v0.1-alpha
*/
- public long getId() { return id; }
+ public long getID() { return id; }
/**
* @param id the id to set
* @since Envoy Server Standalone v0.1-alpha
- * @see Message#getId()
+ * @see Message#getID()
*/
public void setId(long id) { this.id = id; }
diff --git a/src/main/java/envoy/server/data/PersistenceManager.java b/src/main/java/envoy/server/data/PersistenceManager.java
index 57fc469..b7261c2 100755
--- a/src/main/java/envoy/server/data/PersistenceManager.java
+++ b/src/main/java/envoy/server/data/PersistenceManager.java
@@ -16,6 +16,7 @@ import envoy.server.net.ConnectionManager;
* Created: 1 Jan 2020
*
* @author Leon Hofmeister
+ * @author Maximilian Käfer
* @since Envoy Server Standalone v0.1-alpha
*/
public class PersistenceManager {
@@ -49,12 +50,12 @@ public class PersistenceManager {
public static PersistenceManager getInstance() { return persistenceManager; }
/**
- * Adds a {@link User} to the database.
+ * Adds a {@link Contact} to the database.
*
- * @param user the {@link User} to add to the database
+ * @param user the {@link Contact} to add to the database
* @since Envoy Server Standalone v0.1-alpha
*/
- public void addUser(User user) { persist(user); }
+ public void addContact(Contact contact) { persist(contact); }
/**
* Adds a {@link Message} to the database.
@@ -73,12 +74,12 @@ public class PersistenceManager {
public void addConfigItem(ConfigItem configItem) { persist(configItem); }
/**
- * Updates a {@link User} in the database
+ * Updates a {@link Contact} in the database
*
- * @param user the {@link User} to add to the database
+ * @param user the {@link Contact} to add to the database
* @since Envoy Server Standalone v0.1-alpha
*/
- public void updateUser(User user) { merge(user); }
+ public void updateContact(Contact contact) { merge(contact); }
/**
* Updates a {@link Message} in the database.
@@ -97,12 +98,12 @@ public class PersistenceManager {
public void updateConfigItem(ConfigItem configItem) { merge(configItem); }
/**
- * Deletes a {@link User} in the database.
+ * Deletes a {@link Contact} in the database.
*
- * @param user the {@link User} to delete
+ * @param user the {@link Contact} to delete
* @since Envoy Server Standalone v0.1-alpha
*/
- public void deleteUser(User user) { remove(user); }
+ public void deleteContact(Contact contact) { remove(contact); }
/**
* Deletes a {@link Message} in the database.
@@ -113,7 +114,7 @@ public class PersistenceManager {
public void deleteMessage(Message message) { remove(message); }
/**
- * Searches for a {@link User} with a specific id.
+ * Searches for a {@link User} with a specific ID.
*
* @param id the id to search for
* @return the user with the specified id
@@ -121,6 +122,24 @@ public class PersistenceManager {
*/
public User getUserById(long id) { return entityManager.find(User.class, id); }
+ /**
+ * Searches for a {@link Group} with a specific ID.
+ *
+ * @param id the id to search for
+ * @return the group with the specific id
+ * @since Envoy Server Standalone v0.1-beta
+ */
+ public Group getGroupById(long id) { return entityManager.find(Group.class, id); }
+
+ /**
+ * Searches for a {@link Contact} with a specific ID.
+ *
+ * @param id the id to search for
+ * @return the contact with the specific id
+ * @since Envoy Server Standalone v0.1-beta
+ */
+ public Contact getContactById(long id) { return entityManager.find(Contact.class, id); }
+
/**
* Searched for a {@link User} with a specific name.
*
@@ -132,6 +151,28 @@ public class PersistenceManager {
return (User) entityManager.createNamedQuery("getUserByName").setParameter("name", name).getSingleResult();
}
+ /**
+ * Searched for a {@link Group} with a specific name.
+ *
+ * @param name the name of the group
+ * @return the group with the specified name
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+ public Group getGroupByName(String name) {
+ return (Group) entityManager.createNamedQuery("getGroupByName").setParameter("name", name).getSingleResult();
+ }
+
+ /**
+ * Searched for a {@link Contact} with a specific name.
+ *
+ * @param name the name of the contact
+ * @return the contact with the specified name
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+ public Contact getContactByName(String name) {
+ return (Contact) entityManager.createNamedQuery("getContactByName").setParameter("name", name).getSingleResult();
+ }
+
/**
* Searches for a {@link Message} with a specific id.
*
@@ -183,7 +224,7 @@ public class PersistenceManager {
* @param userId2 the ID of the second user
* @since Envoy Server Standalone v0.1-alpha
*/
- public void addContact(long userId1, long userId2) {
+ public void addUserContact(long userId1, long userId2) {
// Get users by ID
User u1 = getUserById(userId1);
diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java
index b356147..15ecd4d 100755
--- a/src/main/java/envoy/server/data/User.java
+++ b/src/main/java/envoy/server/data/User.java
@@ -31,12 +31,8 @@ import javax.persistence.*;
name = "searchUsers"
) }
)
-public class User {
+public class User extends Contact {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private long id;
- private String name;
private byte[] passwordHash;
@Temporal(TemporalType.TIMESTAMP)
@@ -47,55 +43,10 @@ public class User {
private List contacts;
/**
- * Creates an instance of {@link User}.
- *
- * @since Envoy Server Standalone v0.1-alpha
+ * {@inheritDoc}
*/
- public User() {}
-
- /**
- * Creates an instance of {@link User}.
- *
- * @param user the {@link envoy.data.User} to convert
- * @since Envoy Server Standalone v0.1-alpha
- */
- public User(envoy.data.User user) {
- id = user.getId();
- name = user.getName();
- status = user.getStatus();
- }
-
- /**
- * @return a database {@link User} converted into an {@link envoy.data.User}
- * @since Envoy Server Standalone v0.1-alpha
- */
- public envoy.data.User toCommonUser() { return new envoy.data.User(id, name, status); }
-
- /**
- * @return the id of a {link envoy.data.User}
- * @since Envoy Server Standalone v0.1-alpha
- */
- public long getId() { return id; }
-
- /**
- * @param id the id to set
- * @since Envoy Server Standalone v0.1-alpha
- * @see User#getId
- */
- public void setId(long id) { this.id = id; }
-
- /**
- * @return the name of a {link envoy.data.User}
- * @since Envoy Server Standalone v0.1-alpha
- */
- public String getName() { return name; }
-
- /**
- * @param name the username to set
- * @since Envoy Server Standalone v0.1-alpha
- * @see User#getName()
- */
- public void setName(String name) { this.name = name; }
+ @Override
+ public envoy.data.User toCommon() { return new envoy.data.User(id, name, status); }
/**
* @return the passwordHash of a {link envoy.data.User}
@@ -148,4 +99,5 @@ public class User {
* @see User#getContacts()
*/
public void setContacts(List contacts) { this.contacts = contacts; }
+
}
diff --git a/src/main/java/envoy/server/net/ConnectionManager.java b/src/main/java/envoy/server/net/ConnectionManager.java
index e8ca932..bf79091 100755
--- a/src/main/java/envoy/server/net/ConnectionManager.java
+++ b/src/main/java/envoy/server/net/ConnectionManager.java
@@ -1,6 +1,10 @@
package envoy.server.net;
-import java.util.*;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
import com.jenkov.nioserver.ISocketIdListener;
diff --git a/src/main/java/envoy/server/processors/ContactOperationProcessor.java b/src/main/java/envoy/server/processors/ContactOperationProcessor.java
index 3c6c6d8..f906fb7 100755
--- a/src/main/java/envoy/server/processors/ContactOperationProcessor.java
+++ b/src/main/java/envoy/server/processors/ContactOperationProcessor.java
@@ -29,11 +29,11 @@ public class ContactOperationProcessor implements ObjectProcessor {
+public class IDGeneratorRequestProcessor implements ObjectProcessor {
private static final long ID_RANGE = 200;
@Override
- public Class getInputClass() { return IdGeneratorRequest.class; }
+ public Class getInputClass() { return IDGeneratorRequest.class; }
@Override
- public void process(IdGeneratorRequest input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
+ public void process(IDGeneratorRequest input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
System.out.println("Received id generation request.");
ConfigItem currentId = PersistenceManager.getInstance().getConfigItemById("currentMessageId");
- IdGenerator generator = new IdGenerator(Integer.parseInt(currentId.getValue()), ID_RANGE);
+ IDGenerator generator = new IDGenerator(Integer.parseInt(currentId.getValue()), ID_RANGE);
currentId.setValue(String.valueOf(Integer.parseInt(currentId.getValue()) + ID_RANGE));
PersistenceManager.getInstance().updateConfigItem(currentId);
diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java
index 1bc736f..acde251 100755
--- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java
+++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java
@@ -1,7 +1,11 @@
package envoy.server.processors;
import java.io.IOException;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.InputMismatchException;
+import java.util.List;
import java.util.stream.Collectors;
import javax.persistence.NoResultException;
@@ -45,7 +49,7 @@ public class LoginCredentialProcessor implements ObjectProcessor());
- persistenceManager.addUser(user);
+ persistenceManager.addContact(user);
return user;
}
}
diff --git a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java
index b01772e..3dce652 100755
--- a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java
+++ b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java
@@ -33,7 +33,7 @@ public class MessageStatusChangeProcessor implements ObjectProcessor