chore: add code split for refactoring in module context (#30097)

This commit is contained in:
subratadeypappu 2024-01-09 11:03:12 +06:00 committed by GitHub
parent b32b669daf
commit 68b774c27e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 91 additions and 50 deletions

View File

@ -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;
}

View File

@ -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<String> 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<NewAction> getActionsByContextId(String contextId, CreatorContextType contextType) {
return newActionService.findAllActionsByContextIdAndContextTypeAndViewMode(
contextId, contextType, actionPermission.getEditPermission(), false, true);
}
@Override
public Mono<Void> updateRefactoredEntity(RefactorEntityNameDTO refactorEntityNameDTO, String branchName) {
return newActionService

View File

@ -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<PageDTO> pageMono = newPageService
// fetch the unpublished page
.findPageById(pageId, pagePermission.getEditPermission(), false)
.cache();
Mono<Integer> 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<Void> 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<Integer> getContextBasedEvalVersionMono(
String contextId, RefactorEntityNameDTO refactorEntityNameDTO, RefactoringMetaDTO refactoringMetaDTO) {
Mono<PageDTO> pageMono = newPageService
// fetch the unpublished page
.findPageById(contextId, pagePermission.getEditPermission(), false)
.cache();
refactoringMetaDTO.setPageDTOMono(pageMono);
Mono<Integer> 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<String> pageIdMono = Mono.just(refactorEntityNameDTO.getPageId());
// Make sure to retrieve correct page id for branched page
if (StringUtils.hasLength(branchName)) {
pageIdMono = getBranchedPageIdMono(refactorEntityNameDTO, branchName);
}
Mono<String> contextIdMono = getBranchedContextIdMono(refactorEntityNameDTO, branchName);
final Map<String, String> 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<Map<String, String>> validateAndPrepareAnalyticsForRefactor(
RefactorEntityNameDTO refactorEntityNameDTO,
Mono<String> contextIdMono,
Map<String, String> 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<LayoutDTO> refactorWithoutContext(
RefactorEntityNameDTO refactorEntityNameDTO,
String branchName,
EntityRefactoringService<?> service,
Map<String, String> 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<String> getBranchedPageIdMono(RefactorEntityNameDTO refactorEntityNameDTO, String branchName) {
protected Mono<String> getBranchedContextIdMono(RefactorEntityNameDTO refactorEntityNameDTO, String branchName) {
if (!StringUtils.hasLength(branchName)) {
return Mono.just(refactorEntityNameDTO.getPageId());
}
return newPageService
.findByBranchNameAndDefaultPageId(
branchName, refactorEntityNameDTO.getPageId(), pagePermission.getEditPermission())

View File

@ -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));
}

View File

@ -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<Void> refactorReferencesInExistingEntities(
RefactorEntityNameDTO refactorEntityNameDTO, RefactoringMetaDTO refactoringMetaDTO) {
if (!isPageContext(refactorEntityNameDTO.getContextType())) {
return Mono.empty().then();
}
Mono<PageDTO> pageMono = refactoringMetaDTO.getPageDTOMono();
Mono<Integer> evalVersionMono = refactoringMetaDTO.getEvalVersionMono();
Set<String> updatedBindingPaths = refactoringMetaDTO.getUpdatedBindingPaths();