From bc3e2d93be205b46518325f59ca3bdde06118824 Mon Sep 17 00:00:00 2001 From: Boubaker Khanfir Date: Fri, 17 May 2024 17:50:53 +0100 Subject: [PATCH] feat: Allow to delete a page template - MEED-6846 - Meeds-io/MIPs#133 --- .../layout/entity/PageTemplateEntity.java | 3 + .../io/meeds/layout/model/PageTemplate.java | 9 ++- .../layout/model/PageTemplateDescriptor.java | 2 + .../service/PageTemplateImportService.java | 1 + .../layout/service/PageTemplateService.java | 7 ++ .../layout/storage/PageTemplateStorage.java | 6 ++ .../layout-rdbms.db.changelog-1.0.0.xml | 11 +++ .../service/PageTemplateServiceTest.java | 13 +++- .../locale/portlet/LayoutEditor_en.properties | 10 +++ .../js/PageTemplateService.js | 11 +++ .../components/list/PageTemplateItem.vue | 76 ++++++++++++++++++- .../components/list/PageTemplates.vue | 75 ++++++++++++++---- 12 files changed, 202 insertions(+), 22 deletions(-) diff --git a/layout-service/src/main/java/io/meeds/layout/entity/PageTemplateEntity.java b/layout-service/src/main/java/io/meeds/layout/entity/PageTemplateEntity.java index 450eb7bb0..e5c9caefb 100644 --- a/layout-service/src/main/java/io/meeds/layout/entity/PageTemplateEntity.java +++ b/layout-service/src/main/java/io/meeds/layout/entity/PageTemplateEntity.java @@ -45,6 +45,9 @@ public class PageTemplateEntity { @Column(name = "DISABLED") private boolean disabled; + @Column(name = "SYSTEM_PAGE_TEMPLATE") + private boolean system; + @Column(name = "CATEGORY") private String category; diff --git a/layout-service/src/main/java/io/meeds/layout/model/PageTemplate.java b/layout-service/src/main/java/io/meeds/layout/model/PageTemplate.java index dc2fe3c13..5e998f93a 100644 --- a/layout-service/src/main/java/io/meeds/layout/model/PageTemplate.java +++ b/layout-service/src/main/java/io/meeds/layout/model/PageTemplate.java @@ -31,6 +31,8 @@ public class PageTemplate { private boolean disabled; + private boolean system; + private String category; private String content; @@ -41,10 +43,15 @@ public class PageTemplate { private long illustrationId; - public PageTemplate(long id, boolean disabled, String category, String content) { + public PageTemplate(long id, + boolean disabled, + boolean system, + String category, + String content) { this.id = id; this.content = content; this.disabled = disabled; + this.system = system; this.category = category; } diff --git a/layout-service/src/main/java/io/meeds/layout/model/PageTemplateDescriptor.java b/layout-service/src/main/java/io/meeds/layout/model/PageTemplateDescriptor.java index 5ce03c077..eaef9ca9c 100644 --- a/layout-service/src/main/java/io/meeds/layout/model/PageTemplateDescriptor.java +++ b/layout-service/src/main/java/io/meeds/layout/model/PageTemplateDescriptor.java @@ -39,6 +39,8 @@ public class PageTemplateDescriptor { private String category; + private boolean system; + private String illustrationPath; private String layoutPath; diff --git a/layout-service/src/main/java/io/meeds/layout/service/PageTemplateImportService.java b/layout-service/src/main/java/io/meeds/layout/service/PageTemplateImportService.java index 14569be5f..55eaeaf8c 100644 --- a/layout-service/src/main/java/io/meeds/layout/service/PageTemplateImportService.java +++ b/layout-service/src/main/java/io/meeds/layout/service/PageTemplateImportService.java @@ -238,6 +238,7 @@ protected PageTemplate createPageTemplate(PageTemplateDescriptor d, long oldTemp isNew = true; } pageTemplate.setCategory(d.getCategory()); + pageTemplate.setSystem(d.isSystem()); try (InputStream is = configurationManager.getInputStream(d.getLayoutPath())) { String xml = IOUtil.getStreamContentAsString(is); Container layout = fromXML(xml); diff --git a/layout-service/src/main/java/io/meeds/layout/service/PageTemplateService.java b/layout-service/src/main/java/io/meeds/layout/service/PageTemplateService.java index 7cf962c58..782aa19dd 100644 --- a/layout-service/src/main/java/io/meeds/layout/service/PageTemplateService.java +++ b/layout-service/src/main/java/io/meeds/layout/service/PageTemplateService.java @@ -102,6 +102,13 @@ public void deletePageTemplate(long templateId, String username) throws IllegalA if (!layoutAclService.isAdministrator(username)) { throw new IllegalAccessException("User isn't authorized to create a page template"); } + PageTemplate pageTemplate = getPageTemplate(templateId); + if (pageTemplate == null) { + throw new ObjectNotFoundException("Page template doesn't exist"); + } + if (pageTemplate.isSystem()) { + throw new IllegalAccessException("Can't delete a system page template"); + } deletePageTemplate(templateId); } diff --git a/layout-service/src/main/java/io/meeds/layout/storage/PageTemplateStorage.java b/layout-service/src/main/java/io/meeds/layout/storage/PageTemplateStorage.java index 4f80a7906..58d8a8755 100644 --- a/layout-service/src/main/java/io/meeds/layout/storage/PageTemplateStorage.java +++ b/layout-service/src/main/java/io/meeds/layout/storage/PageTemplateStorage.java @@ -40,6 +40,7 @@ public List getPageTemplates() { return entities.stream() .map(e -> new PageTemplate(e.getId(), e.isDisabled(), + e.isSystem(), e.getCategory(), e.getContent())) .toList(); @@ -49,6 +50,7 @@ public PageTemplate getPageTemplate(long id) { return pageTemplateDAO.findById(id) .map(e -> new PageTemplate(e.getId(), e.isDisabled(), + e.isSystem(), e.getCategory(), e.getContent())) .orElse(null); @@ -57,11 +59,13 @@ public PageTemplate getPageTemplate(long id) { public PageTemplate createPageTemplate(PageTemplate pageTemplate) { PageTemplateEntity entity = new PageTemplateEntity(null, pageTemplate.isDisabled(), + pageTemplate.isSystem(), pageTemplate.getCategory(), pageTemplate.getContent()); entity = pageTemplateDAO.save(entity); return new PageTemplate(entity.getId(), entity.isDisabled(), + entity.isSystem(), entity.getCategory(), entity.getContent()); } @@ -72,11 +76,13 @@ public PageTemplate updatePageTemplate(PageTemplate pageTemplate) throws ObjectN } PageTemplateEntity entity = new PageTemplateEntity(pageTemplate.getId(), pageTemplate.isDisabled(), + pageTemplate.isSystem(), pageTemplate.getCategory(), pageTemplate.getContent()); entity = pageTemplateDAO.save(entity); return new PageTemplate(entity.getId(), entity.isDisabled(), + entity.isSystem(), entity.getCategory(), entity.getContent()); } diff --git a/layout-service/src/main/resources/changelog/layout-rdbms.db.changelog-1.0.0.xml b/layout-service/src/main/resources/changelog/layout-rdbms.db.changelog-1.0.0.xml index 927ef9a56..771a4c1e7 100644 --- a/layout-service/src/main/resources/changelog/layout-rdbms.db.changelog-1.0.0.xml +++ b/layout-service/src/main/resources/changelog/layout-rdbms.db.changelog-1.0.0.xml @@ -79,4 +79,15 @@ + + + + + + \ No newline at end of file diff --git a/layout-service/src/test/java/io/meeds/layout/service/PageTemplateServiceTest.java b/layout-service/src/test/java/io/meeds/layout/service/PageTemplateServiceTest.java index dc4604c00..2486b4362 100644 --- a/layout-service/src/test/java/io/meeds/layout/service/PageTemplateServiceTest.java +++ b/layout-service/src/test/java/io/meeds/layout/service/PageTemplateServiceTest.java @@ -111,7 +111,7 @@ public void getPageTemplates() { @Test public void getPageTemplatesWithExpand() throws ObjectNotFoundException { - PageTemplate template = new PageTemplate(2l, false, LAYOUT_CATEGORY, LAYOUT_CONTENT); + PageTemplate template = new PageTemplate(2l, false, false, LAYOUT_CATEGORY, LAYOUT_CONTENT); when(localeConfigService.getDefaultLocaleConfig()).thenReturn(defaultLocaleConfig); when(defaultLocaleConfig.getLocale()).thenReturn(Locale.ENGLISH); @@ -178,7 +178,7 @@ public void getPageTemplatesWithExpand() throws ObjectNotFoundException { @Test public void getPageTemplateWithExpand() throws ObjectNotFoundException { - PageTemplate template = new PageTemplate(2l, false, LAYOUT_CATEGORY, LAYOUT_CONTENT); + PageTemplate template = new PageTemplate(2l, false, false, LAYOUT_CATEGORY, LAYOUT_CONTENT); when(localeConfigService.getDefaultLocaleConfig()).thenReturn(defaultLocaleConfig); when(defaultLocaleConfig.getLocale()).thenReturn(Locale.ENGLISH); @@ -261,7 +261,15 @@ public void deletePageTemplate() throws ObjectNotFoundException, IllegalAccessEx assertThrows(IllegalAccessException.class, () -> pageTemplateService.deletePageTemplate(2l, testuser)); when(layoutAclService.isAdministrator(testuser)).thenReturn(true); + assertThrows(ObjectNotFoundException.class, () -> pageTemplateService.deletePageTemplate(2l, testuser)); + + when(pageTemplateStorage.getPageTemplate(2l)).thenReturn(pageTemplate); + when(pageTemplate.isSystem()).thenReturn(true); + assertThrows(IllegalAccessException.class, () -> pageTemplateService.deletePageTemplate(2l, testuser)); + + when(pageTemplate.isSystem()).thenReturn(false); pageTemplateService.deletePageTemplate(2l, testuser); + verify(attachmentService, times(1)).deleteAttachments(PageTemplateAttachmentPlugin.OBJECT_TYPE, "2"); verify(translationService, times(1)).deleteTranslationLabels(PageTemplateTranslationPlugin.OBJECT_TYPE, 2l); verify(pageTemplateStorage, times(1)).deletePageTemplate(2l); @@ -272,6 +280,7 @@ public void deletePageTemplateWhenException() throws ObjectNotFoundException, Il assertThrows(IllegalAccessException.class, () -> pageTemplateService.deletePageTemplate(2l, testuser)); when(layoutAclService.isAdministrator(testuser)).thenReturn(true); + when(pageTemplateStorage.getPageTemplate(2l)).thenReturn(pageTemplate); doThrow(RuntimeException.class).when(attachmentService).deleteAttachments(anyString(), any()); doThrow(ObjectNotFoundException.class).when(translationService).deleteTranslationLabels(anyString(), anyLong()); pageTemplateService.deletePageTemplate(2l, testuser); diff --git a/layout-webapp/src/main/resources/locale/portlet/LayoutEditor_en.properties b/layout-webapp/src/main/resources/locale/portlet/LayoutEditor_en.properties index 0aa89d8ca..b38d4795e 100644 --- a/layout-webapp/src/main/resources/locale/portlet/LayoutEditor_en.properties +++ b/layout-webapp/src/main/resources/locale/portlet/LayoutEditor_en.properties @@ -118,6 +118,16 @@ pageTemplates.label.name=Name pageTemplates.label.description=Description pageTemplates.label.category=Category pageTemplates.label.status=Status +pageTemplates.label.actions=Actions +pageTemplates.menu.open=Open Menu pageTemplate.label.preview=Preview of {0} template pageTemplate.status.update.success=Template status successfully updated pageTemplate.status.update.error=An unknown error occurred while updating template status. Please contact the administrator or try agan later. +pageTemplate.status.delete.success=Template status successfully deleted +pageTemplate.status.delete.error=An unknown error occurred while deleting template status. Please contact the administrator or try agan later. +pageTemplate.label.delete=Delete +pageTemplate.label.system.noDelete=This product template cannot be deleted +pageTemplate.label.confirmDeleteTitle=Delete page template? +pageTemplate.label.confirmDeleteMessage=Would you like to delete page template: {0} +pageTemplate.label.confirm=Confirm +pageTemplate.label.cancel=Cancel diff --git a/layout-webapp/src/main/webapp/vue-app/common-layout-components/js/PageTemplateService.js b/layout-webapp/src/main/webapp/vue-app/common-layout-components/js/PageTemplateService.js index 7e874ddf9..460b2b9a4 100644 --- a/layout-webapp/src/main/webapp/vue-app/common-layout-components/js/PageTemplateService.js +++ b/layout-webapp/src/main/webapp/vue-app/common-layout-components/js/PageTemplateService.js @@ -76,3 +76,14 @@ export function updatePageTemplate(pageTemplate) { } }); } + +export function deletePageTemplate(id) { + return fetch(`/layout/rest/pageTemplates/${id}`, { + credentials: 'include', + method: 'DELETE', + }).then((resp) => { + if (!resp?.ok) { + throw new Error('Error when creating page template'); + } + }); +} diff --git a/layout-webapp/src/main/webapp/vue-app/page-templates-management/components/list/PageTemplateItem.vue b/layout-webapp/src/main/webapp/vue-app/page-templates-management/components/list/PageTemplateItem.vue index c8f1f9916..faa8a1e1d 100644 --- a/layout-webapp/src/main/webapp/vue-app/page-templates-management/components/list/PageTemplateItem.vue +++ b/layout-webapp/src/main/webapp/vue-app/page-templates-management/components/list/PageTemplateItem.vue @@ -30,15 +30,70 @@ + align="center" + width="50px"> + + + + + + + + {{ $t('pageTemplate.label.system.noDelete') }} + + + + +