parent
329339f05c
commit
73eb312a90
@ -30,12 +30,17 @@ import envoy.schema.User;
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public class LocalDB {
|
||||
|
||||
private File localDB;
|
||||
private User sender;
|
||||
private List<Chat> chats = new ArrayList<>();
|
||||
private ObjectFactory objectFactory = new ObjectFactory();
|
||||
private DatatypeFactory datatypeFactory;
|
||||
|
||||
private Sync unreadMessagesSync = objectFactory.createSync();
|
||||
private Sync sync = objectFactory.createSync();
|
||||
private Sync readMessages = objectFactory.createSync();
|
||||
|
||||
/**
|
||||
* Constructs an empty local database.
|
||||
*
|
||||
@ -52,7 +57,6 @@ public class LocalDB {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initializes the local database and fills it with values
|
||||
* if the user has already sent or received messages.
|
||||
@ -62,8 +66,8 @@ public class LocalDB {
|
||||
* @since Envoy v0.1-alpha
|
||||
**/
|
||||
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()));
|
||||
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");
|
||||
if (localDB.exists()) loadFromLocalDB();
|
||||
}
|
||||
@ -130,12 +134,7 @@ public class LocalDB {
|
||||
return message;
|
||||
}
|
||||
|
||||
private Sync unreadMessagesSync = objectFactory.createSync();
|
||||
public Sync sync = objectFactory.createSync();
|
||||
public Sync readMessages = objectFactory.createSync();
|
||||
|
||||
public Sync fillSync(long userId) {
|
||||
|
||||
addWaitingMessagesToSync();
|
||||
|
||||
getSentStateMessagesFromLocalDB();
|
||||
@ -156,47 +155,32 @@ public class LocalDB {
|
||||
// SENT)
|
||||
for (int j = 0; j < sync.getMessages().size(); j++) {
|
||||
if (j == i) {
|
||||
sync.getMessages()
|
||||
.get(j)
|
||||
.getMetadata()
|
||||
.setMessageId(returnSync.getMessages().get(j).getMetadata().getMessageId());
|
||||
sync.getMessages()
|
||||
.get(j)
|
||||
.getMetadata()
|
||||
.setState(returnSync.getMessages().get(j).getMetadata().getState());
|
||||
sync.getMessages().get(j).getMetadata().setMessageId(returnSync.getMessages().get(j).getMetadata().getMessageId());
|
||||
sync.getMessages().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
|
||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() != 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
||||
// these are the unread Messages from the server
|
||||
unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i));
|
||||
|
||||
}
|
||||
|
||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getSender() == 0
|
||||
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()) {
|
||||
if (getChats().get(j).getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
|
||||
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
||||
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync
|
||||
.getMessages()
|
||||
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages()
|
||||
.get(i)
|
||||
.getMetadata()
|
||||
.getMessageId()) {
|
||||
// Update Message in LocalDB
|
||||
getChats().get(j)
|
||||
.getModel()
|
||||
.get(k)
|
||||
.getMetadata()
|
||||
.setState(returnSync.getMessages().get(j).getMetadata().getState());
|
||||
getChats().get(j).getModel().get(k).getMetadata().setState(returnSync.getMessages().get(j).getMetadata().getState());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -210,26 +194,18 @@ public class LocalDB {
|
||||
System.out.println("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()) {
|
||||
if (getChats().get(j).getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
|
||||
System.out.println("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected.");
|
||||
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
||||
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync
|
||||
.getMessages()
|
||||
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages()
|
||||
.get(i)
|
||||
.getMetadata()
|
||||
.getMessageId()) {
|
||||
System.out.println("Message with ID: "
|
||||
+ getChats().get(j).getModel().get(k).getMetadata().getMessageId()
|
||||
+ "was selected.");
|
||||
getChats().get(j)
|
||||
.getModel()
|
||||
.get(k)
|
||||
.getMetadata()
|
||||
.setState(returnSync.getMessages().get(i).getMetadata().getState());
|
||||
System.out.println("Message State is now: "
|
||||
+ getChats().get(j).getModel().get(k).getMetadata().getState().toString());
|
||||
System.out.println(
|
||||
"Message with ID: " + getChats().get(j).getModel().get(k).getMetadata().getMessageId() + "was selected.");
|
||||
getChats().get(j).getModel().get(k).getMetadata().setState(returnSync.getMessages().get(i).getMetadata().getState());
|
||||
System.out
|
||||
.println("Message State is now: " + getChats().get(j).getModel().get(k).getMetadata().getState().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -260,15 +236,7 @@ public class LocalDB {
|
||||
* @param message
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void addMessageToSync(Message message) { sync.getMessages().add(message); }
|
||||
|
||||
/**
|
||||
* Adds a user to the {@code sync} {@link Sync} object.
|
||||
*
|
||||
* @param user
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void addUserToSync(User user) { sync.getUsers().add(user); }
|
||||
private void addMessageToSync(Message message) { sync.getMessages().add(message); }
|
||||
|
||||
/**
|
||||
* Adds the unread messages returned from the server in the latest sync to the
|
||||
@ -281,21 +249,19 @@ public class LocalDB {
|
||||
Sync unreadMessages = unreadMessagesSync;
|
||||
for (int i = 0; i < unreadMessages.getMessages().size(); i++)
|
||||
for (int j = 0; j < getChats().size(); j++)
|
||||
if (getChats().get(j)
|
||||
.getRecipient()
|
||||
.getID() == unreadMessages.getMessages().get(i).getMetadata().getSender()) {
|
||||
if (getChats().get(j).getRecipient().getID() == unreadMessages.getMessages().get(i).getMetadata().getSender()) {
|
||||
getChats().get(j).appendMessage(unreadMessages.getMessages().get(i));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all messages with state SENT from the LocalDB and adds them to the
|
||||
* "sync" Sync object.
|
||||
* Gets all messages with state {@code SENT} from the LocalDB and adds them to
|
||||
* the {@code sync} {@link Sync} object.
|
||||
*
|
||||
* @param localDB
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void getSentStateMessagesFromLocalDB() {
|
||||
private void getSentStateMessagesFromLocalDB() {
|
||||
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.SENT) {
|
||||
@ -306,9 +272,10 @@ public class LocalDB {
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes all messages with State RECEIVED of a specific chat to State READ.
|
||||
* Changes all messages with state {@code RECEIVED} of a specific chat to state
|
||||
* {@code READ}.
|
||||
* <br>
|
||||
* Adds these Messages to the {@code readMessages} {@link Sync} object.
|
||||
* Adds these messages to the {@code readMessages} {@link Sync} object.
|
||||
*
|
||||
* @param currentChat
|
||||
* @since Envoy v0.1-alpha
|
||||
@ -337,7 +304,7 @@ public class LocalDB {
|
||||
* @param localDB
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void addWaitingMessagesToSync() {
|
||||
private 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) {
|
||||
@ -364,7 +331,7 @@ public class LocalDB {
|
||||
public List<Chat> getChats() { return chats; }
|
||||
|
||||
/**
|
||||
* @return the User who initialised the local Database
|
||||
* @return the {@link User} who initialized the local database
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public User getUser() { return sender; }
|
||||
|
@ -223,7 +223,7 @@ public class ChatWindow extends JFrame {
|
||||
.get();
|
||||
|
||||
// Set all unread messages in the chat to read
|
||||
if (currentChat != null) { localDB.setMessagesToRead(currentChat); }
|
||||
readCurrentChat();
|
||||
|
||||
client.setRecipient(user);
|
||||
|
||||
@ -319,9 +319,14 @@ public class ChatWindow extends JFrame {
|
||||
// Synchronize
|
||||
localDB.applySync(
|
||||
client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID())));
|
||||
|
||||
// Process unread messages
|
||||
localDB.addUnreadMessagesToLocalDB();
|
||||
localDB.clearUnreadMessagesSync();
|
||||
|
||||
// Mark unread messages as read when they are in the current chat
|
||||
readCurrentChat();
|
||||
|
||||
// Update UI
|
||||
SwingUtilities
|
||||
.invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); });
|
||||
@ -335,4 +340,9 @@ public class ChatWindow extends JFrame {
|
||||
if (userList.getModel().getElementAt(i).getID() == localDB.getChats().get(j).getRecipient().getID())
|
||||
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