Fixing user deleter not deleting contact info and credentials

This commit is contained in:
Thomas Georgios Giannos 2024-02-01 13:30:47 +02:00
parent 30abd31974
commit 20ffacad0d
3 changed files with 190 additions and 80 deletions

View File

@ -1,5 +1,6 @@
package gr.cite.notification.model.deleter; package gr.cite.notification.model.deleter;
import gr.cite.notification.common.enums.IsActive;
import gr.cite.notification.data.TenantScopedEntityManager; import gr.cite.notification.data.TenantScopedEntityManager;
import gr.cite.notification.data.UserContactInfoEntity; import gr.cite.notification.data.UserContactInfoEntity;
import gr.cite.notification.query.UserContactInfoQuery; import gr.cite.notification.query.UserContactInfoQuery;
@ -24,50 +25,51 @@ import java.util.UUID;
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserContactInfoDeleter implements Deleter { public class UserContactInfoDeleter implements Deleter {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UserContactInfoDeleter.class)); private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UserContactInfoDeleter.class));
private final TenantScopedEntityManager entityManager; private final TenantScopedEntityManager entityManager;
private final QueryFactory queryFactory;
private final DeleterFactory deleterFactory;
@Autowired private final QueryFactory queryFactory;
public UserContactInfoDeleter(
TenantScopedEntityManager entityManager,
QueryFactory queryFactory,
DeleterFactory deleterFactory
) {
this.entityManager = entityManager;
this.queryFactory = queryFactory;
this.deleterFactory = deleterFactory;
}
public void deleteAndSaveByIds(List<UUID> ids) throws InvalidApplicationException { @Autowired
logger.debug(new MapLogEntry("collecting to delete").And("count", Optional.ofNullable(ids).map(List::size).orElse(0)).And("ids", ids)); public UserContactInfoDeleter(
List<UserContactInfoEntity> data = this.queryFactory.query(UserContactInfoQuery.class).ids(ids).collect(); TenantScopedEntityManager entityManager,
logger.trace("retrieved {} items", Optional.ofNullable(data).map(List::size).orElse(0)); QueryFactory queryFactory
this.deleteAndSave(data); ) {
} this.entityManager = entityManager;
this.queryFactory = queryFactory;
}
public void deleteAndSave(List<UserContactInfoEntity> data) throws InvalidApplicationException { public void deleteAndSaveByIds(List<UUID> ids) throws InvalidApplicationException {
logger.debug("will delete {} items", Optional.ofNullable(data).map(List::size).orElse(0)); logger.debug(new MapLogEntry("collecting to delete").And("count", Optional.ofNullable(ids).map(List::size).orElse(0)).And("ids", ids));
this.delete(data); List<UserContactInfoEntity> data = this.queryFactory.query(UserContactInfoQuery.class).ids(ids).collect();
logger.trace("saving changes"); logger.trace("retrieved {} items", Optional.ofNullable(data).map(List::size).orElse(0));
this.entityManager.flush(); this.deleteAndSave(data);
logger.trace("changes saved"); }
}
public void delete(List<UserContactInfoEntity> data) throws InvalidApplicationException { public void deleteAndSave(List<UserContactInfoEntity> data) throws InvalidApplicationException {
logger.debug("will delete {} items", Optional.ofNullable(data).map(List::size).orElse(0)); logger.debug("will delete {} items", Optional.ofNullable(data).map(List::size).orElse(0));
if (data == null || data.isEmpty()) return; this.delete(data);
logger.trace("saving changes");
this.entityManager.flush();
logger.trace("changes saved");
}
Instant now = Instant.now(); public void delete(List<UserContactInfoEntity> data) throws InvalidApplicationException {
logger.debug("will delete {} items", Optional.ofNullable(data).map(List::size).orElse(0));
if (data == null || data.isEmpty())
return;
for (UserContactInfoEntity item : data) { Instant now = Instant.now();
logger.trace("deleting item {}", item);
logger.trace("updating item"); for (UserContactInfoEntity item : data) {
this.entityManager.merge(item); logger.trace("deleting item {}", item);
logger.trace("updated item"); item.setIsActive(IsActive.Inactive);
} item.setUpdatedAt(now);
} logger.trace("updating item");
this.entityManager.merge(item);
logger.trace("updated item");
}
}
} }

