From f9c3f8a64b9c62f2db9433dc88db64ed879a4b16 Mon Sep 17 00:00:00 2001 From: "CITE\\spapacharalampous" Date: Wed, 21 Aug 2024 12:41:36 +0300 Subject: [PATCH] added plan status entity --- .../org/opencdmp/audit/AuditableAction.java | 7 +- .../opencdmp/authorization/Permission.java | 4 + .../commons/enums/UsageLimitTargetMetric.java | 2 + ...anStatusDefinitionAuthorizationEntity.java | 21 ++ ...atusDefinitionAuthorizationItemEntity.java | 36 +++ .../PlanStatusDefinitionEntity.java | 17 ++ .../org/opencdmp/data/PlanStatusEntity.java | 104 ++++++++ .../builder/planstatus/PlanStatusBuilder.java | 76 ++++++ ...nStatusDefinitionAuthorizationBuilder.java | 56 +++++ ...tusDefinitionAuthorizationItemBuilder.java | 59 +++++ .../PlanStatusDefinitionBuilder.java | 56 +++++ .../planstatus/PlanStatusCensor.java | 44 ++++ ...anStatusDefinitionAuthorizationCensor.java | 45 ++++ ...atusDefinitionAuthorizationItemCensor.java | 40 ++++ .../PlanStatusDefinitionCensor.java | 41 ++++ .../model/deleter/PlanStatusDeleter.java | 85 +++++++ ...tusDefinitionAuthorizationItemPersist.java | 76 ++++++ ...nStatusDefinitionAuthorizationPersist.java | 63 +++++ .../PlanStatusDefinitionPersist.java | 64 +++++ .../persist/planstatus/PlanStatusPersist.java | 129 ++++++++++ .../opencdmp/model/planstatus/PlanStatus.java | 124 ++++++++++ .../planstatus/PlanStatusDefinition.java | 12 + .../PlanStatusDefinitionAuthorization.java | 18 ++ ...PlanStatusDefinitionAuthorizationItem.java | 30 +++ .../org/opencdmp/query/PlanStatusQuery.java | 224 ++++++++++++++++++ .../query/lookup/PlanStatusLookup.java | 65 +++++ .../service/planstatus/PlanStatusService.java | 19 ++ .../planstatus/PlanStatusServiceImpl.java | 145 ++++++++++++ .../controllers/PlanStatusController.java | 187 +++++++++++++++ 29 files changed, 1848 insertions(+), 1 deletion(-) create mode 100644 backend/core/src/main/java/org/opencdmp/commons/types/planstatus/PlanStatusDefinitionAuthorizationEntity.java create mode 100644 backend/core/src/main/java/org/opencdmp/commons/types/planstatus/PlanStatusDefinitionAuthorizationItemEntity.java create mode 100644 backend/core/src/main/java/org/opencdmp/commons/types/planstatus/PlanStatusDefinitionEntity.java create mode 100644 backend/core/src/main/java/org/opencdmp/data/PlanStatusEntity.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/builder/planstatus/PlanStatusBuilder.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/builder/planstatus/PlanStatusDefinitionAuthorizationBuilder.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/builder/planstatus/PlanStatusDefinitionAuthorizationItemBuilder.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/builder/planstatus/PlanStatusDefinitionBuilder.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusCensor.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusDefinitionAuthorizationCensor.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusDefinitionAuthorizationItemCensor.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusDefinitionCensor.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/deleter/PlanStatusDeleter.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusDefinitionAuthorizationItemPersist.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusDefinitionAuthorizationPersist.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusDefinitionPersist.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusPersist.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatus.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatusDefinition.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatusDefinitionAuthorization.java create mode 100644 backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatusDefinitionAuthorizationItem.java create mode 100644 backend/core/src/main/java/org/opencdmp/query/PlanStatusQuery.java create mode 100644 backend/core/src/main/java/org/opencdmp/query/lookup/PlanStatusLookup.java create mode 100644 backend/core/src/main/java/org/opencdmp/service/planstatus/PlanStatusService.java create mode 100644 backend/core/src/main/java/org/opencdmp/service/planstatus/PlanStatusServiceImpl.java create mode 100644 backend/web/src/main/java/org/opencdmp/controllers/PlanStatusController.java 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 88441f042..dda9850f5 100644 --- a/backend/core/src/main/java/org/opencdmp/audit/AuditableAction.java +++ b/backend/core/src/main/java/org/opencdmp/audit/AuditableAction.java @@ -196,7 +196,12 @@ public class AuditableAction { public static final EventId UsageLimit_Lookup = new EventId(290001, "UsageLimit_Lookup"); public static final EventId UsageLimit_Persist = new EventId(290002, "UsageLimit_Persist"); public static final EventId UsageLimit_Delete = new EventId(290003, "UsageLimit_Delete"); - + + public static final EventId PlanStatus_Query = new EventId(300000, "PlanStatus_Query"); + public static final EventId PlanStatus_Lookup = new EventId(300001, "PlanStatus_Lookup"); + public static final EventId PlanStatus_Persist = new EventId(300002, "PlanStatus_Persist"); + public static final EventId PlanStatus_Delete = new EventId(300003, "PlanStatus_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 e4a4762a4..f41393fc5 100644 --- a/backend/core/src/main/java/org/opencdmp/authorization/Permission.java +++ b/backend/core/src/main/java/org/opencdmp/authorization/Permission.java @@ -90,6 +90,10 @@ public final class Permission { public static String InvitePlanUsers = "InvitePlanUsers"; public static String AnnotatePlan = "AnnotatePlan"; + //PlanStatus + public static String BrowsePlanStatus = "BrowsePlanStatus"; + public static String EditPlanStatus = "EditPlanStatus"; + public static String DeletePlanStatus = "DeletePlanStatus"; //PlanBlueprint public static String BrowsePlanBlueprint = "BrowsePlanBlueprint"; 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 4699559ff..d0b33eae2 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 @@ -8,6 +8,7 @@ import java.util.Map; public enum UsageLimitTargetMetric implements DatabaseEnum { USER_COUNT(TargetMetrics.UserCount), PLAN_COUNT(TargetMetrics.PlanCount), + PLAN_STATUS_COUNT(TargetMetrics.PlanStatusCount), BLUEPRINT_COUNT(TargetMetrics.BlueprintCount), DESCRIPTION_COUNT(TargetMetrics.DescriptionCount), DESCRIPTION_TEMPLATE_COUNT(TargetMetrics.DescriptionTemplateCount), @@ -34,6 +35,7 @@ public enum UsageLimitTargetMetric implements DatabaseEnum { public static class TargetMetrics { public static final String UserCount = "user_count"; public static final String PlanCount = "plan_count"; + 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 DescriptionTemplateCount = "description_template_count"; diff --git a/backend/core/src/main/java/org/opencdmp/commons/types/planstatus/PlanStatusDefinitionAuthorizationEntity.java b/backend/core/src/main/java/org/opencdmp/commons/types/planstatus/PlanStatusDefinitionAuthorizationEntity.java new file mode 100644 index 000000000..533bf6931 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/commons/types/planstatus/PlanStatusDefinitionAuthorizationEntity.java @@ -0,0 +1,21 @@ +package org.opencdmp.commons.types.planstatus; + +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; + +import java.util.List; + +@XmlAccessorType(XmlAccessType.FIELD) +public class PlanStatusDefinitionAuthorizationEntity { + @XmlElement(name = "edit") + private List edit; + + public List getEdit() { + return edit; + } + + public void setEdit(List edit) { + this.edit = edit; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/commons/types/planstatus/PlanStatusDefinitionAuthorizationItemEntity.java b/backend/core/src/main/java/org/opencdmp/commons/types/planstatus/PlanStatusDefinitionAuthorizationItemEntity.java new file mode 100644 index 000000000..d14779de1 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/commons/types/planstatus/PlanStatusDefinitionAuthorizationItemEntity.java @@ -0,0 +1,36 @@ +package org.opencdmp.commons.types.planstatus; + +import jakarta.xml.bind.annotation.*; + +import java.util.List; + +@XmlRootElement(name = "edit") +@XmlAccessorType(XmlAccessType.FIELD) +public class PlanStatusDefinitionAuthorizationItemEntity { + + @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/planstatus/PlanStatusDefinitionEntity.java b/backend/core/src/main/java/org/opencdmp/commons/types/planstatus/PlanStatusDefinitionEntity.java new file mode 100644 index 000000000..a1a118564 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/commons/types/planstatus/PlanStatusDefinitionEntity.java @@ -0,0 +1,17 @@ +package org.opencdmp.commons.types.planstatus; + +import jakarta.xml.bind.annotation.*; + +@XmlRootElement(name = "definition") +@XmlAccessorType(XmlAccessType.FIELD) +public class PlanStatusDefinitionEntity { + + @XmlElement(name = "authorization") + private PlanStatusDefinitionAuthorizationEntity authorization; + + public PlanStatusDefinitionAuthorizationEntity getAuthorization() { + return authorization; + } + + public void setAuthorization(PlanStatusDefinitionAuthorizationEntity authorization) { this.authorization = authorization; } +} diff --git a/backend/core/src/main/java/org/opencdmp/data/PlanStatusEntity.java b/backend/core/src/main/java/org/opencdmp/data/PlanStatusEntity.java new file mode 100644 index 000000000..dc93a3f26 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/data/PlanStatusEntity.java @@ -0,0 +1,104 @@ +package org.opencdmp.data; + +import jakarta.persistence.*; +import org.hibernate.annotations.Type; +import org.opencdmp.commons.enums.IsActive; +import org.opencdmp.commons.enums.PlanStatus; +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 = "\"PlanStatus\"") +public class PlanStatusEntity 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 = PlanStatusEntity._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 PlanStatus 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 PlanStatus getInternalStatus() { + return this.internalStatus; + } + public void setInternalStatus(PlanStatus 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/planstatus/PlanStatusBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/planstatus/PlanStatusBuilder.java new file mode 100644 index 000000000..cc3ff6bf5 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/builder/planstatus/PlanStatusBuilder.java @@ -0,0 +1,76 @@ +package org.opencdmp.model.builder.planstatus; + +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.planstatus.PlanStatusDefinitionEntity; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.data.PlanStatusEntity; +import org.opencdmp.model.builder.BaseBuilder; +import org.opencdmp.model.planstatus.PlanStatus; +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(ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class PlanStatusBuilder extends BaseBuilder { + + private final XmlHandlingService xmlHandlingService; + + private final BuilderFactory builderFactory; + + private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); + + public PlanStatusBuilder(ConventionService conventionService, XmlHandlingService xmlHandlingService, BuilderFactory builderFactory) { + super(conventionService, new LoggerService(LoggerFactory.getLogger(PlanStatusDefinitionAuthorizationItemBuilder.class))); + this.xmlHandlingService = xmlHandlingService; + this.builderFactory = builderFactory; + } + + public PlanStatusBuilder 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<>(); + + FieldSet definitionFields = fields.extractPrefixed(this.asPrefix(PlanStatus._definition)); + List models = new ArrayList<>(); + + for (PlanStatusEntity d : data) { + PlanStatus m = new PlanStatus(); + + if (fields.hasField(this.asIndexer(PlanStatus._id))) + m.setId(d.getId()); + if (fields.hasField(this.asIndexer(PlanStatus._name))) + m.setName(d.getName()); + if (fields.hasField(this.asIndexer(PlanStatus._description))) + m.setDescription(d.getDescription()); + if (fields.hasField(this.asIndexer(PlanStatus._internalStatus))) + m.setInternalStatus(d.getInternalStatus()); + if (fields.hasField(this.asIndexer(PlanStatus._hash))) + m.setHash(this.hashValue(d.getUpdatedAt())); + + if (!definitionFields.isEmpty() && d.getDefinition() != null) { + PlanStatusDefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(PlanStatusDefinitionEntity.class, d.getDefinition()); + m.setDefinition(this.builderFactory.builder(PlanStatusDefinitionBuilder.class).authorize(this.authorize).build(definitionFields, definition)); + } + models.add(m); + } + + return models; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/builder/planstatus/PlanStatusDefinitionAuthorizationBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/planstatus/PlanStatusDefinitionAuthorizationBuilder.java new file mode 100644 index 000000000..1e52fd34b --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/builder/planstatus/PlanStatusDefinitionAuthorizationBuilder.java @@ -0,0 +1,56 @@ +package org.opencdmp.model.builder.planstatus; + +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.planstatus.PlanStatusDefinitionAuthorizationEntity; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.model.builder.BaseBuilder; +import org.opencdmp.model.planstatus.PlanStatusDefinitionAuthorization; +import org.slf4j.LoggerFactory; + +import java.util.*; + +public class PlanStatusDefinitionAuthorizationBuilder extends BaseBuilder { + + private final BuilderFactory builderFactory; + + private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); + + public PlanStatusDefinitionAuthorizationBuilder(ConventionService conventionService, BuilderFactory builderFactory) { + super(conventionService, new LoggerService(LoggerFactory.getLogger(PlanStatusDefinitionAuthorizationItemBuilder.class))); + this.builderFactory = builderFactory; + } + + public PlanStatusDefinitionAuthorizationBuilder 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(PlanStatusDefinitionAuthorization._edit)); + + for (PlanStatusDefinitionAuthorizationEntity d : data) { + PlanStatusDefinitionAuthorization m = new PlanStatusDefinitionAuthorization(); + + if (!editFields.isEmpty() && d.getEdit() != null) { + m.setEdit(this.builderFactory.builder(PlanStatusDefinitionAuthorizationItemBuilder.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/planstatus/PlanStatusDefinitionAuthorizationItemBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/planstatus/PlanStatusDefinitionAuthorizationItemBuilder.java new file mode 100644 index 000000000..1a100c947 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/builder/planstatus/PlanStatusDefinitionAuthorizationItemBuilder.java @@ -0,0 +1,59 @@ +package org.opencdmp.model.builder.planstatus; + +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.planstatus.PlanStatusDefinitionAuthorizationItemEntity; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.model.builder.BaseBuilder; +import org.opencdmp.model.planstatus.PlanStatusDefinitionAuthorizationItem; +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 PlanStatusDefinitionAuthorizationItemBuilder extends BaseBuilder { + + private final BuilderFactory builderFactory; + + private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); + + + public PlanStatusDefinitionAuthorizationItemBuilder(ConventionService conventionService, BuilderFactory builderFactory) { + super(conventionService, new LoggerService(LoggerFactory.getLogger(PlanStatusDefinitionAuthorizationItemBuilder.class))); + this.builderFactory = builderFactory; + } + + public PlanStatusDefinitionAuthorizationItemBuilder 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 (PlanStatusDefinitionAuthorizationItemEntity d : data) { + PlanStatusDefinitionAuthorizationItem m = new PlanStatusDefinitionAuthorizationItem(); + if (fields.hasField(this.asIndexer(PlanStatusDefinitionAuthorizationItem._planRoles))) m.setPlanRoles(d.getPlanRoles()); + if (fields.hasField(this.asIndexer(PlanStatusDefinitionAuthorizationItem._roles))) m.setRoles(d.getRoles()); + if (fields.hasField(this.asIndexer(PlanStatusDefinitionAuthorizationItem._allowAuthenticated))) m.setAllowAuthenticated(d.getAllowAuthenticated()); + if (fields.hasField(this.asIndexer(PlanStatusDefinitionAuthorizationItem._allowAnonymous))) m.setAllowAnonymous(d.getAllowAnonymous()); + models.add(m); + } + + this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0)); + return models; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/builder/planstatus/PlanStatusDefinitionBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/planstatus/PlanStatusDefinitionBuilder.java new file mode 100644 index 000000000..604f5cbb3 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/builder/planstatus/PlanStatusDefinitionBuilder.java @@ -0,0 +1,56 @@ +package org.opencdmp.model.builder.planstatus; + +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.planstatus.PlanStatusDefinitionEntity; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.model.builder.BaseBuilder; +import org.opencdmp.model.planstatus.PlanStatusDefinition; +import org.slf4j.LoggerFactory; + +import java.util.*; + +public class PlanStatusDefinitionBuilder extends BaseBuilder { + + private final BuilderFactory builderFactory; + + private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); + + public PlanStatusDefinitionBuilder(ConventionService conventionService, BuilderFactory builderFactory) { + super(conventionService, new LoggerService(LoggerFactory.getLogger(PlanStatusDefinitionAuthorizationItemBuilder.class))); + this.builderFactory = builderFactory; + } + + public PlanStatusDefinitionBuilder 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(PlanStatusDefinition._authorization)); + + for (PlanStatusDefinitionEntity d : data) { + PlanStatusDefinition m = new PlanStatusDefinition(); + + if (!authorizationFields.isEmpty() && d.getAuthorization() != null) { + m.setAuthorization(this.builderFactory.builder(PlanStatusDefinitionAuthorizationBuilder.class).authorize(this.authorize).build(authorizationFields, d.getAuthorization())); + } + + models.add(m); + } + + return models; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusCensor.java b/backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusCensor.java new file mode 100644 index 000000000..b857fac9f --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusCensor.java @@ -0,0 +1,44 @@ + +package org.opencdmp.model.censorship.planstatus; + +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.planstatus.PlanStatus; +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 PlanStatusCensor extends BaseCensor { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(PlanStatusCensor.class)); + + protected final AuthorizationService authService; + protected final CensorFactory censorFactory; + + public PlanStatusCensor(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.BrowsePlanStatus); + FieldSet definitionFields = fields.extractPrefixed(this.asIndexerPrefix(PlanStatus._definition)); + this.censorFactory.censor(PlanStatusDefinitionCensor.class).censor(definitionFields, userId); + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusDefinitionAuthorizationCensor.java b/backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusDefinitionAuthorizationCensor.java new file mode 100644 index 000000000..d48bd115e --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusDefinitionAuthorizationCensor.java @@ -0,0 +1,45 @@ +package org.opencdmp.model.censorship.planstatus; + + +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.planstatus.PlanStatusDefinitionAuthorization; +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 PlanStatusDefinitionAuthorizationCensor extends BaseCensor { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(PlanStatusDefinitionAuthorizationCensor.class)); + + protected final AuthorizationService authService; + protected final CensorFactory censorFactory; + + public PlanStatusDefinitionAuthorizationCensor(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.BrowsePlanStatus); + FieldSet editFields = fields.extractPrefixed(this.asIndexerPrefix(PlanStatusDefinitionAuthorization._edit)); + this.censorFactory.censor(PlanStatusDefinitionAuthorizationItemCensor.class).censor(editFields, userId); + } + +} \ No newline at end of file diff --git a/backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusDefinitionAuthorizationItemCensor.java b/backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusDefinitionAuthorizationItemCensor.java new file mode 100644 index 000000000..1ac4853d4 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusDefinitionAuthorizationItemCensor.java @@ -0,0 +1,40 @@ +package org.opencdmp.model.censorship.planstatus; + +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 PlanStatusDefinitionAuthorizationItemCensor extends BaseCensor { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(PlanStatusCensor.class)); + + protected final AuthorizationService authService; + protected final CensorFactory censorFactory; + + public PlanStatusDefinitionAuthorizationItemCensor(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.BrowsePlanStatus); + } +} \ No newline at end of file diff --git a/backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusDefinitionCensor.java b/backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusDefinitionCensor.java new file mode 100644 index 000000000..82590ef88 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/censorship/planstatus/PlanStatusDefinitionCensor.java @@ -0,0 +1,41 @@ +package org.opencdmp.model.censorship.planstatus; + +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.planstatus.PlanStatusDefinition; +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 PlanStatusDefinitionCensor extends BaseCensor { + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(PlanStatusDefinitionCensor.class)); + + protected final AuthorizationService authService; + protected final CensorFactory censorFactory; + + public PlanStatusDefinitionCensor(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.BrowsePlanStatus); + FieldSet authotizationFields = fields.extractPrefixed(this.asIndexerPrefix(PlanStatusDefinition._authorization)); + this.censorFactory.censor(PlanStatusDefinitionAuthorizationCensor.class).censor(authotizationFields, userId); + } +} \ No newline at end of file diff --git a/backend/core/src/main/java/org/opencdmp/model/deleter/PlanStatusDeleter.java b/backend/core/src/main/java/org/opencdmp/model/deleter/PlanStatusDeleter.java new file mode 100644 index 000000000..7fb638f8e --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/deleter/PlanStatusDeleter.java @@ -0,0 +1,85 @@ +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.PlanStatusEntity; +import org.opencdmp.data.TenantEntityManager; +import org.opencdmp.query.PlanStatusQuery; +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(ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class PlanStatusDeleter implements Deleter { + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionTemplateDeleter.class)); + + private final TenantEntityManager entityManager; + + protected final QueryFactory queryFactory; + + protected final DeleterFactory deleterFactory; + + protected final AccountingService accountingService; + + @Autowired + public PlanStatusDeleter( + TenantEntityManager entityManager, + QueryFactory queryFactory, + DeleterFactory deleterFactory, + AccountingService accountingService) { + this.entityManager = entityManager; + this.queryFactory = queryFactory; + this.deleterFactory = deleterFactory; + 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(PlanStatusQuery.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 (PlanStatusEntity 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.PLAN_STATUS_COUNT.getValue()); + } + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusDefinitionAuthorizationItemPersist.java b/backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusDefinitionAuthorizationItemPersist.java new file mode 100644 index 000000000..69b846e52 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusDefinitionAuthorizationItemPersist.java @@ -0,0 +1,76 @@ +package org.opencdmp.model.persist.planstatus; + +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.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 PlanStatusDefinitionAuthorizationItemPersist { + + public List roles = null; + public final static String _roles = "roles"; + + public List planRoles = null; + public final static String _planRoles = "planRoles"; + + public Boolean allowAuthenticated; + public final static String _allowAuthenticated = "allowAuthenticated"; + + public Boolean allowAnonymous; + public final static String _allowAnonymous = "allowAuthenticated"; + + + 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; } + + @Component(PlanStatusDefinitionAuthorizationItemPersist.PlanStatusDefinitionAuthorizationItemPersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class PlanStatusDefinitionAuthorizationItemPersistValidator extends BaseValidator { + + public static final String ValidatorName = "PlanStatus.PlanStatusDefinitionAuthorizationItemPersistValidator"; + + private final MessageSource messageSource; + + private final ValidatorFactory validatorFactory; + + protected PlanStatusDefinitionAuthorizationItemPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) { + super(conventionService, errors); + this.messageSource = messageSource; + this.validatorFactory = validatorFactory; + } + + @Override + protected Class modelClass() { + return PlanStatusDefinitionAuthorizationItemPersist.class; + } + + @Override + protected List specifications(PlanStatusDefinitionAuthorizationItemPersist 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())) + ); + } + } +} \ No newline at end of file diff --git a/backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusDefinitionAuthorizationPersist.java b/backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusDefinitionAuthorizationPersist.java new file mode 100644 index 000000000..62af5787d --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusDefinitionAuthorizationPersist.java @@ -0,0 +1,63 @@ +package org.opencdmp.model.persist.planstatus; + +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.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 PlanStatusDefinitionAuthorizationPersist { + + public final static String _edit = "edit"; + private List edit = null; + + + public List getEdit() { + return this.edit; + } + + public void setEdit(List edit) { + this.edit = edit; + } + + @Component(PlanStatusDefinitionAuthorizationPersist.PlanStatusDefinitionAuthorizationPersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class PlanStatusDefinitionAuthorizationPersistValidator extends BaseValidator { + public static final String ValidatorName = "PlanStatus.PlanStatusDefinitionAuthorizationPersistValidator"; + private final MessageSource messageSource; + private final ValidatorFactory validatorFactory; + + public PlanStatusDefinitionAuthorizationPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) { + super(conventionService, errors); + this.messageSource = messageSource; + this.validatorFactory = validatorFactory; + } + + @Override + protected Class modelClass() { + return PlanStatusDefinitionAuthorizationPersist.class; + } + + @Override + protected List specifications(PlanStatusDefinitionAuthorizationPersist item) { + return Arrays.asList( + this.spec() + .must(() -> !this.isListNullOrEmpty(item.getEdit())) + .failOn(PlanStatusDefinitionAuthorizationPersist._edit).failWith(messageSource.getMessage("Validation_Required", new Object[]{PlanStatusDefinitionAuthorizationPersist._edit}, LocaleContextHolder.getLocale())), + this.navSpec() + .iff(() -> !this.isListNullOrEmpty(item.getEdit())) + .on(PlanStatusDefinitionAuthorizationPersist._edit) + .over(item.getEdit()) + .using((itm) -> this.validatorFactory.validator(PlanStatusDefinitionAuthorizationItemPersist.PlanStatusDefinitionAuthorizationItemPersistValidator.class)) + ); + } + } +} \ No newline at end of file diff --git a/backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusDefinitionPersist.java b/backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusDefinitionPersist.java new file mode 100644 index 000000000..646f2ad46 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusDefinitionPersist.java @@ -0,0 +1,64 @@ +package org.opencdmp.model.persist.planstatus; + +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.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 org.springframework.validation.Errors; + +import java.util.Arrays; +import java.util.List; + +public class PlanStatusDefinitionPersist { + public final static String _authorization = "authorization"; + private PlanStatusDefinitionAuthorizationPersist authorization = null; + + + public PlanStatusDefinitionAuthorizationPersist getAuthorization() { return authorization; } + + public void setAuthorization(PlanStatusDefinitionAuthorizationPersist authorization) { this.authorization = authorization; } + + @Component(PlanStatusDefinitionPersist.PlanStatusDefinitionPersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class PlanStatusDefinitionPersistValidator extends BaseValidator { + public static final String ValidatorName = "PlanStatus.PlanStatusDefinitionPersistValidator"; + private final MessageSource messageSource; + private final ValidatorFactory validatorFactory; + + public PlanStatusDefinitionPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) { + super(conventionService, errors); + this.messageSource = messageSource; + this.validatorFactory = validatorFactory; + } + + @Override + protected Class modelClass() { + return PlanStatusDefinitionPersist.class; + } + + @Override + protected List specifications(PlanStatusDefinitionPersist item) { + return Arrays.asList( + this.spec() + .must(() -> !this.isNull(item.getAuthorization())) + .failOn(PlanStatusDefinitionPersist._authorization).failWith(messageSource.getMessage("Validation_Required", new Object[]{PlanStatusDefinitionPersist._authorization}, LocaleContextHolder.getLocale())), + this.refSpec() + .iff(() -> !this.isNull(item.getAuthorization())) + .on(PlanStatusDefinitionPersist._authorization) + .over(item.getAuthorization()) + .using(() -> this.validatorFactory.validator(PlanStatusDefinitionAuthorizationPersist.PlanStatusDefinitionAuthorizationPersistValidator.class)) + ); + } + + @Override + public Errors validateObject(Object target) { + return super.validateObject(target); + } + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusPersist.java b/backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusPersist.java new file mode 100644 index 000000000..edc98ff33 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/persist/planstatus/PlanStatusPersist.java @@ -0,0 +1,129 @@ +package org.opencdmp.model.persist.planstatus; + +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.data.PlanStatusEntity; +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 PlanStatusPersist { + private UUID id; + + private String name; + + public static final String _name = "name"; + + private String description; + public static final String _description = "description"; + + private org.opencdmp.commons.enums.PlanStatus internalStatus; + public static final String _internalStatus = "internalStatus"; + + private PlanStatusDefinitionPersist definition; + public static final String _definition = "definition"; + private String hash; + public static final String _hash = "hash"; + + + 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 description; } + + public void setDescription(String description) { this.description = description; } + + public org.opencdmp.commons.enums.PlanStatus getInternalStatus() { return this.internalStatus; } + public void setInternalStatus(org.opencdmp.commons.enums.PlanStatus internalStatus) { this.internalStatus = internalStatus; } + + public PlanStatusDefinitionPersist getDefinition() { return this.definition; } + public void setDefinition(PlanStatusDefinitionPersist definition) { this.definition = definition; } + + public String getHash() { + return this.hash; + } + public void setHash(String hash) { + this.hash = hash; + } + + @Component(org.opencdmp.model.persist.planstatus.PlanStatusPersist.PlanStatusPersistValidator.ValidatorName) + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public static class PlanStatusPersistValidator extends BaseValidator { + + public static final String ValidatorName = "PlanStatusPersistValidator"; + + private final MessageSource messageSource; + + private final ValidatorFactory validatorFactory; + + protected PlanStatusPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) { + super(conventionService, errors); + this.messageSource = messageSource; + this.validatorFactory = validatorFactory; + } + + @Override + protected Class modelClass() { + return org.opencdmp.model.persist.planstatus.PlanStatusPersist.class; + } + + @Override + protected List specifications(org.opencdmp.model.persist.planstatus.PlanStatusPersist item) { + return Arrays.asList( + this.spec() + .iff(() -> this.isValidGuid(item.getId())) + .must(() -> this.isValidHash(item.getHash())) + .failOn(org.opencdmp.model.persist.planstatus.PlanStatusPersist._hash).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{org.opencdmp.model.persist.planstatus.PlanStatusPersist._hash}, LocaleContextHolder.getLocale())), + this.spec() + .iff(() -> !this.isValidGuid(item.getId())) + .must(() -> !this.isValidHash(item.getHash())) + .failOn(org.opencdmp.model.persist.planstatus.PlanStatusPersist._hash).failWith(this.messageSource.getMessage("Validation_OverPosting", new Object[]{}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isEmpty(item.getName())) + .failOn(org.opencdmp.model.persist.planstatus.PlanStatusPersist._name).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{org.opencdmp.model.persist.planstatus.PlanStatusPersist._name}, LocaleContextHolder.getLocale())), + this.spec() + .iff(() -> !this.isEmpty(item.getName())) + .must(() -> this.lessEqualLength(item.getName(), PlanStatusEntity._nameLength)) + .failOn(org.opencdmp.model.persist.planstatus.PlanStatusPersist._name).failWith(this.messageSource.getMessage("Validation_MaxLength", new Object[]{org.opencdmp.model.persist.planstatus.PlanStatusPersist._name}, LocaleContextHolder.getLocale())), + this.spec() + .iff(() -> item.getInternalStatus() == org.opencdmp.commons.enums.PlanStatus.Finalized) + .must(() -> !this.isNull(item.getDefinition())) + .failOn(org.opencdmp.model.persist.planstatus.PlanStatusPersist._definition).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{org.opencdmp.model.persist.planstatus.PlanStatusPersist._definition}, LocaleContextHolder.getLocale())), + this.spec() + .must(() -> !this.isNull(item.getInternalStatus())) + .failOn(org.opencdmp.model.persist.planstatus.PlanStatusPersist._internalStatus).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{org.opencdmp.model.persist.planstatus.PlanStatusPersist._internalStatus}, LocaleContextHolder.getLocale())), + + this.refSpec() + .iff(() -> !this.isNull(item.getDefinition())) + .on(org.opencdmp.model.persist.planstatus.PlanStatusPersist._definition) + .over(item.getDefinition()) + .using(() -> this.validatorFactory.validator(PlanStatusDefinitionPersist.PlanStatusDefinitionPersistValidator.class)), + this.refSpec() + .iff(() -> item.getInternalStatus() == org.opencdmp.commons.enums.PlanStatus.Finalized) + .on(org.opencdmp.model.persist.planstatus.PlanStatusPersist._definition) + .over(item.getDefinition()) + .using(() -> this.validatorFactory.validator(PlanStatusDefinitionPersist.PlanStatusDefinitionPersistValidator.class)) + ); + } + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatus.java b/backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatus.java new file mode 100644 index 000000000..df9ca9ddd --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatus.java @@ -0,0 +1,124 @@ +package org.opencdmp.model.planstatus; + +import org.opencdmp.commons.enums.IsActive; +import java.time.Instant; +import java.util.List; +import java.util.UUID; + +public class PlanStatus { + + 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 updatedAt; + + public final static String _isActive = "isActive"; + private IsActive isActive; + + public final static String _internalStatus = "internalStatus"; + private org.opencdmp.commons.enums.PlanStatus internalStatus; + + public final static String _definition = "definition"; + private PlanStatusDefinition definition; + + public final static String _hash = "hash"; + private String hash; + + public static final String _authorizationFlags = "authorizationFlags"; + private List authorizationFlags; + + public static final String _belongsToCurrentTenant = "belongsToCurrentTenant"; + private Boolean belongsToCurrentTenant; + + + 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 description; } + + public void setDescription(String description) { this.description = description; } + + public PlanStatusDefinition getDefinition() { + return this.definition; + } + + public void setDefinition(PlanStatusDefinition definition) { + this.definition = definition; + } + + 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 org.opencdmp.commons.enums.PlanStatus getInternalStatus() { + return this.internalStatus; + } + public void setInternalStatus(org.opencdmp.commons.enums.PlanStatus internalStatus) { this.internalStatus = internalStatus; } + + public String getHash() { + return this.hash; + } + + public void setHash(String hash) { + this.hash = hash; + } + + public List getAuthorizationFlags() { + return this.authorizationFlags; + } + + public void setAuthorizationFlags(List authorizationFlags) { + this.authorizationFlags = authorizationFlags; + } + + public Boolean getBelongsToCurrentTenant() { + return this.belongsToCurrentTenant; + } + + public void setBelongsToCurrentTenant(Boolean belongsToCurrentTenant) { + this.belongsToCurrentTenant = belongsToCurrentTenant; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatusDefinition.java b/backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatusDefinition.java new file mode 100644 index 000000000..5090ccdd2 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatusDefinition.java @@ -0,0 +1,12 @@ +package org.opencdmp.model.planstatus; + +public class PlanStatusDefinition { + + public final static String _authorization = "authorization"; + private PlanStatusDefinitionAuthorization authorization; + + + public PlanStatusDefinitionAuthorization getAuthorization() { return authorization; } + + public void setAuthorization(PlanStatusDefinitionAuthorization authorization) { this.authorization = authorization; } +} diff --git a/backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatusDefinitionAuthorization.java b/backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatusDefinitionAuthorization.java new file mode 100644 index 000000000..9a46e48e9 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatusDefinitionAuthorization.java @@ -0,0 +1,18 @@ +package org.opencdmp.model.planstatus; + +import java.util.List; + +public class PlanStatusDefinitionAuthorization { + + public final static String _edit = "edit"; + private List edit; + + public List getEdit() { + return edit; + } + + public void setEdit(List edit) { + this.edit = edit; + } + +} diff --git a/backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatusDefinitionAuthorizationItem.java b/backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatusDefinitionAuthorizationItem.java new file mode 100644 index 000000000..574f496e9 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/model/planstatus/PlanStatusDefinitionAuthorizationItem.java @@ -0,0 +1,30 @@ +package org.opencdmp.model.planstatus; + +import java.util.List; + +public class PlanStatusDefinitionAuthorizationItem { + + public final static String _roles = "roles"; + public List roles; + + public final static String _planRoles = "planRoles"; + public List planRoles; + + public final static String _allowAuthenticated = "allowAuthenticated"; + public Boolean allowAuthenticated; + + public final static String _allowAnonymous = "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; } +} \ No newline at end of file diff --git a/backend/core/src/main/java/org/opencdmp/query/PlanStatusQuery.java b/backend/core/src/main/java/org/opencdmp/query/PlanStatusQuery.java new file mode 100644 index 000000000..26ef46021 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/query/PlanStatusQuery.java @@ -0,0 +1,224 @@ +package org.opencdmp.query; + +import gr.cite.commons.web.authz.service.AuthorizationService; +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.commons.scope.user.UserScope; +import org.opencdmp.data.PlanEntity; +import org.opencdmp.data.PlanStatusEntity; +import org.opencdmp.data.TenantEntityManager; +import org.opencdmp.model.planstatus.PlanStatus; +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 PlanStatusQuery extends QueryBase { + + private String like; + + private Collection ids; + + private Collection isActives; + + private Collection internalStatuses; + + private Collection excludedIds; + + private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); + + public PlanStatusQuery like(String value) { + this.like = value; + return this; + } + + public PlanStatusQuery ids(UUID value) { + this.ids = List.of(value); + return this; + } + + public PlanStatusQuery ids(UUID... value) { + this.ids = Arrays.asList(value); + return this; + } + + public PlanStatusQuery ids(Collection values) { + this.ids = values; + return this; + } + + public PlanStatusQuery excludedIds(UUID value) { + this.excludedIds = List.of(value); + return this; + } + + public PlanStatusQuery excludedIds(UUID... value) { + this.excludedIds = Arrays.asList(value); + return this; + } + + public PlanStatusQuery excludedIds(Collection values) { + this.excludedIds = values; + return this; + } + + public PlanStatusQuery isActive(IsActive value) { + this.isActives = List.of(value); + return this; + } + + public PlanStatusQuery isActive(IsActive... value) { + this.isActives = Arrays.asList(value); + return this; + } + + public PlanStatusQuery isActive(Collection values) { + this.isActives = values; + return this; + } + + public PlanStatusQuery statuses(org.opencdmp.commons.enums.PlanStatus value) { + this.internalStatuses = List.of(value); + return this; + } + + public PlanStatusQuery statuses(org.opencdmp.commons.enums.PlanStatus... value) { + this.internalStatuses = Arrays.asList(value); + return this; + } + + public PlanStatusQuery statuses(Collection values) { + this.internalStatuses = values; + return this; + } + + public PlanStatusQuery enableTracking() { + this.noTracking = false; + return this; + } + + public PlanStatusQuery disableTracking() { + this.noTracking = true; + return this; + } + + public PlanStatusQuery authorize(EnumSet values) { + this.authorize = values; + return this; + } + + private final QueryUtilsService queryUtilsService; + private final TenantEntityManager tenantEntityManager; + + public PlanStatusQuery( + QueryUtilsService queryUtilsService, TenantEntityManager tenantEntityManager) { + this.queryUtilsService = queryUtilsService; + this.tenantEntityManager = tenantEntityManager; + } + + @Override + protected EntityManager entityManager(){ + return this.tenantEntityManager.getEntityManager(); + } + + @Override + protected Boolean isFalseQuery() { + return this.isEmpty(this.ids) || this.isEmpty(this.isActives) || this.isEmpty(this.internalStatuses); + } + + @Override + protected Class entityClass() { return PlanStatusEntity.class; } + + @Override + protected Predicate applyFilters(QueryContext queryContext) { + List predicates = new ArrayList<>(); + + if (this.ids != null) { + CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(PlanStatusEntity._id)); + for (UUID item : this.ids) + inClause.value(item); + predicates.add(inClause); + } + if (this.like != null && !this.like.isBlank()) { + predicates.add(queryContext.CriteriaBuilder.or( + this.queryUtilsService.ilike(queryContext.CriteriaBuilder, queryContext.Root.get(PlanStatusEntity._name), this.like), + this.queryUtilsService.ilike(queryContext.CriteriaBuilder, queryContext.Root.get(PlanStatusEntity._description), this.like))); + } + if (this.isActives != null) { + CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(PlanStatusEntity._isActive)); + for (IsActive item : this.isActives) + inClause.value(item); + predicates.add(inClause); + } + if (this.internalStatuses != null) { + CriteriaBuilder.In inClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(PlanStatusEntity._internalStatus)); + for (org.opencdmp.commons.enums.PlanStatus item : this.internalStatuses) + inClause.value(item); + predicates.add(inClause); + } + if (this.excludedIds != null) { + CriteriaBuilder.In notInClause = queryContext.CriteriaBuilder.in(queryContext.Root.get(PlanStatusEntity._id)); + for (UUID item : this.excludedIds) + notInClause.value(item); + 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(PlanStatus._id)) + return PlanStatus._id; + else if (item.match(PlanStatus._description)) + return PlanStatus._description; + else if (item.match(PlanStatus._name)) + return PlanStatus._name; + else if (item.match(PlanStatus._internalStatus)) + return PlanStatus._internalStatus; + else if (item.match(PlanStatus._definition)) + return PlanStatus._definition; + else if (item.prefix(PlanStatus._definition)) + return PlanStatus._definition; + else if (item.match(PlanStatus._createdAt)) + return PlanStatus._createdAt; + else if (item.match(PlanStatus._updatedAt)) + return PlanStatus._updatedAt; + else if (item.match(PlanStatus._isActive)) + return PlanStatus._isActive; + else if (item.match(PlanStatus._belongsToCurrentTenant)) + return PlanStatus._belongsToCurrentTenant; + else + return null; + } + + @Override + protected PlanStatusEntity convert(Tuple tuple, Set columns) { + PlanStatusEntity item = new PlanStatusEntity(); + item.setId(QueryBase.convertSafe(tuple, columns, PlanStatusEntity._id, UUID.class)); + item.setTenantId(QueryBase.convertSafe(tuple, columns, PlanStatusEntity._tenantId, UUID.class)); + item.setDescription(QueryBase.convertSafe(tuple, columns, PlanStatusEntity._description, String.class)); + item.setDefinition(QueryBase.convertSafe(tuple, columns, PlanStatusEntity._definition, String.class)); + item.setName(QueryBase.convertSafe(tuple, columns, PlanStatusEntity._name, String.class)); + item.setInternalStatus(QueryBase.convertSafe(tuple, columns, PlanStatusEntity._internalStatus, org.opencdmp.commons.enums.PlanStatus.class)); + item.setCreatedAt(QueryBase.convertSafe(tuple, columns, PlanStatusEntity._createdAt, Instant.class)); + item.setUpdatedAt(QueryBase.convertSafe(tuple, columns, PlanStatusEntity._updatedAt, Instant.class)); + return item; + } +} diff --git a/backend/core/src/main/java/org/opencdmp/query/lookup/PlanStatusLookup.java b/backend/core/src/main/java/org/opencdmp/query/lookup/PlanStatusLookup.java new file mode 100644 index 000000000..b0e9db833 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/query/lookup/PlanStatusLookup.java @@ -0,0 +1,65 @@ +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.PlanStatusQuery; + +import java.util.List; +import java.util.UUID; + +public class PlanStatusLookup 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 PlanStatusQuery enrich(QueryFactory queryFactory) { + PlanStatusQuery query = queryFactory.query(PlanStatusQuery.class); + if (this.like != null) query.like(this.like); + if (this.ids != null) query.ids(this.ids); + if (this.excludedIds != null) query.excludedIds(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/planstatus/PlanStatusService.java b/backend/core/src/main/java/org/opencdmp/service/planstatus/PlanStatusService.java new file mode 100644 index 000000000..f98cb87dd --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/service/planstatus/PlanStatusService.java @@ -0,0 +1,19 @@ +package org.opencdmp.service.planstatus; + +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.persist.planstatus.PlanStatusPersist; +import org.opencdmp.model.planstatus.PlanStatus; + +import javax.management.InvalidApplicationException; +import java.util.UUID; + +public interface PlanStatusService { + PlanStatus persist(PlanStatusPersist 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/planstatus/PlanStatusServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/planstatus/PlanStatusServiceImpl.java new file mode 100644 index 000000000..8ba9cf16e --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/service/planstatus/PlanStatusServiceImpl.java @@ -0,0 +1,145 @@ +package org.opencdmp.service.planstatus; + +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.jetbrains.annotations.NotNull; +import org.opencdmp.authorization.Permission; +import org.opencdmp.commons.XmlHandlingService; +import org.opencdmp.commons.enums.IsActive; +import org.opencdmp.commons.types.planstatus.PlanStatusDefinitionAuthorizationEntity; +import org.opencdmp.commons.types.planstatus.PlanStatusDefinitionAuthorizationItemEntity; +import org.opencdmp.commons.types.planstatus.PlanStatusDefinitionEntity; +import org.opencdmp.convention.ConventionService; +import org.opencdmp.data.PlanStatusEntity; +import org.opencdmp.data.TenantEntityManager; +import org.opencdmp.errorcode.ErrorThesaurusProperties; +import org.opencdmp.event.EventBroker; +import org.opencdmp.model.builder.planstatus.PlanStatusBuilder; +import org.opencdmp.model.deleter.PlanStatusDeleter; +import org.opencdmp.model.persist.planstatus.PlanStatusDefinitionAuthorizationItemPersist; +import org.opencdmp.model.persist.planstatus.PlanStatusDefinitionPersist; +import org.opencdmp.model.persist.planstatus.PlanStatusPersist; +import org.opencdmp.model.planstatus.PlanStatus; +import org.opencdmp.service.tag.TagServiceImpl; +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.ArrayList; +import java.util.List; +import java.util.UUID; + +public class PlanStatusServiceImpl implements PlanStatusService { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(PlanStatusServiceImpl.class)); + + private final BuilderFactory builderFactory; + private final DeleterFactory deleterFactory; + + private final AuthorizationService authorizationService; + private final ConventionService conventionService; + private final XmlHandlingService xmlHandlingService; + private final TenantEntityManager entityManager; + private final MessageSource messageSource; + private final ErrorThesaurusProperties errors; + + public PlanStatusServiceImpl(BuilderFactory builderFactory, DeleterFactory deleterFactory, AuthorizationService authorizationService, ConventionService conventionService, XmlHandlingService xmlHandlingService, TenantEntityManager entityManager, MessageSource messageSource, ErrorThesaurusProperties errors, EventBroker eventBroker) { + this.builderFactory = builderFactory; + this.deleterFactory = deleterFactory; + + this.authorizationService = authorizationService; + this.conventionService = conventionService; + this.xmlHandlingService = xmlHandlingService; + this.entityManager = entityManager; + this.messageSource = messageSource; + this.errors = errors; + } + + @Override + public PlanStatus persist(PlanStatusPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException { + logger.debug(new MapLogEntry("persisting data plan status").And("model", model).And("fields", fields)); + + this.authorizationService.authorizeForce(Permission.EditPlanStatus); + + Boolean isUpdate = this.conventionService.isValidGuid(model.getId()); + + PlanStatusEntity data; + if (isUpdate) { + data = this.entityManager.find(PlanStatusEntity.class, model.getId()); + if (data == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), PlanStatus.class.getSimpleName()}, LocaleContextHolder.getLocale())); + if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage()); + } else { + data = new PlanStatusEntity(); + data.setId(UUID.randomUUID()); + data.setIsActive(IsActive.Active); + data.setCreatedAt(Instant.now()); + } + + data.setName(model.getName()); + data.setDescription(model.getDescription()); + data.setDefinition(this.xmlHandlingService.toXml(this.buildPlanStatusDefinitionEntity(model.getDefinition()))); + data.setUpdatedAt(Instant.now()); + + if (isUpdate) + this.entityManager.merge(data); + else + this.entityManager.persist(data); + + this.entityManager.flush(); + + return this.builderFactory.builder(PlanStatusBuilder.class).build(BaseFieldSet.build(fields, PlanStatus._id), data); + } + + @Override + public void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException { + logger.debug("deleting plan status: {}", id); + + this.authorizationService.authorizeForce(Permission.DeletePlanStatus); + + this.deleterFactory.deleter(PlanStatusDeleter.class).deleteAndSaveByIds(List.of(id)); + } + + private @NotNull PlanStatusDefinitionEntity buildPlanStatusDefinitionEntity(PlanStatusDefinitionPersist persist) { + PlanStatusDefinitionEntity data = new PlanStatusDefinitionEntity(); + if (persist == null) + return data; + + if (persist.getAuthorization() != null) { + PlanStatusDefinitionAuthorizationEntity definitionAuthorizationData = new PlanStatusDefinitionAuthorizationEntity(); + data.setAuthorization(definitionAuthorizationData); + if (!this.conventionService.isListNullOrEmpty(persist.getAuthorization().getEdit())) { + definitionAuthorizationData.setEdit(new ArrayList<>()); + + for (PlanStatusDefinitionAuthorizationItemPersist p : persist.getAuthorization().getEdit()) { + definitionAuthorizationData.getEdit().add(this.buildPlanStatusDefinitionAuthorizationItemEntity(p)); + } + } + } + return data; + } + + private @NotNull PlanStatusDefinitionAuthorizationItemEntity buildPlanStatusDefinitionAuthorizationItemEntity(PlanStatusDefinitionAuthorizationItemPersist persist) { + PlanStatusDefinitionAuthorizationItemEntity data = new PlanStatusDefinitionAuthorizationItemEntity(); + if (persist == null) + return data; + + data.setPlanRoles(persist.getPlanRoles()); + data.setRoles(persist.getRoles()); + data.setAllowAuthenticated(persist.getAllowAuthenticated()); + data.setAllowAnonymous(persist.getAllowAnonymous()); + + return data; + } +} diff --git a/backend/web/src/main/java/org/opencdmp/controllers/PlanStatusController.java b/backend/web/src/main/java/org/opencdmp/controllers/PlanStatusController.java new file mode 100644 index 000000000..bf29f1190 --- /dev/null +++ b/backend/web/src/main/java/org/opencdmp/controllers/PlanStatusController.java @@ -0,0 +1,187 @@ +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.PlanStatusEntity; +import org.opencdmp.model.builder.planstatus.PlanStatusBuilder; +import org.opencdmp.model.censorship.planstatus.PlanStatusCensor; +import org.opencdmp.model.persist.planstatus.PlanStatusPersist; +import org.opencdmp.model.planstatus.PlanStatus; +import org.opencdmp.model.result.QueryResult; +import org.opencdmp.query.PlanStatusQuery; +import org.opencdmp.query.lookup.PlanStatusLookup; +import org.opencdmp.service.planstatus.PlanStatusService; +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/plan-status") +@io.swagger.v3.oas.annotations.tags.Tag(name = "PlanStatuses", description = "Manage tags") +@SwaggerCommonErrorResponses +public class PlanStatusController { + + private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(PlanStatusController.class)); + + private final BuilderFactory builderFactory; + + private final AuditService auditService; + + private final PlanStatusService planStatusService; + + private final CensorFactory censorFactory; + + private final QueryFactory queryFactory; + + private final MessageSource messageSource; + + public PlanStatusController( + BuilderFactory builderFactory, + AuditService auditService, + PlanStatusService planStatusService, + CensorFactory censorFactory, + QueryFactory queryFactory, + MessageSource messageSource) { + this.builderFactory = builderFactory; + this.auditService = auditService; + this.planStatusService = planStatusService; + this.censorFactory = censorFactory; + this.queryFactory = queryFactory; + this.messageSource = messageSource; + } + + @PostMapping("query") + @OperationWithTenantHeader(summary = "Query all planStatuses", 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 = PlanStatus.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 PlanStatusLookup lookup) throws MyApplicationException, MyForbiddenException { + logger.debug("querying {}", PlanStatus.class.getSimpleName()); + + this.censorFactory.censor(PlanStatusCensor.class).censor(lookup.getProject(), null); + + PlanStatusQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.AllExceptPublic); + + List data = query.collectAs(lookup.getProject()); + List models = this.builderFactory.builder(PlanStatusBuilder.class).authorize(AuthorizationFlags.AllExceptPublic).build(lookup.getProject(), data); + long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size(); + + this.auditService.track(AuditableAction.PlanStatus_Query, "lookup", lookup); + + return new QueryResult<>(models, count); + } + + @GetMapping("{id}") + @OperationWithTenantHeader(summary = "Fetch a specific planStatus by id", description = "", + responses = @ApiResponse(description = "OK", responseCode = "200", content = @Content( + schema = @Schema( + implementation = PlanStatus.class + )) + )) + @Swagger404 + public PlanStatus Get( + @Parameter(name = "id", description = "The id of a planStatus 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" + PlanStatus.class.getSimpleName()).And("id", id).And("fields", fieldSet)); + + this.censorFactory.censor(PlanStatusCensor.class).censor(fieldSet, null); + + PlanStatusQuery query = this.queryFactory.query(PlanStatusQuery.class).disableTracking().authorize(AuthorizationFlags.AllExceptPublic).ids(id); + PlanStatus model = this.builderFactory.builder(PlanStatusBuilder.class).authorize(AuthorizationFlags.AllExceptPublic).build(fieldSet, query.firstAs(fieldSet)); + if (model == null) + throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{id, PlanStatus.class.getSimpleName()}, LocaleContextHolder.getLocale())); + + this.auditService.track(AuditableAction.PlanStatus_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 planStatus", description = "", + responses = @ApiResponse(description = "OK", responseCode = "200", content = @Content( + schema = @Schema( + implementation = PlanStatus.class + )) + )) + @Swagger400 + @Swagger404 + @Transactional + @ValidationFilterAnnotation(validator = PlanStatusPersist.PlanStatusPersistValidator.ValidatorName, argumentName = "model") + public PlanStatus Persist( + @RequestBody PlanStatusPersist model, + @Parameter(name = "fieldSet", description = SwaggerHelpers.Commons.fieldset_description, required = true) FieldSet fieldSet + ) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JAXBException { + logger.debug(new MapLogEntry("persisting" + PlanStatus.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet)); + PlanStatus persisted = this.planStatusService.persist(model, fieldSet); + + this.auditService.track(AuditableAction.PlanStatus_Persist, Map.ofEntries( + new AbstractMap.SimpleEntry("model", model), + new AbstractMap.SimpleEntry("fields", fieldSet) + )); + + return persisted; + } + + @DeleteMapping("{id}") + @OperationWithTenantHeader(summary = "Delete a planStatus by id", description = "", + responses = @ApiResponse(description = "OK", responseCode = "200")) + @Swagger404 + @Transactional + public void Delete( + @Parameter(name = "id", description = "The id of a planStatus to delete", example = "c0c163dc-2965-45a5-9608-f76030578609", required = true) @PathVariable("id") UUID id + ) throws MyForbiddenException, InvalidApplicationException { + logger.debug(new MapLogEntry("retrieving" + PlanStatus.class.getSimpleName()).And("id", id)); + + this.planStatusService.deleteAndSave(id); + + this.auditService.track(AuditableAction.PlanStatus_Delete, "id", id); + } +}