Added user list serialization to LocalDB
* Added user list to LocalDB * Removed client user from LocalDB constructor
This commit is contained in:
		@@ -34,9 +34,11 @@ import envoy.schema.User;
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
public class LocalDB {
 | 
					public class LocalDB {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private File			localDB;
 | 
						private File		localDBFile, usersFile;
 | 
				
			||||||
	private User			sender;
 | 
						private User		user;
 | 
				
			||||||
 | 
						private List<User>	users	= new ArrayList<>();
 | 
				
			||||||
	private List<Chat>	chats	= new ArrayList<>();
 | 
						private List<Chat>	chats	= new ArrayList<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private ObjectFactory	objectFactory	= new ObjectFactory();
 | 
						private ObjectFactory	objectFactory	= new ObjectFactory();
 | 
				
			||||||
	private DatatypeFactory	datatypeFactory;
 | 
						private DatatypeFactory	datatypeFactory;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -47,13 +49,12 @@ public class LocalDB {
 | 
				
			|||||||
	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.
 | 
						 * Constructs an empty local database. To serialize any chats to the file
 | 
				
			||||||
 | 
						 * system, call {@link LocalDB#initializeDBFile(File)}.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param sender the user that is logged in with this client
 | 
					 | 
				
			||||||
	 * @since Envoy v0.1-alpha
 | 
						 * @since Envoy v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public LocalDB(User sender) {
 | 
						public LocalDB() {
 | 
				
			||||||
		this.sender = sender;
 | 
					 | 
				
			||||||
		try {
 | 
							try {
 | 
				
			||||||
			datatypeFactory = DatatypeFactory.newInstance();
 | 
								datatypeFactory = DatatypeFactory.newInstance();
 | 
				
			||||||
		} catch (DatatypeConfigurationException e) {
 | 
							} catch (DatatypeConfigurationException e) {
 | 
				
			||||||
@@ -70,10 +71,17 @@ public class LocalDB {
 | 
				
			|||||||
	 * @since Envoy v0.1-alpha
 | 
						 * @since Envoy v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public void initializeDBFile(File localDBDir) throws EnvoyException {
 | 
						public void initializeDBFile(File localDBDir) throws EnvoyException {
 | 
				
			||||||
 | 
							if (user == null) throw new NullPointerException("Client user is null");
 | 
				
			||||||
		if (localDBDir.exists() && !localDBDir.isDirectory())
 | 
							if (localDBDir.exists() && !localDBDir.isDirectory())
 | 
				
			||||||
			throw new EnvoyException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
 | 
								throw new EnvoyException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
 | 
				
			||||||
		localDB = new File(localDBDir, sender.getID() + ".db");
 | 
							usersFile	= new File(localDBDir, "users.db");
 | 
				
			||||||
		if (localDB.exists()) loadFromLocalDB();
 | 
							localDBFile	= new File(localDBDir, user.getID() + ".db");
 | 
				
			||||||
 | 
							try {
 | 
				
			||||||
 | 
								loadUsers();
 | 
				
			||||||
 | 
								loadChats();
 | 
				
			||||||
 | 
							} catch (ClassNotFoundException | IOException e) {
 | 
				
			||||||
 | 
								throw new EnvoyException(e);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@@ -82,29 +90,46 @@ public class LocalDB {
 | 
				
			|||||||
	 * @throws IOException if something went wrong during saving
 | 
						 * @throws IOException if something went wrong during saving
 | 
				
			||||||
	 * @since Envoy v0.1-alpha
 | 
						 * @since Envoy v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public void saveToLocalDB() throws IOException {
 | 
						public void save() throws IOException {
 | 
				
			||||||
		localDB.getParentFile().mkdirs();
 | 
							// Save users
 | 
				
			||||||
		localDB.createNewFile();
 | 
							write(usersFile, users);
 | 
				
			||||||
		try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) {
 | 
					
 | 
				
			||||||
			out.writeObject(chats);
 | 
							// Save chats
 | 
				
			||||||
		} catch (IOException ex) {
 | 
							write(localDBFile, chats);
 | 
				
			||||||
			throw ex;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@SuppressWarnings("unchecked")
 | 
				
			||||||
 | 
						private void loadUsers() throws ClassNotFoundException, IOException { users = read(usersFile, ArrayList.class); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Loads all chats saved by Envoy for the client user.
 | 
						 * Loads all chats saved by Envoy for the client user.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @throws EnvoyException         if something fails while loading.
 | 
						 * @throws EnvoyException         if something fails while loading.
 | 
				
			||||||
 | 
						 * @throws IOException
 | 
				
			||||||
 | 
						 * @throws ClassNotFoundException
 | 
				
			||||||
	 * @since Envoy v0.1-alpha
 | 
						 * @since Envoy v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	@SuppressWarnings("unchecked")
 | 
						@SuppressWarnings("unchecked")
 | 
				
			||||||
	private void loadFromLocalDB() throws EnvoyException {
 | 
						private void loadChats() throws ClassNotFoundException, IOException { chats = read(localDBFile, ArrayList.class); }
 | 
				
			||||||
		try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) {
 | 
					
 | 
				
			||||||
			Object obj = in.readObject();
 | 
						private <T> T read(File file, Class<T> serializedClass) throws ClassNotFoundException, IOException {
 | 
				
			||||||
			if (obj instanceof ArrayList<?>) chats = (ArrayList<Chat>) obj;
 | 
							try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
 | 
				
			||||||
 | 
								return serializedClass.cast(in.readObject());
 | 
				
			||||||
		} catch (ClassNotFoundException | IOException e) {
 | 
							} catch (ClassNotFoundException | IOException e) {
 | 
				
			||||||
			throw new EnvoyException(e);
 | 
								throw e;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private <T> void write(File file, T obj) throws IOException {
 | 
				
			||||||
 | 
							if (file == null) throw new NullPointerException("File is null");
 | 
				
			||||||
 | 
							if (!file.exists()) {
 | 
				
			||||||
 | 
								file.getParentFile().mkdirs();
 | 
				
			||||||
 | 
								file.createNewFile();
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) {
 | 
				
			||||||
 | 
								out.writeObject(obj);
 | 
				
			||||||
 | 
							} catch (IOException e) {
 | 
				
			||||||
 | 
								throw e;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -118,7 +143,7 @@ public class LocalDB {
 | 
				
			|||||||
	 */
 | 
						 */
 | 
				
			||||||
	public Message createMessage(String textContent, long recipientID) {
 | 
						public Message createMessage(String textContent, long recipientID) {
 | 
				
			||||||
		Message.Metadata metaData = objectFactory.createMessageMetadata();
 | 
							Message.Metadata metaData = objectFactory.createMessageMetadata();
 | 
				
			||||||
		metaData.setSender(sender.getID());
 | 
							metaData.setSender(user.getID());
 | 
				
			||||||
		metaData.setRecipient(recipientID);
 | 
							metaData.setRecipient(recipientID);
 | 
				
			||||||
		metaData.setState(MessageState.WAITING);
 | 
							metaData.setState(MessageState.WAITING);
 | 
				
			||||||
		metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
 | 
							metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
 | 
				
			||||||
@@ -280,6 +305,16 @@ public class LocalDB {
 | 
				
			|||||||
	 */
 | 
						 */
 | 
				
			||||||
	public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); }
 | 
						public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return the users
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public List<User> getUsers() { return users; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @param users the users to set
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public void setUsers(List<User> users) { this.users = users; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @return all saved {@link Chat} objects that list the client user as the
 | 
						 * @return all saved {@link Chat} objects that list the client user as the
 | 
				
			||||||
	 *         sender
 | 
						 *         sender
 | 
				
			||||||
@@ -288,8 +323,19 @@ public class LocalDB {
 | 
				
			|||||||
	public List<Chat> getChats() { return chats; }
 | 
						public List<Chat> getChats() { return chats; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @return the {@link User} who initialized the local database
 | 
						 * @param chats the chats to set
 | 
				
			||||||
	 * @since Envoy v0.1-alpha
 | 
					 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public User getUser() { return sender; }
 | 
						public void setChats(List<Chat> chats) { this.chats = chats; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return the {@link User} who initialized the local database
 | 
				
			||||||
 | 
						 * @since Envoy v0.2-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public User getUser() { return user; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @param user the user to set
 | 
				
			||||||
 | 
						 * @since Envoy v0.2-alpha
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public void setUser(User user) { this.user = user; }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -86,7 +86,7 @@ public class ChatWindow extends JFrame {
 | 
				
			|||||||
			@Override
 | 
								@Override
 | 
				
			||||||
			public void windowClosing(WindowEvent evt) {
 | 
								public void windowClosing(WindowEvent evt) {
 | 
				
			||||||
				try {
 | 
									try {
 | 
				
			||||||
					localDB.saveToLocalDB();
 | 
										localDB.save();
 | 
				
			||||||
					Settings.getInstance().save();
 | 
										Settings.getInstance().save();
 | 
				
			||||||
				} catch (IOException e1) {
 | 
									} catch (IOException e1) {
 | 
				
			||||||
					e1.printStackTrace();
 | 
										e1.printStackTrace();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -68,7 +68,8 @@ public class Startup {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Load the local database
 | 
							// Load the local database
 | 
				
			||||||
		LocalDB localDB = new LocalDB(client.getSender());
 | 
							LocalDB localDB = new LocalDB();
 | 
				
			||||||
 | 
							localDB.setUser(client.getSender());
 | 
				
			||||||
		try {
 | 
							try {
 | 
				
			||||||
			localDB.initializeDBFile(config.getLocalDB());
 | 
								localDB.initializeDBFile(config.getLocalDB());
 | 
				
			||||||
		} catch (EnvoyException e) {
 | 
							} catch (EnvoyException e) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user