From 5b18809da09bf0e4664c4cb22fc142cb5a606413 Mon Sep 17 00:00:00 2001 From: sgiannopoulos Date: Thu, 4 Jan 2024 13:05:06 +0200 Subject: [PATCH] remove old lock --- .../src/main/java/eu/eudat/data/old/Lock.java | 94 ------------- .../eudat/data/dao/criteria/LockCriteria.java | 38 ------ .../eu/eudat/data/dao/entities/LockDao.java | 13 -- .../eudat/data/dao/entities/LockDaoImpl.java | 66 --------- .../items/item/lock/LockCriteriaRequest.java | 20 --- .../items/table/lock/LockTableRequest.java | 28 ---- .../main/java/eu/eudat/query/LockQuery.java | 80 ----------- .../eu/eudat/logic/managers/LockManager.java | 127 ------------------ .../operations/DatabaseRepository.java | 2 - .../operations/DatabaseRepositoryImpl.java | 12 -- .../java/eu/eudat/models/data/lock/Lock.java | 73 ---------- 11 files changed, 553 deletions(-) delete mode 100644 dmp-backend/core/src/main/java/eu/eudat/data/old/Lock.java delete mode 100644 dmp-backend/data/src/main/java/eu/eudat/data/dao/criteria/LockCriteria.java delete mode 100644 dmp-backend/data/src/main/java/eu/eudat/data/dao/entities/LockDao.java delete mode 100644 dmp-backend/data/src/main/java/eu/eudat/data/dao/entities/LockDaoImpl.java delete mode 100644 dmp-backend/data/src/main/java/eu/eudat/data/query/items/item/lock/LockCriteriaRequest.java delete mode 100644 dmp-backend/data/src/main/java/eu/eudat/data/query/items/table/lock/LockTableRequest.java delete mode 100644 dmp-backend/data/src/main/java/eu/eudat/query/LockQuery.java delete mode 100644 dmp-backend/web/src/main/java/eu/eudat/logic/managers/LockManager.java delete mode 100644 dmp-backend/web/src/main/java/eu/eudat/models/data/lock/Lock.java diff --git a/dmp-backend/core/src/main/java/eu/eudat/data/old/Lock.java b/dmp-backend/core/src/main/java/eu/eudat/data/old/Lock.java deleted file mode 100644 index c1a0b2ba2..000000000 --- a/dmp-backend/core/src/main/java/eu/eudat/data/old/Lock.java +++ /dev/null @@ -1,94 +0,0 @@ -package eu.eudat.data.old; - -import eu.eudat.data.UserEntity; -import eu.eudat.data.converters.DateToUTCConverter; -import eu.eudat.data.old.helpers.EntityBinder; -import eu.eudat.data.old.queryableentity.DataEntity; - -import jakarta.persistence.*; -import java.util.Date; -import java.util.List; -import java.util.UUID; - -@Entity -@Table(name = "\"Lock\"") -public class Lock implements DataEntity { - - @Id - @GeneratedValue - @Column(name = "id", updatable = false, nullable = false, columnDefinition = "BINARY(16)") - private UUID id; - - @Column(name = "\"Target\"", nullable = false) - private UUID target; - - @ManyToOne(fetch = FetchType.EAGER) - @JoinColumn(name = "\"LockedBy\"", nullable = false) - private UserEntity lockedBy; - - @Column(name = "\"LockedAt\"") - @Convert(converter = DateToUTCConverter.class) - private Date lockedAt = new Date(); - - @Column(name = "\"TouchedAt\"") - @Convert(converter = DateToUTCConverter.class) - private Date touchedAt = null; - - - public UUID getId() { - return id; - } - - public void setId(UUID id) { - this.id = id; - } - - public UUID getTarget() { - return target; - } - - public void setTarget(UUID target) { - this.target = target; - } - - public UserEntity getLockedBy() { - return lockedBy; - } - - public void setLockedBy(UserEntity lockedBy) { - this.lockedBy = lockedBy; - } - - public Date getLockedAt() { - return lockedAt; - } - - public void setLockedAt(Date lockedAt) { - this.lockedAt = lockedAt; - } - - public Date getTouchedAt() { - return touchedAt; - } - - public void setTouchedAt(Date touchedAt) { - this.touchedAt = touchedAt; - } - - @Override - public void update(Lock entity) { - this.touchedAt = entity.touchedAt; - } - - @Override - public UUID getKeys() { - return this.id; - } - - @Override - public Lock buildFromTuple(List tuple, List fields, String base) { - String currentBase = base.isEmpty() ? "" : base + "."; - if (fields.contains(currentBase + "id")) this.id = EntityBinder.fromTuple(tuple, currentBase + "id"); - return this; - } -} diff --git a/dmp-backend/data/src/main/java/eu/eudat/data/dao/criteria/LockCriteria.java b/dmp-backend/data/src/main/java/eu/eudat/data/dao/criteria/LockCriteria.java deleted file mode 100644 index ccdc7eff9..000000000 --- a/dmp-backend/data/src/main/java/eu/eudat/data/dao/criteria/LockCriteria.java +++ /dev/null @@ -1,38 +0,0 @@ -package eu.eudat.data.dao.criteria; - -import eu.eudat.data.old.Lock; -import eu.eudat.data.UserEntity; - -import java.util.Date; -import java.util.UUID; - -public class LockCriteria extends Criteria { - - private UUID target; - private UserEntity lockedBy; - private Date touchedAt; - - public UUID getTarget() { - return target; - } - - public void setTarget(UUID target) { - this.target = target; - } - - public UserEntity getLockedBy() { - return lockedBy; - } - - public void setLockedBy(UserEntity lockedBy) { - this.lockedBy = lockedBy; - } - - public Date getTouchedAt() { - return touchedAt; - } - - public void setTouchedAt(Date touchedAt) { - this.touchedAt = touchedAt; - } -} diff --git a/dmp-backend/data/src/main/java/eu/eudat/data/dao/entities/LockDao.java b/dmp-backend/data/src/main/java/eu/eudat/data/dao/entities/LockDao.java deleted file mode 100644 index ae5fffa08..000000000 --- a/dmp-backend/data/src/main/java/eu/eudat/data/dao/entities/LockDao.java +++ /dev/null @@ -1,13 +0,0 @@ -package eu.eudat.data.dao.entities; - -import eu.eudat.data.dao.DatabaseAccessLayer; -import eu.eudat.data.dao.criteria.LockCriteria; -import eu.eudat.data.old.Lock; -import eu.eudat.queryable.QueryableList; - -import java.util.UUID; - -public interface LockDao extends DatabaseAccessLayer { - - QueryableList getWithCriteria(LockCriteria criteria); -} diff --git a/dmp-backend/data/src/main/java/eu/eudat/data/dao/entities/LockDaoImpl.java b/dmp-backend/data/src/main/java/eu/eudat/data/dao/entities/LockDaoImpl.java deleted file mode 100644 index dbbce603f..000000000 --- a/dmp-backend/data/src/main/java/eu/eudat/data/dao/entities/LockDaoImpl.java +++ /dev/null @@ -1,66 +0,0 @@ -package eu.eudat.data.dao.entities; - -import eu.eudat.data.dao.DatabaseAccess; -import eu.eudat.data.dao.criteria.LockCriteria; -import eu.eudat.data.dao.databaselayer.service.DatabaseService; -import eu.eudat.data.old.Lock; -import eu.eudat.queryable.QueryableList; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.scheduling.annotation.Async; -import org.springframework.stereotype.Service; - -import javax.management.InvalidApplicationException; -import java.util.UUID; -import java.util.concurrent.CompletableFuture; - -@Service("LockDao") -public class LockDaoImpl extends DatabaseAccess implements LockDao { - - @Autowired - public LockDaoImpl(DatabaseService databaseService) { - super(databaseService); - } - - @Override - public QueryableList getWithCriteria(LockCriteria criteria) { - QueryableList query = this.getDatabaseService().getQueryable(Lock.class); - if (criteria.getTouchedAt() != null) - query.where((builder, root) -> builder.equal(root.get("touchedAt"), criteria.getTouchedAt())); - if (criteria.getLockedBy() != null) - query.where(((builder, root) -> builder.equal(root.get("lockedBy"), criteria.getLockedBy()))); - if (criteria.getTarget() != null) - query.where(((builder, root) -> builder.equal(root.get("target"), criteria.getTarget()))); - return query; - } - - @Override - public Lock createOrUpdate(Lock item) { - return this.getDatabaseService().createOrUpdate(item, Lock.class); - } - - @Async - @Override - public CompletableFuture createOrUpdateAsync(Lock item) { - return CompletableFuture.supplyAsync(() -> this.createOrUpdate(item)); - } - - @Override - public Lock find(UUID id) throws InvalidApplicationException { - return this.getDatabaseService().getQueryable(Lock.class).where(((builder, root) -> builder.equal(root.get("id"), id))).getSingle(); - } - - @Override - public Lock find(UUID id, String hint) { - throw new UnsupportedOperationException(); - } - - @Override - public void delete(Lock item) { - this.getDatabaseService().delete(item); - } - - @Override - public QueryableList asQueryable() { - return this.getDatabaseService().getQueryable(Lock.class); - } -} diff --git a/dmp-backend/data/src/main/java/eu/eudat/data/query/items/item/lock/LockCriteriaRequest.java b/dmp-backend/data/src/main/java/eu/eudat/data/query/items/item/lock/LockCriteriaRequest.java deleted file mode 100644 index ab13fc6ff..000000000 --- a/dmp-backend/data/src/main/java/eu/eudat/data/query/items/item/lock/LockCriteriaRequest.java +++ /dev/null @@ -1,20 +0,0 @@ -package eu.eudat.data.query.items.item.lock; - -import eu.eudat.data.dao.criteria.LockCriteria; -import eu.eudat.data.old.Lock; -import eu.eudat.data.query.definition.Query; -import eu.eudat.queryable.QueryableList; - -public class LockCriteriaRequest extends Query { - @Override - public QueryableList applyCriteria() { - QueryableList query = this.getQuery(); - if (this.getCriteria().getTouchedAt() != null) - query.where((builder, root) -> builder.equal(root.get("touchedAt"), this.getCriteria().getTouchedAt())); - if (this.getCriteria().getLockedBy() != null) - query.where(((builder, root) -> builder.equal(root.get("lockedBy"), this.getCriteria().getLockedBy()))); - if (this.getCriteria().getTarget() != null) - query.where(((builder, root) -> builder.equal(root.get("target"), this.getCriteria().getTarget()))); - return query; - } -} diff --git a/dmp-backend/data/src/main/java/eu/eudat/data/query/items/table/lock/LockTableRequest.java b/dmp-backend/data/src/main/java/eu/eudat/data/query/items/table/lock/LockTableRequest.java deleted file mode 100644 index 3d4616bd4..000000000 --- a/dmp-backend/data/src/main/java/eu/eudat/data/query/items/table/lock/LockTableRequest.java +++ /dev/null @@ -1,28 +0,0 @@ -package eu.eudat.data.query.items.table.lock; - -import eu.eudat.data.dao.criteria.LockCriteria; -import eu.eudat.data.old.Lock; -import eu.eudat.data.query.PaginationService; -import eu.eudat.data.query.definition.TableQuery; -import eu.eudat.queryable.QueryableList; - -import java.util.UUID; - -public class LockTableRequest extends TableQuery { - @Override - public QueryableList applyCriteria() { - QueryableList query = this.getQuery(); - if (this.getCriteria().getTouchedAt() != null) - query.where((builder, root) -> builder.equal(root.get("touchedAt"), this.getCriteria().getTouchedAt())); - if (this.getCriteria().getLockedBy() != null) - query.where(((builder, root) -> builder.equal(root.get("lockedBy"), this.getCriteria().getLockedBy()))); - if (this.getCriteria().getTarget() != null) - query.where(((builder, root) -> builder.equal(root.get("target"), this.getCriteria().getTarget()))); - return query; - } - - @Override - public QueryableList applyPaging(QueryableList items) { - return PaginationService.applyPaging(items, this); - } -} diff --git a/dmp-backend/data/src/main/java/eu/eudat/query/LockQuery.java b/dmp-backend/data/src/main/java/eu/eudat/query/LockQuery.java deleted file mode 100644 index ead721b73..000000000 --- a/dmp-backend/data/src/main/java/eu/eudat/query/LockQuery.java +++ /dev/null @@ -1,80 +0,0 @@ -package eu.eudat.query; - -import eu.eudat.data.dao.DatabaseAccessLayer; -import eu.eudat.data.old.Lock; -import eu.eudat.data.UserEntity; -import eu.eudat.queryable.QueryableList; -import eu.eudat.queryable.types.FieldSelectionType; -import eu.eudat.queryable.types.SelectionField; - -import jakarta.persistence.criteria.Subquery; - -import javax.management.InvalidApplicationException; -import java.util.Arrays; -import java.util.Date; -import java.util.List; -import java.util.UUID; - -public class LockQuery extends Query { - - private UUID id; - private UUID target; -// private UserQueryOld userQuery; - private Date touchedAt; - - public UUID getId() { - return id; - } - - public void setId(UUID id) { - this.id = id; - } - - public UUID getTarget() { - return target; - } - - public void setTarget(UUID target) { - this.target = target; - } - -// public UserQueryOld getUserQuery() { -// return userQuery; -// } -// -// public void setUserQuery(UserQueryOld userQuery) { -// this.userQuery = userQuery; -// } - - public Date getTouchedAt() { - return touchedAt; - } - - public void setTouchedAt(Date touchedAt) { - this.touchedAt = touchedAt; - } - - public LockQuery(DatabaseAccessLayer databaseAccessLayer, List selectionFields) { - super(databaseAccessLayer, selectionFields); - } - - public LockQuery(DatabaseAccessLayer databaseAccessLayer) { - super(databaseAccessLayer); - } - - @Override - public QueryableList getQuery() throws InvalidApplicationException { - QueryableList query = this.databaseAccessLayer.asQueryable(); - if (this.id != null) { - query.where((builder, root) -> builder.equal(root.get("id"), this.id)); - } - if (this.target != null) { - query.where(((builder, root) -> builder.equal(root.get("target"), this.target))); - } -// if (this.userQuery != null) { -// Subquery userSubQuery = this.userQuery.getQuery().query(Arrays.asList(new SelectionField(FieldSelectionType.FIELD, "id"))); -// query.where((builder, root) -> root.get("lockedBy").get("id").in(userSubQuery)); -// } - return query; - } -} diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/LockManager.java b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/LockManager.java deleted file mode 100644 index 41e101f2e..000000000 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/LockManager.java +++ /dev/null @@ -1,127 +0,0 @@ -package eu.eudat.logic.managers; - -import eu.eudat.commons.scope.user.UserScope; -import eu.eudat.data.dao.criteria.LockCriteria; -import eu.eudat.logic.services.ApiContext; -import eu.eudat.models.data.lock.Lock; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.env.Environment; -import org.springframework.stereotype.Component; - -import jakarta.persistence.NoResultException; - -import javax.management.InvalidApplicationException; -import java.util.Comparator; -import java.util.Date; -import java.util.List; -import java.util.UUID; - -@Component -public class LockManager { - private final Comparator compareByTouchedAt = Comparator.comparing(o -> o.getTouchedAt().getTime()); - - private ApiContext apiContext; - private Environment environment; - private final UserScope userScope; - - @Autowired - public LockManager(ApiContext apiContext, Environment environment, UserScope userScope) { - this.apiContext = apiContext; - this.environment = environment; - this.userScope = userScope; - } - - public eu.eudat.data.old.Lock createOrUpdate(Lock lock) throws Exception { - if (lock.getId() != null) { - try { - eu.eudat.data.old.Lock entity = this.apiContext.getOperationsContext().getDatabaseRepository().getLockDao().find(lock.getId()); - if (entity != null) { - if (!entity.getLockedBy().getId().equals(this.userScope.getUserId())) { - throw new Exception("Is not locked by that user"); - } - } - }catch(NoResultException e) { - lock.setId(null); - } - } - eu.eudat.data.old.Lock newLock = lock.toDataModel(); - newLock = this.apiContext.getOperationsContext().getDatabaseRepository().getLockDao().createOrUpdate(newLock); - - return newLock; - } - - public boolean isLocked(String targetId) throws Exception { - LockCriteria criteria = new LockCriteria(); - criteria.setTarget(UUID.fromString(targetId)); - Long availableLocks = this.apiContext.getOperationsContext().getDatabaseRepository().getLockDao().getWithCriteria(criteria).count(); - if (availableLocks == 1) { - eu.eudat.data.old.Lock lock = this.apiContext.getOperationsContext().getDatabaseRepository().getLockDao().getWithCriteria(criteria).getSingle(); - if (lock.getLockedBy().getId().equals(this.userScope.getUserId())) { - lock.setTouchedAt(new Date()); - this.createOrUpdate(new Lock().fromDataModel(lock)); - return false; - } - return this.forceUnlock(targetId) > 0; - } else if (availableLocks > 1) { - this.forceUnlock(targetId); - return this.isLocked(targetId); - } - return false; - } - - private Long forceUnlock(String targetId) throws InvalidApplicationException { - LockCriteria criteria = new LockCriteria(); - criteria.setTarget(UUID.fromString(targetId)); - Long availableLocks = this.apiContext.getOperationsContext().getDatabaseRepository().getLockDao().getWithCriteria(criteria).count(); - long deletedLocks = 0L; - if (availableLocks > 0) { - List locks = this.apiContext.getOperationsContext().getDatabaseRepository().getLockDao().getWithCriteria(criteria).toList(); - for (eu.eudat.data.old.Lock lock : locks) { - if (new Date().getTime() - lock.getTouchedAt().getTime() > environment.getProperty("database.lock-fail-interval", Integer.class)) { - this.apiContext.getOperationsContext().getDatabaseRepository().getLockDao().delete(lock); - deletedLocks++; - } - } - if (deletedLocks == 0) { - eu.eudat.data.old.Lock recentlock = locks.stream().max(compareByTouchedAt).get(); - for (eu.eudat.data.old.Lock lock : locks) { - if (lock != recentlock) { - this.apiContext.getOperationsContext().getDatabaseRepository().getLockDao().delete(lock); - deletedLocks++; - } - } - } - } - return availableLocks - deletedLocks; - } - - public void unlock(String targetId) throws Exception { - LockCriteria criteria = new LockCriteria(); - criteria.setTarget(UUID.fromString(targetId)); - Long availableLocks = this.apiContext.getOperationsContext().getDatabaseRepository().getLockDao().getWithCriteria(criteria).count(); - if (availableLocks == 1) { - eu.eudat.data.old.Lock lock = this.apiContext.getOperationsContext().getDatabaseRepository().getLockDao().getWithCriteria(criteria).getSingle(); - if (!lock.getLockedBy().getId().equals(this.userScope.getUserId())) { - throw new Exception("Only the user who created that lock can delete it"); - } - this.apiContext.getOperationsContext().getDatabaseRepository().getLockDao().delete(lock); - } else if (availableLocks > 1) { - List locks = this.apiContext.getOperationsContext().getDatabaseRepository().getLockDao().getWithCriteria(criteria).toList(); - locks.stream().filter(lock -> lock.getLockedBy().getId().equals(this.userScope.getUserIdSafe())).forEach(lock -> this.apiContext.getOperationsContext().getDatabaseRepository().getLockDao().delete(lock)); - } - } - - public Lock getFromTarget(String targetId) throws Exception { - LockCriteria criteria = new LockCriteria(); - criteria.setTarget(UUID.fromString(targetId)); - Long availableLocks = this.apiContext.getOperationsContext().getDatabaseRepository().getLockDao().getWithCriteria(criteria).count(); - if (availableLocks > 0) { - eu.eudat.data.old.Lock lock = this.apiContext.getOperationsContext().getDatabaseRepository().getLockDao().getWithCriteria(criteria).getSingle(); - if (!lock.getLockedBy().getId().equals(this.userScope.getUserId())) { - throw new Exception("Only the user who created that lock can access it"); - } - return new Lock().fromDataModel(lock); - } - return null; - } -} diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/services/operations/DatabaseRepository.java b/dmp-backend/web/src/main/java/eu/eudat/logic/services/operations/DatabaseRepository.java index eb8364d24..b366583b2 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/services/operations/DatabaseRepository.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/services/operations/DatabaseRepository.java @@ -19,8 +19,6 @@ public interface DatabaseRepository { EmailConfirmationDao getLoginConfirmationEmailDao(); - LockDao getLockDao(); - NotificationDao getNotificationDao(); FileUploadDao getFileUploadDao(); diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/services/operations/DatabaseRepositoryImpl.java b/dmp-backend/web/src/main/java/eu/eudat/logic/services/operations/DatabaseRepositoryImpl.java index db281130e..4f48d5b06 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/services/operations/DatabaseRepositoryImpl.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/services/operations/DatabaseRepositoryImpl.java @@ -22,8 +22,6 @@ public class DatabaseRepositoryImpl implements DatabaseRepository { private EmailConfirmationDao loginConfirmationEmailDao; - private LockDao lockDao; - private NotificationDao notificationDao; private FileUploadDao fileUploadDao; @@ -99,16 +97,6 @@ public class DatabaseRepositoryImpl implements DatabaseRepository { this.loginConfirmationEmailDao = loginConfirmationEmailDao; } - @Autowired - public void setLockDao(LockDao lockDao) { - this.lockDao = lockDao; - } - - @Override - public LockDao getLockDao() { - return lockDao; - } - @Override public NotificationDao getNotificationDao() { return notificationDao; diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/data/lock/Lock.java b/dmp-backend/web/src/main/java/eu/eudat/models/data/lock/Lock.java deleted file mode 100644 index 1ab6afa73..000000000 --- a/dmp-backend/web/src/main/java/eu/eudat/models/data/lock/Lock.java +++ /dev/null @@ -1,73 +0,0 @@ -package eu.eudat.models.data.lock; - -import eu.eudat.model.User; -import eu.eudat.models.DataModel; - -import java.util.Date; -import java.util.UUID; - -public class Lock implements DataModel { - private UUID id; - private UUID target; - private User lockedBy; - private Date lockedAt; - private Date touchedAt; - - public UUID getId() { - return id; - } - - public void setId(UUID id) { - this.id = id; - } - - public UUID getTarget() { - return target; - } - - public void setTarget(UUID target) { - this.target = target; - } - - public Date getLockedAt() { - return lockedAt; - } - - public void setLockedAt(Date lockedAt) { - this.lockedAt = lockedAt; - } - - public Date getTouchedAt() { - return touchedAt; - } - - public void setTouchedAt(Date touchedAt) { - this.touchedAt = touchedAt; - } - - @Override - public Lock fromDataModel(eu.eudat.data.old.Lock entity) { - this.id = entity.getId(); - this.target = entity.getTarget(); -// this.lockedBy = new UserInfo().fromDataModel(entity.getLockedBy()); - this.lockedAt = entity.getLockedAt(); - this.touchedAt = entity.getTouchedAt(); - return this; - } - - @Override - public eu.eudat.data.old.Lock toDataModel() throws Exception { - eu.eudat.data.old.Lock entity = new eu.eudat.data.old.Lock(); - entity.setId(this.getId()); - entity.setTarget(this.getTarget()); - entity.setLockedAt(this.getLockedAt()); - entity.setTouchedAt(this.getTouchedAt()); -// entity.setLockedBy(this.getLockedBy().toDataModel()); - return entity; - } - - @Override - public String getHint() { - return null; - } -}