2020-02-15 22:43:20 +01:00
|
|
|
package envoy.server.data;
|
2020-01-03 16:21:35 +01:00
|
|
|
|
2020-07-16 17:04:35 +02:00
|
|
|
import java.time.Instant;
|
2020-10-22 23:58:55 +02:00
|
|
|
import java.util.*;
|
2020-10-14 23:41:24 +02:00
|
|
|
import java.util.logging.Level;
|
2020-01-03 18:17:26 +01:00
|
|
|
|
2020-09-22 17:02:50 +02:00
|
|
|
import javax.persistence.*;
|
2020-01-03 18:17:26 +01:00
|
|
|
|
2020-02-09 16:37:53 +01:00
|
|
|
import envoy.data.User.UserStatus;
|
2020-10-14 23:41:24 +02:00
|
|
|
import envoy.util.EnvoyLog;
|
2020-01-03 16:21:35 +01:00
|
|
|
|
2020-10-19 18:17:51 +02:00
|
|
|
import envoy.server.net.ConnectionManager;
|
|
|
|
|
2020-01-03 16:21:35 +01:00
|
|
|
/**
|
2020-09-30 20:50:58 +02:00
|
|
|
* Contains operations used for persistence.
|
|
|
|
*
|
2020-01-03 16:21:35 +01:00
|
|
|
* @author Leon Hofmeister
|
2020-03-25 16:34:55 +01:00
|
|
|
* @author Maximilian Käfer
|
2020-01-03 16:21:35 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-08-22 13:37:07 +02:00
|
|
|
public final class PersistenceManager {
|
2020-01-03 16:21:35 +01:00
|
|
|
|
2020-10-19 18:17:51 +02:00
|
|
|
private final EntityManager entityManager =
|
|
|
|
Persistence.createEntityManagerFactory("envoy").createEntityManager();
|
2020-02-07 09:34:02 +01:00
|
|
|
private final EntityTransaction transaction = entityManager.getTransaction();
|
|
|
|
|
2020-01-11 18:28:48 +01:00
|
|
|
private static final PersistenceManager persistenceManager = new PersistenceManager();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the singleton instance of the @link{PersistenceManager}.
|
|
|
|
*
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-02-07 09:34:02 +01:00
|
|
|
private PersistenceManager() {
|
2020-10-07 22:13:42 +02:00
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> transaction(() -> {
|
2020-02-09 16:37:53 +01:00
|
|
|
ConnectionManager.getInstance()
|
|
|
|
.getOnlineUsers()
|
|
|
|
.stream()
|
2020-04-06 22:55:27 +02:00
|
|
|
.map(this::getUserByID)
|
2020-10-19 18:17:51 +02:00
|
|
|
.forEach(user -> {
|
|
|
|
user.setStatus(UserStatus.OFFLINE);
|
|
|
|
user.setLastSeen(Instant.now());
|
|
|
|
entityManager.merge(user);
|
|
|
|
});
|
2020-10-07 22:13:42 +02:00
|
|
|
})));
|
2020-02-07 09:34:02 +01:00
|
|
|
}
|
2020-01-11 18:28:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the {@link PersistenceManager} singleton
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-02-12 07:10:33 +01:00
|
|
|
public static PersistenceManager getInstance() { return persistenceManager; }
|
2020-01-11 18:28:48 +01:00
|
|
|
|
2020-01-03 18:17:26 +01:00
|
|
|
/**
|
2020-03-25 16:34:55 +01:00
|
|
|
* Adds a {@link Contact} to the database.
|
2020-01-03 18:17:26 +01:00
|
|
|
*
|
2020-03-26 17:09:52 +01:00
|
|
|
* @param contact the {@link Contact} to add to the database
|
2020-01-03 18:17:26 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public void addContact(Contact contact) {
|
|
|
|
persist(contact);
|
|
|
|
}
|
2020-01-03 18:17:26 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-04 15:39:43 +01:00
|
|
|
* Adds a {@link Message} to the database.
|
2020-01-03 18:17:26 +01:00
|
|
|
*
|
|
|
|
* @param message the {@link Message} to add to the database
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public void addMessage(Message message) {
|
|
|
|
persist(message);
|
|
|
|
}
|
2020-01-03 18:17:26 +01:00
|
|
|
|
2020-01-28 20:46:27 +01:00
|
|
|
/**
|
|
|
|
* Adds a {@link ConfigItem} to the database.
|
|
|
|
*
|
|
|
|
* @param configItem the {@link ConfigItem} to add to the database
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public void addConfigItem(ConfigItem configItem) {
|
|
|
|
persist(configItem);
|
|
|
|
}
|
2020-01-28 20:46:27 +01:00
|
|
|
|
2020-01-03 18:17:26 +01:00
|
|
|
/**
|
2020-03-25 16:34:55 +01:00
|
|
|
* Updates a {@link Contact} in the database
|
2020-01-03 18:17:26 +01:00
|
|
|
*
|
2020-03-26 17:09:52 +01:00
|
|
|
* @param contact the {@link Contact} to add to the database
|
2020-01-03 18:17:26 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public void updateContact(Contact contact) {
|
|
|
|
merge(contact);
|
|
|
|
}
|
2020-01-03 18:17:26 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-04 15:39:43 +01:00
|
|
|
* Updates a {@link Message} in the database.
|
2020-01-03 18:17:26 +01:00
|
|
|
*
|
2020-01-04 15:39:43 +01:00
|
|
|
* @param message the message to update
|
2020-01-03 18:17:26 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public void updateMessage(Message message) {
|
|
|
|
merge(message);
|
|
|
|
}
|
2020-01-28 20:46:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a {@link ConfigItem} in the database.
|
|
|
|
*
|
|
|
|
* @param configItem the configItem to update
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public void updateConfigItem(ConfigItem configItem) {
|
|
|
|
merge(configItem);
|
|
|
|
}
|
2020-01-03 18:17:26 +01:00
|
|
|
|
2020-02-02 12:32:10 +01:00
|
|
|
/**
|
2020-03-25 16:34:55 +01:00
|
|
|
* Deletes a {@link Contact} in the database.
|
2020-02-02 12:32:10 +01:00
|
|
|
*
|
2020-03-26 17:09:52 +01:00
|
|
|
* @param contact the {@link Contact} to delete
|
2020-02-02 12:32:10 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-10-10 22:25:39 +02:00
|
|
|
public void deleteContact(Contact contact) {
|
|
|
|
transaction(() -> {
|
|
|
|
|
|
|
|
// Remove this contact from the contact list of his contacts
|
2020-10-24 12:19:11 +02:00
|
|
|
for (final var remainingContact : contact.contacts)
|
2020-10-10 22:25:39 +02:00
|
|
|
remainingContact.getContacts().remove(contact);
|
2020-10-24 12:19:11 +02:00
|
|
|
contact.contacts.clear();
|
2020-10-10 22:25:39 +02:00
|
|
|
});
|
|
|
|
remove(contact);
|
|
|
|
}
|
2020-02-02 12:32:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a {@link Message} in the database.
|
|
|
|
*
|
|
|
|
* @param message the {@link Message} to delete
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public void deleteMessage(Message message) {
|
|
|
|
remove(message);
|
|
|
|
}
|
2020-02-02 12:32:10 +01:00
|
|
|
|
2020-01-03 18:17:26 +01:00
|
|
|
/**
|
2020-03-25 16:34:55 +01:00
|
|
|
* Searches for a {@link User} with a specific ID.
|
2020-01-03 18:17:26 +01:00
|
|
|
*
|
2020-01-28 19:25:30 +01:00
|
|
|
* @param id the id to search for
|
2020-06-14 16:30:46 +02:00
|
|
|
* @return the user with the specified ID or {@code null} if none was found
|
2020-01-03 18:17:26 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public User getUserByID(long id) {
|
|
|
|
return entityManager.find(User.class, id);
|
|
|
|
}
|
2020-01-03 18:17:26 +01:00
|
|
|
|
2020-03-25 16:34:55 +01:00
|
|
|
/**
|
|
|
|
* Searches for a {@link Group} with a specific ID.
|
2020-04-02 16:32:23 +02:00
|
|
|
*
|
2020-03-25 16:34:55 +01:00
|
|
|
* @param id the id to search for
|
2020-06-14 16:30:46 +02:00
|
|
|
* @return the group with the specified ID or {@code null} if none was found
|
2020-03-25 16:34:55 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-beta
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public Group getGroupByID(long id) {
|
|
|
|
return entityManager.find(Group.class, id);
|
|
|
|
}
|
2020-03-25 16:34:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Searches for a {@link Contact} with a specific ID.
|
2020-04-02 16:32:23 +02:00
|
|
|
*
|
2020-03-25 16:34:55 +01:00
|
|
|
* @param id the id to search for
|
2020-06-14 16:30:46 +02:00
|
|
|
* @return the contact with the specified ID or {@code null} if none was found
|
2020-03-25 16:34:55 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-beta
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public Contact getContactByID(long id) {
|
|
|
|
return entityManager.find(Contact.class, id);
|
|
|
|
}
|
2020-03-25 16:34:55 +01:00
|
|
|
|
2020-01-28 19:25:30 +01:00
|
|
|
/**
|
|
|
|
* Searched for a {@link User} with a specific name.
|
|
|
|
*
|
|
|
|
* @param name the name of the user
|
|
|
|
* @return the user with the specified name
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-01-18 23:50:41 +01:00
|
|
|
public User getUserByName(String name) {
|
2020-10-19 18:17:51 +02:00
|
|
|
return (User) entityManager.createNamedQuery(User.findByName).setParameter("name", name)
|
|
|
|
.getSingleResult();
|
2020-01-18 23:50:41 +01:00
|
|
|
}
|
2020-01-28 19:25:30 +01:00
|
|
|
|
2020-03-25 16:34:55 +01:00
|
|
|
/**
|
|
|
|
* Searched for a {@link Group} with a specific name.
|
|
|
|
*
|
|
|
|
* @param name the name of the group
|
|
|
|
* @return the group with the specified name
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
|
|
|
public Group getGroupByName(String name) {
|
2020-10-19 18:17:51 +02:00
|
|
|
return (Group) entityManager.createNamedQuery(Group.findByName).setParameter("name", name)
|
|
|
|
.getSingleResult();
|
2020-03-25 16:34:55 +01:00
|
|
|
}
|
|
|
|
|
2020-01-11 18:28:48 +01:00
|
|
|
/**
|
|
|
|
* Searches for a {@link Message} with a specific id.
|
|
|
|
*
|
2020-01-28 19:25:30 +01:00
|
|
|
* @param id the id to search for
|
2020-06-14 16:30:46 +02:00
|
|
|
* @return the message with the specified ID or {@code null} if none is found
|
2020-01-11 18:28:48 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public Message getMessageByID(long id) {
|
|
|
|
return entityManager.find(Message.class, id);
|
|
|
|
}
|
2020-01-11 18:28:48 +01:00
|
|
|
|
2020-01-30 12:52:27 +01:00
|
|
|
/**
|
|
|
|
* @param key the name of this {@link ConfigItem}
|
|
|
|
* @return the {@link ConfigItem} with the given name
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public ConfigItem getConfigItemByID(String key) {
|
|
|
|
return entityManager.find(ConfigItem.class, key);
|
|
|
|
}
|
2020-01-28 20:46:27 +01:00
|
|
|
|
2020-01-03 18:17:26 +01:00
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* Returns all messages received while being offline or the ones that have changed.
|
2020-01-03 18:17:26 +01:00
|
|
|
*
|
2020-07-16 17:04:35 +02:00
|
|
|
* @param user the user who wants to receive his unread messages
|
|
|
|
* @param lastSync the time stamp of the last synchronization
|
2020-01-03 18:17:26 +01:00
|
|
|
* @return all messages that the client does not yet have (unread messages)
|
2020-07-16 17:04:35 +02:00
|
|
|
* @since Envoy Server Standalone v0.2-beta
|
2020-01-03 18:17:26 +01:00
|
|
|
*/
|
2020-07-16 17:04:35 +02:00
|
|
|
public List<Message> getPendingMessages(User user, Instant lastSync) {
|
2020-10-22 23:58:55 +02:00
|
|
|
if (user == null)
|
|
|
|
return new ArrayList<>();
|
|
|
|
lastSync = Objects.requireNonNullElse(lastSync, Instant.EPOCH);
|
2020-10-19 18:17:51 +02:00
|
|
|
return entityManager.createNamedQuery(Message.getPending).setParameter("user", user)
|
|
|
|
.setParameter("lastSeen", lastSync).getResultList();
|
2020-01-03 18:17:26 +01:00
|
|
|
}
|
2020-01-18 13:28:54 +01:00
|
|
|
|
2020-06-27 21:58:53 +02:00
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* Returns all groupMessages received while being offline or the ones that have changed.
|
2020-06-27 21:58:53 +02:00
|
|
|
*
|
2020-07-16 17:04:35 +02:00
|
|
|
* @param user the user who wants to receive his unread groupMessages
|
|
|
|
* @param lastSync the time stamp of the last synchronization
|
2020-10-19 18:17:51 +02:00
|
|
|
* @return all groupMessages that the client does not yet have (unread groupMessages)
|
2020-07-16 17:04:35 +02:00
|
|
|
* @since Envoy Server Standalone v0.2-beta
|
2020-06-27 21:58:53 +02:00
|
|
|
*/
|
2020-07-16 17:04:35 +02:00
|
|
|
public List<GroupMessage> getPendingGroupMessages(User user, Instant lastSync) {
|
2020-10-22 23:58:55 +02:00
|
|
|
if (user == null)
|
|
|
|
return new ArrayList<>();
|
|
|
|
lastSync = Objects.requireNonNullElse(lastSync, Instant.EPOCH);
|
2020-06-27 21:58:53 +02:00
|
|
|
return entityManager.createNamedQuery(GroupMessage.getPendingGroupMsg)
|
|
|
|
.setParameter("userId", user.getID())
|
2020-07-16 17:04:35 +02:00
|
|
|
.setParameter("lastSeen", lastSync)
|
2020-06-27 21:58:53 +02:00
|
|
|
.getResultList();
|
|
|
|
}
|
|
|
|
|
2020-02-10 22:29:34 +01:00
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* Searches for users matching a search phrase. Contacts of the attached user and the attached
|
|
|
|
* user is ignored.
|
2020-02-10 22:29:34 +01:00
|
|
|
*
|
|
|
|
* @param searchPhrase the search phrase
|
2020-10-19 18:17:51 +02:00
|
|
|
* @param userId the ID of the user in whose context the search is performed
|
2020-02-10 22:29:34 +01:00
|
|
|
* @return a list of all users who matched the criteria
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
|
|
|
public List<User> searchUsers(String searchPhrase, long userId) {
|
2020-07-16 17:04:35 +02:00
|
|
|
return entityManager.createNamedQuery(User.searchByName)
|
2020-02-10 22:29:34 +01:00
|
|
|
.setParameter("searchPhrase", searchPhrase + "%")
|
2020-04-06 22:55:27 +02:00
|
|
|
.setParameter("context", getUserByID(userId))
|
2020-02-10 22:29:34 +01:00
|
|
|
.getResultList();
|
2020-02-08 13:53:58 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 22:29:34 +01:00
|
|
|
/**
|
2020-06-14 16:40:51 +02:00
|
|
|
* Adds a contact to the contact list of another contact and vice versa.
|
2020-02-10 22:29:34 +01:00
|
|
|
*
|
2020-06-14 16:40:51 +02:00
|
|
|
* @param contactID1 the ID of the first contact
|
|
|
|
* @param contactID2 the ID of the second contact
|
2020-02-10 22:29:34 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-06-14 16:40:51 +02:00
|
|
|
public void addContactBidirectional(long contactID1, long contactID2) {
|
2020-10-14 23:41:24 +02:00
|
|
|
addContactBidirectional(getContactByID(contactID1), getContactByID(contactID2));
|
|
|
|
}
|
2020-02-18 16:36:31 +01:00
|
|
|
|
2020-10-14 23:41:24 +02:00
|
|
|
/**
|
|
|
|
* Adds a contact to the contact list of another contact and vice versa.
|
|
|
|
*
|
|
|
|
* @param contact1 the first contact
|
|
|
|
* @param contact2 the second contact
|
|
|
|
* @since Envoy Server v0.3-beta
|
|
|
|
*/
|
|
|
|
public void addContactBidirectional(Contact contact1, Contact contact2) {
|
2020-10-22 23:58:55 +02:00
|
|
|
if (!(contact1 == null || contact2 == null)) {
|
|
|
|
|
|
|
|
// Add users to each others contact list
|
|
|
|
contact1.getContacts().add(contact2);
|
|
|
|
contact2.getContacts().add(contact1);
|
|
|
|
|
|
|
|
// Synchronize changes with the database
|
|
|
|
transaction(() -> {
|
|
|
|
entityManager.merge(contact1);
|
|
|
|
entityManager.merge(contact2);
|
|
|
|
});
|
|
|
|
}
|
2020-02-08 15:03:03 +01:00
|
|
|
}
|
|
|
|
|
2020-10-10 22:25:39 +02:00
|
|
|
/**
|
|
|
|
* Removes a contact from the contact list of another contact and vice versa.
|
|
|
|
*
|
|
|
|
* @param contactID1 the ID of the first contact
|
|
|
|
* @param contactID2 the ID of the second contact
|
|
|
|
* @since Envoy Server v0.3-beta
|
|
|
|
*/
|
|
|
|
public void removeContactBidirectional(long contactID1, long contactID2) {
|
2020-10-14 23:41:24 +02:00
|
|
|
removeContactBidirectional(getContactByID(contactID1), getContactByID(contactID2));
|
|
|
|
}
|
2020-10-10 22:25:39 +02:00
|
|
|
|
2020-10-14 23:41:24 +02:00
|
|
|
/**
|
|
|
|
* Removes a contact from the contact list of another contact and vice versa.
|
|
|
|
*
|
|
|
|
* @param contact1 the first contact
|
|
|
|
* @param contact2 the second contact
|
|
|
|
* @since Envoy Server v0.3-beta
|
|
|
|
*/
|
|
|
|
public void removeContactBidirectional(Contact contact1, Contact contact2) {
|
2020-10-22 23:58:55 +02:00
|
|
|
if (!(contact1 == null || contact2 == null)) {
|
|
|
|
|
|
|
|
// Remove users from each others contact list
|
|
|
|
contact1.getContacts().remove(contact2);
|
|
|
|
contact2.getContacts().remove(contact1);
|
|
|
|
|
|
|
|
// Synchronize changes with the database
|
|
|
|
transaction(() -> {
|
|
|
|
entityManager.merge(contact1);
|
|
|
|
entityManager.merge(contact2);
|
|
|
|
});
|
|
|
|
}
|
2020-10-10 22:25:39 +02:00
|
|
|
}
|
|
|
|
|
2020-01-18 13:28:54 +01:00
|
|
|
/**
|
|
|
|
* @param user the User whose contacts should be retrieved
|
2020-02-10 20:02:05 +01:00
|
|
|
* @return the contacts of this User
|
2020-01-18 13:28:54 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-02-09 22:16:33 +01:00
|
|
|
public List<User> getContacts(User user) {
|
2020-10-19 18:17:51 +02:00
|
|
|
return entityManager.createNamedQuery(User.findContacts).setParameter("user", user)
|
|
|
|
.getResultList();
|
2020-02-09 22:16:33 +01:00
|
|
|
}
|
2020-02-15 22:43:20 +01:00
|
|
|
|
2020-10-19 18:17:51 +02:00
|
|
|
private void persist(Object obj) {
|
2020-10-23 00:15:37 +02:00
|
|
|
try {
|
|
|
|
transaction(() -> entityManager.persist(obj));
|
|
|
|
} catch (EntityExistsException e) {
|
|
|
|
if (transaction.isActive())
|
|
|
|
transaction.rollback();
|
|
|
|
EnvoyLog.getLogger(PersistenceManager.class).log(Level.WARNING,
|
|
|
|
String.format("Could not persist %s: entity exists already.", obj));
|
|
|
|
}
|
2020-10-19 18:17:51 +02:00
|
|
|
}
|
2020-02-15 22:43:20 +01:00
|
|
|
|
2020-10-19 18:17:51 +02:00
|
|
|
private void merge(Object obj) {
|
2020-10-23 00:15:37 +02:00
|
|
|
try {
|
|
|
|
transaction(() -> entityManager.merge(obj));
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
if (transaction.isActive())
|
|
|
|
transaction.rollback();
|
|
|
|
EnvoyLog.getLogger(PersistenceManager.class).log(Level.WARNING,
|
|
|
|
String.format("Could not merge %s: entity doesn't exist.", obj));
|
|
|
|
}
|
2020-10-19 18:17:51 +02:00
|
|
|
}
|
2020-09-22 17:02:50 +02:00
|
|
|
|
2020-10-19 18:17:51 +02:00
|
|
|
private void remove(Object obj) {
|
2020-10-23 00:15:37 +02:00
|
|
|
try {
|
|
|
|
transaction(() -> entityManager.remove(obj));
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
if (transaction.isActive())
|
|
|
|
transaction.rollback();
|
|
|
|
EnvoyLog.getLogger(PersistenceManager.class).log(Level.WARNING,
|
|
|
|
String.format("Could not remove %s: entity didn't exist (for the database).", obj));
|
|
|
|
}
|
2020-10-19 18:17:51 +02:00
|
|
|
}
|
2020-02-15 22:43:20 +01:00
|
|
|
|
2020-10-07 22:13:42 +02:00
|
|
|
/**
|
|
|
|
* Performs a transaction with the given Runnable, that should somewhere call
|
|
|
|
* {@link EntityManager}.
|
|
|
|
*
|
2020-10-19 18:17:51 +02:00
|
|
|
* @param entityManagerRelatedAction the action that changes something in the database
|
2020-10-07 22:13:42 +02:00
|
|
|
* @since Envoy Server v0.3-beta
|
|
|
|
*/
|
|
|
|
private void transaction(Runnable entityManagerRelatedAction) {
|
2020-09-22 17:02:50 +02:00
|
|
|
try {
|
|
|
|
transaction.begin();
|
2020-10-07 22:13:42 +02:00
|
|
|
entityManagerRelatedAction.run();
|
2020-09-22 17:02:50 +02:00
|
|
|
transaction.commit();
|
|
|
|
|
|
|
|
// Last transaction threw an error resulting in the transaction not being closed
|
|
|
|
} catch (final IllegalStateException e) {
|
|
|
|
if (transaction.isActive()) {
|
|
|
|
transaction.rollback();
|
2020-10-07 22:13:42 +02:00
|
|
|
transaction.begin();
|
|
|
|
entityManagerRelatedAction.run();
|
|
|
|
transaction.commit();
|
2020-09-22 17:02:50 +02:00
|
|
|
}
|
2020-10-14 23:41:24 +02:00
|
|
|
} catch (final RollbackException e2) {
|
|
|
|
|
|
|
|
// Apparently a major problem exists. Discard faulty transaction and then go on.
|
|
|
|
if (transaction.isActive()) {
|
|
|
|
transaction.rollback();
|
|
|
|
EnvoyLog.getLogger(PersistenceManager.class)
|
2020-10-19 18:17:51 +02:00
|
|
|
.log(Level.SEVERE,
|
|
|
|
"Could not perform transaction, hence discarding it. It's likely that a serious issue exists.");
|
|
|
|
} else
|
|
|
|
throw e2;
|
2020-09-22 17:02:50 +02:00
|
|
|
}
|
2020-02-15 22:43:20 +01:00
|
|
|
}
|
2020-02-18 16:36:31 +01:00
|
|
|
}
|