Moved PersistenceManager into data package, refactored transaction use
This commit is contained in:
parent
8b6356e48a
commit
0347715d72
@ -7,7 +7,7 @@ import java.util.Set;
|
||||
import com.jenkov.nioserver.Server;
|
||||
|
||||
import envoy.server.data.ConfigItem;
|
||||
import envoy.server.database.PersistenceManager;
|
||||
import envoy.server.data.PersistenceManager;
|
||||
import envoy.server.net.ConnectionManager;
|
||||
import envoy.server.net.ObjectMessageProcessor;
|
||||
import envoy.server.net.ObjectMessageReader;
|
||||
|
@ -5,7 +5,6 @@ import java.util.Date;
|
||||
import javax.persistence.*;
|
||||
|
||||
import envoy.data.MessageBuilder;
|
||||
import envoy.server.database.PersistenceManager;
|
||||
|
||||
/**
|
||||
* This class serves as a way to let Hibernate communicate with the server
|
||||
|
@ -1,4 +1,4 @@
|
||||
package envoy.server.database;
|
||||
package envoy.server.data;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
@ -8,9 +8,6 @@ import javax.persistence.EntityTransaction;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import envoy.data.User.UserStatus;
|
||||
import envoy.server.data.ConfigItem;
|
||||
import envoy.server.data.Message;
|
||||
import envoy.server.data.User;
|
||||
import envoy.server.net.ConnectionManager;
|
||||
|
||||
/**
|
||||
@ -34,13 +31,13 @@ public class PersistenceManager {
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
private PersistenceManager() {
|
||||
transaction.begin();
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
transaction.begin();
|
||||
ConnectionManager.getInstance()
|
||||
.getOnlineUsers()
|
||||
.stream()
|
||||
.map(this::getUserById)
|
||||
.forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(new Date()); updateUser(user); });
|
||||
.forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(new Date()); entityManager.merge(user); });
|
||||
transaction.commit();
|
||||
}));
|
||||
}
|
||||
@ -54,13 +51,10 @@ public class PersistenceManager {
|
||||
/**
|
||||
* Adds a {@link User} to the database.
|
||||
*
|
||||
* @param User the {@link User} to add to the database
|
||||
* @param user the {@link User} to add to the database
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void addUser(User User) {
|
||||
entityManager.persist(User);
|
||||
entityManager.flush();
|
||||
}
|
||||
public void addUser(User user) { persist(user); }
|
||||
|
||||
/**
|
||||
* Adds a {@link Message} to the database.
|
||||
@ -68,10 +62,7 @@ public class PersistenceManager {
|
||||
* @param message the {@link Message} to add to the database
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void addMessage(Message message) {
|
||||
entityManager.persist(message);
|
||||
entityManager.flush();
|
||||
}
|
||||
public void addMessage(Message message) { persist(message); }
|
||||
|
||||
/**
|
||||
* Adds a {@link ConfigItem} to the database.
|
||||
@ -79,10 +70,7 @@ public class PersistenceManager {
|
||||
* @param configItem the {@link ConfigItem} to add to the database
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void addConfigItem(ConfigItem configItem) {
|
||||
entityManager.persist(configItem);
|
||||
entityManager.flush();
|
||||
}
|
||||
public void addConfigItem(ConfigItem configItem) { persist(configItem); }
|
||||
|
||||
/**
|
||||
* Updates a {@link User} in the database
|
||||
@ -90,10 +78,7 @@ public class PersistenceManager {
|
||||
* @param user the {@link User} to add to the database
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void updateUser(User user) {
|
||||
entityManager.merge(user);
|
||||
entityManager.flush();
|
||||
}
|
||||
public void updateUser(User user) { merge(user); }
|
||||
|
||||
/**
|
||||
* Updates a {@link Message} in the database.
|
||||
@ -101,10 +86,7 @@ public class PersistenceManager {
|
||||
* @param message the message to update
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void updateMessage(Message message) {
|
||||
entityManager.merge(message);
|
||||
entityManager.flush();
|
||||
}
|
||||
public void updateMessage(Message message) { merge(message); }
|
||||
|
||||
/**
|
||||
* Updates a {@link ConfigItem} in the database.
|
||||
@ -112,10 +94,7 @@ public class PersistenceManager {
|
||||
* @param configItem the configItem to update
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void updateConfigItem(ConfigItem configItem) {
|
||||
entityManager.merge(configItem);
|
||||
entityManager.flush();
|
||||
}
|
||||
public void updateConfigItem(ConfigItem configItem) { merge(configItem); }
|
||||
|
||||
/**
|
||||
* Deletes a {@link User} in the database.
|
||||
@ -123,10 +102,7 @@ public class PersistenceManager {
|
||||
* @param user the {@link User} to delete
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void deleteUser(User user) {
|
||||
entityManager.remove(user);
|
||||
entityManager.flush();
|
||||
}
|
||||
public void deleteUser(User user) { remove(user); }
|
||||
|
||||
/**
|
||||
* Deletes a {@link Message} in the database.
|
||||
@ -134,10 +110,7 @@ public class PersistenceManager {
|
||||
* @param message the {@link Message} to delete
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void deleteMessage(Message message) {
|
||||
entityManager.remove(message);
|
||||
entityManager.flush();
|
||||
}
|
||||
public void deleteMessage(Message message) { remove(message); }
|
||||
|
||||
/**
|
||||
* Searches for a {@link User} with a specific id.
|
||||
@ -211,12 +184,20 @@ public class PersistenceManager {
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
public void addContact(long userId1, long userId2) {
|
||||
|
||||
// Get users by ID
|
||||
User u1 = getUserById(userId1);
|
||||
User u2 = getUserById(userId2);
|
||||
|
||||
// Add users to each others contact lists
|
||||
u1.getContacts().add(u2);
|
||||
u2.getContacts().add(u1);
|
||||
updateUser(u1);
|
||||
updateUser(u2);
|
||||
|
||||
// Synchronize changes with the database
|
||||
transaction.begin();
|
||||
entityManager.merge(u1);
|
||||
entityManager.merge(u2);
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -227,4 +208,22 @@ public class PersistenceManager {
|
||||
public List<User> getContacts(User user) {
|
||||
return entityManager.createNamedQuery("getContactsOfUser").setParameter("user", user).getResultList();
|
||||
}
|
||||
|
||||
private void persist(Object obj) {
|
||||
transaction.begin();
|
||||
entityManager.persist(obj);
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
private void merge(Object obj) {
|
||||
transaction.begin();
|
||||
entityManager.merge(obj);
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
private void remove(Object obj) {
|
||||
transaction.begin();
|
||||
entityManager.remove(obj);
|
||||
transaction.commit();
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ import java.util.*;
|
||||
import com.jenkov.nioserver.ISocketIdListener;
|
||||
|
||||
import envoy.data.User.UserStatus;
|
||||
import envoy.server.database.PersistenceManager;
|
||||
import envoy.server.data.PersistenceManager;
|
||||
import envoy.server.processors.UserStatusChangeProcessor;
|
||||
|
||||
/**
|
||||
|
@ -6,7 +6,7 @@ import java.util.Arrays;
|
||||
import envoy.data.Contacts;
|
||||
import envoy.event.ContactOperationEvent;
|
||||
import envoy.server.ObjectProcessor;
|
||||
import envoy.server.database.PersistenceManager;
|
||||
import envoy.server.data.PersistenceManager;
|
||||
import envoy.server.net.ConnectionManager;
|
||||
import envoy.server.net.ObjectWriteProxy;
|
||||
|
||||
|
@ -7,8 +7,8 @@ import envoy.data.Contacts;
|
||||
import envoy.event.ContactSearchRequest;
|
||||
import envoy.event.ContactSearchResult;
|
||||
import envoy.server.ObjectProcessor;
|
||||
import envoy.server.data.PersistenceManager;
|
||||
import envoy.server.data.User;
|
||||
import envoy.server.database.PersistenceManager;
|
||||
import envoy.server.net.ConnectionManager;
|
||||
import envoy.server.net.ObjectWriteProxy;
|
||||
|
||||
|
@ -6,7 +6,7 @@ import envoy.data.IdGenerator;
|
||||
import envoy.event.IdGeneratorRequest;
|
||||
import envoy.server.ObjectProcessor;
|
||||
import envoy.server.data.ConfigItem;
|
||||
import envoy.server.database.PersistenceManager;
|
||||
import envoy.server.data.PersistenceManager;
|
||||
import envoy.server.net.ObjectWriteProxy;
|
||||
|
||||
/**
|
||||
|
@ -14,7 +14,7 @@ import envoy.data.User.UserStatus;
|
||||
import envoy.event.HandshakeRejectionEvent;
|
||||
import envoy.server.ObjectProcessor;
|
||||
import envoy.server.data.Message;
|
||||
import envoy.server.database.PersistenceManager;
|
||||
import envoy.server.data.PersistenceManager;
|
||||
import envoy.server.net.ConnectionManager;
|
||||
import envoy.server.net.ObjectWriteProxy;
|
||||
|
||||
|
@ -6,7 +6,7 @@ import java.util.Date;
|
||||
import envoy.data.Message;
|
||||
import envoy.event.MessageStatusChangeEvent;
|
||||
import envoy.server.ObjectProcessor;
|
||||
import envoy.server.database.PersistenceManager;
|
||||
import envoy.server.data.PersistenceManager;
|
||||
import envoy.server.net.ConnectionManager;
|
||||
import envoy.server.net.ObjectWriteProxy;
|
||||
|
||||
|
@ -6,7 +6,7 @@ import envoy.data.Message.MessageStatus;
|
||||
import envoy.event.MessageStatusChangeEvent;
|
||||
import envoy.exception.EnvoyException;
|
||||
import envoy.server.ObjectProcessor;
|
||||
import envoy.server.database.PersistenceManager;
|
||||
import envoy.server.data.PersistenceManager;
|
||||
import envoy.server.net.ConnectionManager;
|
||||
import envoy.server.net.ObjectWriteProxy;
|
||||
|
||||
|
@ -5,8 +5,8 @@ import java.io.IOException;
|
||||
import envoy.data.User.UserStatus;
|
||||
import envoy.event.UserStatusChangeEvent;
|
||||
import envoy.server.ObjectProcessor;
|
||||
import envoy.server.data.PersistenceManager;
|
||||
import envoy.server.data.User;
|
||||
import envoy.server.database.PersistenceManager;
|
||||
import envoy.server.net.ConnectionManager;
|
||||
import envoy.server.net.ObjectWriteProxy;
|
||||
|
||||
|
Reference in New Issue
Block a user