From e6cf3af745f4ce8b56adacc5b41502e8e940a330 Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 2 Apr 2020 16:32:23 +0200 Subject: [PATCH] Restored compatability with envoy-common additionally added NameChangeProcessor --- pom.xml | 2 +- src/main/java/envoy/server/data/Contact.java | 139 ++++++++++-------- src/main/java/envoy/server/data/Group.java | 88 +++++------ .../envoy/server/data/PersistenceManager.java | 8 +- src/main/java/envoy/server/data/User.java | 23 +-- .../envoy/server/net/ConnectionManager.java | 3 +- .../processors/ContactOperationProcessor.java | 5 +- .../ContactsRequestEventProcessor.java | 8 +- .../processors/LoginCredentialProcessor.java | 13 +- .../server/processors/MessageProcessor.java | 2 +- .../processors/NameChangeProcessor.java | 42 ++++++ .../processors/UserStatusChangeProcessor.java | 2 +- 12 files changed, 175 insertions(+), 160 deletions(-) create mode 100644 src/main/java/envoy/server/processors/NameChangeProcessor.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 index 9f1320a..d1737d8 100644 --- a/src/main/java/envoy/server/data/Contact.java +++ b/src/main/java/envoy/server/data/Contact.java @@ -1,62 +1,77 @@ -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; } -} +package envoy.server.data; + +import java.util.Set; + +import javax.persistence.*; + +/** + * 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; + + @ManyToMany(targetEntity = Contact.class, cascade = CascadeType.ALL) + protected Set contacts; + + /** + * @return a {@link 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; } + + /** + * @return the contacts + * @since Envoy Server Standalone v0.1-beta + */ + public Set getContacts() { return contacts; } + + /** + * @param contacts the contacts to set + * @since Envoy Server Standalone v0.1-beta + */ + public void setContacts(Set contacts) { this.contacts = contacts; } +} diff --git a/src/main/java/envoy/server/data/Group.java b/src/main/java/envoy/server/data/Group.java index 592475f..6c76df9 100644 --- a/src/main/java/envoy/server/data/Group.java +++ b/src/main/java/envoy/server/data/Group.java @@ -1,54 +1,34 @@ -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; } - -} +package envoy.server.data; + +import java.util.stream.Collectors; + +import javax.persistence.*; + +import envoy.data.User; + +/** + * 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 { + + /** + * {@inheritDoc} + */ + @Override + public envoy.data.Group toCommon() { + return new envoy.data.Group(id, name, contacts.parallelStream().map(Contact::toCommon).map(User.class::cast).collect(Collectors.toSet())); + } +} diff --git a/src/main/java/envoy/server/data/PersistenceManager.java b/src/main/java/envoy/server/data/PersistenceManager.java index 669cf7e..1dd82a8 100755 --- a/src/main/java/envoy/server/data/PersistenceManager.java +++ b/src/main/java/envoy/server/data/PersistenceManager.java @@ -124,7 +124,7 @@ public class PersistenceManager { /** * 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 @@ -133,7 +133,7 @@ public class PersistenceManager { /** * 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 @@ -227,8 +227,8 @@ public class PersistenceManager { public void addUserContact(long userId1, long userId2) { // Get users by ID - User u1 = getUserById(userId1); - User u2 = getUserById(userId2); + Contact u1 = getContactById(userId1); + Contact u2 = getContactById(userId2); // Add users to each others contact lists u1.getContacts().add(u2); diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index 2913a54..2f788f0 100755 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -1,7 +1,7 @@ package envoy.server.data; import java.util.Date; -import java.util.List; +import java.util.stream.Collectors; import javax.persistence.*; @@ -38,14 +38,13 @@ public class User extends Contact { private Date lastSeen; private envoy.data.User.UserStatus status; - @ManyToMany(targetEntity = User.class, cascade = CascadeType.ALL) - private List contacts; - /** * {@inheritDoc} */ @Override - public envoy.data.User toCommon() { return new envoy.data.User(id, name, status); } + public envoy.data.User toCommon() { + return new envoy.data.User(id, name, status, contacts.stream().map(Contact::toCommon).collect(Collectors.toSet())); + } /** * @return the passwordHash of a {link envoy.data.User} @@ -85,18 +84,4 @@ public class User extends Contact { * @see User#getStatus() */ public void setStatus(envoy.data.User.UserStatus status) { this.status = status; } - - /** - * @return the contacts of a {link envoy.data.User} - * @since Envoy Server Standalone v0.1-alpha - */ - public List getContacts() { return contacts; } - - /** - * @param contacts the contacts to set - * @since Envoy Server Standalone v0.1-alpha - * @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 9314b5c..883f510 100755 --- a/src/main/java/envoy/server/net/ConnectionManager.java +++ b/src/main/java/envoy/server/net/ConnectionManager.java @@ -8,7 +8,6 @@ import com.jenkov.nioserver.ISocketIdListener; import envoy.data.User.UserStatus; import envoy.server.data.Group; import envoy.server.data.PersistenceManager; -import envoy.server.data.User; import envoy.server.processors.UserStatusChangeProcessor; /** @@ -113,7 +112,7 @@ public class ConnectionManager implements ISocketIdListener { */ public Set getOnlineUsersOfGroup(Group group) { Set onlineMembers = new HashSet<>(); - Set members = group.getMembers().stream().map(User::getID).collect(Collectors.toSet()); + Set members = group.getContacts().stream().map(envoy.server.data.Contact::getID).collect(Collectors.toSet()); members.forEach(userID -> { if (isOnline(userID)) onlineMembers.add(userID); }); return onlineMembers; } diff --git a/src/main/java/envoy/server/processors/ContactOperationProcessor.java b/src/main/java/envoy/server/processors/ContactOperationProcessor.java index f9f9133..fa67c55 100755 --- a/src/main/java/envoy/server/processors/ContactOperationProcessor.java +++ b/src/main/java/envoy/server/processors/ContactOperationProcessor.java @@ -3,8 +3,7 @@ package envoy.server.processors; import java.io.IOException; import java.util.Arrays; -import envoy.data.Contacts; -import envoy.event.ContactOperationEvent; +import envoy.event.contact.ContactOperationEvent; import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; @@ -33,7 +32,7 @@ public class ContactOperationProcessor implements ObjectProcessor { /** - * Writes a {@link Contacts} list to the client containing all {@link User}s + * Writes a list of contacts to the client containing all {@link Contact}s * matching the search phrase contained inside the request. The client and their * contacts are excluded from the result. * diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 523dce3..faa9f6c 100755 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -1,16 +1,11 @@ package envoy.server.processors; import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.InputMismatchException; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; import javax.persistence.NoResultException; -import envoy.data.Contacts; import envoy.data.LoginCredentials; import envoy.data.Message.MessageStatus; import envoy.data.User; @@ -57,8 +52,8 @@ public class LoginCredentialProcessor implements ObjectProcessor()); + user.setContacts(new HashSet<>()); persistenceManager.addContact(user); return user; } diff --git a/src/main/java/envoy/server/processors/MessageProcessor.java b/src/main/java/envoy/server/processors/MessageProcessor.java index 5e8d7f0..fd41a87 100755 --- a/src/main/java/envoy/server/processors/MessageProcessor.java +++ b/src/main/java/envoy/server/processors/MessageProcessor.java @@ -43,7 +43,7 @@ public class MessageProcessor implements ObjectProcessor { } } else { System.out.println("The received message is a group message."); - final var members = PersistenceManager.getInstance().getGroupById(message.getRecipientID()).getMembers(); + final var members = PersistenceManager.getInstance().getGroupById(message.getRecipientID()).getContacts(); final var generator = IDGeneratorRequestProcessor.createIDGenerator(members.size()); members.forEach(user -> { envoy.data.Message returnMessage = new MessageBuilder(message.getRecipientID(), user.getID(), generator) diff --git a/src/main/java/envoy/server/processors/NameChangeProcessor.java b/src/main/java/envoy/server/processors/NameChangeProcessor.java new file mode 100644 index 0000000..319d302 --- /dev/null +++ b/src/main/java/envoy/server/processors/NameChangeProcessor.java @@ -0,0 +1,42 @@ +package envoy.server.processors; + +import java.io.IOException; + +import envoy.event.NameChangeEvent; +import envoy.server.data.Contact; +import envoy.server.data.PersistenceManager; +import envoy.server.data.User; +import envoy.server.net.ConnectionManager; +import envoy.server.net.ObjectWriteProxy; + +/** + * Project: envoy-server-standalone
+ * File: NameChangeProcessor.java
+ * Created: 26 Mar 2020
+ * + * @author Leon Hofmeister + * @since Envoy Server Standalone v0.1-beta + */ +public class NameChangeProcessor implements ObjectProcessor { + + @Override + public void process(NameChangeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { + PersistenceManager persistenceManager = PersistenceManager.getInstance(); + ConnectionManager connectionManager = ConnectionManager.getInstance(); + Contact toUpdate = persistenceManager.getContactById(input.getID()); + toUpdate.setName(input.get()); + persistenceManager.updateContact(toUpdate); + + // notifying online contacts of this client of his name change + toUpdate.getContacts().stream().filter(contact -> (contact instanceof User && connectionManager.isOnline(contact.getID()))).forEach(user -> { + try { + writeProxy.write(user.getID(), input); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + + @Override + public Class getInputClass() { return NameChangeEvent.class; } +} diff --git a/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java b/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java index 3e1b5d3..68547fc 100755 --- a/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java @@ -71,7 +71,7 @@ public class UserStatusChangeProcessor implements ObjectProcessor