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;
@ -27,18 +28,16 @@ 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 QueryFactory queryFactory;
private final DeleterFactory deleterFactory;
@Autowired @Autowired
public UserContactInfoDeleter( public UserContactInfoDeleter(
TenantScopedEntityManager entityManager, TenantScopedEntityManager entityManager,
QueryFactory queryFactory, QueryFactory queryFactory
DeleterFactory deleterFactory
) { ) {
this.entityManager = entityManager; this.entityManager = entityManager;
this.queryFactory = queryFactory; this.queryFactory = queryFactory;
this.deleterFactory = deleterFactory;
} }
public void deleteAndSaveByIds(List<UUID> ids) throws InvalidApplicationException { public void deleteAndSaveByIds(List<UUID> ids) throws InvalidApplicationException {
@ -58,12 +57,15 @@ public class UserContactInfoDeleter implements Deleter {
public void delete(List<UserContactInfoEntity> data) throws InvalidApplicationException { public void delete(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; if (data == null || data.isEmpty())
return;
Instant now = Instant.now(); Instant now = Instant.now();
for (UserContactInfoEntity item : data) { for (UserContactInfoEntity item : data) {
logger.trace("deleting item {}", item); logger.trace("deleting item {}", item);
item.setIsActive(IsActive.Inactive);
item.setUpdatedAt(now);
logger.trace("updating item"); logger.trace("updating item");
this.entityManager.merge(item); this.entityManager.merge(item);
logger.trace("updated 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,19 +24,23 @@ 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 static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UserDeleter.class));
private final TenantScopedEntityManager entityManager; private final EntityManager entityManager;
private final QueryFactory queryFactory;
private final DeleterFactory deleterFactory; protected final QueryFactory queryFactory;
protected final DeleterFactory deleterFactory;
@Autowired @Autowired
public UserDeleter( public UserDeleter(
TenantScopedEntityManager entityManager, EntityManager entityManager,
QueryFactory queryFactory, QueryFactory queryFactory,
DeleterFactory deleterFactory DeleterFactory deleterFactory
) { ) {
@ -41,27 +50,52 @@ public class UserDeleter implements Deleter {
} }
public void deleteAndSaveByIds(List<UUID> ids) throws InvalidApplicationException { public void deleteAndSaveByIds(List<UUID> ids) throws InvalidApplicationException {
logger.debug(new MapLogEntry("collecting to delete").And("count", Optional.ofNullable(ids).map(e -> e.size()).orElse(0)).And("ids", ids)); logger.debug(new MapLogEntry("collecting to delete").And("count", Optional.ofNullable(ids).map(List::size).orElse(0)).And("ids", ids));
List<UserEntity> datas = this.queryFactory.query(UserQuery.class).ids(ids).collect(); List<UserEntity> data = this.queryFactory.query(UserQuery.class).ids(ids).collect();
logger.trace("retrieved {} items", Optional.ofNullable(datas).map(e -> e.size()).orElse(0)); logger.trace("retrieved {} items", Optional.ofNullable(data).map(List::size).orElse(0));
this.deleteAndSave(datas); this.deleteAndSave(data);
} }
public void deleteAndSave(List<UserEntity> datas) throws InvalidApplicationException { public void deleteAndSave(List<UserEntity> data) throws InvalidApplicationException {
logger.debug("will delete {} items", Optional.ofNullable(datas).map(e -> e.size()).orElse(0)); logger.debug("will delete {} items", Optional.ofNullable(data).map(List::size).orElse(0));
this.delete(datas); this.delete(data);
logger.trace("saving changes"); logger.trace("saving changes");
this.entityManager.flush(); this.entityManager.flush();
logger.trace("changes saved"); logger.trace("changes saved");
} }
public void delete(List<UserEntity> datas) throws InvalidApplicationException { public void delete(List<UserEntity> data) throws InvalidApplicationException {
logger.debug("will delete {} items", Optional.ofNullable(datas).map(x -> x.size()).orElse(0)); logger.debug("will delete {} items", Optional.ofNullable(data).map(List::size).orElse(0));
if (datas == null || datas.isEmpty()) return; 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(); Instant now = Instant.now();
for (UserEntity item : datas) { for (UserEntity item : data) {
logger.trace("deleting item {}", item.getId()); logger.trace("deleting item {}", item.getId());
item.setIsActive(IsActive.Inactive); item.setIsActive(IsActive.Inactive);
item.setUpdatedAt(now); item.setUpdatedAt(now);