Merge branch 'develop' into f/logging
This commit is contained in:
commit
9e83cc1c66
@ -1,374 +1,304 @@
|
|||||||
package envoy.client;
|
package envoy.client;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.xml.datatype.DatatypeConfigurationException;
|
import javax.xml.datatype.DatatypeConfigurationException;
|
||||||
import javax.xml.datatype.DatatypeFactory;
|
import javax.xml.datatype.DatatypeFactory;
|
||||||
|
|
||||||
import envoy.exception.EnvoyException;
|
import envoy.exception.EnvoyException;
|
||||||
import envoy.schema.Message;
|
import envoy.schema.Message;
|
||||||
import envoy.schema.Message.Metadata.MessageState;
|
import envoy.schema.Message.Metadata.MessageState;
|
||||||
import envoy.schema.ObjectFactory;
|
import envoy.schema.ObjectFactory;
|
||||||
import envoy.schema.Sync;
|
import envoy.schema.Sync;
|
||||||
import envoy.schema.User;
|
import envoy.schema.User;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>envoy-client</strong><br>
|
* Project: <strong>envoy-client</strong><br>
|
||||||
* File: <strong>LocalDB.java</strong><br>
|
* File: <strong>LocalDB.java</strong><br>
|
||||||
* Created: <strong>27.10.2019</strong><br>
|
* Created: <strong>27.10.2019</strong><br>
|
||||||
*
|
*
|
||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
* @author Maximilian Käfer
|
* @author Maximilian Käfer
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public class LocalDB {
|
public class LocalDB {
|
||||||
private File localDB;
|
|
||||||
private User sender;
|
private File localDB;
|
||||||
private List<Chat> chats = new ArrayList<>();
|
private User sender;
|
||||||
private ObjectFactory objectFactory = new ObjectFactory();
|
private List<Chat> chats = new ArrayList<>();
|
||||||
private DatatypeFactory datatypeFactory;
|
private ObjectFactory objectFactory = new ObjectFactory();
|
||||||
|
private DatatypeFactory datatypeFactory;
|
||||||
private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName());
|
|
||||||
|
private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName());
|
||||||
/**
|
|
||||||
* Constructs an empty local database.
|
private Sync unreadMessagesSync = objectFactory.createSync();
|
||||||
*
|
private Sync sync = objectFactory.createSync();
|
||||||
* @param client the user that is logged in with this client
|
private Sync readMessages = objectFactory.createSync();
|
||||||
* @since Envoy v0.1-alpha
|
|
||||||
**/
|
/**
|
||||||
|
* Constructs an empty local database.
|
||||||
public LocalDB(User sender) {
|
*
|
||||||
this.sender = sender;
|
* @param client the user that is logged in with this client
|
||||||
try {
|
* @since Envoy v0.1-alpha
|
||||||
datatypeFactory = DatatypeFactory.newInstance();
|
*/
|
||||||
} catch (DatatypeConfigurationException e) {
|
|
||||||
e.printStackTrace();
|
public LocalDB(User sender) {
|
||||||
}
|
this.sender = sender;
|
||||||
}
|
try {
|
||||||
|
datatypeFactory = DatatypeFactory.newInstance();
|
||||||
|
} catch (DatatypeConfigurationException e) {
|
||||||
/**
|
e.printStackTrace();
|
||||||
* Initializes the local database and fills it with values
|
}
|
||||||
* if the user has already sent or received messages.
|
}
|
||||||
*
|
|
||||||
* @param localDBDir the directory where we wish to save/load the database from.
|
/**
|
||||||
* @throws EnvoyException if the directory selected is not an actual directory.
|
* Initializes the local database and fills it with values
|
||||||
* @since Envoy v0.1-alpha
|
* if the user has already sent or received messages.
|
||||||
**/
|
*
|
||||||
public void initializeDBFile(File localDBDir) throws EnvoyException {
|
* @param localDBDir the directory where we wish to save/load the database from.
|
||||||
if (localDBDir.exists() && !localDBDir.isDirectory()) throw new EnvoyException(
|
* @throws EnvoyException if the directory selected is not an actual directory.
|
||||||
String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
|
* @since Envoy v0.1-alpha
|
||||||
localDB = new File(localDBDir, sender.getID() + ".db");
|
*/
|
||||||
if (localDB.exists()) loadFromLocalDB();
|
public void initializeDBFile(File localDBDir) throws EnvoyException {
|
||||||
}
|
if (localDBDir.exists() && !localDBDir.isDirectory())
|
||||||
|
throw new EnvoyException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
|
||||||
/**
|
localDB = new File(localDBDir, sender.getID() + ".db");
|
||||||
* Saves the database into a file for future use.
|
if (localDB.exists()) loadFromLocalDB();
|
||||||
*
|
}
|
||||||
* @throws IOException if something went wrong during saving
|
|
||||||
* @since Envoy v0.1-alpha
|
/**
|
||||||
**/
|
* Saves the database into a file for future use.
|
||||||
public void saveToLocalDB() {
|
*
|
||||||
try {
|
* @throws IOException if something went wrong during saving
|
||||||
localDB.getParentFile().mkdirs();
|
* @since Envoy v0.1-alpha
|
||||||
localDB.createNewFile();
|
*/
|
||||||
} catch (IOException e) {
|
public void saveToLocalDB() {
|
||||||
e.printStackTrace();
|
try {
|
||||||
logger.warning("unable to save the messages");
|
localDB.getParentFile().mkdirs();
|
||||||
}
|
localDB.createNewFile();
|
||||||
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) {
|
} catch (IOException e) {
|
||||||
out.writeObject(chats);
|
e.printStackTrace();
|
||||||
} catch (IOException ex) {
|
logger.warning("unable to save the messages");
|
||||||
ex.printStackTrace();
|
}
|
||||||
logger.warning("unable to save the messages");
|
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) {
|
||||||
}
|
out.writeObject(chats);
|
||||||
}
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
/**
|
logger.warning("unable to save the messages");
|
||||||
* Loads all chats saved by Envoy for the client user.
|
}
|
||||||
*
|
}
|
||||||
* @throws EnvoyException if something fails while loading.
|
|
||||||
* @since Envoy v0.1-alpha
|
/**
|
||||||
**/
|
* Loads all chats saved by Envoy for the client user.
|
||||||
@SuppressWarnings("unchecked")
|
*
|
||||||
private void loadFromLocalDB() throws EnvoyException {
|
* @throws EnvoyException if something fails while loading.
|
||||||
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) {
|
* @since Envoy v0.1-alpha
|
||||||
Object obj = in.readObject();
|
*/
|
||||||
if (obj instanceof ArrayList<?>) chats = (ArrayList<Chat>) obj;
|
@SuppressWarnings("unchecked")
|
||||||
} catch (ClassNotFoundException | IOException e) {
|
private void loadFromLocalDB() throws EnvoyException {
|
||||||
throw new EnvoyException(e);
|
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) {
|
||||||
}
|
Object obj = in.readObject();
|
||||||
}
|
if (obj instanceof ArrayList<?>) chats = (ArrayList<Chat>) obj;
|
||||||
|
} catch (ClassNotFoundException | IOException e) {
|
||||||
/**
|
throw new EnvoyException(e);
|
||||||
* Creates a {@link Message} object serializable to XML.
|
}
|
||||||
*
|
}
|
||||||
* @param textContent The content (text) of the message
|
|
||||||
* @return prepared {@link Message} object
|
/**
|
||||||
*/
|
* Creates a {@link Message} object serializable to XML.
|
||||||
public Message createMessage(String textContent, User recipient) {
|
*
|
||||||
Message.Metadata metaData = objectFactory.createMessageMetadata();
|
* @param textContent The content (text) of the message
|
||||||
metaData.setSender(sender.getID());
|
* @return prepared {@link Message} object
|
||||||
metaData.setRecipient(recipient.getID());
|
*/
|
||||||
metaData.setState(MessageState.WAITING);
|
public Message createMessage(String textContent, User recipient) {
|
||||||
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
|
Message.Metadata metaData = objectFactory.createMessageMetadata();
|
||||||
|
metaData.setSender(sender.getID());
|
||||||
Message.Content content = objectFactory.createMessageContent();
|
metaData.setRecipient(recipient.getID());
|
||||||
content.setType("text");
|
metaData.setState(MessageState.WAITING);
|
||||||
content.setText(textContent);
|
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
|
||||||
|
|
||||||
Message message = objectFactory.createMessage();
|
Message.Content content = objectFactory.createMessageContent();
|
||||||
message.setMetadata(metaData);
|
content.setType("text");
|
||||||
message.getContent().add(content);
|
content.setText(textContent);
|
||||||
|
|
||||||
return message;
|
Message message = objectFactory.createMessage();
|
||||||
}
|
message.setMetadata(metaData);
|
||||||
|
message.getContent().add(content);
|
||||||
private Sync unreadMessagesSync = objectFactory.createSync();
|
|
||||||
public Sync sync = objectFactory.createSync();
|
return message;
|
||||||
public Sync readMessages = objectFactory.createSync();
|
}
|
||||||
|
|
||||||
public Sync fillSync(long userId) {
|
public Sync fillSync(long userId) {
|
||||||
|
addWaitingMessagesToSync();
|
||||||
addWaitingMessagesToSync();
|
|
||||||
|
sync.getMessages().addAll(readMessages.getMessages());
|
||||||
getSentStateMessagesFromLocalDB();
|
readMessages.getMessages().clear();
|
||||||
for (int i = 0; i < readMessages.getMessages().size(); i++) {
|
|
||||||
sync.getMessages().add(readMessages.getMessages().get(i));
|
logger.info(String.format("Filled sync with %d messages.", sync.getMessages().size()));
|
||||||
}
|
return sync;
|
||||||
readMessages.getMessages().clear();
|
}
|
||||||
|
|
||||||
logger.info(String.format("Filled sync with %d messages.", sync.getMessages().size()));
|
public void applySync(Sync returnSync) {
|
||||||
return sync;
|
for (int i = 0; i < returnSync.getMessages().size(); i++) {
|
||||||
}
|
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
||||||
|
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.SENT) {
|
||||||
public void applySync(Sync returnSync) {
|
// Update Local Messages with State WAITING (add Message ID and change State to
|
||||||
for (int i = 0; i < returnSync.getMessages().size(); i++) {
|
// SENT)
|
||||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
for (int j = 0; j < sync.getMessages().size(); j++) {
|
||||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.SENT) {
|
if (j == i) {
|
||||||
// Update Local Messages with State WAITING (add Message ID and change State to
|
sync.getMessages().get(j).getMetadata().setMessageId(returnSync.getMessages().get(j).getMetadata().getMessageId());
|
||||||
// SENT)
|
sync.getMessages().get(j).getMetadata().setState(returnSync.getMessages().get(j).getMetadata().getState());
|
||||||
for (int j = 0; j < sync.getMessages().size(); j++) {
|
}
|
||||||
if (j == i) {
|
}
|
||||||
sync.getMessages()
|
}
|
||||||
.get(j)
|
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() != 0
|
||||||
.getMetadata()
|
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
||||||
.setMessageId(returnSync.getMessages().get(j).getMetadata().getMessageId());
|
// these are the unread Messages from the server
|
||||||
sync.getMessages()
|
unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i));
|
||||||
.get(j)
|
}
|
||||||
.getMetadata()
|
|
||||||
.setState(returnSync.getMessages().get(j).getMetadata().getState());
|
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() == 0
|
||||||
}
|
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
||||||
|
// Update Messages in localDB to state RECEIVED
|
||||||
}
|
for (int j = 0; j < getChats().size(); j++) {
|
||||||
|
if (getChats().get(j).getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
|
||||||
}
|
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
||||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages()
|
||||||
&& returnSync.getMessages().get(i).getMetadata().getSender() != 0
|
.get(i)
|
||||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
.getMetadata()
|
||||||
// these are the unread Messages from the server
|
.getMessageId()) {
|
||||||
unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i));
|
// Update Message in LocalDB
|
||||||
|
getChats().get(j).getModel().get(k).getMetadata().setState(returnSync.getMessages().get(j).getMetadata().getState());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
}
|
||||||
&& returnSync.getMessages().get(i).getMetadata().getSender() == 0
|
}
|
||||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
|
||||||
// Update Messages in localDB to state RECEIVED
|
}
|
||||||
for (int j = 0; j < getChats().size(); j++) {
|
|
||||||
if (getChats().get(j)
|
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
||||||
.getRecipient()
|
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) {
|
||||||
.getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
|
// Update local Messages to state READ
|
||||||
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
logger.info("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId()
|
||||||
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync
|
+ "was initialized to be set to READ in localDB.");
|
||||||
.getMessages()
|
for (int j = 0; j < getChats().size(); j++) {
|
||||||
.get(i)
|
if (getChats().get(j)
|
||||||
.getMetadata()
|
.getRecipient()
|
||||||
.getMessageId()) {
|
.getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
|
||||||
// Update Message in LocalDB
|
logger.info("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected.");
|
||||||
getChats().get(j)
|
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
||||||
.getModel()
|
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages()
|
||||||
.get(k)
|
.get(i)
|
||||||
.getMetadata()
|
.getMetadata()
|
||||||
.setState(returnSync.getMessages().get(j).getMetadata().getState());
|
.getMessageId()) {
|
||||||
}
|
logger.info("Message with ID: "
|
||||||
}
|
+ getChats().get(j).getModel().get(k).getMetadata().getMessageId()
|
||||||
}
|
+ "was selected.");
|
||||||
}
|
getChats().get(j)
|
||||||
|
.getModel()
|
||||||
}
|
.get(k)
|
||||||
|
.getMetadata()
|
||||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
.setState(returnSync.getMessages().get(i).getMetadata().getState());
|
||||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) {
|
logger.info("Message State is now: "
|
||||||
// Update local Messages to state READ
|
+ getChats().get(j).getModel().get(k).getMetadata().getState().toString());
|
||||||
logger.info("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId()
|
}
|
||||||
+ "was initialized to be set to READ in localDB.");
|
}
|
||||||
for (int j = 0; j < getChats().size(); j++) {
|
}
|
||||||
if (getChats().get(j)
|
}
|
||||||
.getRecipient()
|
}
|
||||||
.getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
|
}
|
||||||
logger.info("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected.");
|
|
||||||
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
// Updating UserStatus of all users in LocalDB
|
||||||
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync
|
for (int j = 0; j < returnSync.getUsers().size(); j++) {
|
||||||
.getMessages()
|
for (int k = 0; k < getChats().size(); k++) {
|
||||||
.get(i)
|
if (getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) {
|
||||||
.getMetadata()
|
|
||||||
.getMessageId()) {
|
getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus());
|
||||||
logger.info("Message with ID: "
|
logger.info(getChats().get(k).getRecipient().getStatus().toString());
|
||||||
+ getChats().get(j).getModel().get(k).getMetadata().getMessageId()
|
}
|
||||||
+ "was selected.");
|
}
|
||||||
getChats().get(j)
|
}
|
||||||
.getModel()
|
|
||||||
.get(k)
|
sync.getMessages().clear();
|
||||||
.getMetadata()
|
sync.getUsers().clear();
|
||||||
.setState(returnSync.getMessages().get(i).getMetadata().getState());
|
}
|
||||||
logger.info("Message State is now: "
|
|
||||||
+ getChats().get(j).getModel().get(k).getMetadata().getState().toString());
|
/**
|
||||||
}
|
* Adds the unread messages returned from the server in the latest sync to the
|
||||||
}
|
* right chats in the LocalDB.
|
||||||
}
|
*
|
||||||
}
|
* @param localDB
|
||||||
|
* @since Envoy v0.1-alpha
|
||||||
}
|
*/
|
||||||
}
|
public void addUnreadMessagesToLocalDB() {
|
||||||
|
Sync unreadMessages = unreadMessagesSync;
|
||||||
// Updating UserStatus of all users in LocalDB
|
for (int i = 0; i < unreadMessages.getMessages().size(); i++)
|
||||||
for (int j = 0; j < returnSync.getUsers().size(); j++) {
|
for (int j = 0; j < getChats().size(); j++)
|
||||||
for (int k = 0; k < getChats().size(); k++) {
|
if (getChats().get(j).getRecipient().getID() == unreadMessages.getMessages().get(i).getMetadata().getSender()) {
|
||||||
if (getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) {
|
getChats().get(j).appendMessage(unreadMessages.getMessages().get(i));
|
||||||
|
}
|
||||||
getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus());
|
}
|
||||||
logger.info(getChats().get(k).getRecipient().getStatus().toString());
|
|
||||||
}
|
/**
|
||||||
}
|
* Changes all messages with state {@code RECEIVED} of a specific chat to state
|
||||||
}
|
* {@code READ}.
|
||||||
|
* <br>
|
||||||
sync.getMessages().clear();
|
* Adds these messages to the {@code readMessages} {@link Sync} object.
|
||||||
sync.getUsers().clear();
|
*
|
||||||
|
* @param currentChat
|
||||||
}
|
* @since Envoy v0.1-alpha
|
||||||
|
*/
|
||||||
/**
|
public void setMessagesToRead(Chat currentChat) {
|
||||||
* Adds a message to the "sync" Sync object.
|
for (int i = currentChat.getModel().size() - 1; i >= 0; --i)
|
||||||
*
|
if (currentChat.getModel().get(i).getMetadata().getRecipient() != currentChat.getRecipient().getID())
|
||||||
* @param message
|
if (currentChat.getModel().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
||||||
* @since Envoy v0.1-alpha
|
currentChat.getModel().get(i).getMetadata().setState(MessageState.READ);
|
||||||
*/
|
readMessages.getMessages().add(currentChat.getModel().get(i));
|
||||||
public void addMessageToSync(Message message) { sync.getMessages().add(message); }
|
} else break;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Adds a user to the {@code sync} {@link Sync} object.
|
/**
|
||||||
*
|
* Adds all messages with state {@code WAITING} from the {@link LocalDB} to the
|
||||||
* @param user
|
* {@link Sync} object.
|
||||||
* @since Envoy v0.1-alpha
|
*
|
||||||
*/
|
* @since Envoy v0.1-alpha
|
||||||
public void addUserToSync(User user) { sync.getUsers().add(user); }
|
*/
|
||||||
|
private void addWaitingMessagesToSync() {
|
||||||
/**
|
for (Chat chat : getChats())
|
||||||
* Adds the unread messages returned from the server in the latest sync to the
|
for (int i = 0; i < chat.getModel().size(); i++)
|
||||||
* right chats in the LocalDB.
|
if (chat.getModel().get(i).getMetadata().getState() == MessageState.WAITING) {
|
||||||
*
|
// addMessageToSync(localDB.getChats().get(i).getModel().get(j));
|
||||||
* @param localDB
|
logger.info("Got Waiting Message");
|
||||||
* @since Envoy v0.1-alpha
|
sync.getMessages().add(chat.getModel().get(i));
|
||||||
*/
|
}
|
||||||
public void addUnreadMessagesToLocalDB() {
|
}
|
||||||
Sync unreadMessages = unreadMessagesSync;
|
|
||||||
for (int i = 0; i < unreadMessages.getMessages().size(); i++)
|
/**
|
||||||
for (int j = 0; j < getChats().size(); j++)
|
* Clears the {@code unreadMessagesSync} {@link Sync} object.
|
||||||
if (getChats().get(j)
|
*
|
||||||
.getRecipient()
|
* @since Envoy v0.1-alpha
|
||||||
.getID() == unreadMessages.getMessages().get(i).getMetadata().getSender()) {
|
*/
|
||||||
getChats().get(j).appendMessage(unreadMessages.getMessages().get(i));
|
public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); }
|
||||||
}
|
|
||||||
}
|
/**
|
||||||
|
* @return all saves {@link Chat} objects that list the client user as the
|
||||||
/**
|
* client
|
||||||
* Gets all messages with state SENT from the LocalDB and adds them to the
|
* @since Envoy v0.1-alpha
|
||||||
* "sync" Sync object.
|
**/
|
||||||
*
|
public List<Chat> getChats() { return chats; }
|
||||||
* @param localDB
|
|
||||||
* @since Envoy v0.1-alpha
|
/**
|
||||||
*/
|
* @return the {@link User} who initialized the local database
|
||||||
public void getSentStateMessagesFromLocalDB() {
|
* @since Envoy v0.1-alpha
|
||||||
for (int i = 0; i < getChats().size(); i++) {
|
*/
|
||||||
for (int j = 0; j < getChats().get(i).getModel().getSize(); j++) {
|
public User getUser() { return sender; }
|
||||||
if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.SENT) {
|
}
|
||||||
addMessageToSync(getChats().get(i).getModel().get(j));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Changes all messages with State RECEIVED of a specific chat to State READ.
|
|
||||||
* <br>
|
|
||||||
* Adds these Messages to the {@code readMessages} {@link Sync} object.
|
|
||||||
*
|
|
||||||
* @param currentChat
|
|
||||||
* @since Envoy v0.1-alpha
|
|
||||||
*/
|
|
||||||
public void setMessagesToRead(Chat currentChat) {
|
|
||||||
for (int i = currentChat.getModel().size() - 1; i >= 0; --i)
|
|
||||||
if (currentChat.getModel().get(i).getMetadata().getRecipient() != currentChat.getRecipient().getID())
|
|
||||||
if (currentChat.getModel().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
|
||||||
currentChat.getModel().get(i).getMetadata().setState(MessageState.READ);
|
|
||||||
readMessages.getMessages().add(currentChat.getModel().get(i));
|
|
||||||
} else break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a message with State WAITING to a specific chat in the LocalDB.
|
|
||||||
*
|
|
||||||
* @param message
|
|
||||||
* @param currentChat
|
|
||||||
* @since Envoy v0.1-alpha
|
|
||||||
*/
|
|
||||||
public void addWaitingMessageToLocalDB(Message message, Chat currentChat) { currentChat.appendMessage(message); }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds all messages with State WAITING from the {@link LocalDB} to the Sync.
|
|
||||||
*
|
|
||||||
* @param localDB
|
|
||||||
* @since Envoy v0.1-alpha
|
|
||||||
*/
|
|
||||||
public void addWaitingMessagesToSync() {
|
|
||||||
for (int i = 0; i < getChats().size(); i++) {
|
|
||||||
for (int j = 0; j < getChats().get(i).getModel().getSize(); j++) {
|
|
||||||
if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.WAITING) {
|
|
||||||
// addMessageToSync(localDB.getChats().get(i).getModel().get(j));
|
|
||||||
logger.info("Got Waiting Message");
|
|
||||||
sync.getMessages().add(0, getChats().get(i).getModel().get(j));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clears the {@code unreadMessagesSync} {@link Sync} object.
|
|
||||||
*
|
|
||||||
* @since Envoy v0.1-alpha
|
|
||||||
*/
|
|
||||||
public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return all saves {@link Chat} objects that list the client user as the
|
|
||||||
* client
|
|
||||||
* @since Envoy v0.1-alpha
|
|
||||||
**/
|
|
||||||
public List<Chat> getChats() { return chats; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the User who initialised the local Database
|
|
||||||
* @since Envoy v0.1-alpha
|
|
||||||
*/
|
|
||||||
public User getUser() { return sender; }
|
|
||||||
}
|
|
||||||
|
@ -226,7 +226,7 @@ public class ChatWindow extends JFrame {
|
|||||||
.get();
|
.get();
|
||||||
|
|
||||||
// Set all unread messages in the chat to read
|
// Set all unread messages in the chat to read
|
||||||
if (currentChat != null) { localDB.setMessagesToRead(currentChat); }
|
readCurrentChat();
|
||||||
|
|
||||||
client.setRecipient(user);
|
client.setRecipient(user);
|
||||||
|
|
||||||
@ -272,7 +272,7 @@ public class ChatWindow extends JFrame {
|
|||||||
|
|
||||||
// Create and send message object
|
// Create and send message object
|
||||||
final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient());
|
final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient());
|
||||||
localDB.addWaitingMessageToLocalDB(message, currentChat);
|
currentChat.appendMessage(message);
|
||||||
messageList.setModel(currentChat.getModel());
|
messageList.setModel(currentChat.getModel());
|
||||||
|
|
||||||
// Clear text field
|
// Clear text field
|
||||||
@ -309,7 +309,8 @@ public class ChatWindow extends JFrame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the data model and the ui every x seconds.
|
* Updates the data model and the UI repeatedly after a certain amount of
|
||||||
|
* time.
|
||||||
*
|
*
|
||||||
* @param timeout the amount of time that passes between two requests sent to
|
* @param timeout the amount of time that passes between two requests sent to
|
||||||
* the server
|
* the server
|
||||||
@ -322,9 +323,14 @@ public class ChatWindow extends JFrame {
|
|||||||
// Synchronize
|
// Synchronize
|
||||||
localDB.applySync(
|
localDB.applySync(
|
||||||
client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID())));
|
client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID())));
|
||||||
|
|
||||||
|
// Process unread messages
|
||||||
localDB.addUnreadMessagesToLocalDB();
|
localDB.addUnreadMessagesToLocalDB();
|
||||||
localDB.clearUnreadMessagesSync();
|
localDB.clearUnreadMessagesSync();
|
||||||
|
|
||||||
|
// Mark unread messages as read when they are in the current chat
|
||||||
|
readCurrentChat();
|
||||||
|
|
||||||
// Update UI
|
// Update UI
|
||||||
SwingUtilities
|
SwingUtilities
|
||||||
.invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); });
|
.invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); });
|
||||||
@ -338,4 +344,9 @@ public class ChatWindow extends JFrame {
|
|||||||
if (userList.getModel().getElementAt(i).getID() == localDB.getChats().get(j).getRecipient().getID())
|
if (userList.getModel().getElementAt(i).getID() == localDB.getChats().get(j).getRecipient().getID())
|
||||||
userList.getModel().getElementAt(i).setStatus(localDB.getChats().get(j).getRecipient().getStatus());
|
userList.getModel().getElementAt(i).setStatus(localDB.getChats().get(j).getRecipient().getStatus());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* Marks messages in the current chat as {@code READ}.
|
||||||
|
*/
|
||||||
|
private void readCurrentChat() { if (currentChat != null) { localDB.setMessagesToRead(currentChat); } }
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user