2020-02-05 20:58:30 +01:00
|
|
|
package envoy.client.data;
|
2020-02-03 21:52:48 +01:00
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
2020-04-02 09:23:47 +02:00
|
|
|
import envoy.data.*;
|
2020-06-20 10:00:38 +02:00
|
|
|
import envoy.event.GroupResize;
|
|
|
|
import envoy.event.MessageStatusChange;
|
|
|
|
import envoy.event.NameChange;
|
2020-02-03 21:52:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores information about the current {@link User} and their {@link Chat}s.
|
2020-06-26 23:36:14 +02:00
|
|
|
* For message ID generation a {@link IDGenerator} is stored as well.
|
|
|
|
* <p>
|
2020-02-03 21:52:48 +01:00
|
|
|
* Project: <strong>envoy-client</strong><br>
|
2020-03-24 18:38:47 +01:00
|
|
|
* File: <strong>LocalDB.java</strong><br>
|
2020-02-03 21:52:48 +01:00
|
|
|
* Created: <strong>3 Feb 2020</strong><br>
|
|
|
|
*
|
|
|
|
* @author Kai S. K. Engelbart
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
2020-03-24 18:38:47 +01:00
|
|
|
public abstract class LocalDB {
|
2020-02-03 21:52:48 +01:00
|
|
|
|
2020-06-26 09:08:41 +02:00
|
|
|
protected User user;
|
|
|
|
protected Map<String, Contact> users = new HashMap<>();
|
|
|
|
protected List<Chat> chats = new ArrayList<>();
|
|
|
|
protected IDGenerator idGenerator;
|
|
|
|
protected Cache<Message> messageCache = new Cache<>();
|
2020-06-20 10:00:38 +02:00
|
|
|
protected Cache<MessageStatusChange> statusCache = new Cache<>();
|
2020-02-03 21:52:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a storage space for a user-specific list of chats.
|
|
|
|
*
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
|
|
|
public void initializeUserStorage() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores all users. If the client user is specified, their chats will be stored
|
|
|
|
* as well. The message id generator will also be saved if present.
|
|
|
|
*
|
|
|
|
* @throws Exception if the saving process failed
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
|
|
|
public void save() throws Exception {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads all user data.
|
|
|
|
*
|
|
|
|
* @throws Exception if the loading process failed
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
|
|
|
public void loadUsers() throws Exception {}
|
|
|
|
|
|
|
|
/**
|
2020-02-06 21:42:17 +01:00
|
|
|
* Loads all data of the client user.
|
2020-02-03 21:52:48 +01:00
|
|
|
*
|
|
|
|
* @throws Exception if the loading process failed
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
2020-02-06 21:42:17 +01:00
|
|
|
public void loadUserData() throws Exception {}
|
2020-02-03 21:52:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the ID generator. Any exception thrown during this process is ignored.
|
|
|
|
*
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
2020-03-26 16:06:18 +01:00
|
|
|
public void loadIDGenerator() {}
|
2020-02-03 21:52:48 +01:00
|
|
|
|
2020-06-26 09:08:41 +02:00
|
|
|
/**
|
|
|
|
* Synchronizes the contact list of the client user with the chat and user
|
|
|
|
* storage.
|
|
|
|
*
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
|
|
|
public void synchronize() {
|
2020-06-26 16:37:00 +02:00
|
|
|
user.getContacts().stream().filter(u -> u instanceof User && !users.containsKey(u.getName())).forEach(u -> users.put(u.getName(), u));
|
2020-06-26 09:08:41 +02:00
|
|
|
users.put(user.getName(), user);
|
|
|
|
|
|
|
|
// Synchronize user status data
|
|
|
|
for (Contact contact : users.values())
|
|
|
|
if (contact instanceof User)
|
|
|
|
getChat(contact.getID()).ifPresent(chat -> { ((User) chat.getRecipient()).setStatus(((User) contact).getStatus()); });
|
|
|
|
|
|
|
|
// Create missing chats
|
2020-07-03 13:47:40 +02:00
|
|
|
user.getContacts().stream().filter(u -> !u.equals(user) && getChat(u.getID()).isEmpty()).map(u -> new Chat(u, user)).forEach(chats::add);
|
2020-06-26 09:08:41 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 21:52:48 +01:00
|
|
|
/**
|
|
|
|
* @return a {@code Map<String, User>} of all users stored locally with their
|
|
|
|
* user names as keys
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
2020-04-02 22:03:43 +02:00
|
|
|
public Map<String, Contact> getUsers() { return users; }
|
2020-02-03 21:52:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return all saved {@link Chat} objects that list the client user as the
|
|
|
|
* sender
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.1-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
**/
|
|
|
|
public List<Chat> getChats() { return chats; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param chats the chats to set
|
|
|
|
*/
|
|
|
|
public void setChats(List<Chat> chats) { this.chats = chats; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the {@link User} who initialized the local database
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
|
|
|
public User getUser() { return user; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param user the user to set
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.2-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
|
|
|
public void setUser(User user) { this.user = user; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the message ID generator
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
2020-03-26 16:06:18 +01:00
|
|
|
public IDGenerator getIDGenerator() { return idGenerator; }
|
2020-02-03 21:52:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param idGenerator the message ID generator to set
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
2020-03-26 16:06:18 +01:00
|
|
|
public void setIDGenerator(IDGenerator idGenerator) { this.idGenerator = idGenerator; }
|
2020-02-03 21:52:48 +01:00
|
|
|
|
|
|
|
/**
|
2020-03-26 16:06:18 +01:00
|
|
|
* @return {@code true} if an {@link IDGenerator} is present
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-03 21:52:48 +01:00
|
|
|
*/
|
2020-03-26 16:06:18 +01:00
|
|
|
public boolean hasIDGenerator() { return idGenerator != null; }
|
2020-02-06 21:03:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the offline message cache
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-06 21:03:08 +01:00
|
|
|
*/
|
|
|
|
public Cache<Message> getMessageCache() { return messageCache; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param messageCache the offline message cache to set
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-06 21:03:08 +01:00
|
|
|
*/
|
|
|
|
public void setMessageCache(Cache<Message> messageCache) { this.messageCache = messageCache; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the offline status cache
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-06 21:03:08 +01:00
|
|
|
*/
|
2020-06-20 10:00:38 +02:00
|
|
|
public Cache<MessageStatusChange> getStatusCache() { return statusCache; }
|
2020-02-06 21:03:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param statusCache the offline status cache to set
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.3-alpha
|
2020-02-06 21:03:08 +01:00
|
|
|
*/
|
2020-06-20 10:00:38 +02:00
|
|
|
public void setStatusCache(Cache<MessageStatusChange> statusCache) { this.statusCache = statusCache; }
|
2020-03-14 16:58:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Searches for a message by ID.
|
2020-03-26 16:06:18 +01:00
|
|
|
*
|
2020-03-14 16:58:19 +01:00
|
|
|
* @param id the ID of the message to search for
|
2020-06-10 13:05:44 +02:00
|
|
|
* @return an optional containing the message
|
2020-03-23 21:52:33 +01:00
|
|
|
* @since Envoy Client v0.1-beta
|
2020-03-14 16:58:19 +01:00
|
|
|
*/
|
2020-06-10 13:05:44 +02:00
|
|
|
public Optional<Message> getMessage(long id) {
|
|
|
|
return chats.stream().map(Chat::getMessages).flatMap(List::stream).filter(m -> m.getID() == id).findAny();
|
|
|
|
}
|
2020-06-26 09:08:41 +02:00
|
|
|
|
2020-06-10 13:05:44 +02:00
|
|
|
/**
|
|
|
|
* Searches for a chat by recipient ID.
|
|
|
|
*
|
|
|
|
* @param recipientID the ID of the chat's recipient
|
|
|
|
* @return an optional containing the chat
|
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
2020-06-26 09:08:41 +02:00
|
|
|
public Optional<Chat> getChat(long recipientID) { return chats.stream().filter(c -> c.getRecipient().getID() == recipientID).findAny(); }
|
2020-04-02 09:23:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a contact name change if the corresponding contact is present.
|
|
|
|
*
|
2020-06-20 10:00:38 +02:00
|
|
|
* @param event the {@link NameChange} to process
|
2020-04-02 09:23:47 +02:00
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
2020-06-20 10:00:38 +02:00
|
|
|
public void replaceContactName(NameChange event) {
|
2020-04-02 22:03:43 +02:00
|
|
|
chats.stream().map(Chat::getRecipient).filter(c -> c.getID() == event.getID()).findAny().ifPresent(c -> c.setName(event.get()));
|
2020-04-02 09:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a group resize operation if the corresponding group is present.
|
|
|
|
*
|
2020-06-20 10:00:38 +02:00
|
|
|
* @param event the {@link GroupResize} to process
|
2020-04-02 09:23:47 +02:00
|
|
|
* @since Envoy Client v0.1-beta
|
|
|
|
*/
|
2020-06-20 10:00:38 +02:00
|
|
|
public void updateGroup(GroupResize event) {
|
2020-04-02 09:23:47 +02:00
|
|
|
chats.stream()
|
|
|
|
.map(Chat::getRecipient)
|
|
|
|
.filter(Group.class::isInstance)
|
|
|
|
.filter(g -> g.getID() == event.getGroupID() && g.getID() != user.getID())
|
|
|
|
.map(Group.class::cast)
|
|
|
|
.findAny()
|
|
|
|
.ifPresent(group -> {
|
|
|
|
switch (event.getOperation()) {
|
|
|
|
case ADD:
|
2020-04-02 22:03:43 +02:00
|
|
|
group.getContacts().add(event.get());
|
2020-04-02 09:23:47 +02:00
|
|
|
break;
|
|
|
|
case REMOVE:
|
2020-04-02 22:03:43 +02:00
|
|
|
group.getContacts().remove(event.get());
|
2020-04-02 09:23:47 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-02-03 21:52:48 +01:00
|
|
|
}
|