Merge pull request #28 from informatik-ag-ngl/b/db_received_date

Fixed missing message received dates in database
This commit is contained in:
Kai S. K. Engelbart 2020-02-18 16:54:27 +01:00 committed by GitHub
commit 866c8c1a48
12 changed files with 87 additions and 71 deletions

View File

@ -7,7 +7,7 @@ import java.util.Set;
import com.jenkov.nioserver.Server;
import envoy.server.data.ConfigItem;
import envoy.server.database.PersistenceManager;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectMessageProcessor;
import envoy.server.net.ObjectMessageReader;

View File

@ -5,7 +5,6 @@ import java.util.Date;
import javax.persistence.*;
import envoy.data.MessageBuilder;
import envoy.server.database.PersistenceManager;
/**
* This class serves as a way to let Hibernate communicate with the server
@ -53,14 +52,15 @@ public class Message {
private byte[] attachment;
/**
* The constructor for a database object
* The constructor for a database object.
*
* @since Envoy Server Standalone v0.1-alpha
*/
public Message() {}
// TODO: everything except ID
/**
* Constructs a database message from a common message.
*
* @param message the {@link envoy.data.Message} to convert into a database
* {@link Message}
* @since Envoy Server Standalone v0.1-alpha
@ -71,19 +71,29 @@ public class Message {
status = message.getStatus();
text = message.getText();
creationDate = message.getCreationDate();
receivedDate = message.getReceivedDate();
readDate = message.getReadDate();
sender = persMan.getUserById(message.getSenderId());
recipient = persMan.getUserById(message.getRecipientId());
// attachment = message.getAttachment().toByteArray();DOES NOT WORK YET
// TODO: attachment = message.getAttachment().toByteArray();DOES NOT WORK YET
}
/**
* @return a database {@link Message} converted into an
* {@link envoy.data.Message}
* Converts this message into an instance of {@link envoy.data.Message}.
*
* @return a {@link envoy.data.Message} containing the same values as this
* message
* @since Envoy Server Standalone v0.1-alpha
*/
public envoy.data.Message toCommonMessage() {
// TODO: Attachment, dates
return new MessageBuilder(sender.getId(), recipient.getId(), id).setText(text).setDate(creationDate).setStatus(status).build();
// TODO: Attachment
envoy.data.Message message = new MessageBuilder(sender.getId(), recipient.getId(), id).setText(text)
.setDate(creationDate)
.setStatus(status)
.build();
message.setReceivedDate(receivedDate);
message.setReadDate(readDate);
return message;
}
/**

View File

@ -1,4 +1,4 @@
package envoy.server.database;
package envoy.server.data;
import java.util.Date;
import java.util.List;
@ -8,9 +8,6 @@ import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import envoy.data.User.UserStatus;
import envoy.server.data.ConfigItem;
import envoy.server.data.Message;
import envoy.server.data.User;
import envoy.server.net.ConnectionManager;
/**
@ -34,13 +31,13 @@ public class PersistenceManager {
* @since Envoy Server Standalone v0.1-alpha
*/
private PersistenceManager() {
transaction.begin();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
transaction.begin();
ConnectionManager.getInstance()
.getOnlineUsers()
.stream()
.map(this::getUserById)
.forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(new Date()); updateUser(user); });
.forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(new Date()); entityManager.merge(user); });
transaction.commit();
}));
}
@ -54,13 +51,10 @@ public class PersistenceManager {
/**
* Adds a {@link User} to the database.
*
* @param User the {@link User} to add to the database
* @param user the {@link User} to add to the database
* @since Envoy Server Standalone v0.1-alpha
*/
public void addUser(User User) {
entityManager.persist(User);
entityManager.flush();
}
public void addUser(User user) { persist(user); }
/**
* Adds a {@link Message} to the database.
@ -68,10 +62,7 @@ public class PersistenceManager {
* @param message the {@link Message} to add to the database
* @since Envoy Server Standalone v0.1-alpha
*/
public void addMessage(Message message) {
entityManager.persist(message);
entityManager.flush();
}
public void addMessage(Message message) { persist(message); }
/**
* Adds a {@link ConfigItem} to the database.
@ -79,10 +70,7 @@ public class PersistenceManager {
* @param configItem the {@link ConfigItem} to add to the database
* @since Envoy Server Standalone v0.1-alpha
*/
public void addConfigItem(ConfigItem configItem) {
entityManager.persist(configItem);
entityManager.flush();
}
public void addConfigItem(ConfigItem configItem) { persist(configItem); }
/**
* Updates a {@link User} in the database
@ -90,10 +78,7 @@ public class PersistenceManager {
* @param user the {@link User} to add to the database
* @since Envoy Server Standalone v0.1-alpha
*/
public void updateUser(User user) {
entityManager.merge(user);
entityManager.flush();
}
public void updateUser(User user) { merge(user); }
/**
* Updates a {@link Message} in the database.
@ -101,10 +86,7 @@ public class PersistenceManager {
* @param message the message to update
* @since Envoy Server Standalone v0.1-alpha
*/
public void updateMessage(Message message) {
entityManager.merge(message);
entityManager.flush();
}
public void updateMessage(Message message) { merge(message); }
/**
* Updates a {@link ConfigItem} in the database.
@ -112,10 +94,7 @@ public class PersistenceManager {
* @param configItem the configItem to update
* @since Envoy Server Standalone v0.1-alpha
*/
public void updateConfigItem(ConfigItem configItem) {
entityManager.merge(configItem);
entityManager.flush();
}
public void updateConfigItem(ConfigItem configItem) { merge(configItem); }
/**
* Deletes a {@link User} in the database.
@ -123,10 +102,7 @@ public class PersistenceManager {
* @param user the {@link User} to delete
* @since Envoy Server Standalone v0.1-alpha
*/
public void deleteUser(User user) {
entityManager.remove(user);
entityManager.flush();
}
public void deleteUser(User user) { remove(user); }
/**
* Deletes a {@link Message} in the database.
@ -134,10 +110,7 @@ public class PersistenceManager {
* @param message the {@link Message} to delete
* @since Envoy Server Standalone v0.1-alpha
*/
public void deleteMessage(Message message) {
entityManager.remove(message);
entityManager.flush();
}
public void deleteMessage(Message message) { remove(message); }
/**
* Searches for a {@link User} with a specific id.
@ -211,12 +184,20 @@ public class PersistenceManager {
* @since Envoy Server Standalone v0.1-alpha
*/
public void addContact(long userId1, long userId2) {
// Get users by ID
User u1 = getUserById(userId1);
User u2 = getUserById(userId2);
// Add users to each others contact lists
u1.getContacts().add(u2);
u2.getContacts().add(u1);
updateUser(u1);
updateUser(u2);
// Synchronize changes with the database
transaction.begin();
entityManager.merge(u1);
entityManager.merge(u2);
transaction.commit();
}
/**
@ -227,4 +208,22 @@ public class PersistenceManager {
public List<User> getContacts(User user) {
return entityManager.createNamedQuery("getContactsOfUser").setParameter("user", user).getResultList();
}
}
private void persist(Object obj) {
transaction.begin();
entityManager.persist(obj);
transaction.commit();
}
private void merge(Object obj) {
transaction.begin();
entityManager.merge(obj);
transaction.commit();
}
private void remove(Object obj) {
transaction.begin();
entityManager.remove(obj);
transaction.commit();
}
}

View File

@ -5,7 +5,7 @@ import java.util.*;
import com.jenkov.nioserver.ISocketIdListener;
import envoy.data.User.UserStatus;
import envoy.server.database.PersistenceManager;
import envoy.server.data.PersistenceManager;
import envoy.server.processors.UserStatusChangeProcessor;
/**

View File

@ -38,7 +38,12 @@ public class ObjectMessageProcessor implements IMessageProcessor {
public void process(Message message, WriteProxy writeProxy) {
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) {
Object obj = in.readObject();
System.out.println("Read object: " + obj.toString());
if (obj == null) {
System.out.println("received a null object");
return;
}
System.out.println("Read object: " + obj);
// Process object
processors.stream().filter(p -> p.getInputClass().isInstance(obj)).forEach((@SuppressWarnings("rawtypes") ObjectProcessor p) -> {

View File

@ -6,7 +6,7 @@ import java.util.Arrays;
import envoy.data.Contacts;
import envoy.event.ContactOperationEvent;
import envoy.server.ObjectProcessor;
import envoy.server.database.PersistenceManager;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;

View File

@ -7,8 +7,8 @@ import envoy.data.Contacts;
import envoy.event.ContactSearchRequest;
import envoy.event.ContactSearchResult;
import envoy.server.ObjectProcessor;
import envoy.server.data.PersistenceManager;
import envoy.server.data.User;
import envoy.server.database.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;

View File

@ -6,7 +6,7 @@ import envoy.data.IdGenerator;
import envoy.event.IdGeneratorRequest;
import envoy.server.ObjectProcessor;
import envoy.server.data.ConfigItem;
import envoy.server.database.PersistenceManager;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ObjectWriteProxy;
/**

View File

@ -14,7 +14,7 @@ import envoy.data.User.UserStatus;
import envoy.event.HandshakeRejectionEvent;
import envoy.server.ObjectProcessor;
import envoy.server.data.Message;
import envoy.server.database.PersistenceManager;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;

View File

@ -6,7 +6,7 @@ import java.util.Date;
import envoy.data.Message;
import envoy.event.MessageStatusChangeEvent;
import envoy.server.ObjectProcessor;
import envoy.server.database.PersistenceManager;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;
@ -22,9 +22,6 @@ import envoy.server.net.ObjectWriteProxy;
*/
public class MessageProcessor implements ObjectProcessor<Message> {
@Override
public Class<Message> getInputClass() { return Message.class; }
@Override
public void process(Message message, long socketId, ObjectWriteProxy writeProxy) {
@ -44,4 +41,7 @@ public class MessageProcessor implements ObjectProcessor<Message> {
}
PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message));
}
@Override
public Class<Message> getInputClass() { return Message.class; }
}

