diff --git a/backend/core/src/main/java/org/opencdmp/audit/AuditableAction.java b/backend/core/src/main/java/org/opencdmp/audit/AuditableAction.java index dda9850f5..3624dcfbd 100644 --- a/backend/core/src/main/java/org/opencdmp/audit/AuditableAction.java +++ b/backend/core/src/main/java/org/opencdmp/audit/AuditableAction.java @@ -202,6 +202,11 @@ public class AuditableAction { public static final EventId PlanStatus_Persist = new EventId(300002, "PlanStatus_Persist"); public static final EventId PlanStatus_Delete = new EventId(300003, "PlanStatus_Delete"); + public static final EventId DescriptionStatus_Query = new EventId(400000, "DescriptionStatus_Query"); + public static final EventId DescriptionStatus_Lookup = new EventId(400001, "DescriptionStatus_Lookup"); + public static final EventId DescriptionStatus_Persist = new EventId(400002, "DescriptionStatus_Persist"); + public static final EventId DescriptionStatus_Delete = new EventId(400003, "DescriptionStatus_Delete"); + } diff --git a/backend/core/src/main/java/org/opencdmp/authorization/Permission.java b/backend/core/src/main/java/org/opencdmp/authorization/Permission.java index ad0c78e38..2cff235c5 100644 --- a/backend/core/src/main/java/org/opencdmp/authorization/Permission.java +++ b/backend/core/src/main/java/org/opencdmp/authorization/Permission.java @@ -95,6 +95,11 @@ public final class Permission { public static String EditPlanStatus = "EditPlanStatus"; public static String DeletePlanStatus = "DeletePlanStatus"; + //DescriptionStatus + public static String BrowseDescriptionStatus = "BrowseDescriptionStatus"; + public static String EditDescriptionStatus = "EditDescriptionStatus"; + public static String DeleteDescriptionStatus = "DeleteDescriptionStatus"; + //PlanBlueprint public static String BrowsePlanBlueprint = "BrowsePlanBlueprint"; public static String EditPlanBlueprint = "EditPlanBlueprint"; diff --git a/backend/core/src/main/java/org/opencdmp/commons/enums/UsageLimitTargetMetric.java b/backend/core/src/main/java/org/opencdmp/commons/enums/UsageLimitTargetMetric.java index d0b33eae2..522bd5ff0 100644 --- a/backend/core/src/main/java/org/opencdmp/commons/enums/UsageLimitTargetMetric.java +++ b/backend/core/src/main/java/org/opencdmp/commons/enums/UsageLimitTargetMetric.java @@ -11,6 +11,7 @@ public enum UsageLimitTargetMetric implements DatabaseEnum { PLAN_STATUS_COUNT(TargetMetrics.PlanStatusCount), BLUEPRINT_COUNT(TargetMetrics.BlueprintCount), DESCRIPTION_COUNT(TargetMetrics.DescriptionCount), + DESCRIPTION_STATUS_COUNT(TargetMetrics.DescriptionStatusCount), DESCRIPTION_TEMPLATE_COUNT(TargetMetrics.DescriptionTemplateCount), DESCRIPTION_TEMPLATE_TYPE_COUNT(TargetMetrics.DescriptionTemplateTypeCount), PREFILLING_SOURCES_COUNT(TargetMetrics.PrefillingSourcesCount), @@ -38,6 +39,7 @@ public enum UsageLimitTargetMetric implements DatabaseEnum { public static final String PlanStatusCount = "plan_status_count"; public static final String BlueprintCount = "blueprint_count"; public static final String DescriptionCount = "description_count"; + public static final String DescriptionStatusCount = "description_status_count"; public static final String DescriptionTemplateCount = "description_template_count"; public static final String DescriptionTemplateTypeCount = "description_template_type_count"; public static final String PrefillingSourcesCount = "prefilling_sources_count"; diff --git a/backend/core/src/main/java/org/opencdmp/commons/types/descriptionstatus/DescriptionStatusDefinitionAuthorizationEntity.java b/backend/core/src/main/java/org/opencdmp/commons/types/descriptionstatus/DescriptionStatusDefinitionAuthorizationEntity.java new file mode 100644 index 000000000..1d9c86327 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/commons/types/descriptionstatus/DescriptionStatusDefinitionAuthorizationEntity.java @@ -0,0 +1,18 @@ +package org.opencdmp.commons.types.descriptionstatus; + +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "authorization") +@XmlAccessorType(XmlAccessType.FIELD) +public class DescriptionStatusDefinitionAuthorizationEntity { + + @XmlElement(name = "edit") + private DescriptionStatusDefinitionAuthorizationItemEntity edit; + + public DescriptionStatusDefinitionAuthorizationItemEntity getEdit() { return edit; } + + public void setEdit(DescriptionStatusDefinitionAuthorizationItemEntity edit) { this.edit = edit; } +} diff --git a/backend/core/src/main/java/org/opencdmp/commons/types/descriptionstatus/DescriptionStatusDefinitionAuthorizationItemEntity.java b/backend/core/src/main/java/org/opencdmp/commons/types/descriptionstatus/DescriptionStatusDefinitionAuthorizationItemEntity.java new file mode 100644 index 000000000..00fc1cfd0 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/commons/types/descriptionstatus/DescriptionStatusDefinitionAuthorizationItemEntity.java @@ -0,0 +1,36 @@ +package org.opencdmp.commons.types.descriptionstatus; + +import jakarta.xml.bind.annotation.*; + +import java.util.List; + +@XmlRootElement(name = "edit") +@XmlAccessorType(XmlAccessType.FIELD) +public class DescriptionStatusDefinitionAuthorizationItemEntity { + + @XmlElementWrapper(name = "roles") + @XmlElement(name = "roles") + public List roles; + + @XmlElementWrapper(name = "plan_roles") + @XmlElement(name = "plan_roles") + public List planRoles; + + @XmlElement(name = "allowAuthenticated") + public Boolean allowAuthenticated; + + @XmlElement(name = "allowAnonymous") + public Boolean allowAnonymous; + + public List getRoles() { return this.roles; } + public void setRoles(List roles) { this.roles = roles; } + + public List getPlanRoles() { return this.planRoles; } + public void setPlanRoles(List planRoles) { this.planRoles = planRoles; } + + public Boolean getAllowAuthenticated() { return this.allowAuthenticated; } + public void setAllowAuthenticated(Boolean allowAuthenticated) { this.allowAuthenticated = allowAuthenticated; } + + public Boolean getAllowAnonymous() { return this.allowAnonymous; } + public void setAllowAnonymous(Boolean allowAnonymous) { this.allowAnonymous = allowAnonymous; } +} diff --git a/backend/core/src/main/java/org/opencdmp/commons/types/descriptionstatus/DescriptionStatusDefinitionEntity.java b/backend/core/src/main/java/org/opencdmp/commons/types/descriptionstatus/DescriptionStatusDefinitionEntity.java new file mode 100644 index 000000000..088f31a99 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/commons/types/descriptionstatus/DescriptionStatusDefinitionEntity.java @@ -0,0 +1,18 @@ +package org.opencdmp.commons.types.descriptionstatus; + +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "definition") +@XmlAccessorType(XmlAccessType.FIELD) +public class DescriptionStatusDefinitionEntity { + + @XmlElement(name = "authorization") + private DescriptionStatusDefinitionAuthorizationEntity authorization; + + public DescriptionStatusDefinitionAuthorizationEntity getAuthorization() { return authorization; } + + public void setAuthorization(DescriptionStatusDefinitionAuthorizationEntity authorization) { this.authorization = authorization; } +} diff --git a/backend/core/src/main/java/org/opencdmp/data/DescriptionStatusEntity.java b/backend/core/src/main/java/org/opencdmp/data/DescriptionStatusEntity.java new file mode 100644 index 000000000..4902842b6 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/data/DescriptionStatusEntity.java @@ -0,0 +1,100 @@ +package org.opencdmp.data; + +import jakarta.persistence.*; +import org.hibernate.annotations.Type; +import org.opencdmp.commons.enums.DescriptionStatus; +import org.opencdmp.commons.enums.IsActive; +import org.opencdmp.data.converters.enums.DescriptionTemplateStatusConverter; +import org.opencdmp.data.converters.enums.IsActiveConverter; +import org.opencdmp.data.tenant.TenantScopedBaseEntity; +import org.opencdmp.data.types.SQLXMLType; + +import java.time.Instant; +import java.util.UUID; + +@Entity +@Table(name = "\"DescriptionStatus\"") +public class DescriptionStatusEntity extends TenantScopedBaseEntity { + @Id + @Column(name = "id", columnDefinition = "uuid", updatable = false, nullable = false) + private UUID id; + public static final String _id = "id"; + + @Column(name = "name", length = DescriptionStatusEntity._nameLength, nullable = false) + private String name; + public static final String _name = "name"; + public static final int _nameLength = 250; + + @Column(name = "description", nullable = true) + private String description; + public static final String _description = "description"; + + @Column(name = "created_at", nullable = false) + private Instant createdAt; + public static final String _createdAt = "createdAt"; + + @Column(name = "updated_at", nullable = false) + private Instant updatedAt; + public static final String _updatedAt = "updatedAt"; + + @Column(name = "is_active", nullable = false) + @Convert(converter = IsActiveConverter.class) + private IsActive isActive; + public static final String _isActive = "isActive"; + + @Column(name = "internal_status", nullable = false) + @Convert(converter = DescriptionTemplateStatusConverter.class) + private DescriptionStatus internalStatus; + public static final String _internalStatus = "internalStatus"; + + @Type(SQLXMLType.class) + @Column(name = "definition", nullable = false, columnDefinition = "xml") + private String definition; + public static final String _definition = "definition"; + + public UUID getId() { + return this.id; + } + public void setId(UUID id) { this.id = id;} + + public String getName() { + return this.name; + } + public void setName(String name) { + this.name = name; + } + + public String getDescription() { return this.description; } + public void setDescription(String description) { this.description = description;} + + public Instant getCreatedAt() { + return this.createdAt; + } + public void setCreatedAt(Instant createdAt) { + this.createdAt = createdAt; + } + + public Instant getUpdatedAt() { + return this.updatedAt; + } + public void setUpdatedAt(Instant updatedAt) { + this.updatedAt = updatedAt; + } + + public IsActive getIsActive() { + return this.isActive; + } + public void setIsActive(IsActive isActive) { + this.isActive = isActive; + } + + public DescriptionStatus getInternalStatus() { return internalStatus; } + public void setInternalStatus(DescriptionStatus internalStatus) { this.internalStatus = internalStatus; } + + public String getDefinition() { + return this.definition; + } + public void setDefinition(String definition) { + this.definition = definition; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/builder/descriptionstatus/DescriptionStatusBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/descriptionstatus/DescriptionStatusBuilder.java new file mode 100644 index 000000000..4174dc6d2 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/builder/descriptionstatus/DescriptionStatusBuilder.java @@ -0,0 +1,70 @@ +package org.opencdmp.model.builder.descriptionstatus; + +import gr.cite.tools.data.builder.BuilderFactory; +import gr.cite.tools.exception.MyApplicationException; +import gr.cite.tools.fieldset.FieldSet; +import gr.cite.tools.logging.DataLogEntry; +import gr.cite.tools.logging.LoggerService; +import org.opencdmp.authorization.AuthorizationFlags; +import org.opencdmp.commons.XmlHandlingService; +import org.opencdmp.commons.types.descriptionstatus.DescriptionStatusDefinitionEntity; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.data.DescriptionStatusEntity; +import org.opencdmp.model.builder.BaseBuilder; +import org.opencdmp.model.descriptionstatus.DescriptionStatus; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import java.util.*; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class DescriptionStatusBuilder extends BaseBuilder { + + private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); + private final BuilderFactory builderFactory; + private final XmlHandlingService xmlHandlingService; + + public DescriptionStatusBuilder(ConventionService conventionService, BuilderFactory builderFactory, XmlHandlingService xmlHandlingService) { + super(conventionService, new LoggerService(LoggerFactory.getLogger(DescriptionStatusBuilder.class))); + this.builderFactory = builderFactory; + this.xmlHandlingService = xmlHandlingService; + } + + public DescriptionStatusBuilder authorize(EnumSet values) { + this.authorize = values; + return this; + } + + @Override + public List build(FieldSet fields, List data) throws MyApplicationException { + this.logger.debug("building for {} items requesting {} fields", Optional.ofNullable(data).map(List::size).orElse(0), Optional.ofNullable(fields).map(FieldSet::getFields).map(Set::size).orElse(0)); + this.logger.trace(new DataLogEntry("requested fields", fields)); + if (fields == null || data == null || fields.isEmpty()) + return new ArrayList<>(); + + List models = new ArrayList<>(); + + FieldSet definitionFields = fields.extractPrefixed(this.asPrefix(DescriptionStatus._definition)); + for (DescriptionStatusEntity d : data) { + DescriptionStatus m = new DescriptionStatus(); + if (fields.hasField(this.asIndexer(DescriptionStatus._id))) m.setId(d.getId()); + if (fields.hasField(this.asIndexer(DescriptionStatus._name))) m.setName(d.getName()); + if (fields.hasField(this.asIndexer(DescriptionStatus._description))) m.setDescription(d.getDescription()); + if (fields.hasField(this.asIndexer(DescriptionStatus._updatedAt))) m.setUpdateAt(d.getUpdatedAt()); + if (fields.hasField(this.asIndexer(DescriptionStatus._createdAt))) m.setCreatedAt(d.getCreatedAt()); + if (fields.hasField(this.asIndexer(DescriptionStatus._isActive))) m.setIsActive(d.getIsActive()); + if (fields.hasField(this.asIndexer(DescriptionStatus._internalStatus))) m.setInternalStatus(d.getInternalStatus()); + + if (!definitionFields.isEmpty() && d.getDefinition() != null) { + DescriptionStatusDefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(DescriptionStatusDefinitionEntity.class, d.getDefinition()); + m.setDefinition(this.builderFactory.builder(DescriptionStatusDefinitionBuilder.class).authorize(authorize).build(definitionFields, definition)); + } + + models.add(m); + } + return models; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/builder/descriptionstatus/DescriptionStatusDefinitionAuthorizationBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/descriptionstatus/DescriptionStatusDefinitionAuthorizationBuilder.java new file mode 100644 index 000000000..d1ef04cc8 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/builder/descriptionstatus/DescriptionStatusDefinitionAuthorizationBuilder.java @@ -0,0 +1,57 @@ +package org.opencdmp.model.builder.descriptionstatus; + +import gr.cite.tools.data.builder.BuilderFactory; +import gr.cite.tools.exception.MyApplicationException; +import gr.cite.tools.fieldset.FieldSet; +import gr.cite.tools.logging.DataLogEntry; +import gr.cite.tools.logging.LoggerService; +import org.opencdmp.authorization.AuthorizationFlags; +import org.opencdmp.commons.types.descriptionstatus.DescriptionStatusDefinitionAuthorizationEntity; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.model.builder.BaseBuilder; +import org.opencdmp.model.descriptionstatus.DescriptionStatusDefinitionAuthorization; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import java.util.*; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class DescriptionStatusDefinitionAuthorizationBuilder extends BaseBuilder { + + private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); + private final BuilderFactory builderFactory; + public DescriptionStatusDefinitionAuthorizationBuilder(ConventionService conventionService, BuilderFactory builderFactory) { + super(conventionService, new LoggerService(LoggerFactory.getLogger(DescriptionStatusDefinitionAuthorizationBuilder.class))); + this.builderFactory = builderFactory; + } + + public DescriptionStatusDefinitionAuthorizationBuilder authorize(EnumSet values) { + this.authorize = values; + return this; + } + + @Override + public List build(FieldSet fields, List data) throws MyApplicationException { + this.logger.debug("building for {} items requesting {} fields", Optional.ofNullable(data).map(List::size).orElse(0), Optional.ofNullable(fields).map(FieldSet::getFields).map(Set::size).orElse(0)); + this.logger.trace(new DataLogEntry("requested fields", fields)); + if (fields == null || data == null || fields.isEmpty()) + return new ArrayList<>(); + + List models = new ArrayList<>(); + + FieldSet editFields = fields.extractPrefixed(this.asPrefix(DescriptionStatusDefinitionAuthorization._edit)); + for (DescriptionStatusDefinitionAuthorizationEntity d : data) { + DescriptionStatusDefinitionAuthorization m = new DescriptionStatusDefinitionAuthorization(); + if (!editFields.isEmpty() && d.getEdit() != null) { + m.setEdit(this.builderFactory.builder(DescriptionStatusDefinitionAuthorizationItemBuilder.class).authorize(this.authorize).build(editFields, d.getEdit())); + } + + models.add(m); + } + + return models; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/builder/descriptionstatus/DescriptionStatusDefinitionAuthorizationItemBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/descriptionstatus/DescriptionStatusDefinitionAuthorizationItemBuilder.java new file mode 100644 index 000000000..ca93c6e6a --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/builder/descriptionstatus/DescriptionStatusDefinitionAuthorizationItemBuilder.java @@ -0,0 +1,53 @@ +package org.opencdmp.model.builder.descriptionstatus; + +import gr.cite.tools.exception.MyApplicationException; +import gr.cite.tools.fieldset.FieldSet; +import gr.cite.tools.logging.DataLogEntry; +import gr.cite.tools.logging.LoggerService; +import org.opencdmp.authorization.AuthorizationFlags; +import org.opencdmp.commons.types.descriptionstatus.DescriptionStatusDefinitionAuthorizationItemEntity; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.model.builder.BaseBuilder; +import org.opencdmp.model.descriptionstatus.DescriptionStatusDefinitionAuthorizationItem; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import java.util.*; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class DescriptionStatusDefinitionAuthorizationItemBuilder extends BaseBuilder { + + private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); + + public DescriptionStatusDefinitionAuthorizationItemBuilder(ConventionService conventionService) { + super(conventionService, new LoggerService(LoggerFactory.getLogger(DescriptionStatusDefinitionAuthorizationItemBuilder.class))); + } + + public DescriptionStatusDefinitionAuthorizationItemBuilder authorize(EnumSet values) { + this.authorize = values; + return this; + } + + @Override + public List build(FieldSet fields, List data) throws MyApplicationException { + this.logger.debug("building for {} items requesting {} fields", Optional.ofNullable(data).map(List::size).orElse(0), Optional.ofNullable(fields).map(FieldSet::getFields).map(Set::size).orElse(0)); + this.logger.trace(new DataLogEntry("requested fields", fields)); + if (fields == null || data == null || fields.isEmpty()) + return new ArrayList<>(); + + List models = new ArrayList<>(); + for (DescriptionStatusDefinitionAuthorizationItemEntity d : data) { + DescriptionStatusDefinitionAuthorizationItem m = new DescriptionStatusDefinitionAuthorizationItem(); + if (fields.hasField(this.asIndexer(DescriptionStatusDefinitionAuthorizationItem._planRoles))) m.setPlanRoles(d.getPlanRoles()); + if (fields.hasField(this.asIndexer(DescriptionStatusDefinitionAuthorizationItem._roles))) m.setRoles(d.getRoles()); + if (fields.hasField(this.asIndexer(DescriptionStatusDefinitionAuthorizationItem._allowAnonymous))) m.setAllowAnonymous(d.getAllowAnonymous()); + if (fields.hasField(this.asIndexer(DescriptionStatusDefinitionAuthorizationItem._allowAuthenticated))) m.setAllowAuthenticated(d.getAllowAuthenticated()); + + models.add(m); + } + return models; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/builder/descriptionstatus/DescriptionStatusDefinitionBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/descriptionstatus/DescriptionStatusDefinitionBuilder.java new file mode 100644 index 000000000..7da9d5dd8 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/builder/descriptionstatus/DescriptionStatusDefinitionBuilder.java @@ -0,0 +1,59 @@ +package org.opencdmp.model.builder.descriptionstatus; + +import gr.cite.tools.data.builder.BuilderFactory; +import gr.cite.tools.exception.MyApplicationException; +import gr.cite.tools.fieldset.FieldSet; +import gr.cite.tools.logging.DataLogEntry; +import gr.cite.tools.logging.LoggerService; +import org.opencdmp.authorization.AuthorizationFlags; +import org.opencdmp.commons.types.descriptionstatus.DescriptionStatusDefinitionEntity; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.model.builder.BaseBuilder; +import org.opencdmp.model.descriptionstatus.DescriptionStatusDefinition; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import java.util.*; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class DescriptionStatusDefinitionBuilder extends BaseBuilder { + + private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); + + private final BuilderFactory builderFactory; + + public DescriptionStatusDefinitionBuilder(ConventionService conventionService, BuilderFactory builderFactory) { + super(conventionService, new LoggerService(LoggerFactory.getLogger(DescriptionStatusDefinitionBuilder.class))); + this.builderFactory = builderFactory; + } + + public DescriptionStatusDefinitionBuilder authorize(EnumSet values) { + this.authorize = values; + return this; + } + + @Override + public List build(FieldSet fields, List data) throws MyApplicationException { + this.logger.debug("building for {} items requesting {} fields", Optional.ofNullable(data).map(List::size).orElse(0), Optional.ofNullable(fields).map(FieldSet::getFields).map(Set::size).orElse(0)); + this.logger.trace(new DataLogEntry("requested fields", fields)); + if (fields == null || data == null || fields.isEmpty()) + return new ArrayList<>(); + + List models = new ArrayList<>(); + + FieldSet authorizationFields = fields.extractPrefixed(this.asPrefix(DescriptionStatusDefinition._authorization)); + for (DescriptionStatusDefinitionEntity d : data) { + DescriptionStatusDefinition m = new DescriptionStatusDefinition(); + if (!authorizationFields.isEmpty() && d.getAuthorization() != null) { + this.builderFactory.builder(DescriptionStatusDefinitionAuthorizationBuilder.class).authorize(authorize).build(authorizationFields, d.getAuthorization()); + } + + models.add(m); + } + + return models; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/censorship/descriptionstatus/DescriptionStatusCensor.java b/backend/core/src/main/java/org/opencdmp/model/censorship/descriptionstatus/DescriptionStatusCensor.java new file mode 100644 index 000000000..5ae1327e7 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/censorship/descriptionstatus/DescriptionStatusCensor.java @@ -0,0 +1,43 @@ +package org.opencdmp.model.censorship.descriptionstatus; + +import gr.cite.commons.web.authz.service.AuthorizationService; +import gr.cite.tools.data.censor.CensorFactory; +import gr.cite.tools.fieldset.FieldSet; +import gr.cite.tools.logging.DataLogEntry; +import gr.cite.tools.logging.LoggerService; +import org.opencdmp.authorization.Permission; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.model.censorship.BaseCensor; +import org.opencdmp.model.descriptionstatus.DescriptionStatus; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import java.util.UUID; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class DescriptionStatusCensor extends BaseCensor { + + protected final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionStatusCensor.class)); + protected final AuthorizationService authService; + protected final CensorFactory censorFactory; + + public DescriptionStatusCensor(ConventionService conventionService, AuthorizationService authService, CensorFactory censorFactory) { + super(conventionService); + this.authService = authService; + this.censorFactory = censorFactory; + } + + public void censor(FieldSet fields, UUID userId) { + this.logger.debug(new DataLogEntry("censoring fields", fields)); + if (fields == null || fields.isEmpty()) + return; + + this.authService.authorizeForce(Permission.BrowseDescriptionStatus); + + FieldSet definitionFields = fields.extractPrefixed(this.asIndexerPrefix(DescriptionStatus._definition)); + this.censorFactory.censor(DescriptionStatusDefinitionCensor.class).censor(definitionFields, userId); + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/censorship/descriptionstatus/DescriptionStatusDefinitionAuthorizationCensor.java b/backend/core/src/main/java/org/opencdmp/model/censorship/descriptionstatus/DescriptionStatusDefinitionAuthorizationCensor.java new file mode 100644 index 000000000..349165609 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/censorship/descriptionstatus/DescriptionStatusDefinitionAuthorizationCensor.java @@ -0,0 +1,44 @@ +package org.opencdmp.model.censorship.descriptionstatus; + +import gr.cite.commons.web.authz.service.AuthorizationService; +import gr.cite.tools.data.censor.CensorFactory; +import gr.cite.tools.fieldset.FieldSet; +import gr.cite.tools.logging.DataLogEntry; +import gr.cite.tools.logging.LoggerService; +import org.opencdmp.authorization.Permission; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.model.censorship.BaseCensor; +import org.opencdmp.model.descriptionstatus.DescriptionStatusDefinitionAuthorization; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import java.util.UUID; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class DescriptionStatusDefinitionAuthorizationCensor extends BaseCensor { + + protected final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionStatusDefinitionAuthorizationCensor.class)); + + protected final AuthorizationService authService; + protected final CensorFactory censorFactory; + + public DescriptionStatusDefinitionAuthorizationCensor(ConventionService conventionService, AuthorizationService authService, CensorFactory censorFactory) { + super(conventionService); + this.authService = authService; + this.censorFactory = censorFactory; + } + + public void censor(FieldSet fields, UUID userId) { + this.logger.debug(new DataLogEntry("censoring fields", fields)); + if (fields == null || fields.isEmpty()) + return; + + this.authService.authorizeForce(Permission.BrowsePlanStatus); + + FieldSet editFields = fields.extractPrefixed(this.asIndexerPrefix(DescriptionStatusDefinitionAuthorization._edit)); + this.censorFactory.censor(DescriptionStatusDefinitionAuthorizationItemCensor.class).censor(editFields, userId); + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/censorship/descriptionstatus/DescriptionStatusDefinitionAuthorizationItemCensor.java b/backend/core/src/main/java/org/opencdmp/model/censorship/descriptionstatus/DescriptionStatusDefinitionAuthorizationItemCensor.java new file mode 100644 index 000000000..b552b2ae1 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/censorship/descriptionstatus/DescriptionStatusDefinitionAuthorizationItemCensor.java @@ -0,0 +1,41 @@ +package org.opencdmp.model.censorship.descriptionstatus; + +import gr.cite.commons.web.authz.service.AuthorizationService; +import gr.cite.tools.data.censor.CensorFactory; +import gr.cite.tools.fieldset.FieldSet; +import gr.cite.tools.logging.DataLogEntry; +import gr.cite.tools.logging.LoggerService; +import org.opencdmp.authorization.Permission; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.model.censorship.BaseCensor; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import java.util.UUID; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class DescriptionStatusDefinitionAuthorizationItemCensor extends BaseCensor { + + private static LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionStatusDefinitionAuthorizationItemCensor.class)); + + + protected final AuthorizationService authService; + protected final CensorFactory censorFactory; + + public DescriptionStatusDefinitionAuthorizationItemCensor(ConventionService conventionService, AuthorizationService authService, CensorFactory censorFactory) { + super(conventionService); + this.authService = authService; + this.censorFactory = censorFactory; + } + + public void censor(FieldSet fields, UUID userId) { + logger.debug(new DataLogEntry("censoring fields", fields)); + if (fields == null || fields.isEmpty()) + return; + + this.authService.authorizeForce(Permission.BrowseDescriptionStatus); + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/censorship/descriptionstatus/DescriptionStatusDefinitionCensor.java b/backend/core/src/main/java/org/opencdmp/model/censorship/descriptionstatus/DescriptionStatusDefinitionCensor.java new file mode 100644 index 000000000..0041258db --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/censorship/descriptionstatus/DescriptionStatusDefinitionCensor.java @@ -0,0 +1,43 @@ +package org.opencdmp.model.censorship.descriptionstatus; + +import gr.cite.commons.web.authz.service.AuthorizationService; +import gr.cite.tools.data.censor.CensorFactory; +import gr.cite.tools.fieldset.FieldSet; +import gr.cite.tools.logging.DataLogEntry; +import gr.cite.tools.logging.LoggerService; +import org.opencdmp.authorization.Permission; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.model.censorship.BaseCensor; +import org.opencdmp.model.descriptionstatus.DescriptionStatusDefinition; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import java.util.UUID; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class DescriptionStatusDefinitionCensor extends BaseCensor { + + protected final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionStatusDefinitionCensor.class)); + protected final AuthorizationService authService; + protected final CensorFactory censorFactory; + + public DescriptionStatusDefinitionCensor(ConventionService conventionService, AuthorizationService authService, CensorFactory censorFactory) { + super(conventionService); + this.authService = authService; + this.censorFactory = censorFactory; + } + + public void censor(FieldSet fields, UUID userId) { + this.logger.debug(new DataLogEntry("censoring fields", fields)); + if (fields == null || fields.isEmpty()) + return; + + this.authService.authorizeForce(Permission.BrowseDescriptionStatus); + + FieldSet authorizationFields = fields.extractPrefixed(this.asIndexerPrefix(DescriptionStatusDefinition._authorization)); + this.censorFactory.censor(DescriptionStatusDefinitionAuthorizationCensor.class).censor(authorizationFields, userId); + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/deleter/DescriptionStatusDeleter.java b/backend/core/src/main/java/org/opencdmp/model/deleter/DescriptionStatusDeleter.java new file mode 100644 index 000000000..3798e4c38 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/deleter/DescriptionStatusDeleter.java @@ -0,0 +1,78 @@ +package org.opencdmp.model.deleter; + +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.opencdmp.commons.enums.IsActive; +import org.opencdmp.commons.enums.UsageLimitTargetMetric; +import org.opencdmp.data.DescriptionStatusEntity; +import org.opencdmp.data.TenantEntityManager; +import org.opencdmp.query.DescriptionStatusQuery; +import org.opencdmp.service.accounting.AccountingService; +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 DescriptionStatusDeleter implements Deleter { + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionStatusDeleter.class)); + private final TenantEntityManager entityManager; + private final DeleterFactory deleterFactory; + private final QueryFactory queryFactory; + private final AccountingService accountingService; + @Autowired + public DescriptionStatusDeleter( + TenantEntityManager entityManager, + DeleterFactory deleterFactory, + QueryFactory queryFactory, + AccountingService accountingService) { + this.entityManager = entityManager; + this.deleterFactory = deleterFactory; + this.queryFactory = queryFactory; + this.accountingService = accountingService; + } + + public void deleteAndSaveByIds(List ids) throws InvalidApplicationException { + logger.debug(new MapLogEntry("collecting to delete").And("count", Optional.ofNullable(ids).map(List::size).orElse(0)).And("ids", ids)); + List data = this.queryFactory.query(DescriptionStatusQuery.class).ids(ids).collect(); + logger.trace("retrieved {} items", Optional.ofNullable(data).map(List::size).orElse(0)); + this.deleteAndSave(data); + } + + public void deleteAndSave(List 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 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 (DescriptionStatusEntity 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"); + this.accountingService.decrease(UsageLimitTargetMetric.DESCRIPTION_STATUS_COUNT.getValue()); + } + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/descriptionstatus/DescriptionStatus.java b/backend/core/src/main/java/org/opencdmp/model/descriptionstatus/DescriptionStatus.java new file mode 100644 index 000000000..c1a075937 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/descriptionstatus/DescriptionStatus.java @@ -0,0 +1,64 @@ +package org.opencdmp.model.descriptionstatus; + +import org.opencdmp.commons.enums.IsActive; + +import java.time.Instant; +import java.util.UUID; + +public class DescriptionStatus { + + public final static String _id = "id"; + private UUID id; + + public final static String _name = "name"; + private String name; + + public final static String _description = "description"; + private String description; + + public final static String _createdAt = "createdAt"; + private Instant createdAt; + + public final static String _updatedAt = "updatedAt"; + private Instant updateAt; + + public final static String _isActive = "isActive"; + private IsActive isActive; + + public final static String _belongsToCurrentTenant = "belongsToCurrentTenant"; + private Boolean belongsToCurrentTenant; + + public final static String _internalStatus = "internalStatus"; + private org.opencdmp.commons.enums.DescriptionStatus internalStatus; + + public final static String _definition = "definition"; + private DescriptionStatusDefinition definition; + + public UUID getId() { return id; } + public void setId(UUID id) { this.id = id; } + + public String getName() { return name; } + public void setName(String name) { this.name = name; } + + public String getDescription() { return description; } + public void setDescription(String description) { this.description = description; } + + public Instant getCreatedAt() { return createdAt; } + public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; } + + public Instant getUpdateAt() { return updateAt; } + public void setUpdateAt(Instant updateAt) { this.updateAt = updateAt; } + + public IsActive getIsActive() { return isActive; } + public void setIsActive(IsActive isActive) { this.isActive = isActive; } + + public org.opencdmp.commons.enums.DescriptionStatus getInternalStatus() { return internalStatus; } + public void setInternalStatus(org.opencdmp.commons.enums.DescriptionStatus internalStatus) { this.internalStatus = internalStatus; } + + public DescriptionStatusDefinition getDefinition() { return definition; } + public void setDefinition(DescriptionStatusDefinition definition) { this.definition = definition; } + + public Boolean getBelongsToCurrentTenant() { return belongsToCurrentTenant; } + + public void setBelongsToCurrentTenant(Boolean belongsToCurrentTenant) { this.belongsToCurrentTenant = belongsToCurrentTenant; } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/descriptionstatus/DescriptionStatusDefinition.java b/backend/core/src/main/java/org/opencdmp/model/descriptionstatus/DescriptionStatusDefinition.java new file mode 100644 index 000000000..de43047e0 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/descriptionstatus/DescriptionStatusDefinition.java @@ -0,0 +1,11 @@ +package org.opencdmp.model.descriptionstatus; + +public class DescriptionStatusDefinition { + + public final static String _authorization = "authorization"; + private DescriptionStatusDefinitionAuthorization authorization; + + public DescriptionStatusDefinitionAuthorization getAuthorization() { return authorization; } + + public void setAuthorization(DescriptionStatusDefinitionAuthorization authorization) { this.authorization = authorization; } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/descriptionstatus/DescriptionStatusDefinitionAuthorization.java b/backend/core/src/main/java/org/opencdmp/model/descriptionstatus/DescriptionStatusDefinitionAuthorization.java new file mode 100644 index 000000000..22f144a08 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/descriptionstatus/DescriptionStatusDefinitionAuthorization.java @@ -0,0 +1,11 @@ +package org.opencdmp.model.descriptionstatus; + +public class DescriptionStatusDefinitionAuthorization { + + public final static String _edit = "edit"; + private DescriptionStatusDefinitionAuthorizationItem edit; + + public DescriptionStatusDefinitionAuthorizationItem getEdit() { return edit; } + + public void setEdit(DescriptionStatusDefinitionAuthorizationItem edit) { this.edit = edit; } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/descriptionstatus/DescriptionStatusDefinitionAuthorizationItem.java b/backend/core/src/main/java/org/opencdmp/model/descriptionstatus/DescriptionStatusDefinitionAuthorizationItem.java new file mode 100644 index 000000000..2512c9a36 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/descriptionstatus/DescriptionStatusDefinitionAuthorizationItem.java @@ -0,0 +1,34 @@ +package org.opencdmp.model.descriptionstatus; + +import java.util.List; + +public class DescriptionStatusDefinitionAuthorizationItem { + + public final static String _roles = "roles"; + private List roles; + + public final static String _planRoles = "planRoles"; + private List planRoles; + + public final static String _allowAuthenticated = "allowAuthenticated"; + private Boolean allowAuthenticated; + + public final static String _allowAnonymous = "allowAnonymous"; + private Boolean allowAnonymous; + + public List getRoles() { return roles; } + + public void setRoles(List roles) { this.roles = roles; } + + public List getPlanRoles() { return planRoles; } + + public void setPlanRoles(List planRoles) { this.planRoles = planRoles; } + + public Boolean getAllowAuthenticated() { return allowAuthenticated; } + + public void setAllowAuthenticated(Boolean allowAuthenticated) { this.allowAuthenticated = allowAuthenticated; } + + public Boolean getAllowAnonymous() { return allowAnonymous; } + + public void setAllowAnonymous(Boolean allowAnonymous) { this.allowAnonymous = allowAnonymous; } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/persist/descriptionstatus/DescriptionStatusDefinitionAuthorizationItemPersist.java b/backend/core/src/main/java/org/opencdmp/model/persist/descriptionstatus/DescriptionStatusDefinitionAuthorizationItemPersist.java new file mode 100644 index 000000000..1da6c4949 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/persist/descriptionstatus/DescriptionStatusDefinitionAuthorizationItemPersist.java @@ -0,0 +1,72 @@ +package org.opencdmp.model.persist.descriptionstatus; + +import gr.cite.tools.validation.specification.Specification; +import org.opencdmp.commons.validation.BaseValidator; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.errorcode.ErrorThesaurusProperties; +import org.opencdmp.model.persist.planstatus.PlanStatusDefinitionAuthorizationItemPersist; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.MessageSource; +import org.springframework.context.annotation.Scope; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.List; + +public class DescriptionStatusDefinitionAuthorizationItemPersist { + + private List roles = null; + public final static String _roles = "roles"; + + private List planRoles = null; + public final static String _planRoles = "planRoles"; + + private Boolean allowAuthenticated; + public final static String _allowAuthenticated = "allowAuthenticated"; + + private Boolean allowAnonymous; + public final static String _allowAnonymous = "allowAnonymous"; + + public List getRoles() { return roles; } + public void setRoles(List roles) { this.roles = roles; } + + public List getPlanRoles() { return planRoles; } + public void setPlanRoles(List planRoles) { this.planRoles = planRoles; } + + public Boolean getAllowAuthenticated() { return allowAuthenticated; } + public void setAllowAuthenticated(Boolean allowAuthenticated) { this.allowAuthenticated = allowAuthenticated; } + + public Boolean getAllowAnonymous() { return allowAnonymous; } + public void setAllowAnonymous(Boolean allowAnonymous) { this.allowAnonymous = allowAnonymous; } + + @Component(DescriptionStatusDefinitionAuthorizationItemPersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class DescriptionStatusDefinitionAuthorizationItemPersistValidator extends BaseValidator { + + public static final String ValidatorName = "DescriptionStatusPersistValidation.DescriptionStatusDefinitionPersistValidator.DescriptionStatusDefinitionAuthorizationPersistValidator.DescriptionStatusDefinitionAuthorizationItemPersistValidator"; + + private final MessageSource messageSource; + + protected DescriptionStatusDefinitionAuthorizationItemPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) { + super(conventionService, errors); + this.messageSource = messageSource; + } + + @Override + protected Class modelClass() { + return DescriptionStatusDefinitionAuthorizationItemPersist.class; + } + + @Override + protected List specifications(DescriptionStatusDefinitionAuthorizationItemPersist item) { + return Arrays.asList( + this.spec() + .must(() -> !this.isListNullOrEmpty(item.getRoles())) + .failOn(PlanStatusDefinitionAuthorizationItemPersist._roles).failWith(messageSource.getMessage("Validation_Required", new Object[]{PlanStatusDefinitionAuthorizationItemPersist._roles}, LocaleContextHolder.getLocale())), this.spec() + .must(() -> !this.isListNullOrEmpty(item.getPlanRoles())) + .failOn(PlanStatusDefinitionAuthorizationItemPersist._planRoles).failWith(messageSource.getMessage("Validation_Required", new Object[]{PlanStatusDefinitionAuthorizationItemPersist._planRoles}, LocaleContextHolder.getLocale())) + ); + } + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/persist/descriptionstatus/DescriptionStatusDefinitionAuthorizationPersist.java b/backend/core/src/main/java/org/opencdmp/model/persist/descriptionstatus/DescriptionStatusDefinitionAuthorizationPersist.java new file mode 100644 index 000000000..7c5ee005d --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/persist/descriptionstatus/DescriptionStatusDefinitionAuthorizationPersist.java @@ -0,0 +1,61 @@ +package org.opencdmp.model.persist.descriptionstatus; + +import gr.cite.tools.validation.ValidatorFactory; +import gr.cite.tools.validation.specification.Specification; +import org.opencdmp.commons.validation.BaseValidator; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.errorcode.ErrorThesaurusProperties; +import org.opencdmp.model.descriptionstatus.DescriptionStatusDefinitionAuthorization; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.MessageSource; +import org.springframework.context.annotation.Scope; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.List; + +public class DescriptionStatusDefinitionAuthorizationPersist { + + private DescriptionStatusDefinitionAuthorizationItemPersist edit; + public final static String _edit = "edit"; + + public DescriptionStatusDefinitionAuthorizationItemPersist getEdit() { return edit; } + + public void setEdit(DescriptionStatusDefinitionAuthorizationItemPersist edit) { this.edit = edit; } + + @Component(DescriptionStatusDefinitionAuthorizationPersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class DescriptionStatusDefinitionAuthorizationPersistValidator extends BaseValidator { + + public static final String ValidatorName = "DescriptionStatusPersistValidation.DescriptionStatusDefinitionPersistValidator.DescriptionStatusDefinitionAuthorizationPersistValidator"; + + private final MessageSource messageSource; + private final ValidatorFactory validatorFactory; + + protected DescriptionStatusDefinitionAuthorizationPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) { + super(conventionService, errors); + this.messageSource = messageSource; + this.validatorFactory = validatorFactory; + } + + @Override + protected Class modelClass() { + return DescriptionStatusDefinitionAuthorizationPersist.class; + } + + @Override + protected List specifications(DescriptionStatusDefinitionAuthorizationPersist item) { + return Arrays.asList( + this.spec() + .must(() -> !this.isNull(item.getEdit())) + .failOn(DescriptionStatusDefinitionAuthorizationPersist._edit).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionStatusDefinitionAuthorizationPersist._edit}, LocaleContextHolder.getLocale())), + this.refSpec() + .iff(() -> !this.isNull(item.getEdit())) + .on(DescriptionStatusDefinitionAuthorization._edit) + .over(item.getEdit()) + .using(() -> this.validatorFactory.validator(DescriptionStatusDefinitionAuthorizationItemPersist.DescriptionStatusDefinitionAuthorizationItemPersistValidator.class)) + ); + } + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/persist/descriptionstatus/DescriptionStatusDefinitionPersist.java b/backend/core/src/main/java/org/opencdmp/model/persist/descriptionstatus/DescriptionStatusDefinitionPersist.java new file mode 100644 index 000000000..6e2121027 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/persist/descriptionstatus/DescriptionStatusDefinitionPersist.java @@ -0,0 +1,61 @@ +package org.opencdmp.model.persist.descriptionstatus; + +import gr.cite.tools.validation.ValidatorFactory; +import gr.cite.tools.validation.specification.Specification; +import org.opencdmp.commons.validation.BaseValidator; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.errorcode.ErrorThesaurusProperties; +import org.opencdmp.model.descriptionstatus.DescriptionStatusDefinitionAuthorization; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.MessageSource; +import org.springframework.context.annotation.Scope; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.List; + +public class DescriptionStatusDefinitionPersist { + + private DescriptionStatusDefinitionAuthorizationPersist authorization; + public final static String _authorization = "authorization"; + + public DescriptionStatusDefinitionAuthorizationPersist getAuthorization() { return authorization; } + public void setAuthorization(DescriptionStatusDefinitionAuthorizationPersist authorization) { this.authorization = authorization; } + + + @Component(DescriptionStatusDefinitionPersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class DescriptionStatusDefinitionPersistValidator extends BaseValidator { + + public final static String ValidatorName = "DescriptionStatusPersistValidation.DescriptionStatusDefinitionPersistValidator"; + + private final MessageSource messageSource; + private final ValidatorFactory validatorFactory; + + protected DescriptionStatusDefinitionPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) { + super(conventionService, errors); + this.messageSource = messageSource; + this.validatorFactory = validatorFactory; + } + + @Override + protected Class modelClass() { + return DescriptionStatusDefinitionPersist.class; + } + + @Override + protected List specifications(DescriptionStatusDefinitionPersist item) { + return Arrays.asList( + this.spec() + .must(() -> !this.isNull(item.getAuthorization())) + .failOn(DescriptionStatusDefinitionPersist._authorization).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionStatusDefinitionPersist._authorization}, LocaleContextHolder.getLocale())), + this.refSpec() + .iff(() -> !this.isNull(item.getAuthorization())) + .on(DescriptionStatusDefinitionPersist._authorization) + .over(item.getAuthorization()) + .using(() -> this.validatorFactory.validator(DescriptionStatusDefinitionAuthorizationPersist.DescriptionStatusDefinitionAuthorizationPersistValidator.class)) + ); + } + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/persist/descriptionstatus/DescriptionStatusPersist.java b/backend/core/src/main/java/org/opencdmp/model/persist/descriptionstatus/DescriptionStatusPersist.java new file mode 100644 index 000000000..c78fe095a --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/persist/descriptionstatus/DescriptionStatusPersist.java @@ -0,0 +1,116 @@ +package org.opencdmp.model.persist.descriptionstatus; + +import gr.cite.tools.validation.ValidatorFactory; +import gr.cite.tools.validation.specification.Specification; +import org.opencdmp.commons.enums.PlanStatus; +import org.opencdmp.commons.validation.BaseValidator; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.data.DescriptionStatusEntity; +import org.opencdmp.errorcode.ErrorThesaurusProperties; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.MessageSource; +import org.springframework.context.annotation.Scope; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +public class DescriptionStatusPersist { + + private UUID id; + + private String name; + public final static String _name = "name"; + + private String description; + public final static String _description = "description"; + + private org.opencdmp.commons.enums.DescriptionStatus internalStatus; + public final static String _internalStatus = "internalStatus"; + + private DescriptionStatusDefinitionPersist definition; + public final static String _definition = "definition"; + + private String hash; + public final static String _hash = "hash"; + + + public UUID getId() { return id; } + public void setId(UUID id) { this.id = id; } + + public String getName() { return name; } + public void setName(String name) { this.name = name; } + + public String getDescription() { return description; } + public void setDescription(String description) { this.description = description; } + + public org.opencdmp.commons.enums.DescriptionStatus getInternalStatus() { return internalStatus; } + public void setInternalStatus(org.opencdmp.commons.enums.DescriptionStatus internalStatus) { this.internalStatus = internalStatus; } + + public DescriptionStatusDefinitionPersist getDefinition() { return definition; } + public void setDefinition(DescriptionStatusDefinitionPersist definition) { this.definition = definition; } + + public String getHash() { return hash; } + public void setHash(String hash) { this.hash = hash; } + + @Component(DescriptionStatusPersist.DescriptionStatusPersistValidation.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class DescriptionStatusPersistValidation extends BaseValidator { + + public final static String ValidatorName = "DescriptionStatusPersistValidation"; + + private final MessageSource messageSource; + private final ValidatorFactory validatorFactory; + + protected DescriptionStatusPersistValidation(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) { + super(conventionService, errors); + this.messageSource = messageSource; + this.validatorFactory = validatorFactory; + } + + @Override + protected Class modelClass() { + return DescriptionStatusPersist.class; + } + + @Override + protected List specifications(DescriptionStatusPersist item) { + return Arrays.asList( + this.spec() + .iff(() -> this.isValidGuid(item.getId())) + .must(() -> this.isValidHash(item.getHash())) + .failOn(DescriptionStatusPersist._hash).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DescriptionStatusPersist._hash}, LocaleContextHolder.getLocale())), + this.spec() + .iff(() -> !this.isValidGuid(item.getId())) + .must(() -> !this.isValidHash(item.getHash())) + .failOn(DescriptionStatusPersist._hash).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DescriptionStatusPersist._hash}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isEmpty(item.getName())) + .failOn(DescriptionStatusPersist._name).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DescriptionStatusPersist._name}, LocaleContextHolder.getLocale())), + this.spec() + .iff(() -> !this.isEmpty(item.getName())) + .must(() -> this.lessEqualLength(item.getName(), DescriptionStatusEntity._nameLength)) + .failOn(DescriptionStatusPersist._name).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DescriptionStatusPersist._name}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isNull(item.getInternalStatus())) + .failOn(DescriptionStatusPersist._internalStatus).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DescriptionStatusPersist._internalStatus}, LocaleContextHolder.getLocale())), + this.spec() + .iff(() -> item.getInternalStatus() == org.opencdmp.commons.enums.DescriptionStatus.Finalized) + .must(() -> !this.isNull(item.getDefinition())) + .failOn(DescriptionStatusPersist._internalStatus).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DescriptionStatusPersist._internalStatus}, LocaleContextHolder.getLocale())), + this.refSpec() + .iff(() -> !this.isNull(item.getDefinition())) + .on(DescriptionStatusPersist._definition) + .over(item.getDefinition()) + .using(() -> this.validatorFactory.validator(DescriptionStatusDefinitionPersist.DescriptionStatusDefinitionPersistValidator.class)), + this.refSpec() + .iff(() -> item.getInternalStatus() == org.opencdmp.commons.enums.DescriptionStatus.Finalized) + .on(DescriptionStatusPersist._definition) + .over(item.getDefinition()) + .using(() -> this.validatorFactory.validator(DescriptionStatusDefinitionPersist.DescriptionStatusDefinitionPersistValidator.class)) + ); + } + } +} diff --git a/backend/core/src/main/java/org/opencdmp/query/DescriptionStatusQuery.java b/backend/core/src/main/java/org/opencdmp/query/DescriptionStatusQuery.java new file mode 100644 index 000000000..486bc155c --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/query/DescriptionStatusQuery.java @@ -0,0 +1,221 @@ +package org.opencdmp.query; + +import gr.cite.tools.data.query.FieldResolver; +import gr.cite.tools.data.query.QueryBase; +import gr.cite.tools.data.query.QueryContext; +import jakarta.persistence.EntityManager; +import jakarta.persistence.Tuple; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.Predicate; +import org.opencdmp.authorization.AuthorizationFlags; +import org.opencdmp.commons.enums.IsActive; +import org.opencdmp.data.DescriptionStatusEntity; +import org.opencdmp.data.TenantEntityManager; +import org.opencdmp.model.descriptionstatus.DescriptionStatus; +import org.opencdmp.query.utils.QueryUtilsService; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import java.time.Instant; +import java.util.*; + +@Component +@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class DescriptionStatusQuery extends QueryBase { + + private String like; + + private Collection ids; + + private Collection isActives; + + private Collection internalStatuses; + + private Collection excludeIds; + + private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); + + public DescriptionStatusQuery like(String value) { + this.like = value; + return this; + } + + public DescriptionStatusQuery ids(UUID value) { + this.ids = List.of(value); + return this; + } + + public DescriptionStatusQuery ids(UUID... value) { + this.ids = Arrays.asList(value); + return this; + } + + public DescriptionStatusQuery ids(Collection values) { + this.ids = values; + return this; + } + + public DescriptionStatusQuery excludeIds(UUID value) { + this.excludeIds = List.of(value); + return this; + } + + public DescriptionStatusQuery excludeIds(UUID... value) { + this.excludeIds = Arrays.asList(value); + return this; + } + + public DescriptionStatusQuery excludeIds(Collection values) { + this.excludeIds = values; + return this; + } + + public DescriptionStatusQuery isActive(IsActive value) { + this.isActives = List.of(value); + return this; + } + + public DescriptionStatusQuery isActive(IsActive... value) { + this.isActives = Arrays.asList(value); + return this; + } + + public DescriptionStatusQuery isActive(Collection values) { + this.isActives = values; + return this; + } + + public DescriptionStatusQuery internalStatuses(org.opencdmp.commons.enums.DescriptionStatus value) { + this.internalStatuses = List.of(value); + return this; + } + + public DescriptionStatusQuery internalStatuses(org.opencdmp.commons.enums.DescriptionStatus... value) { + this.internalStatuses = Arrays.asList(value); + return this; + } + + public DescriptionStatusQuery internalStatuses(Collection values) { + this.internalStatuses = values; + return this; + } + + public DescriptionStatusQuery enableTracking() { + this.noTracking = false; + return this; + } + + public DescriptionStatusQuery disableTracking() { + this.noTracking = true; + return this; + } + + public DescriptionStatusQuery authorize(EnumSet values) { + this.authorize = values; + return this; + } + + private final QueryUtilsService queryUtilsService; + private final TenantEntityManager entityManager; + + public DescriptionStatusQuery( + QueryUtilsService queryUtilsService, TenantEntityManager entityManager) { + this.queryUtilsService = queryUtilsService; + this.entityManager = entityManager; + } + + @Override + protected EntityManager entityManager(){ + return this.entityManager.getEntityManager(); + } + + @Override + protected Boolean isFalseQuery() { + return this.isEmpty(this.ids) || this.isEmpty(this.isActives) || this.isEmpty(this.internalStatuses); + } + + @Override + protected Class entityClass() { return DescriptionStatusEntity.class; } + + @Override + protected Predicate applyFilters(QueryContext queryContext) { + List predicates = new ArrayList<>(); + + if (this.ids != null) { + CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionStatusEntity._id)); + for (UUID id : this.ids) + inClause.value(id); + predicates.add(inClause); + } + if (this.like != null && !this.like.isBlank()) { + predicates.add(queryContext.CriteriaBuilder.or( + this.queryUtilsService.ilike(queryContext.CriteriaBuilder, queryContext.Root.get(DescriptionStatusEntity._name), this.like), + this.queryUtilsService.ilike(queryContext.CriteriaBuilder, queryContext.Root.get(DescriptionStatusEntity._description), this.like))); + } + if (this.isActives != null) { + CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionStatusEntity._isActive)); + for (IsActive isActive : this.isActives) + inClause.value(isActive); + predicates.add(inClause); + } + if (this.internalStatuses != null) { + CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionStatusEntity._internalStatus)); + for (org.opencdmp.commons.enums.DescriptionStatus internalStatus : this.internalStatuses) + inClause.value(internalStatus); + predicates.add(inClause); + } + if (this.excludeIds != null) { + CriteriaBuilder.In notInClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(DescriptionStatusEntity._id)); + for (UUID id : this.excludeIds) { + notInClause.value(id); + } + predicates.add(notInClause.not()); + } + if (!predicates.isEmpty()) { + Predicate[] predicatesArray = predicates.toArray(new Predicate[0]); + return queryContext.CriteriaBuilder.and(predicatesArray); + } else { + return null; + } + } + + @Override + protected String fieldNameOf(FieldResolver item) { + if (item.match(DescriptionStatus._id)) + return DescriptionStatusEntity._id; + else if (item.match(DescriptionStatus._name)) + return DescriptionStatusEntity._name; + else if (item.match(DescriptionStatus._description)) + return DescriptionStatusEntity._description; + else if (item.match(DescriptionStatus._createdAt)) + return DescriptionStatusEntity._createdAt; + else if (item.match(DescriptionStatus._updatedAt)) + return DescriptionStatusEntity._updatedAt; + else if (item.match(DescriptionStatus._isActive)) + return DescriptionStatusEntity._isActive; + else if (item.match(DescriptionStatus._belongsToCurrentTenant)) + return DescriptionStatusEntity._tenantId; + else if (item.match(DescriptionStatus._internalStatus)) + return DescriptionStatusEntity._internalStatus; + else if (item.match(DescriptionStatusEntity._definition)) + return DescriptionStatusEntity._definition; + else + return null; + } + + @Override + protected DescriptionStatusEntity convert(Tuple tuple, Set columns) { + DescriptionStatusEntity item = new DescriptionStatusEntity(); + item.setId(QueryBase.convertSafe(tuple, columns, DescriptionStatusEntity._id, UUID.class)); + item.setName(QueryBase.convertSafe(tuple, columns, DescriptionStatusEntity._name, String.class)); + item.setDescription(QueryBase.convertSafe(tuple, columns, DescriptionStatusEntity._description, String.class)); + item.setTenantId(QueryBase.convertSafe(tuple, columns, DescriptionStatusEntity._tenantId, UUID.class)); + item.setCreatedAt(QueryBase.convertSafe(tuple, columns, DescriptionStatusEntity._createdAt, Instant.class)); + item.setUpdatedAt(QueryBase.convertSafe(tuple, columns, DescriptionStatusEntity._updatedAt, Instant.class)); + item.setIsActive(QueryBase.convertSafe(tuple, columns, DescriptionStatusEntity._isActive, IsActive.class)); + item.setInternalStatus(QueryBase.convertSafe(tuple, columns, DescriptionStatusEntity._internalStatus, org.opencdmp.commons.enums.DescriptionStatus.class)); + item.setDefinition(QueryBase.convertSafe(tuple, columns, DescriptionStatusEntity._definition, String.class)); + return item; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/query/lookup/DescriptionStatusLookup.java b/backend/core/src/main/java/org/opencdmp/query/lookup/DescriptionStatusLookup.java new file mode 100644 index 000000000..70639a35d --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/query/lookup/DescriptionStatusLookup.java @@ -0,0 +1,48 @@ +package org.opencdmp.query.lookup; + +import gr.cite.tools.data.query.Lookup; +import gr.cite.tools.data.query.QueryFactory; +import org.opencdmp.commons.enums.IsActive; +import org.opencdmp.query.DescriptionStatusQuery; + +import java.util.List; +import java.util.UUID; + +public class DescriptionStatusLookup extends Lookup { + + private String like; + + private List ids; + + private List excludedIds; + + private List isActive; + + public String getLike() { return like; } + + public void setLike(String like) { this.like = like; } + + public List getIds() { return ids; } + + public void setIds(List ids) { this.ids = ids; } + + public List getExcludedIds() { return excludedIds; } + + public void setExcludedIds(List excludedIds) { this.excludedIds = excludedIds; } + + public List getIsActive() { return isActive; } + + public void setIsActive(List isActive) { this.isActive = isActive; } + + public DescriptionStatusQuery enrich(QueryFactory queryFactory) { + DescriptionStatusQuery query = queryFactory.query(DescriptionStatusQuery.class); + if (this.like != null) query.like(this.like); + if (this.ids != null) query.ids(this.ids); + if (this.excludedIds != null) query.excludeIds(this.excludedIds); + if (this.isActive != null) query.isActive(this.isActive); + + this.enrichCommon(query); + + return query; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/service/descriptionstatus/DescriptionStatusService.java b/backend/core/src/main/java/org/opencdmp/service/descriptionstatus/DescriptionStatusService.java new file mode 100644 index 000000000..4d90894d1 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/service/descriptionstatus/DescriptionStatusService.java @@ -0,0 +1,20 @@ +package org.opencdmp.service.descriptionstatus; + +import gr.cite.tools.exception.MyApplicationException; +import gr.cite.tools.exception.MyForbiddenException; +import gr.cite.tools.exception.MyNotFoundException; +import gr.cite.tools.exception.MyValidationException; +import gr.cite.tools.fieldset.FieldSet; +import jakarta.xml.bind.JAXBException; +import org.opencdmp.model.descriptionstatus.DescriptionStatus; +import org.opencdmp.model.persist.descriptionstatus.DescriptionStatusPersist; + +import javax.management.InvalidApplicationException; +import java.util.UUID; + +public interface DescriptionStatusService { + + DescriptionStatus persist(DescriptionStatusPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException; + + void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException; +} diff --git a/backend/core/src/main/java/org/opencdmp/service/descriptionstatus/DescriptionStatusServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/descriptionstatus/DescriptionStatusServiceImpl.java new file mode 100644 index 000000000..a0d812929 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/service/descriptionstatus/DescriptionStatusServiceImpl.java @@ -0,0 +1,142 @@ +package org.opencdmp.service.descriptionstatus; + +import gr.cite.commons.web.authz.service.AuthorizationService; +import gr.cite.tools.data.builder.BuilderFactory; +import gr.cite.tools.data.deleter.DeleterFactory; +import gr.cite.tools.exception.MyApplicationException; +import gr.cite.tools.exception.MyForbiddenException; +import gr.cite.tools.exception.MyNotFoundException; +import gr.cite.tools.exception.MyValidationException; +import gr.cite.tools.fieldset.BaseFieldSet; +import gr.cite.tools.fieldset.FieldSet; +import gr.cite.tools.logging.LoggerService; +import gr.cite.tools.logging.MapLogEntry; +import jakarta.xml.bind.JAXBException; +import org.opencdmp.authorization.Permission; +import org.opencdmp.commons.XmlHandlingService; +import org.opencdmp.commons.enums.IsActive; +import org.opencdmp.commons.types.descriptionstatus.DescriptionStatusDefinitionAuthorizationEntity; +import org.opencdmp.commons.types.descriptionstatus.DescriptionStatusDefinitionAuthorizationItemEntity; +import org.opencdmp.commons.types.descriptionstatus.DescriptionStatusDefinitionEntity; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.data.DescriptionStatusEntity; +import org.opencdmp.data.TenantEntityManager; +import org.opencdmp.model.builder.descriptionstatus.DescriptionStatusBuilder; +import org.opencdmp.model.deleter.DescriptionStatusDeleter; +import org.opencdmp.model.descriptionstatus.DescriptionStatus; +import org.opencdmp.model.persist.descriptionstatus.DescriptionStatusDefinitionAuthorizationItemPersist; +import org.opencdmp.model.persist.descriptionstatus.DescriptionStatusDefinitionAuthorizationPersist; +import org.opencdmp.model.persist.descriptionstatus.DescriptionStatusDefinitionPersist; +import org.opencdmp.model.persist.descriptionstatus.DescriptionStatusPersist; +import org.slf4j.LoggerFactory; +import org.springframework.context.MessageSource; +import org.springframework.context.i18n.LocaleContextHolder; + +import javax.management.InvalidApplicationException; +import java.time.Instant; +import java.util.List; +import java.util.UUID; + +public class DescriptionStatusServiceImpl implements DescriptionStatusService { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionStatusServiceImpl.class)); + + private final BuilderFactory builderFactory; + private final DeleterFactory deleterFactory; + + private final AuthorizationService authService; + private final TenantEntityManager entityManager; + private final ConventionService conventionService; + private final MessageSource messageSource; + private final XmlHandlingService xmlHandlingService; + + public DescriptionStatusServiceImpl(BuilderFactory builderFactory, DeleterFactory deleterFactory, AuthorizationService authService, TenantEntityManager entityManager, ConventionService conventionService, MessageSource messageSource, XmlHandlingService xmlHandlingService) { + this.builderFactory = builderFactory; + this.deleterFactory = deleterFactory; + + this.authService = authService; + this.entityManager = entityManager; + this.conventionService = conventionService; + this.messageSource = messageSource; + this.xmlHandlingService = xmlHandlingService; + } + + + @Override + public DescriptionStatus persist(DescriptionStatusPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException { + logger.debug(new MapLogEntry("persisting data description status").And("model", model).And("fields", fields)); + + this.authService.authorizeForce(Permission.EditDescriptionStatus); + + Boolean isUpdate = this.conventionService.isValidGuid(model.getId()); + + DescriptionStatusEntity data; + if (isUpdate) { + data = this.entityManager.find(DescriptionStatusEntity.class, model.getId()); + if (data == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DescriptionStatus.class.getSimpleName()}, LocaleContextHolder.getLocale())); + if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DescriptionStatus.class.getSimpleName()}, LocaleContextHolder.getLocale())); + } else { + data = new DescriptionStatusEntity(); + data.setId(UUID.randomUUID()); + data.setIsActive(IsActive.Active); + data.setCreatedAt(Instant.now()); + } + + data.setName(model.getName()); + data.setDescription(model.getDescription()); + data.setInternalStatus(model.getInternalStatus()); + data.setDefinition(this.xmlHandlingService.toXml(this.buildDescriptionStatusDefinitionEntity(model.getDefinition()))); + data.setUpdatedAt(Instant.now()); + + if (isUpdate) + this.entityManager.merge(data); + else + this.entityManager.persist(data); + + this.entityManager.flush(); + + return this.builderFactory.builder(DescriptionStatusBuilder.class).build(BaseFieldSet.build(fields, DescriptionStatus._id), data); + } + + @Override + public void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException { + logger.debug("deleting description status: {}", id); + + this.authService.authorizeForce(Permission.DeleteDescriptionStatus); + + this.deleterFactory.deleter(DescriptionStatusDeleter.class).deleteAndSaveByIds(List.of(id)); + } + + private DescriptionStatusDefinitionEntity buildDescriptionStatusDefinitionEntity(DescriptionStatusDefinitionPersist persist) { + DescriptionStatusDefinitionEntity data = new DescriptionStatusDefinitionEntity(); + if (persist == null) + return data; + + data.setAuthorization(this.buildDescriptionStatusDefinitionAuthorizationEntity(persist.getAuthorization())); + + return data; + } + + private DescriptionStatusDefinitionAuthorizationEntity buildDescriptionStatusDefinitionAuthorizationEntity(DescriptionStatusDefinitionAuthorizationPersist persist) { + DescriptionStatusDefinitionAuthorizationEntity data = new DescriptionStatusDefinitionAuthorizationEntity(); + if (persist == null) + return data; + + data.setEdit(this.buildDescriptionStatusDefinitionAuthorizationItemEntity(persist.getEdit())); + + return data; + } + + private DescriptionStatusDefinitionAuthorizationItemEntity buildDescriptionStatusDefinitionAuthorizationItemEntity(DescriptionStatusDefinitionAuthorizationItemPersist persist) { + DescriptionStatusDefinitionAuthorizationItemEntity data = new DescriptionStatusDefinitionAuthorizationItemEntity(); + if (persist == null) + return data; + + data.setPlanRoles(persist.getPlanRoles()); + data.setRoles(persist.getRoles()); + data.setAllowAnonymous(persist.getAllowAnonymous()); + data.setAllowAuthenticated(persist.getAllowAuthenticated()); + + return data; + } +} diff --git a/backend/web/src/main/java/org/opencdmp/controllers/DescriptionStatusController.java b/backend/web/src/main/java/org/opencdmp/controllers/DescriptionStatusController.java new file mode 100644 index 000000000..74a581fdd --- /dev/null +++ b/backend/web/src/main/java/org/opencdmp/controllers/DescriptionStatusController.java @@ -0,0 +1,186 @@ +package org.opencdmp.controllers; + +import gr.cite.tools.auditing.AuditService; +import gr.cite.tools.data.builder.BuilderFactory; +import gr.cite.tools.data.censor.CensorFactory; +import gr.cite.tools.data.query.QueryFactory; +import gr.cite.tools.exception.MyApplicationException; +import gr.cite.tools.exception.MyForbiddenException; +import gr.cite.tools.exception.MyNotFoundException; +import gr.cite.tools.fieldset.FieldSet; +import gr.cite.tools.logging.LoggerService; +import gr.cite.tools.logging.MapLogEntry; +import gr.cite.tools.validation.ValidationFilterAnnotation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.ArraySchema; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.ExampleObject; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import jakarta.xml.bind.JAXBException; +import org.opencdmp.audit.AuditableAction; +import org.opencdmp.authorization.AuthorizationFlags; +import org.opencdmp.controllers.swagger.SwaggerHelpers; +import org.opencdmp.controllers.swagger.annotation.OperationWithTenantHeader; +import org.opencdmp.controllers.swagger.annotation.Swagger400; +import org.opencdmp.controllers.swagger.annotation.Swagger404; +import org.opencdmp.controllers.swagger.annotation.SwaggerCommonErrorResponses; +import org.opencdmp.data.DescriptionStatusEntity; +import org.opencdmp.model.builder.descriptionstatus.DescriptionStatusBuilder; +import org.opencdmp.model.censorship.descriptionstatus.DescriptionStatusCensor; +import org.opencdmp.model.descriptionstatus.DescriptionStatus; +import org.opencdmp.model.persist.descriptionstatus.DescriptionStatusPersist; +import org.opencdmp.model.result.QueryResult; +import org.opencdmp.query.DescriptionStatusQuery; +import org.opencdmp.query.lookup.DescriptionStatusLookup; +import org.opencdmp.service.descriptionstatus.DescriptionStatusService; +import org.slf4j.LoggerFactory; +import org.springframework.context.MessageSource; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.*; + +import javax.management.InvalidApplicationException; +import java.util.*; + +@RestController +@RequestMapping(path = "api/description-status") +@io.swagger.v3.oas.annotations.tags.Tag(name = "DescriptionStatuses", description = "Manage tags") +@SwaggerCommonErrorResponses +public class DescriptionStatusController { + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionStatusController.class)); + + private final BuilderFactory builderFactory; + + private final AuditService auditService; + + private final DescriptionStatusService descriptionStatusService; + + private final CensorFactory censorFactory; + + private final QueryFactory queryFactory; + + private final MessageSource messageSource; + + public DescriptionStatusController( + BuilderFactory builderFactory, + AuditService auditService, + DescriptionStatusService descriptionStatusService, + CensorFactory censorFactory, + QueryFactory queryFactory, + MessageSource messageSource) { + this.builderFactory = builderFactory; + this.auditService = auditService; + this.descriptionStatusService = descriptionStatusService; + this.censorFactory = censorFactory; + this.queryFactory = queryFactory; + this.messageSource = messageSource; + } + + @PostMapping("query") + @OperationWithTenantHeader(summary = "Query all descriptionStatuses", description = SwaggerHelpers.Tag.endpoint_query, requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(description = SwaggerHelpers.Tag.endpoint_query_request_body, content = @Content( + examples = { + @ExampleObject( + name = SwaggerHelpers.Commons.pagination_example, + description = SwaggerHelpers.Commons.pagination_example_description, + value = SwaggerHelpers.Tag.endpoint_query_request_body_example + ) + } + )), responses = @ApiResponse(description = "OK", responseCode = "200", content = @Content( + array = @ArraySchema( + schema = @Schema( + implementation = DescriptionStatus.class + ) + ), + examples = @ExampleObject( + name = SwaggerHelpers.Commons.pagination_response_example, + description = SwaggerHelpers.Commons.pagination_response_example_description, + value = SwaggerHelpers.Tag.endpoint_query_response_example + )))) + public QueryResult Query(@RequestBody DescriptionStatusLookup lookup) throws MyApplicationException, MyForbiddenException { + logger.debug("querying {}", DescriptionStatus.class.getSimpleName()); + + this.censorFactory.censor(DescriptionStatusCensor.class).censor(lookup.getProject(), null); + + DescriptionStatusQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.AllExceptPublic); + + List data = query.collectAs(lookup.getProject()); + List models = this.builderFactory.builder(DescriptionStatusBuilder.class).build(lookup.getProject(), data); + long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size(); + + this.auditService.track(AuditableAction.DescriptionStatus_Query); + + return new QueryResult<>(models, count); + } + + @GetMapping("{id}") + @OperationWithTenantHeader(summary = "Fetch a specific descriptionStatus by id", description = "", + responses = @ApiResponse(description = "OK", responseCode = "200", content = @Content( + schema = @Schema( + implementation = DescriptionStatus.class + )) + )) + @Swagger404 + public DescriptionStatus Get( + @Parameter(name = "id", description = "The id of a descriptionStatus to fetch", example = "c0c163dc-2965-45a5-9608-f76030578609", required = true) @PathVariable("id") UUID id, + @Parameter(name = "fieldSet", description = SwaggerHelpers.Commons.fieldset_description, required = true) FieldSet fieldSet, + Locale locale + ) throws MyApplicationException, MyForbiddenException, MyNotFoundException { + logger.debug(new MapLogEntry("retrieving" + DescriptionStatus.class.getSimpleName()).And("id", id).And("fields", fieldSet)); + + this.censorFactory.censor(DescriptionStatusCensor.class).censor(fieldSet, null); + + DescriptionStatusQuery query = this.queryFactory.query(DescriptionStatusQuery.class).disableTracking().authorize(AuthorizationFlags.AllExceptPublic).ids(id); + DescriptionStatus model = this.builderFactory.builder(DescriptionStatusBuilder.class).build(fieldSet, query.firstAs(fieldSet)); + if (model == null) + throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{id, DescriptionStatus.class.getSimpleName()}, LocaleContextHolder.getLocale())); + + this.auditService.track(AuditableAction.DescriptionStatus_Lookup, Map.ofEntries( + new AbstractMap.SimpleEntry("id", id), + new AbstractMap.SimpleEntry("fields", fieldSet) + )); + + return model; + } + + @PostMapping("persist") + @OperationWithTenantHeader(summary = "Create a new or update an existing descriptionStatus", description = "", + responses = @ApiResponse(description = "OK", responseCode = "200", content = @Content( + schema = @Schema( + implementation = DescriptionStatus.class + )) + )) + @Swagger400 + @Swagger404 + @Transactional + @ValidationFilterAnnotation(validator = DescriptionStatusPersist.DescriptionStatusPersistValidation.ValidatorName, argumentName = "model") + public DescriptionStatus Persist( + @RequestBody DescriptionStatusPersist model, + @Parameter(name = "fieldSet",description = SwaggerHelpers.Commons.fieldset_description, required = true) FieldSet fieldSet + ) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JAXBException { + logger.debug(new MapLogEntry("persisting"+DescriptionStatus.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet)); + DescriptionStatus persisted = this.descriptionStatusService.persist(model, fieldSet); + + this.auditService.track(AuditableAction.DescriptionStatus_Persist, Map.ofEntries( + new AbstractMap.SimpleEntry("model", model), + new AbstractMap.SimpleEntry("fields", fieldSet) + )); + + return persisted; + } + + @DeleteMapping("{id}") + @OperationWithTenantHeader(summary = "Delete a descriptionStatus by id", description = "", + responses = @ApiResponse(description = "OK", responseCode = "200")) + @Swagger404 + @Transactional + public void Delete( + @Parameter(name = "id", description = "The id of descriptionStatus to delete", example = "c0c163dc-2965-45a5-9608-f76030578609", required = true) @PathVariable("id") UUID id + ) throws MyForbiddenException, InvalidApplicationException { + logger.debug(new MapLogEntry("deleting"+DescriptionStatus.class.getSimpleName()).And("id", id)); + + this.descriptionStatusService.deleteAndSave(id); + + this.auditService.track(AuditableAction.DescriptionStatus_Delete, "id", id); + } +} diff --git a/backend/web/src/main/resources/config/permissions.yml b/backend/web/src/main/resources/config/permissions.yml index 64e160fce..8bb06feec 100644 --- a/backend/web/src/main/resources/config/permissions.yml +++ b/backend/web/src/main/resources/config/permissions.yml @@ -116,6 +116,33 @@ permissions: allowAnonymous: false allowAuthenticated: false + #DescriptionStatus + BrowseDescriptionStatus: + roles: + - Admin + - TenantAdmin + - TenantConfigManager + clients: [ ] + allowAnonymous: false + allowAuthenticated: false + EditDescriptionStatus: + roles: + - Admin + - TenantAdmin + - TenantConfigManager + clients: [ ] + allowAnonymous: false + allowAuthenticated: false + DeleteDescriptionStatus: + roles: + - Admin + - TenantAdmin + - TenantConfigManager + claims: [ ] + clients: [ ] + allowAnonymous: false + allowAuthenticated: false + #PlanStatus BrowsePlanStatus: roles: @@ -1324,6 +1351,14 @@ permissions: allowAnonymous: false allowAuthenticated: false ViewPlanStatusPage: + roles: + - Admin + - TenantAdmin + - TenantConfigManager + clients: [ ] + allowAnonymous: false + allowAuthenticated: false + ViewDescriptionStatusPage: roles: - Admin - TenantAdmin