82 lines
3.1 KiB
Java
82 lines
3.1 KiB
Java
package eu.eudat.model.deleter;
|
|
|
|
import eu.eudat.commons.enums.IsActive;
|
|
import eu.eudat.data.TenantConfigurationEntity;
|
|
import eu.eudat.data.TenantEntity;
|
|
import eu.eudat.data.TenantEntityManager;
|
|
import eu.eudat.query.TenantConfigurationQuery;
|
|
import eu.eudat.query.TenantQuery;
|
|
import gr.cite.tools.data.deleter.Deleter;
|
|
import gr.cite.tools.data.deleter.DeleterFactory;
|
|
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 TenantConfigurationDeleter implements Deleter {
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(TenantConfigurationDeleter.class));
|
|
|
|
private final TenantEntityManager entityManager;
|
|
|
|
protected final QueryFactory queryFactory;
|
|
|
|
protected final DeleterFactory deleterFactory;
|
|
|
|
@Autowired
|
|
public TenantConfigurationDeleter(
|
|
TenantEntityManager entityManager,
|
|
QueryFactory queryFactory,
|
|
DeleterFactory deleterFactory
|
|
) {
|
|
this.entityManager = entityManager;
|
|
this.queryFactory = queryFactory;
|
|
this.deleterFactory = deleterFactory;
|
|
}
|
|
|
|
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<TenantConfigurationEntity> data = this.queryFactory.query(TenantConfigurationQuery.class).ids(ids).collect();
|
|
logger.trace("retrieved {} items", Optional.ofNullable(data).map(List::size).orElse(0));
|
|
this.deleteAndSave(data);
|
|
}
|
|
|
|
public void deleteAndSave(List<TenantConfigurationEntity> 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<TenantConfigurationEntity> 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 (TenantConfigurationEntity 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");
|
|
}
|
|
}
|
|
|
|
}
|