View File

@ -0,0 +1,74 @@
package gr.cite.notification.model.deleter;
import gr.cite.notification.common.enums.IsActive;
import gr.cite.notification.data.TenantScopedEntityManager;
import gr.cite.notification.data.UserCredentialEntity;
import gr.cite.notification.query.UserCredentialQuery;
import gr.cite.tools.data.deleter.Deleter;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.management.InvalidApplicationException;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserCredentialDeleter implements Deleter {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UserCredentialDeleter.class));
private final TenantScopedEntityManager entityManager;
private final QueryFactory queryFactory;
@Autowired
public UserCredentialDeleter(
TenantScopedEntityManager entityManager,
QueryFactory queryFactory
) {
this.entityManager = entityManager;
this.queryFactory = queryFactory;
}
public void deleteAndSaveByIds(List<UUID> ids) throws InvalidApplicationException {
logger.debug(new MapLogEntry("collecting to delete").And("count", Optional.ofNullable(ids).map(List::size).orElse(0)).And("ids", ids));
List<UserCredentialEntity> data = this.queryFactory.query(UserCredentialQuery.class).ids(ids).collect();
logger.trace("retrieved {} items", Optional.ofNullable(data).map(List::size).orElse(0));
this.deleteAndSave(data);
}
public void deleteAndSave(List<UserCredentialEntity> data) throws InvalidApplicationException {
logger.debug("will delete {} items", Optional.ofNullable(data).map(List::size).orElse(0));
this.delete(data);
logger.trace("saving changes");
this.entityManager.flush();
logger.trace("changes saved");
}
public void delete(List<UserCredentialEntity> data) throws InvalidApplicationException {
logger.debug("will delete {} items", Optional.ofNullable(data).map(List::size).orElse(0));
if (data == null || data.isEmpty())
return;
Instant now = Instant.now();
for (UserCredentialEntity item : data) {
logger.trace("deleting item {}", item);
item.setIsActive(IsActive.Inactive);
item.setUpdatedAt(now);
logger.trace("updating item");
this.entityManager.merge(item);
logger.trace("updated item");
}
}
}

View File

