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