Merge pull request #32 from informatik-ag-ngl/f/logging
Added proper logging
This commit is contained in:
commit
aed4a6a457
@ -1,5 +1,7 @@
|
||||
package envoy.client;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
import javax.ws.rs.client.Entity;
|
||||
import javax.ws.rs.client.WebTarget;
|
||||
@ -28,16 +30,18 @@ public class Client {
|
||||
private Config config;
|
||||
private User sender, recipient;
|
||||
|
||||
private static final Logger logger = Logger.getLogger(Client.class.getSimpleName());
|
||||
|
||||
public Client(Config config, String username) {
|
||||
this.config = config;
|
||||
sender = getUser(username);
|
||||
System.out.println("ID: " + sender.getID());
|
||||
|
||||
logger.info("ID: " + sender.getID());
|
||||
}
|
||||
|
||||
private <T, R> R post(String uri, T body, Class<R> responseBodyClass) {
|
||||
javax.ws.rs.client.Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client.target(uri);
|
||||
|
||||
Response response = target.request().post(Entity.entity(body, "application/xml"));
|
||||
R responseBody = response.readEntity(responseBodyClass);
|
||||
response.close();
|
||||
@ -92,7 +96,7 @@ public class Client {
|
||||
if (returnSenderSync.getUsers().size() == 1) {
|
||||
returnSender = returnSenderSync.getUsers().get(0);
|
||||
} else {
|
||||
System.out.println("ERROR exiting...");
|
||||
logger.warning("ERROR exiting...");
|
||||
}
|
||||
|
||||
return returnSender;
|
||||
|
@ -1,293 +1,305 @@
|
||||
package envoy.client;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
|
||||
import envoy.exception.EnvoyException;
|
||||
import envoy.schema.Message;
|
||||
import envoy.schema.Message.Metadata.MessageState;
|
||||
import envoy.schema.ObjectFactory;
|
||||
import envoy.schema.Sync;
|
||||
import envoy.schema.User;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-client</strong><br>
|
||||
* File: <strong>LocalDB.java</strong><br>
|
||||
* Created: <strong>27.10.2019</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @author Maximilian Käfer
|
||||
* @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.
|
||||
*
|
||||
* @param client the user that is logged in with this client
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
|
||||
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.
|
||||
* @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()));
|
||||
localDB = new File(localDBDir, sender.getID() + ".db");
|
||||
if (localDB.exists()) loadFromLocalDB();
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the database into a file for future use.
|
||||
*
|
||||
* @throws IOException if something went wrong during saving
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void saveToLocalDB() {
|
||||
try {
|
||||
localDB.getParentFile().mkdirs();
|
||||
localDB.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("unable to save the messages");
|
||||
}
|
||||
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) {
|
||||
out.writeObject(chats);
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
System.err.println("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
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void loadFromLocalDB() throws EnvoyException {
|
||||
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
|
||||
*/
|
||||
public Message createMessage(String textContent, User recipient) {
|
||||
Message.Metadata metaData = objectFactory.createMessageMetadata();
|
||||
metaData.setSender(sender.getID());
|
||||
metaData.setRecipient(recipient.getID());
|
||||
metaData.setState(MessageState.WAITING);
|
||||
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
|
||||
|
||||
Message.Content content = objectFactory.createMessageContent();
|
||||
content.setType("text");
|
||||
content.setText(textContent);
|
||||
|
||||
Message message = objectFactory.createMessage();
|
||||
message.setMetadata(metaData);
|
||||
message.getContent().add(content);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
public Sync fillSync(long userId) {
|
||||
addWaitingMessagesToSync();
|
||||
|
||||
sync.getMessages().addAll(readMessages.getMessages());
|
||||
readMessages.getMessages().clear();
|
||||
|
||||
System.out.println(sync.getMessages().size());
|
||||
return sync;
|
||||
}
|
||||
|
||||
public void applySync(Sync returnSync) {
|
||||
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) {
|
||||
// Update Local Messages with State WAITING (add Message ID and change State to
|
||||
// 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
&& 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 (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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) {
|
||||
// Update local Messages to state READ
|
||||
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()) {
|
||||
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()
|
||||
.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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Updating UserStatus of all users in LocalDB
|
||||
for (int j = 0; j < returnSync.getUsers().size(); j++) {
|
||||
for (int k = 0; k < getChats().size(); k++) {
|
||||
if (getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) {
|
||||
|
||||
getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus());
|
||||
System.out.println(getChats().get(k).getRecipient().getStatus().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sync.getMessages().clear();
|
||||
sync.getUsers().clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
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()) {
|
||||
getChats().get(j).appendMessage(unreadMessages.getMessages().get(i));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @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 all messages with state {@code WAITING} from the {@link LocalDB} to the
|
||||
* {@link Sync} object.
|
||||
*
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
private void addWaitingMessagesToSync() {
|
||||
for (Chat chat : getChats())
|
||||
for (int i = 0; i < chat.getModel().size(); i++)
|
||||
if (chat.getModel().get(i).getMetadata().getState() == MessageState.WAITING) {
|
||||
System.out.println("Got Waiting Message");
|
||||
sync.getMessages().add(chat.getModel().get(i));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {@link User} who initialized the local database
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public User getUser() { return sender; }
|
||||
}
|
||||
package envoy.client;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
|
||||
import envoy.exception.EnvoyException;
|
||||
import envoy.schema.Message;
|
||||
import envoy.schema.Message.Metadata.MessageState;
|
||||
import envoy.schema.ObjectFactory;
|
||||
import envoy.schema.Sync;
|
||||
import envoy.schema.User;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-client</strong><br>
|
||||
* File: <strong>LocalDB.java</strong><br>
|
||||
* Created: <strong>27.10.2019</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @author Maximilian Käfer
|
||||
* @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 static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName());
|
||||
|
||||
private Sync unreadMessagesSync = objectFactory.createSync();
|
||||
private Sync sync = objectFactory.createSync();
|
||||
private Sync readMessages = objectFactory.createSync();
|
||||
|
||||
/**
|
||||
* Constructs an empty local database.
|
||||
*
|
||||
* @param sender the user that is logged in with this client
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
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.
|
||||
* @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()));
|
||||
localDB = new File(localDBDir, sender.getID() + ".db");
|
||||
if (localDB.exists()) loadFromLocalDB();
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the database into a file for future use.
|
||||
*
|
||||
* @throws IOException if something went wrong during saving
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void saveToLocalDB() {
|
||||
try {
|
||||
localDB.getParentFile().mkdirs();
|
||||
localDB.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.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
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void loadFromLocalDB() throws EnvoyException {
|
||||
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
|
||||
* @param recipient The recipient of the message
|
||||
* @return prepared {@link Message} object
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public Message createMessage(String textContent, User recipient) {
|
||||
Message.Metadata metaData = objectFactory.createMessageMetadata();
|
||||
metaData.setSender(sender.getID());
|
||||
metaData.setRecipient(recipient.getID());
|
||||
metaData.setState(MessageState.WAITING);
|
||||
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
|
||||
|
||||
Message.Content content = objectFactory.createMessageContent();
|
||||
content.setType("text");
|
||||
content.setText(textContent);
|
||||
|
||||
Message message = objectFactory.createMessage();
|
||||
message.setMetadata(metaData);
|
||||
message.getContent().add(content);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
public Sync fillSync(long userId) {
|
||||
addWaitingMessagesToSync();
|
||||
|
||||
sync.getMessages().addAll(readMessages.getMessages());
|
||||
readMessages.getMessages().clear();
|
||||
|
||||
logger.info(String.format("Filled sync with %d messages.", sync.getMessages().size()));
|
||||
return sync;
|
||||
}
|
||||
|
||||
public void applySync(Sync returnSync) {
|
||||
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) {
|
||||
// Update Local Messages with State WAITING (add Message ID and change State to
|
||||
// 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
&& 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 (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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) {
|
||||
// Update local Messages to state READ
|
||||
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++) {
|
||||
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages()
|
||||
.get(i)
|
||||
.getMetadata()
|
||||
.getMessageId()) {
|
||||
logger.info("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());
|
||||
logger.info("Message State is now: "
|
||||
+ getChats().get(j).getModel().get(k).getMetadata().getState().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Updating UserStatus of all users in LocalDB
|
||||
for (int j = 0; j < returnSync.getUsers().size(); j++) {
|
||||
for (int k = 0; k < getChats().size(); k++) {
|
||||
if (getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) {
|
||||
|
||||
getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus());
|
||||
logger.info(getChats().get(k).getRecipient().getStatus().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sync.getMessages().clear();
|
||||
sync.getUsers().clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
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()) {
|
||||
getChats().get(j).appendMessage(unreadMessages.getMessages().get(i));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @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 all messages with state {@code WAITING} from the {@link LocalDB} to the
|
||||
* {@link Sync} object.
|
||||
*
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
private void addWaitingMessagesToSync() {
|
||||
for (Chat chat : getChats())
|
||||
for (int i = 0; i < chat.getModel().size(); i++)
|
||||
if (chat.getModel().get(i).getMetadata().getState() == MessageState.WAITING) {
|
||||
// addMessageToSync(localDB.getChats().get(i).getModel().get(j));
|
||||
logger.info("Got Waiting Message");
|
||||
sync.getMessages().add(chat.getModel().get(i));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {@link User} who initialized the local database
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public User getUser() { return sender; }
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JButton;
|
||||
@ -56,6 +57,8 @@ public class ChatWindow extends JFrame {
|
||||
private Chat currentChat;
|
||||
|
||||
private JTextArea messageEnterTextArea;
|
||||
|
||||
private static final Logger logger = Logger.getLogger(ChatWindow.class.getSimpleName());
|
||||
|
||||
public ChatWindow(Client client, LocalDB localDB) {
|
||||
this.client = client;
|
||||
@ -186,7 +189,7 @@ public class ChatWindow extends JFrame {
|
||||
SettingsScreen.open(localDB.getUser().getName());
|
||||
} catch (Exception e) {
|
||||
SettingsScreen.open();
|
||||
System.err.println("An error occured while opening the settings screen: " + e);
|
||||
logger.warning("An error occured while opening the settings screen: " + e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
@ -3,6 +3,8 @@ package envoy.client.ui;
|
||||
import java.awt.EventQueue;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
@ -24,7 +26,11 @@ import envoy.exception.EnvoyException;
|
||||
*/
|
||||
public class Startup {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(Client.class.getSimpleName());
|
||||
|
||||
public static void main(String[] args) {
|
||||
logger.setLevel(Level.ALL);
|
||||
|
||||
Config config = Config.getInstance();
|
||||
|
||||
// Load the configuration from client.properties first
|
||||
@ -42,6 +48,7 @@ public class Startup {
|
||||
config.load(args);
|
||||
|
||||
if (!config.isInitialized()) {
|
||||
logger.warning("Server or port are not defined. Exiting...");
|
||||
JOptionPane.showMessageDialog(null, "Error loading configuration values.", "Configuration error",
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
System.exit(1);
|
||||
@ -49,7 +56,7 @@ public class Startup {
|
||||
|
||||
String userName = JOptionPane.showInputDialog("Please enter your username");
|
||||
if (userName == null || userName.isEmpty()) {
|
||||
System.err.println("User name is not set or empty. Exiting...");
|
||||
logger.warning("User name is not set or empty. Exiting...");
|
||||
System.exit(1);
|
||||
}
|
||||
Client client = new Client(config, userName);
|
||||
|
Reference in New Issue
Block a user