Skip to content

Commit

Permalink
feat: Allow to define Category of Page Templates - MEED-6876 - Meeds-…
Browse files Browse the repository at this point in the history
  • Loading branch information
boubaker committed May 28, 2024
1 parent 2812fef commit 606dd3c
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ public class PageTemplateEntity {
@SequenceGenerator(name = "SEQ_PAGE_TEMPLATE_ID", sequenceName = "SEQ_PAGE_TEMPLATE_ID", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_PAGE_TEMPLATE_ID")
@Column(name = "ID")
protected Long id;
protected Long id;

@Column(name = "DISABLED")
private boolean disabled;

@Column(name = "CATEGORY")
private String category;

@Column(name = "CONTENT")
private String content;
private String content;

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,25 @@
@AllArgsConstructor
public class PageTemplate {

private long id;
private long id;

private String content;
private boolean disabled;

private String name;
private String category;

private String description;
private String content;

private long illustrationId;
private String name;

public PageTemplate(long id, String content) {
private String description;

private long illustrationId;

public PageTemplate(long id, boolean disabled, String category, String content) {
this.id = id;
this.content = content;
this.disabled = disabled;
this.category = category;
}

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

private Map<String, String> descriptions;

private String category;

private String illustrationPath;

private String layoutPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ private Page createPageInstance(String siteType,
if (pageTemplate == null) {
throw new ObjectNotFoundException("pageTemplate not found");
}
if (pageTemplate.isDisabled()) {
throw new IllegalArgumentException("pageTemplate with designated Id is disabled");
}
page = userPortalConfigService.createPageTemplate(EMPTY_PAGE_TEMPLATE,
siteType,
siteName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ protected PageTemplate createPageTemplate(PageTemplateDescriptor d, long oldTemp
pageTemplate = new PageTemplate();
isNew = true;
}
pageTemplate.setCategory(d.getCategory());
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 @@ -37,28 +37,48 @@ public class PageTemplateStorage {

public List<PageTemplate> getPageTemplates() {
List<PageTemplateEntity> entities = pageTemplateDAO.findAll();
return entities.stream().map(entity -> new PageTemplate(entity.getId(), entity.getContent())).toList();
return entities.stream()
.map(e -> new PageTemplate(e.getId(),
e.isDisabled(),
e.getCategory(),
e.getContent()))
.toList();
}

public PageTemplate getPageTemplate(long id) {
return pageTemplateDAO.findById(id)
.map(e -> new PageTemplate(e.getId(), e.getContent()))
.map(e -> new PageTemplate(e.getId(),
e.isDisabled(),
e.getCategory(),
e.getContent()))
.orElse(null);
}

public PageTemplate createPageTemplate(PageTemplate pageTemplate) {
PageTemplateEntity entity = new PageTemplateEntity(null, pageTemplate.getContent());
PageTemplateEntity entity = new PageTemplateEntity(null,
pageTemplate.isDisabled(),
pageTemplate.getCategory(),
pageTemplate.getContent());
entity = pageTemplateDAO.save(entity);
return new PageTemplate(entity.getId(), entity.getContent());
return new PageTemplate(entity.getId(),
entity.isDisabled(),
entity.getCategory(),
entity.getContent());
}

public PageTemplate updatePageTemplate(PageTemplate pageTemplate) throws ObjectNotFoundException {
if (!pageTemplateDAO.existsById(pageTemplate.getId())) {
throw new ObjectNotFoundException("Page template doesn't exist");
}
PageTemplateEntity entity = new PageTemplateEntity(pageTemplate.getId(), pageTemplate.getContent());
PageTemplateEntity entity = new PageTemplateEntity(pageTemplate.getId(),
pageTemplate.isDisabled(),
pageTemplate.getCategory(),
pageTemplate.getContent());
entity = pageTemplateDAO.save(entity);
return new PageTemplate(entity.getId(), entity.getContent());
return new PageTemplate(entity.getId(),
entity.isDisabled(),
entity.getCategory(),
entity.getContent());
}

public void deletePageTemplate(long templateId) throws ObjectNotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,37 @@
</modifySql>
</changeSet>

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

<changeSet
author="layout"
id="1.0.0-3">
<addColumn tableName="LAYOUT_PAGE_TEMPLATES">
<column
name="CATEGORY"
type="NVARCHAR(200)" />
</addColumn>
</changeSet>

<changeSet author="layout" id="1.0.0-4" dbms="mysql">
<sql>
ALTER TABLE LAYOUT_PAGE_TEMPLATES MODIFY COLUMN CONTENT LONGTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
</sql>
</changeSet>

<changeSet author="layout" id="1.0.0-5" dbms="mysql">
<sql>
ALTER TABLE LAYOUT_PAGE_TEMPLATES MODIFY COLUMN CATEGORY VARCHAR(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
</sql>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@
import io.meeds.social.translation.service.TranslationService;

@SpringBootTest(classes = {
PageTemplateService.class,
PageTemplateService.class,
})
@ExtendWith(MockitoExtension.class)
public class PageTemplateServiceTest {

private static final String LAYOUT_CONTENT = "...layout...";
private static final String LAYOUT_CONTENT = "...layout...";

private static final String LAYOUT_CATEGORY = "CATEGORY";

@MockBean
private LayoutAclService layoutAclService;
Expand Down Expand Up @@ -89,13 +91,13 @@ public class PageTemplateServiceTest {
@Autowired
private PageTemplateService pageTemplateService;

private String testuser = "testuser";
private String testuser = "testuser";

@Test
public void getPageTemplates() {
when(pageTemplate.getId()).thenReturn(2l);
when(pageTemplate.getContent()).thenReturn(LAYOUT_CONTENT);

when(pageTemplateStorage.getPageTemplates()).thenReturn(Collections.singletonList(pageTemplate));
List<PageTemplate> pageTemplates = pageTemplateService.getPageTemplates();
assertNotNull(pageTemplates);
Expand All @@ -109,7 +111,7 @@ public void getPageTemplates() {

@Test
public void getPageTemplatesWithExpand() throws ObjectNotFoundException {
PageTemplate template = new PageTemplate(2l, LAYOUT_CONTENT);
PageTemplate template = new PageTemplate(2l, false, LAYOUT_CATEGORY, LAYOUT_CONTENT);
when(localeConfigService.getDefaultLocaleConfig()).thenReturn(defaultLocaleConfig);
when(defaultLocaleConfig.getLocale()).thenReturn(Locale.ENGLISH);

Expand Down Expand Up @@ -166,7 +168,8 @@ public void getPageTemplatesWithExpand() throws ObjectNotFoundException {
assertEquals(1, pageTemplates.size());
assertEquals(enDesc, pageTemplates.get(0).getDescription());

when(attachmentService.getAttachmentFileIds(PageTemplateAttachmentPlugin.OBJECT_TYPE, "2")).thenReturn(Collections.singletonList("32"));
when(attachmentService.getAttachmentFileIds(PageTemplateAttachmentPlugin.OBJECT_TYPE,
"2")).thenReturn(Collections.singletonList("32"));
pageTemplates = pageTemplateService.getPageTemplates(Locale.GERMAN, true);
assertNotNull(pageTemplates);
assertEquals(1, pageTemplates.size());
Expand Down Expand Up @@ -196,7 +199,7 @@ public void createPageTemplate() throws IllegalAccessException {
@Test
public void deletePageTemplate() throws ObjectNotFoundException, IllegalAccessException {
assertThrows(IllegalAccessException.class, () -> pageTemplateService.deletePageTemplate(2l, testuser));

when(layoutAclService.isAdministrator(testuser)).thenReturn(true);
pageTemplateService.deletePageTemplate(2l, testuser);
verify(attachmentService, times(1)).deleteAttachments(PageTemplateAttachmentPlugin.OBJECT_TYPE, "2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,5 @@ pageTemplates.add=Add
pageTemplates.filter.placeholder=Filter by name, description
pageTemplates.label.name=Name
pageTemplates.label.description=Description
pageTemplates.label.category=Category
pageTemplate.label.preview=Preview of {0} template
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default {
getPageTemplates() {
if (!this.$root.pageTemplates) {
return this.$pageTemplateService.getPageTemplates()
.then(pageTemplates => this.$root.pageTemplates = pageTemplates || []);
.then(pageTemplates => this.$root.pageTemplates = pageTemplates && pageTemplates.filter(t => !t.disabled) || []);
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@
</td>
<!-- name -->
<td
:width="$root.isMobile && '100%' || 'auto'"
align="left"
class="text-truncate"
width="auto"
v-sanitized-html="name"></td>
<!-- description -->
<td
v-if="!$root.isMobile"
align="left"
class="text-truncate"
v-sanitized-html="description"></td>
<td
v-if="!$root.isMobile"
align="left"
class="text-truncate"
width="120px">
{{ category }}
</td>
</tr>
</template>
<script>
Expand All @@ -47,6 +54,12 @@ export default {
illustrationSrc() {
return this.illustrationId && `${eXo.env.portal.context}/${eXo.env.portal.rest}/v1/social/attachments/pageTemplate/${this.pageTemplateId}/${this.illustrationId}` || '/layout/images/page-templates/DefaultPreview.webp';
},
category() {
const i18nKey = `layout.pageTemplate.category.${this.pageTemplate?.category}`;
return this.pageTemplate?.category
&& (this.$te(i18nKey) ? this.$t(i18nKey) : this.pageTemplate.category)
|| this.$t('layout.pageTemplate.category.customized');
},
},
};
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ export default {
class: 'page-template-description-header',
width: '70%'
},
{
text: this.$t('pageTemplates.label.category'),
value: 'category',
align: 'left',
sortable: false,
class: 'page-template-category-header',
width: '120px'
},
];
},
filteredPageTemplates() {
Expand Down

0 comments on commit 606dd3c

Please sign in to comment.