View File

@ -6,7 +6,7 @@ import envoy.data.Message.MessageStatus;
import envoy.event.MessageStatusChangeEvent;
import envoy.exception.EnvoyException;
import envoy.server.ObjectProcessor;
import envoy.server.database.PersistenceManager;
import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;
@ -20,26 +20,28 @@ import envoy.server.net.ObjectWriteProxy;
*/
public class MessageStatusChangeProcessor implements ObjectProcessor<MessageStatusChangeEvent> {
@Override
public Class<MessageStatusChangeEvent> getInputClass() { return MessageStatusChangeEvent.class; }
private final PersistenceManager persistenceManager = PersistenceManager.getInstance();
private final ConnectionManager connectionManager = ConnectionManager.getInstance();
@Override
public void process(MessageStatusChangeEvent input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
try {
// any other status than read is not supposed to be sent to the server
if (input.get() != MessageStatus.READ) throw new EnvoyException("Message" + input.getId() + "has an invalid status");
if (input.get() != MessageStatus.READ) throw new EnvoyException("Message " + input + " has an invalid status");
} catch (EnvoyException e) {
throw new IOException(e);
}
ConnectionManager conMan = ConnectionManager.getInstance();
PersistenceManager perMan = PersistenceManager.getInstance();
envoy.server.data.Message msg = perMan.getMessageById(input.getId());
envoy.server.data.Message msg = persistenceManager.getMessageById(input.getId());
msg.setStatus(input.get());
msg.setReadDate(input.getDate());
perMan.updateMessage(msg);
persistenceManager.updateMessage(msg);
// Notifies the sender of the message about the status-update to READ
if (conMan.isOnline(msg.getSender().getId())) writeProxy.write(conMan.getSocketId(msg.getSender().getId()), input);
final long senderId = msg.getSender().getId();
if (connectionManager.isOnline(senderId)) writeProxy.write(connectionManager.getSocketId(senderId), input);
}
@Override
public Class<MessageStatusChangeEvent> getInputClass() { return MessageStatusChangeEvent.class; }
}

View File

@ -5,8 +5,8 @@ import java.io.IOException;
import envoy.data.User.UserStatus;
import envoy.event.UserStatusChangeEvent;
import envoy.server.ObjectProcessor;
import envoy.server.data.PersistenceManager;
import envoy.server.data.User;
import envoy.server.database.PersistenceManager;
import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy;