Skip to content

Commit

Permalink
feat: Allow to delete a page template - MEED-6846 - Meeds-io/MIPs#133
Browse files Browse the repository at this point in the history
  • Loading branch information
boubaker committed May 17, 2024
1 parent d507427 commit bc3e2d9
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class PageTemplate {

private boolean disabled;

private boolean system;

private String category;

private String content;
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class PageTemplateDescriptor {

private String category;

private boolean system;

private String illustrationPath;

private String layoutPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public List<PageTemplate> getPageTemplates() {
return entities.stream()
.map(e -> new PageTemplate(e.getId(),
e.isDisabled(),
e.isSystem(),
e.getCategory(),
e.getContent()))
.toList();
Expand All @@ -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);
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,15 @@
</sql>
</changeSet>

<changeSet
author="layout"
id="1.0.0-6">
<addColumn tableName="LAYOUT_PAGE_TEMPLATES">
<column
name="SYSTEM_PAGE_TEMPLATE"
type="BOOLEAN"
defaultValueBoolean="false" />
</addColumn>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,70 @@
</td>
<td
v-if="!$root.isMobile"
align="left"
class="d-flex align-center ps-2"
width="120px">
align="center"
width="50px">
<v-switch
v-model="enabled"
:loading="loading"
class="mt-0 mx-auto"
@click="changeStatus" />
</td>
<td
v-if="!$root.isMobile"
align="center"
width="50px">
<v-menu
v-model="menu"
:left="!$vuetify.rtl"
:right="$vuetify.rtl"
bottom
offset-y
attach>
<template #activator="{ on, attrs }">
<v-btn
:aria-label="$t('pageTemplates.menu.open')"
icon
small
class="mx-auto"
v-bind="attrs"
v-on="on">
<v-icon size="16" class="icon-default-color">fas fa-ellipsis-v</v-icon>
</v-btn>
</template>
<v-hover v-if="menu" @input="hoverMenu = $event">
<v-list
class="pa-0"
dense
@mouseout="menu = false"
@focusout="menu = false">
<v-tooltip :disabled="!pageTemplate.system" bottom>
<template #activator="{ on, attrs }">
<div
v-on="on"
v-bind="attrs">
<v-list-item
:disabled="pageTemplate.system"
dense
@click="$emit('delete')">
<v-icon
:class="!pageTemplate.system && 'error--text' || 'disabled--text'"
size="13">
fa-trash
</v-icon>
<v-list-item-title
:class="!pageTemplate.system && 'error--text' || 'disabled--text'"
class="pl-3">
{{ $t('pageTemplate.label.delete') }}
</v-list-item-title>
</v-list-item>
</div>
</template>
<span>{{ $t('pageTemplate.label.system.noDelete') }}</span>
</v-tooltip>
</v-list>
</v-hover>
</v-menu>
</td>
</tr>
</template>
<script>
Expand All @@ -49,6 +104,10 @@ export default {
default: null,
},
},
data: () => ({
menu: false,
hoverMenu: false,
}),
computed: {
pageTemplateId() {
return this.pageTemplate?.id;
Expand All @@ -75,6 +134,17 @@ export default {
|| this.$t('layout.pageTemplate.category.customized');
},
},
watch: {
hoverMenu() {
if (!this.hoverMenu) {
window.setTimeout(() => {
if (!this.hoverMenu) {
this.menu = false;
}
}, 200);
}
},
},
methods: {
changeStatus() {
this.$root.$emit('close-alert-message');
Expand Down
Loading

0 comments on commit bc3e2d9

Please sign in to comment.