diff --git a/backend/core/src/main/java/org/opencdmp/data/PlanEntity.java b/backend/core/src/main/java/org/opencdmp/data/PlanEntity.java index f3e6d072d..cd92a36c3 100644 --- a/backend/core/src/main/java/org/opencdmp/data/PlanEntity.java +++ b/backend/core/src/main/java/org/opencdmp/data/PlanEntity.java @@ -47,6 +47,10 @@ public class PlanEntity extends TenantScopedBaseEntity { public static final String _status = "status"; + @Column(name = "status_id", nullable = true) + private UUID statusId; + public static final String _statusId = "statusId"; + @Column(name = "properties", nullable = true) private String properties; @@ -141,6 +145,14 @@ public class PlanEntity extends TenantScopedBaseEntity { this.status = status; } + public UUID getStatusId() { + return statusId; + } + + public void setStatusId(UUID statusId) { + this.statusId = statusId; + } + public String getProperties() { return this.properties; } diff --git a/backend/core/src/main/java/org/opencdmp/model/builder/plan/PlanBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/plan/PlanBuilder.java index 030dd26ff..1f84d649e 100644 --- a/backend/core/src/main/java/org/opencdmp/model/builder/plan/PlanBuilder.java +++ b/backend/core/src/main/java/org/opencdmp/model/builder/plan/PlanBuilder.java @@ -27,10 +27,12 @@ import org.opencdmp.model.builder.*; import org.opencdmp.model.builder.description.DescriptionBuilder; import org.opencdmp.model.builder.planblueprint.PlanBlueprintBuilder; import org.opencdmp.model.builder.planreference.PlanReferenceBuilder; +import org.opencdmp.model.builder.planstatus.PlanStatusBuilder; import org.opencdmp.model.description.Description; import org.opencdmp.model.plan.Plan; import org.opencdmp.model.planblueprint.PlanBlueprint; import org.opencdmp.model.planreference.PlanReference; +import org.opencdmp.model.planstatus.PlanStatus; import org.opencdmp.model.user.User; import org.opencdmp.query.*; import org.slf4j.LoggerFactory; @@ -85,6 +87,9 @@ public class PlanBuilder extends BaseBuilder { List models = new ArrayList<>(); + FieldSet statusFields = fields.extractPrefixed(this.asPrefix(Description._status)); + Map statusItemsMap = this.collectPlanStatuses(statusFields, data); + FieldSet entityDoisFields = fields.extractPrefixed(this.asPrefix(Plan._entityDois)); Map> entityDoisMap = this.collectEntityDois(entityDoisFields, data); @@ -121,7 +126,7 @@ public class PlanBuilder extends BaseBuilder { if (fields.hasField(this.asIndexer(Plan._tenantId))) m.setTenantId(d.getTenantId()); if (fields.hasField(this.asIndexer(Plan._label))) m.setLabel(d.getLabel()); if (fields.hasField(this.asIndexer(Plan._version))) m.setVersion(d.getVersion()); - if (fields.hasField(this.asIndexer(Plan._status))) m.setStatus(d.getStatus()); + if (!statusFields.isEmpty() && statusItemsMap != null && statusItemsMap.containsKey(d.getStatusId())) m.setStatus(statusItemsMap.get(d.getStatusId())); if (fields.hasField(this.asIndexer(Plan._groupId))) m.setGroupId(d.getGroupId()); if (fields.hasField(this.asIndexer(Plan._description))) m.setDescription(d.getDescription()); if (fields.hasField(this.asIndexer(Plan._createdAt))) m.setCreatedAt(d.getCreatedAt()); @@ -157,6 +162,36 @@ public class PlanBuilder extends BaseBuilder { return models; } + private Map collectPlanStatuses(FieldSet fields, List data) throws MyApplicationException { + if (fields.isEmpty() || data.isEmpty()) + return null; + this.logger.debug("checking related - {}", PlanStatus.class.getSimpleName()); + + Map itemMap; + if (!fields.hasOtherField(this.asIndexer(PlanStatus._id))) { + itemMap = this.asEmpty( + data.stream().map(PlanEntity::getStatusId).distinct().collect(Collectors.toList()), + x -> { + PlanStatus item = new PlanStatus(); + item.setId(x); + return item; + }, + PlanStatus::getId); + } else { + FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(PlanStatus._id); + PlanStatusQuery q = this.queryFactory.query(PlanStatusQuery.class).disableTracking().authorize(this.authorize).ids(data.stream().map(PlanEntity::getStatusId).distinct().collect(Collectors.toList())); + itemMap = this.builderFactory.builder(PlanStatusBuilder.class).authorize(this.authorize).asForeignKey(q, clone, PlanStatus::getId); + } + if (!fields.hasField(PlanStatus._id)) { + itemMap.forEach((id, item) -> { + if (item != null) + item.setId(null); + }); + } + + return itemMap; + } + private Map> collectPlanReferences(FieldSet fields, List data) throws MyApplicationException { if (fields.isEmpty() || data.isEmpty()) return null; this.logger.debug("checking related - {}", PlanReference.class.getSimpleName()); diff --git a/backend/core/src/main/java/org/opencdmp/model/persist/PlanPersist.java b/backend/core/src/main/java/org/opencdmp/model/persist/PlanPersist.java index 4173d902b..1b39c96a4 100644 --- a/backend/core/src/main/java/org/opencdmp/model/persist/PlanPersist.java +++ b/backend/core/src/main/java/org/opencdmp/model/persist/PlanPersist.java @@ -36,9 +36,9 @@ public class PlanPersist { public static final String _label = "label"; - private PlanStatus status; + private UUID statusId; - public static final String _status = "status"; + public static final String _statusId = "statusId"; private PlanPropertiesPersist properties; @@ -84,12 +84,12 @@ public class PlanPersist { this.label = label; } - public PlanStatus getStatus() { - return this.status; + public UUID getStatusId() { + return statusId; } - public void setStatus(PlanStatus status) { - this.status = status; + public void setStatusId(UUID statusId) { + this.statusId = statusId; } public PlanPropertiesPersist getProperties() { @@ -186,9 +186,10 @@ public class PlanPersist { @Override protected List specifications(PlanPersist item) { PlanBlueprintEntity planBlueprintEntity = null; + PlanStatusEntity statusEntity = null; try { planBlueprintEntity = this.isValidGuid(item.getBlueprint()) ? this.entityManager.find(PlanBlueprintEntity.class, item.getBlueprint(), true) : null; - + statusEntity = this.isValidGuid(item.getStatusId()) ? this.entityManager.find(PlanStatusEntity.class, item.getStatusId(), true) : null; } catch (InvalidApplicationException e) { throw new RuntimeException(e); } @@ -203,6 +204,7 @@ public class PlanPersist { accessFieldLabel = this.getSystemFieldLabel(definition, PlanBlueprintSystemFieldType.AccessRights); } + PlanStatusEntity finalStatusEntity = statusEntity; return Arrays.asList( this.spec() .iff(() -> this.isValidGuid(item.getId())) @@ -220,40 +222,41 @@ public class PlanPersist { .must(() -> this.lessEqualLength(item.getLabel(), PlanEntity._labelLength)) .failOn(PlanPersist._label).failWith(this.messageSource.getMessage("Validation_MaxLength", new Object[]{PlanPersist._label}, LocaleContextHolder.getLocale())), this.spec() - .iff(() -> item.getStatus() == PlanStatus.Finalized) + .iff(() -> finalStatusEntity != null && finalStatusEntity.getInternalStatus() != null && finalStatusEntity.getInternalStatus() == PlanStatus.Finalized) .must(() -> !this.isEmpty(item.getDescription())) .failOn(PlanPersist._description).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{PlanPersist._description}, LocaleContextHolder.getLocale())), this.spec() - .must(() -> !this.isNull(item.getStatus())) - .failOn(PlanPersist._status).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{PlanPersist._status}, LocaleContextHolder.getLocale())), + .iff(() -> this.isValidGuid(item.getId())) + .must(() -> !this.isNull(item.getStatusId())) + .failOn(PlanPersist._statusId).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{PlanPersist._statusId}, LocaleContextHolder.getLocale())), this.spec() - .iff(() -> item.getStatus() == PlanStatus.Finalized) + .iff(() -> finalStatusEntity != null && finalStatusEntity.getInternalStatus() != null && finalStatusEntity.getInternalStatus() == PlanStatus.Finalized) .must(() -> this.isValidGuid(item.getBlueprint())) .failOn(PlanPersist._blueprint).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{PlanPersist._blueprint}, LocaleContextHolder.getLocale())), this.spec() - .iff(() -> item.getStatus() == PlanStatus.Finalized) + .iff(() -> finalStatusEntity != null && finalStatusEntity.getInternalStatus() != null && finalStatusEntity.getInternalStatus() == PlanStatus.Finalized) .must(() -> !this.isNull(item.getProperties())) .failOn(PlanPersist._properties).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{PlanPersist._properties}, LocaleContextHolder.getLocale())), this.spec() - .iff(() -> item.getStatus() == PlanStatus.Finalized) + .iff(() -> finalStatusEntity != null && finalStatusEntity.getInternalStatus() != null && finalStatusEntity.getInternalStatus() == PlanStatus.Finalized) .must(() -> this.isDescriptionTemplateMultiplicityValid(finalPlanBlueprintEntity, item.getId())) .failOn(PlanPersist._descriptionTemplates).failWith(this.messageSource.getMessage("Validation.InvalidDescriptionTemplateMultiplicityOnPlan", new Object[]{PlanPersist._descriptionTemplates}, LocaleContextHolder.getLocale())), this.refSpec() .iff(() -> !this.isNull(item.getProperties())) .on(PlanPersist._properties) .over(item.getProperties()) - .using(() -> this.validatorFactory.validator(PlanPropertiesPersist.PlanPropertiesPersistValidator.class).setStatus(item.getStatus()).withDefinition(definition)), + .using(() -> this.validatorFactory.validator(PlanPropertiesPersist.PlanPropertiesPersistValidator.class).setStatus(finalStatusEntity.getInternalStatus()).withDefinition(definition)), this.spec() - .iff(() -> item.getStatus() == PlanStatus.Finalized) + .iff(() -> finalStatusEntity != null && finalStatusEntity.getInternalStatus() != null && finalStatusEntity.getInternalStatus() == PlanStatus.Finalized) .must(() -> !this.isNull(item.getLanguage())) .failOn(PlanPersist._language).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{languageFieldLabel != null ? languageFieldLabel : PlanBlueprintSystemFieldType.Language.name()}, LocaleContextHolder.getLocale())), this.spec() - .iff(() -> item.getStatus() == PlanStatus.Finalized) + .iff(() -> finalStatusEntity != null && finalStatusEntity.getInternalStatus() != null && finalStatusEntity.getInternalStatus() == PlanStatus.Finalized) .must(() -> !this.isNull(item.getAccessType())) .failOn(PlanPersist._accessType).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{accessFieldLabel != null ? accessFieldLabel : PlanBlueprintSystemFieldType.AccessRights.name()}, LocaleContextHolder.getLocale())), this.spec() - .iff(() -> item.getStatus() == PlanStatus.Finalized) + .iff(() -> finalStatusEntity != null && finalStatusEntity.getInternalStatus() != null && finalStatusEntity.getInternalStatus() == PlanStatus.Finalized) .must(() -> !this.isListNullOrEmpty(item.getDescriptionTemplates())) .failOn(PlanPersist._descriptionTemplates).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{PlanPersist._descriptionTemplates}, LocaleContextHolder.getLocale())), this.navSpec() diff --git a/backend/core/src/main/java/org/opencdmp/model/plan/Plan.java b/backend/core/src/main/java/org/opencdmp/model/plan/Plan.java index 750b59cee..298edbbe5 100644 --- a/backend/core/src/main/java/org/opencdmp/model/plan/Plan.java +++ b/backend/core/src/main/java/org/opencdmp/model/plan/Plan.java @@ -1,13 +1,13 @@ package org.opencdmp.model.plan; import org.opencdmp.commons.enums.PlanAccessType; -import org.opencdmp.commons.enums.PlanStatus; import org.opencdmp.commons.enums.PlanVersionStatus; import org.opencdmp.commons.enums.IsActive; import org.opencdmp.model.*; import org.opencdmp.model.description.Description; import org.opencdmp.model.planblueprint.PlanBlueprint; import org.opencdmp.model.planreference.PlanReference; +import org.opencdmp.model.planstatus.PlanStatus; import org.opencdmp.model.user.User; import java.time.Instant; diff --git a/backend/core/src/main/java/org/opencdmp/service/dashborad/DashboardServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/dashborad/DashboardServiceImpl.java index 20ab26aff..9e5988288 100644 --- a/backend/core/src/main/java/org/opencdmp/service/dashborad/DashboardServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/dashborad/DashboardServiceImpl.java @@ -88,9 +88,9 @@ public class DashboardServiceImpl implements DashboardService { planLookup.getPage().setOffset(0); planLookup.getPage().setSize(model.getPage().getSize()+model.getPage().getOffset()); - QueryResult plans = this.elasticQueryHelperService.collect(planLookup, AuthorizationFlags.AllExceptPublic, new BaseFieldSet().ensure(Plan._id).ensure(Plan._updatedAt).ensure(Plan._label).ensure(Plan._status)); + QueryResult plans = this.elasticQueryHelperService.collect(planLookup, AuthorizationFlags.AllExceptPublic, new BaseFieldSet().ensure(Plan._id).ensure(Plan._updatedAt).ensure(Plan._label).ensure(String.join(".",Plan._status, org.opencdmp.model.planstatus.PlanStatus._internalStatus))); if (!this.conventionService.isListNullOrEmpty(plans.getItems())) { - for (Plan plan : plans.getItems()) recentActivityItemEntities.add(new RecentActivityItemEntity(RecentActivityItemType.Plan, plan.getId(), plan.getUpdatedAt(), plan.getLabel(), plan.getStatus().getValue())); + for (Plan plan : plans.getItems()) recentActivityItemEntities.add(new RecentActivityItemEntity(RecentActivityItemType.Plan, plan.getId(), plan.getUpdatedAt(), plan.getLabel(), plan.getStatus().getInternalStatus().getValue())); } } diff --git a/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java index d7107a022..244ca06db 100644 --- a/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java @@ -102,6 +102,7 @@ import org.opencdmp.service.descriptionworkflow.DescriptionWorkflowService; import org.opencdmp.service.elastic.ElasticService; import org.opencdmp.service.filetransformer.FileTransformerService; import org.opencdmp.service.planblueprint.PlanBlueprintService; +import org.opencdmp.service.planworkflow.PlanWorkflowServiceImpl; import org.opencdmp.service.responseutils.ResponseUtilsService; import org.opencdmp.service.usagelimit.UsageLimitService; import org.slf4j.LoggerFactory; @@ -185,6 +186,7 @@ public class PlanServiceImpl implements PlanService { private final UsageLimitService usageLimitService; private final AccountingService accountingService; private final DescriptionWorkflowService descriptionWorkflowService; + private final PlanWorkflowServiceImpl planWorkflowService; @Autowired public PlanServiceImpl( @@ -207,7 +209,7 @@ public class PlanServiceImpl implements PlanService { FileTransformerService fileTransformerService, ValidatorFactory validatorFactory, ElasticService elasticService, DescriptionTemplateService descriptionTemplateService, - AnnotationEntityTouchedIntegrationEventHandler annotationEntityTouchedIntegrationEventHandler, AnnotationEntityRemovalIntegrationEventHandler annotationEntityRemovalIntegrationEventHandler, AuthorizationContentResolver authorizationContentResolver, TenantScope tenantScope, ResponseUtilsService responseUtilsService, PlanBlueprintService planBlueprintService, UsageLimitService usageLimitService, AccountingService accountingService, DescriptionWorkflowService descriptionWorkflowService) { + AnnotationEntityTouchedIntegrationEventHandler annotationEntityTouchedIntegrationEventHandler, AnnotationEntityRemovalIntegrationEventHandler annotationEntityRemovalIntegrationEventHandler, AuthorizationContentResolver authorizationContentResolver, TenantScope tenantScope, ResponseUtilsService responseUtilsService, PlanBlueprintService planBlueprintService, UsageLimitService usageLimitService, AccountingService accountingService, DescriptionWorkflowService descriptionWorkflowService, PlanWorkflowServiceImpl planWorkflowService) { this.entityManager = entityManager; this.authorizationService = authorizationService; this.deleterFactory = deleterFactory; @@ -237,6 +239,7 @@ public class PlanServiceImpl implements PlanService { this.usageLimitService = usageLimitService; this.accountingService = accountingService; this.descriptionWorkflowService = descriptionWorkflowService; + this.planWorkflowService = planWorkflowService; } public Plan persist(PlanPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException, IOException { @@ -1314,9 +1317,13 @@ public class PlanServiceImpl implements PlanService { data = this.entityManager.find(PlanEntity.class, model.getId()); if (data == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), Plan.class.getSimpleName()}, LocaleContextHolder.getLocale())); if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage()); - if (model.getStatus() != null && model.getStatus() == PlanStatus.Finalized && data.getStatus() != PlanStatus.Finalized) { + PlanStatusEntity oldPlanStatusEntity = this.entityManager.find(PlanStatusEntity.class, data.getStatusId(), true); + if (oldPlanStatusEntity == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{data.getStatusId(), PlanStatusEntity.class.getSimpleName()}, LocaleContextHolder.getLocale())); + PlanStatusEntity newPlanStatusEntity = this.entityManager.find(PlanStatusEntity.class, model.getStatusId(), true); + if (newPlanStatusEntity == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{data.getStatusId(), PlanStatusEntity.class.getSimpleName()}, LocaleContextHolder.getLocale())); + if (newPlanStatusEntity.getInternalStatus() != null && newPlanStatusEntity.getInternalStatus().equals(PlanStatus.Finalized) && oldPlanStatusEntity.getInternalStatus() != null && oldPlanStatusEntity.getInternalStatus().equals(PlanStatus.Finalized)) { this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.planAffiliation(model.getId())), Permission.FinalizePlan); - data.setStatus(model.getStatus()); + data.setStatusId(model.getStatusId()); data.setFinalizedAt(Instant.now()); } } else { @@ -1325,6 +1332,7 @@ public class PlanServiceImpl implements PlanService { data.setGroupId(UUID.randomUUID()); data.setVersion((short) 1); data.setStatus(PlanStatus.Draft); + data.setStatusId(this.planWorkflowService.getWorkFlowDefinition().getStartingStatusId()); data.setVersionStatus(PlanVersionStatus.NotFinalized); data.setCreatorId(this.userScope.getUserId()); data.setBlueprintId(model.getBlueprint()); @@ -1713,7 +1721,8 @@ public class PlanServiceImpl implements PlanService { persist.setId(data.getId()); persist.setHash(data.getId().toString()); persist.setLabel(data.getLabel()); - persist.setStatus(PlanStatus.Finalized); +//TODO status PlanStatusEntity statusEntity = this.queryFactory.query(DescriptionStatusQuery.class).disableTracking().internalStatuses(DescriptionStatus.Finalized).isActive(IsActive.Active).firstAs(new BaseFieldSet().ensure(org.opencdmp.model.descriptionstatus.DescriptionStatus._id)); +// persist.setStatus(PlanStatus.Finalized); persist.setDescription(data.getDescription()); persist.setBlueprint(data.getBlueprintId()); persist.setAccessType(data.getAccessType()); @@ -2307,7 +2316,7 @@ public class PlanServiceImpl implements PlanService { PlanPersist persist = new PlanPersist(); persist.setLabel(label); - persist.setStatus(PlanStatus.Draft); +//TODO status persist.setStatus(PlanStatus.Draft); persist.setDescription(planXml.getDescription()); persist.setAccessType(planXml.getAccess()); persist.setLanguage(planXml.getLanguage()); @@ -2609,7 +2618,7 @@ public class PlanServiceImpl implements PlanService { PlanPersist persist = new PlanPersist(); persist.setLabel(planCommonModelConfig.getLabel()); - persist.setStatus(PlanStatus.Draft); +// TODO status persist.setStatus(PlanStatus.Draft); persist.setDescription(model.getDescription()); switch (model.getAccessType()) { case Public -> persist.setAccessType(PlanAccessType.Public); diff --git a/backend/core/src/main/java/org/opencdmp/service/planworkflow/PlanWorkflowService.java b/backend/core/src/main/java/org/opencdmp/service/planworkflow/PlanWorkflowService.java index 649cbdb38..7f770ba7a 100644 --- a/backend/core/src/main/java/org/opencdmp/service/planworkflow/PlanWorkflowService.java +++ b/backend/core/src/main/java/org/opencdmp/service/planworkflow/PlanWorkflowService.java @@ -6,6 +6,7 @@ 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.commons.types.planworkflow.PlanWorkflowDefinitionEntity; import org.opencdmp.model.persist.planworkflow.PlanWorkflowPersist; import org.opencdmp.model.planworkflow.PlanWorkflow; @@ -16,4 +17,6 @@ public interface PlanWorkflowService { PlanWorkflow persist(PlanWorkflowPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JAXBException; void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException; + + PlanWorkflowDefinitionEntity getWorkFlowDefinition() throws InvalidApplicationException; } diff --git a/backend/core/src/main/java/org/opencdmp/service/planworkflow/PlanWorkflowServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/planworkflow/PlanWorkflowServiceImpl.java index 5c963be91..bee1bc568 100644 --- a/backend/core/src/main/java/org/opencdmp/service/planworkflow/PlanWorkflowServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/planworkflow/PlanWorkflowServiceImpl.java @@ -3,6 +3,7 @@ package org.opencdmp.service.planworkflow; 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.data.query.QueryFactory; import gr.cite.tools.exception.MyApplicationException; import gr.cite.tools.exception.MyForbiddenException; import gr.cite.tools.exception.MyNotFoundException; @@ -12,9 +13,11 @@ 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.AuthorizationFlags; import org.opencdmp.authorization.Permission; import org.opencdmp.commons.XmlHandlingService; import org.opencdmp.commons.enums.IsActive; +import org.opencdmp.commons.scope.tenant.TenantScope; import org.opencdmp.commons.types.planworkflow.PlanWorkflowDefinitionEntity; import org.opencdmp.commons.types.planworkflow.PlanWorkflowDefinitionTransitionEntity; import org.opencdmp.convention.ConventionService; @@ -27,6 +30,7 @@ import org.opencdmp.model.persist.planworkflow.PlanWorkflowDefinitionPersist; import org.opencdmp.model.persist.planworkflow.PlanWorkflowDefinitionTransitionPersist; import org.opencdmp.model.persist.planworkflow.PlanWorkflowPersist; import org.opencdmp.model.planworkflow.PlanWorkflow; +import org.opencdmp.query.PlanWorkflowQuery; import org.slf4j.LoggerFactory; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; @@ -51,7 +55,10 @@ public class PlanWorkflowServiceImpl implements PlanWorkflowService { private final DeleterFactory deleterFactory; private final MessageSource messageSource; private final ErrorThesaurusProperties errors; - public PlanWorkflowServiceImpl(AuthorizationService authorizationService, ConventionService conventionService, XmlHandlingService xmlHandlingService, TenantEntityManager entityManager, BuilderFactory builderFactory, DeleterFactory deleterFactory, MessageSource messageSource, ErrorThesaurusProperties errors) { + private final TenantScope tenantScope; + private final QueryFactory queryFactory; + + public PlanWorkflowServiceImpl(AuthorizationService authorizationService, ConventionService conventionService, XmlHandlingService xmlHandlingService, TenantEntityManager entityManager, BuilderFactory builderFactory, DeleterFactory deleterFactory, MessageSource messageSource, ErrorThesaurusProperties errors, TenantScope tenantScope, QueryFactory queryFactory) { this.authorizationService = authorizationService; this.conventionService = conventionService; this.xmlHandlingService = xmlHandlingService; @@ -60,6 +67,8 @@ public class PlanWorkflowServiceImpl implements PlanWorkflowService { this.deleterFactory = deleterFactory; this.messageSource = messageSource; this.errors = errors; + this.tenantScope = tenantScope; + this.queryFactory = queryFactory; } @Override @@ -136,4 +145,22 @@ public class PlanWorkflowServiceImpl implements PlanWorkflowService { return data; } + + @Override + public PlanWorkflowDefinitionEntity getWorkFlowDefinition() throws InvalidApplicationException { + PlanWorkflowQuery query = this.queryFactory.query(PlanWorkflowQuery.class).authorize(AuthorizationFlags.AllExceptPublic).isActives(IsActive.Active); + + if (this.tenantScope.isDefaultTenant()) + query = query.defaultTenant(true); + else + query = query.tenantIds(this.tenantScope.getTenant()); + + PlanWorkflowEntity entity = query.first(); + if (entity == null) throw new MyApplicationException("Plan workflow not found!"); + + PlanWorkflowDefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(PlanWorkflowDefinitionEntity.class, entity.getDefinition()); + if (definition == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{entity.getId(), PlanWorkflowDefinitionEntity.class.getSimpleName()}, LocaleContextHolder.getLocale())); + + return definition; + } }