fix public plan clone

This commit is contained in:
CITE\amentis 2024-08-01 12:10:36 +03:00
parent 5d460ea5e5
commit 70486ae3f2
1 changed files with 79 additions and 8 deletions

View File

@ -508,9 +508,9 @@ public class PlanServiceImpl implements PlanService {
newTemplate.setIsActive(IsActive.Active);
this.entityManager.persist(newTemplate);
this.entityManager.flush();
this.cloneDescription(newPlan.getId(), null, newVersionPlanDescriptionPersist.getDescriptionId(), newTemplate.getId());
this.cloneDescription(newPlan.getId(), null, newVersionPlanDescriptionPersist.getDescriptionId(), newTemplate.getId(), false);
} else{
this.cloneDescription(newPlan.getId(), null, newVersionPlanDescriptionPersist.getDescriptionId(), existingPlanDescriptionTemplateEntity.getId());
this.cloneDescription(newPlan.getId(), null, newVersionPlanDescriptionPersist.getDescriptionId(), existingPlanDescriptionTemplateEntity.getId(), false);
}
}
}
@ -599,16 +599,32 @@ public class PlanServiceImpl implements PlanService {
return this.builderFactory.builder(PlanBuilder.class).build(BaseFieldSet.build(fields, Plan._id), newPlan);
}
public void cloneDescription(UUID planId, Map<UUID, UUID> planDescriptionTemplateRemap, UUID descriptionId, UUID newPlanDescriptionTemplateId) throws InvalidApplicationException, IOException {
public void cloneDescription(UUID planId, Map<UUID, UUID> planDescriptionTemplateRemap, UUID descriptionId, UUID newPlanDescriptionTemplateId, Boolean isPublicClone) throws InvalidApplicationException, IOException {
logger.debug("cloning description: {} with description: {}", descriptionId, planId);
this.usageLimitService.checkIncrease(UsageLimitTargetMetric.DESCRIPTION_COUNT);
PlanEntity descriptionPlan = this.queryFactory.query(PlanQuery.class).disableTracking().ids(planId).isActive(IsActive.Active).first();
PlanEntity planEntity = this.queryFactory.query(PlanQuery.class).disableTracking().ids(planId).isActive(IsActive.Active).first();
if (descriptionPlan.getAccessType() != null && !descriptionPlan.getAccessType().equals(PlanAccessType.Public)) this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionAffiliation(descriptionId)), Permission.CloneDescription);
if (planEntity == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{planId, Plan.class.getSimpleName()}, LocaleContextHolder.getLocale()));
if (planEntity.getAccessType() != null && !planEntity.getAccessType().equals(PlanAccessType.Public)) this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionAffiliation(descriptionId)), Permission.CloneDescription);
else this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionAffiliation(descriptionId)), Permission.PublicCloneDescription);
DescriptionEntity existing = this.queryFactory.query(DescriptionQuery.class).disableTracking().ids(descriptionId).isActive(IsActive.Active).first();
if (existing == null && isPublicClone) {
// query for public description
try {
this.entityManager.disableTenantFilters();
existing = this.queryFactory.query(DescriptionQuery.class).disableTracking().authorize(EnumSet.of(Public)).ids(descriptionId).planSubQuery(this.queryFactory.query(PlanQuery.class).isActive(IsActive.Active).statuses(PlanStatus.Finalized).accessTypes(PlanAccessType.Public)).first();
this.entityManager.reloadTenantFilters();
} finally {
this.entityManager.reloadTenantFilters();
}
}
if (existing == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{descriptionId, Description.class.getSimpleName()}, LocaleContextHolder.getLocale()));
DescriptionEntity newDescription = new DescriptionEntity();
newDescription.setId(UUID.randomUUID());
newDescription.setLabel(existing.getLabel());
@ -636,6 +652,27 @@ public class PlanServiceImpl implements PlanService {
.isActive(IsActive.Active)
.collect();
if (isPublicClone) {
try {
this.entityManager.disableTenantFilters();
if (this.conventionService.isListNullOrEmpty(descriptionReferences)) {
descriptionReferences = this.queryFactory.query(DescriptionReferenceQuery.class).disableTracking()
.descriptionIds(existing.getId())
.isActive(IsActive.Active)
.collect();
}
if (this.conventionService.isListNullOrEmpty(descriptionTags)) {
descriptionTags = this.queryFactory.query(DescriptionTagQuery.class).disableTracking()
.descriptionIds(existing.getId())
.isActive(IsActive.Active)
.collect();
}
this.entityManager.reloadTenantFilters();
} finally {
this.entityManager.reloadTenantFilters();
}
}
for (DescriptionReferenceEntity descriptionReference : descriptionReferences) {
DescriptionReferenceEntity newReference = new DescriptionReferenceEntity();
newReference.setId(UUID.randomUUID());
@ -715,10 +752,21 @@ public class PlanServiceImpl implements PlanService {
this.usageLimitService.checkIncrease(UsageLimitTargetMetric.PLAN_COUNT);
boolean isPublic = false;
PlanEntity existingPlanEntity = this.queryFactory.query(PlanQuery.class).disableTracking().ids(model.getId()).firstAs(fields);
if (!this.conventionService.isValidGuid(model.getId()) || existingPlanEntity == null)
throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), Plan.class.getSimpleName()}, LocaleContextHolder.getLocale()));
if (existingPlanEntity == null) {
try {
this.entityManager.disableTenantFilters();
// query for public plan
existingPlanEntity = this.queryFactory.query(PlanQuery.class).disableTracking().authorize(EnumSet.of(Public)).ids(model.getId()).isActive(IsActive.Active).statuses(PlanStatus.Finalized).accessTypes(PlanAccessType.Public).firstAs(fields);
if (existingPlanEntity != null) isPublic = true;
this.entityManager.reloadTenantFilters();
} finally {
this.entityManager.reloadTenantFilters();
}
}
if (existingPlanEntity == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), Plan.class.getSimpleName()}, LocaleContextHolder.getLocale()));
if (existingPlanEntity.getAccessType() != null && !existingPlanEntity.getAccessType().equals(PlanAccessType.Public)) this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.planAffiliation( model.getId())), Permission.ClonePlan);
else this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.planAffiliation( model.getId())), Permission.PublicClonePlan);
@ -755,6 +803,29 @@ public class PlanServiceImpl implements PlanService {
.isActive(IsActive.Active)
.collect();
// public plan
if (isPublic) {
try {
this.entityManager.disableTenantFilters();
// query for public plan
if (this.conventionService.isListNullOrEmpty(planDescriptionTemplates)){
planDescriptionTemplates = this.queryFactory.query(PlanDescriptionTemplateQuery.class).disableTracking()
.planIds(model.getId())
.isActive(IsActive.Active)
.collect();
}
if (this.conventionService.isListNullOrEmpty(planReferences)) {
planReferences = this.queryFactory.query(PlanReferenceQuery.class).disableTracking()
.planIds(model.getId())
.isActives(IsActive.Active)
.collect();
}
this.entityManager.reloadTenantFilters();
} finally {
this.entityManager.reloadTenantFilters();
}
}
UUID currentUserId = this.userScope.getUserId();
boolean isCurrentUserInPlan = planUsers.stream().anyMatch(u -> u.getUserId().equals(currentUserId));
@ -823,7 +894,7 @@ public class PlanServiceImpl implements PlanService {
PlanEntity resultingPlanEntity = this.queryFactory.query(PlanQuery.class).disableTracking().ids(newPlan.getId()).firstAs(fields);
if (!this.conventionService.isListNullOrEmpty(model.getDescriptions())){
for (UUID description: model.getDescriptions()) {
this.cloneDescription(newPlan.getId(), planDescriptionTemplateRemap, description, null);
this.cloneDescription(newPlan.getId(), planDescriptionTemplateRemap, description, null, isPublic);
}
}
return this.builderFactory.builder(PlanBuilder.class).build(fields, resultingPlanEntity);