diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ce/ApplicationControllerCE.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ce/ApplicationControllerCE.java index 0545d5d5204..bb6e05b5a9f 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ce/ApplicationControllerCE.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ce/ApplicationControllerCE.java @@ -15,6 +15,7 @@ import com.appsmith.server.dtos.ApplicationPagesDTO; import com.appsmith.server.dtos.ArtifactImportDTO; import com.appsmith.server.dtos.BuildingBlockDTO; +import com.appsmith.server.dtos.BuildingBlockResponseDTO; import com.appsmith.server.dtos.GitAuthDTO; import com.appsmith.server.dtos.PartialExportFileDTO; import com.appsmith.server.dtos.ReleaseItemsDTO; @@ -409,7 +410,7 @@ public Mono> importApplicationPartially( @JsonView(Views.Public.class) @PostMapping("/import/partial/block") - public Mono> importBlock( + public Mono> importBlock( @RequestBody BuildingBlockDTO buildingBlockDTO, @RequestHeader(name = FieldName.BRANCH_NAME, required = false) String branchName) { return partialImportService diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/dtos/BuildingBlockResponseDTO.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/dtos/BuildingBlockResponseDTO.java new file mode 100644 index 00000000000..655b6f22b31 --- /dev/null +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/dtos/BuildingBlockResponseDTO.java @@ -0,0 +1,13 @@ +package com.appsmith.server.dtos; + +import com.appsmith.external.dtos.DslExecutableDTO; +import lombok.Data; + +import java.util.List; + +@Data +public class BuildingBlockResponseDTO { + String widgetDsl; + + List onPageLoadActions; +} diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceCE.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceCE.java index 484773a2116..88635db0770 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceCE.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceCE.java @@ -2,6 +2,7 @@ import com.appsmith.server.domains.Application; import com.appsmith.server.dtos.BuildingBlockDTO; +import com.appsmith.server.dtos.BuildingBlockResponseDTO; import org.springframework.http.codec.multipart.Part; import reactor.core.publisher.Mono; @@ -10,5 +11,5 @@ public interface PartialImportServiceCE { Mono importResourceInPage( String workspaceId, String applicationId, String pageId, String branchName, Part file); - Mono importBuildingBlock(BuildingBlockDTO buildingBlockDTO, String branchName); + Mono importBuildingBlock(BuildingBlockDTO buildingBlockDTO, String branchName); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceCEImpl.java index a73f324adb6..dc3c93a0cf9 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/imports/internal/partial/PartialImportServiceCEImpl.java @@ -18,6 +18,7 @@ import com.appsmith.server.dtos.ApplicationJson; import com.appsmith.server.dtos.BuildingBlockDTO; import com.appsmith.server.dtos.BuildingBlockImportDTO; +import com.appsmith.server.dtos.BuildingBlockResponseDTO; import com.appsmith.server.dtos.ImportingMetaDTO; import com.appsmith.server.dtos.MappedImportableResourcesDTO; import com.appsmith.server.exceptions.AppsmithError; @@ -51,6 +52,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -397,17 +399,69 @@ private Mono paneNameMapForActionAndActionCollectionInAppJson( } @Override - public Mono importBuildingBlock(BuildingBlockDTO buildingBlockDTO, String branchName) { + public Mono importBuildingBlock(BuildingBlockDTO buildingBlockDTO, String branchName) { Mono applicationJsonMono = applicationTemplateService.getApplicationJsonFromTemplate(buildingBlockDTO.getTemplateId()); - return applicationJsonMono - .flatMap(applicationJson -> this.importResourceInPage( - buildingBlockDTO.getWorkspaceId(), - buildingBlockDTO.getApplicationId(), - buildingBlockDTO.getPageId(), - branchName, - applicationJson)) - .map(BuildingBlockImportDTO::getWidgetDsl); + return applicationJsonMono.flatMap(applicationJson -> { + return this.importResourceInPage( + buildingBlockDTO.getWorkspaceId(), + buildingBlockDTO.getApplicationId(), + buildingBlockDTO.getPageId(), + branchName, + applicationJson) + .flatMap(buildingBlockImportDTO -> { + // Fetch layout and get new onPageLoadActions + // This data is not present in a client, since these are created + // after importing the block + Set newOnPageLoadActionNames = new HashSet<>(); + applicationJson + .getPageList() + .get(0) + .getPublishedPage() + .getLayouts() + .get(0) + .getLayoutOnLoadActions() + .forEach(dslExecutableDTOS -> { + dslExecutableDTOS.forEach(dslExecutableDTO -> { + if (dslExecutableDTO.getName() != null) { + newOnPageLoadActionNames.add(dslExecutableDTO.getName()); + } + }); + }); + + BuildingBlockResponseDTO buildingBlockResponseDTO = new BuildingBlockResponseDTO(); + buildingBlockResponseDTO.setWidgetDsl(buildingBlockImportDTO.getWidgetDsl()); + return newPageService + .findBranchedPageId( + branchName, buildingBlockDTO.getPageId(), AclPermission.MANAGE_PAGES) + .flatMap(branchedPageId -> newPageService + .findById(branchedPageId, Optional.empty()) + .map(newPage -> { + if (newPage.getUnpublishedPage() + .getLayouts() + .get(0) + .getLayoutOnLoadActions() + == null) { + return buildingBlockResponseDTO; + } + newPage.getUnpublishedPage() + .getLayouts() + .get(0) + .getLayoutOnLoadActions() + .forEach(dslExecutableDTOS -> { + // Filter the onPageLoadActions based on the json file + buildingBlockResponseDTO + .getOnPageLoadActions() + .addAll(dslExecutableDTOS.stream() + .filter(dslExecutableDTO -> + newOnPageLoadActionNames.contains( + dslExecutableDTO.getName())) + .toList()); + }); + return buildingBlockResponseDTO; + })); + }); + }); } } diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/solutions/PartialImportServiceTest.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/solutions/PartialImportServiceTest.java index 7f1face0438..40ed0ed2198 100644 --- a/app/server/appsmith-server/src/test/java/com/appsmith/server/solutions/PartialImportServiceTest.java +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/solutions/PartialImportServiceTest.java @@ -18,6 +18,7 @@ import com.appsmith.server.domains.Workspace; import com.appsmith.server.dtos.ApplicationJson; import com.appsmith.server.dtos.BuildingBlockDTO; +import com.appsmith.server.dtos.BuildingBlockResponseDTO; import com.appsmith.server.dtos.PageDTO; import com.appsmith.server.helpers.MockPluginExecutor; import com.appsmith.server.helpers.PluginExecutorHelper; @@ -495,16 +496,16 @@ public void testPartialImportWithBuildingBlock_nameClash_success() { buildingBlockDTO1.setWorkspaceId(workspaceId); buildingBlockDTO1.setTemplateId("templatedId1"); - Mono result = partialImportService + Mono result = partialImportService .importBuildingBlock(buildingBlockDTO, null) .flatMap(s -> partialImportService.importBuildingBlock(buildingBlockDTO1, null)); StepVerifier.create(result) - .assertNext(dsl -> { - assertThat(dsl).isNotNull(); + .assertNext(BuildingBlockResponseDTO1 -> { + assertThat(BuildingBlockResponseDTO1.getWidgetDsl()).isNotNull(); // Compare the json string of widget DSL, // the binding names will be updated, and hence the json will be different - assertThat(dsl) + assertThat(BuildingBlockResponseDTO1.getWidgetDsl()) .isNotEqualTo(applicationJson .getPageList() .get(0)