From 68b774c27e87e61a289f4729c20a0bc0e15db54d Mon Sep 17 00:00:00 2001 From: subratadeypappu Date: Tue, 9 Jan 2024 11:03:12 +0600 Subject: [PATCH] chore: add code split for refactoring in module context (#30097) --- .../dtos/ce/RefactorEntityNameCE_DTO.java | 3 + .../NewActionRefactoringServiceCEImpl.java | 21 +++- .../RefactoringServiceCEImpl.java | 96 ++++++++++++------- .../ce/CustomNewActionRepositoryCEImpl.java | 17 ++-- .../WidgetRefactoringServiceCEImpl.java | 4 + 5 files changed, 91 insertions(+), 50 deletions(-) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/dtos/ce/RefactorEntityNameCE_DTO.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/dtos/ce/RefactorEntityNameCE_DTO.java index c34c9ab716..4fada3adb0 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/dtos/ce/RefactorEntityNameCE_DTO.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/dtos/ce/RefactorEntityNameCE_DTO.java @@ -1,5 +1,6 @@ package com.appsmith.server.dtos.ce; +import com.appsmith.external.models.CreatorContextType; import com.appsmith.external.views.Views; import com.appsmith.server.dtos.ActionCollectionDTO; import com.appsmith.server.dtos.EntityType; @@ -35,4 +36,6 @@ public class RefactorEntityNameCE_DTO { @JsonView(Views.Internal.class) Boolean isInternal; + + CreatorContextType contextType; } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/newactions/refactors/NewActionRefactoringServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/newactions/refactors/NewActionRefactoringServiceCEImpl.java index 685225738b..5c28141548 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/newactions/refactors/NewActionRefactoringServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/newactions/refactors/NewActionRefactoringServiceCEImpl.java @@ -36,6 +36,7 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import static com.appsmith.external.constants.AnalyticsEvents.REFACTOR_ACTION; +import static com.appsmith.server.helpers.ContextTypeUtils.getDefaultContextIfNull; @Slf4j @RequiredArgsConstructor @@ -65,11 +66,11 @@ public class NewActionRefactoringServiceCEImpl implements EntityRefactoringServi Set updatedBindingPaths = refactoringMetaDTO.getUpdatedBindingPaths(); Pattern oldNamePattern = refactoringMetaDTO.getOldNamePattern(); - String pageId = refactorEntityNameDTO.getPageId(); + String contextId = extractContextId(refactorEntityNameDTO); + CreatorContextType contextType = getDefaultContextIfNull(refactorEntityNameDTO.getContextType()); String oldName = refactorEntityNameDTO.getOldFullyQualifiedName(); String newName = refactorEntityNameDTO.getNewFullyQualifiedName(); - return newActionService - .findByPageIdAndViewMode(pageId, false, actionPermission.getEditPermission()) + return getActionsByContextId(contextId, contextType) .flatMap(newAction -> Mono.just(newAction).zipWith(evalVersionMono)) /* * Assuming that the datasource should not be dependent on the widget and hence not going through the same @@ -110,10 +111,22 @@ public class NewActionRefactoringServiceCEImpl implements EntityRefactoringServi .map(savedAction -> savedAction.getUnpublishedAction().getName()) .collectList() .doOnNext(updatedActionNames -> log.debug( - "Actions updated due to refactor name in page {} are : {}", pageId, updatedActionNames)) + "Actions updated due to refactor name in {} {} are : {}", + contextType.toString().toLowerCase(), + contextId, + updatedActionNames)) .then(); } + protected String extractContextId(RefactorEntityNameDTO refactorEntityNameDTO) { + return refactorEntityNameDTO.getPageId(); + } + + protected Flux getActionsByContextId(String contextId, CreatorContextType contextType) { + return newActionService.findAllActionsByContextIdAndContextTypeAndViewMode( + contextId, contextType, actionPermission.getEditPermission(), false, true); + } + @Override public Mono updateRefactoredEntity(RefactorEntityNameDTO refactorEntityNameDTO, String branchName) { return newActionService diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/refactors/applications/RefactoringServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/refactors/applications/RefactoringServiceCEImpl.java index fe7a7c1761..a568fb7b1d 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/refactors/applications/RefactoringServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/refactors/applications/RefactoringServiceCEImpl.java @@ -29,6 +29,7 @@ import org.springframework.util.StringUtils; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.util.function.Tuple2; +import reactor.util.function.Tuples; import java.util.ArrayList; import java.util.HashMap; @@ -86,24 +87,8 @@ public class RefactoringServiceCEImpl implements RefactoringServiceCE { refactoringMetaDTO.setOldNamePattern(oldNamePattern); - Mono pageMono = newPageService - // fetch the unpublished page - .findPageById(pageId, pagePermission.getEditPermission(), false) - .cache(); - - Mono evalVersionMono = pageMono.flatMap(page -> { - return applicationService.findById(page.getApplicationId()).map(application -> { - Integer evaluationVersion = application.getEvaluationVersion(); - if (evaluationVersion == null) { - evaluationVersion = EVALUATION_VERSION; - } - return evaluationVersion; - }); - }) - .cache(); - - refactoringMetaDTO.setPageDTOMono(pageMono); - refactoringMetaDTO.setEvalVersionMono(evalVersionMono); + refactoringMetaDTO.setEvalVersionMono( + getContextBasedEvalVersionMono(pageId, refactorEntityNameDTO, refactoringMetaDTO)); Mono refactoredReferencesMono = refactorAllReferences(refactorEntityNameDTO, refactoringMetaDTO); @@ -121,10 +106,32 @@ public class RefactoringServiceCEImpl implements RefactoringServiceCE { } } } - return Mono.empty(); + // Return empty Layout when there is no layout + return Mono.just(Tuples.of(new LayoutDTO(), Set.of())); })); } + protected Mono getContextBasedEvalVersionMono( + String contextId, RefactorEntityNameDTO refactorEntityNameDTO, RefactoringMetaDTO refactoringMetaDTO) { + Mono pageMono = newPageService + // fetch the unpublished page + .findPageById(contextId, pagePermission.getEditPermission(), false) + .cache(); + + refactoringMetaDTO.setPageDTOMono(pageMono); + Mono evalVersionMono = pageMono.flatMap(page -> { + return applicationService.findById(page.getApplicationId()).map(application -> { + Integer evaluationVersion = application.getEvaluationVersion(); + if (evaluationVersion == null) { + evaluationVersion = EVALUATION_VERSION; + } + return evaluationVersion; + }); + }) + .cache(); + return evalVersionMono; + } + protected static Pattern getReplacementPattern(String oldName) { String regexPattern = preWord + oldName + postWord; return Pattern.compile(regexPattern); @@ -168,17 +175,25 @@ public class RefactoringServiceCEImpl implements RefactoringServiceCE { }); } - Mono pageIdMono = Mono.just(refactorEntityNameDTO.getPageId()); - // Make sure to retrieve correct page id for branched page - if (StringUtils.hasLength(branchName)) { - pageIdMono = getBranchedPageIdMono(refactorEntityNameDTO, branchName); - } + Mono contextIdMono = getBranchedContextIdMono(refactorEntityNameDTO, branchName); final Map analyticsProperties = new HashMap<>(); return isValidNameMono - .then(pageIdMono) + .then(validateAndPrepareAnalyticsForRefactor(refactorEntityNameDTO, contextIdMono, analyticsProperties)) + .flatMap(updatedAnalyticsProperties -> { + return refactorWithoutContext( + refactorEntityNameDTO, branchName, service, updatedAnalyticsProperties) + .map(responseUtils::updateLayoutDTOWithDefaultResources); + }); + } + + protected Mono> validateAndPrepareAnalyticsForRefactor( + RefactorEntityNameDTO refactorEntityNameDTO, + Mono contextIdMono, + Map analyticsProperties) { + return contextIdMono .flatMap(branchedPageId -> { refactorEntityNameDTO.setPageId(branchedPageId); return this.isNameAllowed( @@ -198,18 +213,22 @@ public class RefactoringServiceCEImpl implements RefactoringServiceCE { refactorEntityNameDTO.getOldFullyQualifiedName(), refactorEntityNameDTO.getNewFullyQualifiedName())); } + return Mono.just(analyticsProperties); + }); + } - return service.updateRefactoredEntity(refactorEntityNameDTO, branchName) - .as(transactionalOperator::transactional) - .then(this.refactorName(refactorEntityNameDTO)) - .flatMap(tuple2 -> { - AnalyticsEvents event = - service.getRefactorAnalyticsEvent(refactorEntityNameDTO.getEntityType()); - return this.sendRefactorAnalytics(event, analyticsProperties, tuple2.getT2()) - .thenReturn(tuple2.getT1()); - }); - }) - .map(responseUtils::updateLayoutDTOWithDefaultResources); + protected Mono refactorWithoutContext( + RefactorEntityNameDTO refactorEntityNameDTO, + String branchName, + EntityRefactoringService service, + Map analyticsProperties) { + return service.updateRefactoredEntity(refactorEntityNameDTO, branchName) + .then(this.refactorName(refactorEntityNameDTO)) + .flatMap(tuple2 -> { + AnalyticsEvents event = service.getRefactorAnalyticsEvent(refactorEntityNameDTO.getEntityType()); + return this.sendRefactorAnalytics(event, analyticsProperties, tuple2.getT2()) + .thenReturn(tuple2.getT1()); + }); } protected EntityRefactoringService getEntityRefactoringService(RefactorEntityNameDTO refactorEntityNameDTO) { @@ -222,7 +241,10 @@ public class RefactoringServiceCEImpl implements RefactoringServiceCE { }; } - private Mono getBranchedPageIdMono(RefactorEntityNameDTO refactorEntityNameDTO, String branchName) { + protected Mono getBranchedContextIdMono(RefactorEntityNameDTO refactorEntityNameDTO, String branchName) { + if (!StringUtils.hasLength(branchName)) { + return Mono.just(refactorEntityNameDTO.getPageId()); + } return newPageService .findByBranchNameAndDefaultPageId( branchName, refactorEntityNameDTO.getPageId(), pagePermission.getEditPermission()) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomNewActionRepositoryCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomNewActionRepositoryCEImpl.java index c7d533ec98..8dd8c75d85 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomNewActionRepositoryCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomNewActionRepositoryCEImpl.java @@ -648,22 +648,21 @@ public class CustomNewActionRepositoryCEImpl extends BaseAppsmithRepositoryImpl< String contextIdPath = completeFieldName(QNewAction.newAction.unpublishedAction.pageId); String contextTypePath = completeFieldName(QNewAction.newAction.unpublishedAction.contextType); + Criteria contextTypeCriterion = new Criteria() + .orOperator( + where(contextTypePath).is(contextType), + where(contextTypePath).isNull()); Criteria contextIdAndContextTypeCriteria = - where(contextIdPath).is(contextId).and(contextTypePath).is(contextType); + where(contextIdPath).is(contextId).andOperator(contextTypeCriterion); criteriaList.add(contextIdAndContextTypeCriteria); - Criteria jsInclusionOrExclusionCriteria; - if (includeJs) { - jsInclusionOrExclusionCriteria = - where(fieldName(QNewAction.newAction.pluginType)).is(PluginType.JS); - } else { - jsInclusionOrExclusionCriteria = + if (!includeJs) { + Criteria jsInclusionOrExclusionCriteria = where(fieldName(QNewAction.newAction.pluginType)).ne(PluginType.JS); + criteriaList.add(jsInclusionOrExclusionCriteria); } - criteriaList.add(jsInclusionOrExclusionCriteria); - return queryAll(criteriaList, Optional.of(permission)); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/widgets/refactors/WidgetRefactoringServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/widgets/refactors/WidgetRefactoringServiceCEImpl.java index 2ce5136fda..aa2e28d85a 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/widgets/refactors/WidgetRefactoringServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/widgets/refactors/WidgetRefactoringServiceCEImpl.java @@ -38,6 +38,7 @@ import java.util.stream.Collectors; import java.util.stream.StreamSupport; import static com.appsmith.external.constants.AnalyticsEvents.REFACTOR_WIDGET; +import static com.appsmith.server.helpers.ContextTypeUtils.isPageContext; @Slf4j @RequiredArgsConstructor @@ -56,6 +57,9 @@ public class WidgetRefactoringServiceCEImpl implements EntityRefactoringServiceC @Override public Mono refactorReferencesInExistingEntities( RefactorEntityNameDTO refactorEntityNameDTO, RefactoringMetaDTO refactoringMetaDTO) { + if (!isPageContext(refactorEntityNameDTO.getContextType())) { + return Mono.empty().then(); + } Mono pageMono = refactoringMetaDTO.getPageDTOMono(); Mono evalVersionMono = refactoringMetaDTO.getEvalVersionMono(); Set updatedBindingPaths = refactoringMetaDTO.getUpdatedBindingPaths();