From d178b1c729b5f44b3372b3b4e8004d6045ca6718 Mon Sep 17 00:00:00 2001 From: Trisha Anand Date: Wed, 23 Oct 2019 04:41:51 +0000 Subject: [PATCH] Publishing an application has been implemented by introducting publishedDsl field inside Layout. On Publish, for all pages in the application, for all layouts inside each page, the dsl json object is copied into the publishedDsl json object. --- .../controllers/ApplicationController.java | 12 +++ .../server/controllers/LayoutController.java | 12 ++- .../server/domains/ApplicationPage.java | 2 - .../appsmith/server/domains/BaseDomain.java | 2 + .../com/appsmith/server/domains/Layout.java | 15 +++ .../server/services/ApplicationService.java | 6 ++ .../services/ApplicationServiceImpl.java | 91 ++++++++++++++++++- .../server/services/LayoutService.java | 2 +- .../server/services/LayoutServiceImpl.java | 6 +- .../server/services/PageServiceImpl.java | 7 +- 10 files changed, 143 insertions(+), 12 deletions(-) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ApplicationController.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ApplicationController.java index d3e7333bf8..6d79eca5ed 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ApplicationController.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ApplicationController.java @@ -2,10 +2,15 @@ package com.appsmith.server.controllers; import com.appsmith.server.constants.Url; import com.appsmith.server.domains.Application; +import com.appsmith.server.dtos.ResponseDTO; import com.appsmith.server.services.ApplicationService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Mono; @RestController @RequestMapping(Url.APPLICATION_URL) @@ -14,4 +19,11 @@ public class ApplicationController extends BaseController> publish(@PathVariable String applicationId) { + return service.publish(applicationId) + .map(published -> new ResponseDTO<>(HttpStatus.OK.value(), published, null)); + } + } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/LayoutController.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/LayoutController.java index b1a244ec16..c8d9af0a5c 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/LayoutController.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/LayoutController.java @@ -36,14 +36,20 @@ public class LayoutController { @GetMapping("/{layoutId}/pages/{pageId}") public Mono> getLayout(@PathVariable String pageId, @PathVariable String layoutId) { - return service.getLayout(pageId, layoutId) - .map(created -> new ResponseDTO<>(HttpStatus.CREATED.value(), created, null)); + return service.getLayout(pageId, layoutId, false) + .map(created -> new ResponseDTO<>(HttpStatus.OK.value(), created, null)); } @PutMapping("/{layoutId}/pages/{pageId}") public Mono> updateLayout(@PathVariable String pageId, @PathVariable String layoutId, @RequestBody Layout layout) { return service.updateLayout(pageId, layoutId, layout) - .map(created -> new ResponseDTO<>(HttpStatus.CREATED.value(), created, null)); + .map(created -> new ResponseDTO<>(HttpStatus.OK.value(), created, null)); + } + + @GetMapping("/{layoutId}/pages/{pageId}/view") + public Mono> getLayoutView(@PathVariable String pageId, @PathVariable String layoutId) { + return service.getLayout(pageId, layoutId, true) + .map(created -> new ResponseDTO<>(HttpStatus.OK.value(), created, null)); } } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/ApplicationPage.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/ApplicationPage.java index 1351e0a693..ae0bc9f8b9 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/ApplicationPage.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/ApplicationPage.java @@ -13,7 +13,5 @@ public class ApplicationPage { String id; - String name; - Boolean isDefault; } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/BaseDomain.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/BaseDomain.java index 11d38bea08..3f0a16b225 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/BaseDomain.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/BaseDomain.java @@ -1,5 +1,6 @@ package com.appsmith.server.domains; +import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Getter; import lombok.Setter; import lombok.ToString; @@ -36,6 +37,7 @@ public abstract class BaseDomain implements Persistable { protected Boolean deleted = false; + @JsonIgnore @Override public boolean isNew() { return this.getId() == null; diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/Layout.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/Layout.java index b3af4d0654..745b5ac68b 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/Layout.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/Layout.java @@ -1,5 +1,6 @@ package com.appsmith.server.domains; +import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -14,5 +15,19 @@ public class Layout extends BaseDomain { ScreenType screen; + @JsonIgnore + Boolean viewMode = false; + JSONObject dsl; + + @JsonIgnore + JSONObject publishedDsl; + + /** + * If view mode, the dsl returned should be the publishedDSL, else if the edit mode is on (view mode = false) + * the dsl returned should be JSONObject dsl + */ + public JSONObject getDsl() { + return viewMode ? publishedDsl : dsl; + } } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationService.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationService.java index 712b91dcf1..ecc9344aaa 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationService.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationService.java @@ -1,6 +1,7 @@ package com.appsmith.server.services; import com.appsmith.server.domains.Application; +import com.appsmith.server.domains.Page; import reactor.core.publisher.Mono; public interface ApplicationService extends CrudService { @@ -9,4 +10,9 @@ public interface ApplicationService extends CrudService { Mono findByIdAndOrganizationId(String id, String organizationId); Mono findByName(String name); + + Mono publish(String applicationId); + + Mono addPageToApplication(String applicationId, Page page); + } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationServiceImpl.java index 7ba833195c..1521c8daee 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationServiceImpl.java @@ -1,12 +1,15 @@ package com.appsmith.server.services; -import com.appsmith.server.constants.AnalyticsEvents; import com.appsmith.server.constants.FieldName; import com.appsmith.server.domains.Application; +import com.appsmith.server.domains.ApplicationPage; +import com.appsmith.server.domains.Layout; +import com.appsmith.server.domains.Page; import com.appsmith.server.domains.User; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; import com.appsmith.server.repositories.ApplicationRepository; +import com.appsmith.server.repositories.PageRepository; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; @@ -17,6 +20,8 @@ import reactor.core.publisher.Mono; import reactor.core.scheduler.Scheduler; import javax.validation.Validator; +import java.util.ArrayList; +import java.util.List; @Slf4j @@ -25,6 +30,10 @@ public class ApplicationServiceImpl extends BaseService findByName(String name) { return repository.findByName(name); } -} + + /** + * This function is called during page create in Page Service. It adds the newly created + * page to its ApplicationPages list. + * @param applicationId + * @param page + * @return Updated application + */ + @Override + public Mono addPageToApplication(String applicationId, Page page) { + Mono applicationMono = findById(applicationId); + + return applicationMono + .map(application -> { + List applicationPages = application.getPages(); + if (applicationPages == null) { + applicationPages = new ArrayList<>(); + } + ApplicationPage applicationPage = new ApplicationPage(); + applicationPage.setId(page.getId()); + applicationPages.add(applicationPage); + application.setPages(applicationPages); + return application; + }) + .flatMap(repository::save); + } + + /** + * This function walks through all the pages in the application. In each page, it walks through all the layouts. + * In a layout, dsl and publishedDsl JSONObjects exist. Publish function is responsible for copying the dsl into + * the publishedDsl. + * @param applicationId + * @return Application + */ + + @Override + public Mono publish(String applicationId) { + Mono applicationMono = findById(applicationId) + .switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "application", applicationId))); + + return applicationMono + //Return all the pages in the Application + .map(application -> application.getPages()) + .flatMapMany(Flux::fromIterable) + //In each page, copy each layout's dsl to publishedDsl field + .flatMap(applicationPage -> { + return pageRepository + .findById(applicationPage.getId()) + .switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "page", applicationPage.getId()))) + .map(page -> { + List layoutList = page.getLayouts(); + for (Layout layout : layoutList) { + layout.setPublishedDsl(layout.getDsl()); + } + return page; + }) + .flatMap(pageRepository::save); + }) + .collectList() + //The only reason the following has been added to ensure that the DAG completes. If the following is missing, + //the previous flatMap responsible for editing the layouts doesn't execute. + .flatMap(pages -> { + List pageIds = new ArrayList<>(); + for (Page page : pages) { + ApplicationPage applicationPage = new ApplicationPage(); + applicationPage.setId(page.getId()); + pageIds.add(applicationPage); + } + return applicationMono.map(application -> { + application.setPages(pageIds); + return application; + }); + }); + } +} \ No newline at end of file diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/LayoutService.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/LayoutService.java index 5153c358b2..39368407cd 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/LayoutService.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/LayoutService.java @@ -6,7 +6,7 @@ import reactor.core.publisher.Mono; public interface LayoutService { Mono createLayout(String pageId, Layout layout); - Mono getLayout(String pageId, String layoutId); + Mono getLayout(String pageId, String layoutId, Boolean viewMode); Mono updateLayout(String pageId, String layoutId, Layout layout); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/LayoutServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/LayoutServiceImpl.java index c3af0c7417..995cfeb115 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/LayoutServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/LayoutServiceImpl.java @@ -54,7 +54,7 @@ public class LayoutServiceImpl implements LayoutService { } @Override - public Mono getLayout(String pageId, String layoutId) { + public Mono getLayout(String pageId, String layoutId, Boolean viewMode) { return pageService.findByIdAndLayoutsId(pageId, layoutId) .switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.PAGEID + " or " + FieldName.LAYOUTID))) .flatMap(pageService::doesPageIdBelongToCurrentUserOrganization) @@ -62,7 +62,9 @@ public class LayoutServiceImpl implements LayoutService { .map(page -> { List layoutList = page.getLayouts(); //Because the findByIdAndLayoutsId call returned non-empty result, we are guaranteed to find the layoutId here. - return layoutList.stream().filter(layout -> layout.getId().equals(layoutId)).findFirst().get(); + Layout matchedLayout = layoutList.stream().filter(layout -> layout.getId().equals(layoutId)).findFirst().get(); + matchedLayout.setViewMode(viewMode); + return matchedLayout; }); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageServiceImpl.java index 85304a8d43..1523ea0764 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageServiceImpl.java @@ -59,7 +59,12 @@ public class PageServiceImpl extends BaseService i layoutList.add(createDefaultLayout()); page.setLayouts(layoutList); } - return super.create(page); + + return super.create(page) + //After the page has been saved, update the application (save the page id inside the application) + .flatMap(savedPage -> + applicationService.addPageToApplication(savedPage.getApplicationId(), savedPage) + .thenReturn(savedPage)); }