not allow delete templates used by descriptions

This commit is contained in:
Efstratios Giannopoulos 2024-08-19 14:48:17 +03:00
parent 50dd089dac
commit 0e05a8a97c
2 changed files with 17 additions and 11 deletions

View File

@ -9,6 +9,7 @@ import gr.cite.tools.logging.DataLogEntry;
import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.LoggerService;
import org.opencdmp.authorization.AuthorizationFlags; import org.opencdmp.authorization.AuthorizationFlags;
import org.opencdmp.commons.enums.DescriptionTemplateVersionStatus; import org.opencdmp.commons.enums.DescriptionTemplateVersionStatus;
import org.opencdmp.commons.enums.IsActive;
import org.opencdmp.commons.scope.tenant.TenantScope; import org.opencdmp.commons.scope.tenant.TenantScope;
import org.opencdmp.convention.ConventionService; import org.opencdmp.convention.ConventionService;
import org.opencdmp.data.PlanDescriptionTemplateEntity; import org.opencdmp.data.PlanDescriptionTemplateEntity;
@ -155,7 +156,7 @@ public class PlanDescriptionTemplateBuilder extends BaseBuilder<PlanDescriptionT
DescriptionTemplate::getGroupId); DescriptionTemplate::getGroupId);
} else { } else {
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(DescriptionTemplate._id, DescriptionTemplate._groupId); FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(DescriptionTemplate._id, DescriptionTemplate._groupId);
DescriptionTemplateQuery q = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking().authorize(this.authorize).versionStatuses(DescriptionTemplateVersionStatus.Current).groupIds(data.stream().map(PlanDescriptionTemplateEntity::getDescriptionTemplateGroupId).distinct().collect(Collectors.toList())); DescriptionTemplateQuery q = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking().authorize(this.authorize).versionStatuses(DescriptionTemplateVersionStatus.Current).isActive(IsActive.Active).groupIds(data.stream().map(PlanDescriptionTemplateEntity::getDescriptionTemplateGroupId).distinct().collect(Collectors.toList()));
itemMap = this.builderFactory.builder(DescriptionTemplateBuilder.class).authorize(this.authorize).asForeignKey(q, clone, DescriptionTemplate::getGroupId); itemMap = this.builderFactory.builder(DescriptionTemplateBuilder.class).authorize(this.authorize).asForeignKey(q, clone, DescriptionTemplate::getGroupId);
} }
if (!fields.hasField(DescriptionTemplate._groupId)) { if (!fields.hasField(DescriptionTemplate._groupId)) {

View File

@ -1,17 +1,22 @@
package org.opencdmp.model.deleter; package org.opencdmp.model.deleter;
import org.opencdmp.commons.enums.IsActive;
import org.opencdmp.commons.enums.UsageLimitTargetMetric;
import org.opencdmp.data.DescriptionTemplateEntity;
import org.opencdmp.data.TenantEntityManager;
import org.opencdmp.data.UserDescriptionTemplateEntity;
import org.opencdmp.query.DescriptionTemplateQuery;
import org.opencdmp.query.UserDescriptionTemplateQuery;
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.exception.MyApplicationException;
import gr.cite.tools.fieldset.BaseFieldSet;
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 org.opencdmp.commons.enums.IsActive;
import org.opencdmp.commons.enums.UsageLimitTargetMetric;
import org.opencdmp.data.DescriptionEntity;
import org.opencdmp.data.DescriptionTemplateEntity;
import org.opencdmp.data.TenantEntityManager;
import org.opencdmp.data.UserDescriptionTemplateEntity;
import org.opencdmp.model.description.Description;
import org.opencdmp.query.DescriptionQuery;
import org.opencdmp.query.DescriptionTemplateQuery;
import org.opencdmp.query.UserDescriptionTemplateQuery;
import org.opencdmp.service.accounting.AccountingService; import org.opencdmp.service.accounting.AccountingService;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -27,7 +32,7 @@ import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Component @Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DescriptionTemplateDeleter implements Deleter { public class DescriptionTemplateDeleter implements Deleter {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionTemplateDeleter.class)); private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionTemplateDeleter.class));
@ -80,12 +85,12 @@ public class DescriptionTemplateDeleter implements Deleter {
UserDescriptionTemplateDeleter deleter = this.deleterFactory.deleter(UserDescriptionTemplateDeleter.class); UserDescriptionTemplateDeleter deleter = this.deleterFactory.deleter(UserDescriptionTemplateDeleter.class);
deleter.delete(items); deleter.delete(items);
} }
List<DescriptionEntity> activeDescriptions = this.queryFactory.query(DescriptionQuery.class).isActive(IsActive.Active).descriptionTemplateSubQuery(this.queryFactory.query(DescriptionTemplateQuery.class).ids(ids)).collectAs(new BaseFieldSet().ensure(Description._id).ensure(Description._descriptionTemplate));
//TODO can not delete profile if has Datasets
Instant now = Instant.now(); Instant now = Instant.now();
for (DescriptionTemplateEntity item : data) { for (DescriptionTemplateEntity item : data) {
if (activeDescriptions.stream().map(DescriptionEntity::getDescriptionTemplateId).anyMatch(x-> x.equals(item.getId()))) throw new MyApplicationException("template " + item.getCode() + " used by descriptions");
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);