remove old lock

This commit is contained in:
Efstratios Giannopoulos 2024-01-04 13:05:06 +02:00
parent 73b0272672
commit 5b18809da0
11 changed files with 0 additions and 553 deletions

View File

@ -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<Lock, UUID> {
@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> tuple, List<String> fields, String base) {
String currentBase = base.isEmpty() ? "" : base + ".";
if (fields.contains(currentBase + "id")) this.id = EntityBinder.fromTuple(tuple, currentBase + "id");
return this;
}
}

View File

@ -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<Lock> {
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;
}
}

View File

@ -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<Lock, UUID> {
QueryableList<Lock> getWithCriteria(LockCriteria criteria);
}

View File

@ -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<Lock> implements LockDao {
@Autowired
public LockDaoImpl(DatabaseService<Lock> databaseService) {
super(databaseService);
}
@Override
public QueryableList<Lock> getWithCriteria(LockCriteria criteria) {
QueryableList<Lock> 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<Lock> 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<Lock> asQueryable() {
return this.getDatabaseService().getQueryable(Lock.class);
}
}

View File

@ -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<LockCriteria, Lock> {
@Override
public QueryableList<Lock> applyCriteria() {
QueryableList<Lock> 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;
}
}

View File

@ -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<LockCriteria, Lock, UUID> {
@Override
public QueryableList<Lock> applyCriteria() {
QueryableList<Lock> 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<Lock> applyPaging(QueryableList<Lock> items) {
return PaginationService.applyPaging(items, this);
}
}

View File

@ -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<Lock, UUID> {
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<Lock, UUID> databaseAccessLayer, List<String> selectionFields) {
super(databaseAccessLayer, selectionFields);
}
public LockQuery(DatabaseAccessLayer<Lock, UUID> databaseAccessLayer) {
super(databaseAccessLayer);
}
@Override
public QueryableList<Lock> getQuery() throws InvalidApplicationException {
QueryableList<Lock> 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<UserEntity> 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;
}
}

View File

@ -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<eu.eudat.data.old.Lock> 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<eu.eudat.data.old.Lock> 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<eu.eudat.data.old.Lock> 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;
}
}

View File

@ -19,8 +19,6 @@ public interface DatabaseRepository {
EmailConfirmationDao getLoginConfirmationEmailDao(); EmailConfirmationDao getLoginConfirmationEmailDao();
LockDao getLockDao();
NotificationDao getNotificationDao(); NotificationDao getNotificationDao();
FileUploadDao getFileUploadDao(); FileUploadDao getFileUploadDao();

View File

@ -22,8 +22,6 @@ public class DatabaseRepositoryImpl implements DatabaseRepository {
private EmailConfirmationDao loginConfirmationEmailDao; private EmailConfirmationDao loginConfirmationEmailDao;
private LockDao lockDao;
private NotificationDao notificationDao; private NotificationDao notificationDao;
private FileUploadDao fileUploadDao; private FileUploadDao fileUploadDao;
@ -99,16 +97,6 @@ public class DatabaseRepositoryImpl implements DatabaseRepository {
this.loginConfirmationEmailDao = loginConfirmationEmailDao; this.loginConfirmationEmailDao = loginConfirmationEmailDao;
} }
@Autowired
public void setLockDao(LockDao lockDao) {
this.lockDao = lockDao;
}
@Override
public LockDao getLockDao() {
return lockDao;
}
@Override @Override
public NotificationDao getNotificationDao() { public NotificationDao getNotificationDao() {
return notificationDao; return notificationDao;

View File

@ -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<eu.eudat.data.old.Lock, Lock> {
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;
}
}