Implemented Groups on the database and implemented MSChange management

This commit is contained in:
DieGurke 2020-04-02 20:41:05 +02:00
parent b3ee23cd8d
commit 64bc34cf61
5 changed files with 46 additions and 23 deletions

View File

@ -2,7 +2,14 @@ package envoy.server.data;
import java.util.Set;
import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToMany;
/**
* This class acts as a superclass for all contacts, being {@link User}s and
@ -16,12 +23,12 @@ import javax.persistence.*;
* @since Envoy Server Standalone v0.1-alpha
*/
@MappedSuperclass
// TODO add queries
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Contact {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue(strategy = GenerationType.TABLE)
protected long id;
protected String name;

View File

@ -2,7 +2,9 @@ package envoy.server.data;
import java.util.stream.Collectors;
import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import envoy.data.User;
@ -21,7 +23,7 @@ import envoy.data.User;
*/
@Entity
@Table(name = "groups")
@NamedQueries({ @NamedQuery(query = "SELECT g FROM Group g WHERE g.name = :name", name = "getGroupByName") })
@NamedQuery(query = "SELECT g FROM Group g WHERE g.name = :name", name = "getGroupByName")
public class Group extends Contact {
/**

View File

@ -6,7 +6,6 @@ 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;
@ -29,11 +28,11 @@ import envoy.data.MessageBuilder;
*/
@Entity
@Table(name = "messages")
@NamedQueries(
{ @NamedQuery(
query = "SELECT m FROM Message m WHERE m.recipient =:recipient AND m.status = envoy.data.Message$MessageStatus.SENT",
name = "getUnreadMessages"
) }
@NamedQuery(
query = "SELECT m FROM Message m WHERE (m.recipient = :user AND m.status = envoy.data.Message$MessageStatus.SENT) "
+ "OR (m.sender = :user) AND ((m.status = envoy.data.Message$MessageStatus.RECEIVED) AND (m.receivedDate > :lastSeen)"
+ "OR (m.status = envoy.data.Message$MessageStatus.READ) AND (m.readDate > :lastSeen))",
name = "getPendingMessages"
)
public class Message {

View File

@ -190,14 +190,18 @@ public class PersistenceManager {
public ConfigItem getConfigItemById(String key) { return entityManager.find(ConfigItem.class, key); }
/**
* Returns all messages received while being offline.
* Returns all messages received while being offline or the ones that have
* changed.
*
* @param user - the user who wants to receive his unread messages
* @param user the user who wants to receive his unread messages
* @return all messages that the client does not yet have (unread messages)
* @since Envoy Server Standalone v0.1-alpha
*/
public List<Message> getUnreadMessages(User user) {
return entityManager.createNamedQuery("getUnreadMessages").setParameter("recipient", user).getResultList();
public List<Message> getPendingMessages(User user) {
return entityManager.createNamedQuery("getPendingMessages")
.setParameter("user", user)
.setParameter("lastSeen", user.getLastSeen())
.getResultList();
}
/**

View File

@ -1,7 +1,11 @@
package envoy.server.processors;
import java.io.IOException;
import java.util.*;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.List;
import java.util.stream.Collectors;
import javax.persistence.NoResultException;
@ -11,6 +15,7 @@ import envoy.data.Message.MessageStatus;
import envoy.data.User;
import envoy.data.User.UserStatus;
import envoy.event.HandshakeRejectionEvent;
import envoy.event.MessageStatusChangeEvent;
import envoy.server.data.Message;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
@ -61,13 +66,19 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
System.out.println("Sending contacts...");
writeProxy.write(socketID, contacts);
System.out.println("Acquiring pending messages for the client...");
List<Message> pendingMessages = PersistenceManager.getInstance().getUnreadMessages(user);
List<Message> pendingMessages = PersistenceManager.getInstance().getPendingMessages(user);
for (Message msg : pendingMessages) {
System.out.println("Sending message " + msg.toCommonMessage());
writeProxy.write(socketID, msg.toCommonMessage());
msg.setReceivedDate(new Date());
msg.setStatus(MessageStatus.RECEIVED);
PersistenceManager.getInstance().updateMessage(msg);
if (msg.getStatus() == MessageStatus.SENT) {
System.out.println("Sending message " + msg.toCommonMessage());
writeProxy.write(socketID, msg.toCommonMessage());
msg.setReceivedDate(new Date());
msg.setStatus(MessageStatus.RECEIVED);
PersistenceManager.getInstance().updateMessage(msg);
} else {
var evt = new MessageStatusChangeEvent(msg.toCommonMessage());
System.out.println("Sending messageStatusChangeEvent " + evt);
writeProxy.write(socketID, evt);
}
}
}