@ -1,13 +1,18 @@
package gr.cite.notification.model.deleter; package gr.cite.notification.model.deleter;
import gr.cite.notification.common.enums.IsActive; import gr.cite.notification.common.enums.IsActive;
import gr.cite.notification.data.*; import gr.cite.notification.data.UserContactInfoEntity;
import gr.cite.notification.query.*; import gr.cite.notification.data.UserCredentialEntity;
import gr.cite.notification.data.UserEntity;
import gr.cite.notification.query.UserContactInfoQuery;
import gr.cite.notification.query.UserCredentialQuery;
import gr.cite.notification.query.UserQuery;
import gr.cite.tools.data.deleter.Deleter; import gr.cite.tools.data.deleter.Deleter;
import gr.cite.tools.data.deleter.DeleterFactory; import gr.cite.tools.data.deleter.DeleterFactory;
import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry; import gr.cite.tools.logging.MapLogEntry;
import jakarta.persistence.EntityManager;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
@ -19,55 +24,84 @@ import java.time.Instant;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
@Component @Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserDeleter implements Deleter { public class UserDeleter implements Deleter {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UserDeleter.class));
private final TenantScopedEntityManager entityManager; private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UserDeleter.class));
private final QueryFactory queryFactory;
private final DeleterFactory deleterFactory;
@Autowired private final EntityManager entityManager;
public UserDeleter(
TenantScopedEntityManager entityManager,
QueryFactory queryFactory,
DeleterFactory deleterFactory
) {
this.entityManager = entityManager;
this.queryFactory = queryFactory;
this.deleterFactory = deleterFactory;
}
public void deleteAndSaveByIds(List<UUID> ids) throws InvalidApplicationException { protected final QueryFactory queryFactory;
logger.debug(new MapLogEntry("collecting to delete").And("count", Optional.ofNullable(ids).map(e -> e.size()).orElse(0)).And("ids", ids));
List<UserEntity> datas = this.queryFactory.query(UserQuery.class).ids(ids).collect();
logger.trace("retrieved {} items", Optional.ofNullable(datas).map(e -> e.size()).orElse(0));
this.deleteAndSave(datas);
}
public void deleteAndSave(List<UserEntity> datas) throws InvalidApplicationException { protected final DeleterFactory deleterFactory;
logger.debug("will delete {} items", Optional.ofNullable(datas).map(e -> e.size()).orElse(0));
this.delete(datas);
logger.trace("saving changes");
this.entityManager.flush();
logger.trace("changes saved");
}
public void delete(List<UserEntity> datas) throws InvalidApplicationException { @Autowired
logger.debug("will delete {} items", Optional.ofNullable(datas).map(x -> x.size()).orElse(0)); public UserDeleter(
if (datas == null || datas.isEmpty()) return; EntityManager entityManager,
QueryFactory queryFactory,
DeleterFactory deleterFactory
) {
this.entityManager = entityManager;
this.queryFactory = queryFactory;
this.deleterFactory = deleterFactory;
}
Instant now = Instant.now(); public void deleteAndSaveByIds(List<UUID> ids) throws InvalidApplicationException {
logger.debug(new MapLogEntry("collecting to delete").And("count", Optional.ofNullable(ids).map(List::size).orElse(0)).And("ids", ids));
List<UserEntity> data = this.queryFactory.query(UserQuery.class).ids(ids).collect();
logger.trace("retrieved {} items", Optional.ofNullable(data).map(List::size).orElse(0));
this.deleteAndSave(data);
}
for (UserEntity item : datas) { public void deleteAndSave(List<UserEntity> data) throws InvalidApplicationException {
logger.trace("deleting item {}", item.getId()); logger.debug("will delete {} items", Optional.ofNullable(data).map(List::size).orElse(0));
item.setIsActive(IsActive.Inactive); this.delete(data);
item.setUpdatedAt(now); logger.trace("saving changes");
logger.trace("updating item"); this.entityManager.flush();
this.entityManager.merge(item); logger.trace("changes saved");
logger.trace("updated item"); }
}
} public void delete(List<UserEntity> data) throws InvalidApplicationException {
logger.debug("will delete {} items", Optional.ofNullable(data).map(List::size).orElse(0));
if (data == null || data.isEmpty())
return;
List<UUID> ids = data.stream().map(UserEntity::getId).distinct().collect(Collectors.toList());
// {
// logger.debug("checking related - {}", UserRoleEntity.class.getSimpleName());
// List<UserRoleEntity> items = this.queryFactory.query(UserRoleQuery.class).userIds(ids).collect();
// UserRoleDeleter deleter = this.deleterFactory.deleter(UserRoleDeleter.class);
// deleter.delete(items);
// }
{
logger.debug("checking related - {}", UserCredentialEntity.class.getSimpleName());
List<UserCredentialEntity> items = this.queryFactory.query(UserCredentialQuery.class).userIds(ids).collect();
UserCredentialDeleter deleter = this.deleterFactory.deleter(UserCredentialDeleter.class);
deleter.delete(items);
}
{
logger.debug("checking related - {}", UserContactInfoEntity.class.getSimpleName());
List<UserContactInfoEntity> items = this.queryFactory.query(UserContactInfoQuery.class).userIds(ids).collect();
UserContactInfoDeleter deleter = this.deleterFactory.deleter(UserContactInfoDeleter.class);
deleter.delete(items);
}
// {
// logger.debug("checking related - {}", TenantUserEntity.class.getSimpleName());
// List<TenantUserEntity> items = this.queryFactory.query(TenantUserQuery.class).userIds(ids).collect();
// TenantUserDeleter deleter = this.deleterFactory.deleter(TenantUserDeleter.class);
// deleter.delete(items);
// }
Instant now = Instant.now();
for (UserEntity item : data) {
logger.trace("deleting item {}", item.getId());
item.setIsActive(IsActive.Inactive);
item.setUpdatedAt(now);
logger.trace("updating item");
this.entityManager.merge(item);
logger.trace("updated item");
}
}
} }