From 430524207966a87ad63a67df329a5b3a4bcb85ed Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Wed, 27 Oct 2021 20:09:53 +0200 Subject: [PATCH] Some more models for MVC based pages application. --- .../librecms/pages/models/ArticleModel.java | 15 ++- .../pages/models/ContentItemModel.java | 32 +++---- .../pages/models/ContentItemTypeModel.java | 11 +-- .../org/librecms/pages/models/EventModel.java | 96 +++++++++++++++++++ .../org/librecms/pages/models/NewsModel.java | 92 ++++++++++++++++++ 5 files changed, 210 insertions(+), 36 deletions(-) create mode 100644 ccm-cms/src/main/java/org/librecms/pages/models/EventModel.java create mode 100644 ccm-cms/src/main/java/org/librecms/pages/models/NewsModel.java diff --git a/ccm-cms/src/main/java/org/librecms/pages/models/ArticleModel.java b/ccm-cms/src/main/java/org/librecms/pages/models/ArticleModel.java index c9c45ea33..88a45d662 100644 --- a/ccm-cms/src/main/java/org/librecms/pages/models/ArticleModel.java +++ b/ccm-cms/src/main/java/org/librecms/pages/models/ArticleModel.java @@ -45,21 +45,18 @@ public class ArticleModel { private GlobalizationHelper globalizationHelper; public String getTitle() { - return getArticle() - .getTitle() - .getValue(globalizationHelper.getNegotiatedLocale()); + return globalizationHelper + .getValueFromLocalizedString(getArticle().getTitle()); } public String getDescription() { - return getArticle() - .getDescription() - .getValue(globalizationHelper.getNegotiatedLocale()); + return globalizationHelper + .getValueFromLocalizedString(getArticle().getDescription()); } public String getText() { - return getArticle() - .getText() - .getValue(globalizationHelper.getNegotiatedLocale()); + return globalizationHelper + .getValueFromLocalizedString(getArticle().getText()); } protected Article getArticle() { diff --git a/ccm-cms/src/main/java/org/librecms/pages/models/ContentItemModel.java b/ccm-cms/src/main/java/org/librecms/pages/models/ContentItemModel.java index a6486a11a..61ce14e8b 100644 --- a/ccm-cms/src/main/java/org/librecms/pages/models/ContentItemModel.java +++ b/ccm-cms/src/main/java/org/librecms/pages/models/ContentItemModel.java @@ -24,6 +24,7 @@ import org.libreccm.categorization.Categorization; import org.libreccm.categorization.Category; import org.libreccm.categorization.CategoryManager; import org.libreccm.l10n.GlobalizationHelper; +import org.librecms.contentsection.AttachmentList; import org.librecms.contentsection.ContentItem; import org.librecms.contentsection.ContentItemVersion; import org.librecms.pages.PagesRouter; @@ -32,6 +33,7 @@ import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; +import java.util.List; import java.util.Objects; import java.util.Optional; @@ -59,7 +61,8 @@ import javax.ws.rs.NotFoundException; @Named("CmsPagesCategorizedItemModel") public class ContentItemModel { - private static final Logger LOGGER = LogManager.getLogger(ContentItemModel.class + private static final Logger LOGGER = LogManager.getLogger( + ContentItemModel.class ); @Inject @@ -74,7 +77,7 @@ public class ContentItemModel { @Inject private GlobalizationHelper globalizationHelper; - private DateTimeFormatter dateTimeFormatter + private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.systemDefault()); private String itemName; @@ -140,32 +143,21 @@ public class ContentItemModel { public String getName() { return getOrRetrieveContentItem() .map(ContentItem::getName) - .map( - localized -> localized.getValue( - globalizationHelper.getNegotiatedLocale() - ) - ) + .map(globalizationHelper::getValueFromLocalizedString) .orElse(""); } public String getTitle() { return getOrRetrieveContentItem() .map(ContentItem::getTitle) - .map( - localized -> localized.getValue( - globalizationHelper.getNegotiatedLocale() - ) - ).orElse(""); + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(""); } public String getDescription() { return getOrRetrieveContentItem() .map(ContentItem::getDescription) - .map( - localized -> localized.getValue( - globalizationHelper.getNegotiatedLocale() - ) - ) + .map(globalizationHelper::getValueFromLocalizedString) .orElse(""); } @@ -207,7 +199,11 @@ public class ContentItemModel { .map(ContentItem::getLastModifyingUserName) .orElse(""); } - + + public List getAttachments() { + return getContentItem().getAttachments(); + } + private Optional getOrRetrieveContentItem() { if (contentItem == null) { retrieveContentItem(); diff --git a/ccm-cms/src/main/java/org/librecms/pages/models/ContentItemTypeModel.java b/ccm-cms/src/main/java/org/librecms/pages/models/ContentItemTypeModel.java index b61f27c66..e7d6914aa 100644 --- a/ccm-cms/src/main/java/org/librecms/pages/models/ContentItemTypeModel.java +++ b/ccm-cms/src/main/java/org/librecms/pages/models/ContentItemTypeModel.java @@ -78,21 +78,14 @@ public class ContentItemTypeModel { public String getLabel() { return getOrRetrieveContentType() .map(ContentType::getLabel) - .map( - label -> label.getValue( - globalizationHelper.getNegotiatedLocale()) - ) + .map(globalizationHelper::getValueFromLocalizedString) .orElse(""); } public String getDescription() { return getOrRetrieveContentType() .map(ContentType::getDescription) - .map( - description -> description.getValue( - globalizationHelper.getNegotiatedLocale() - ) - ) + .map(globalizationHelper::getValueFromLocalizedString) .orElse(""); } diff --git a/ccm-cms/src/main/java/org/librecms/pages/models/EventModel.java b/ccm-cms/src/main/java/org/librecms/pages/models/EventModel.java new file mode 100644 index 000000000..1bf0569e2 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/pages/models/EventModel.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2021 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.librecms.pages.models; + +import org.libreccm.l10n.GlobalizationHelper; +import org.librecms.contentsection.ContentItem; +import org.librecms.contenttypes.Article; +import org.librecms.contenttypes.Event; + +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.inject.Named; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +@Named("CmsPagesEventModel") +public class EventModel { + + @Inject + private ContentItemModel contentItemModel; + + @Inject + private GlobalizationHelper globalizationHelper; + + public String getTitle() { + return globalizationHelper + .getValueFromLocalizedString(getEvent().getTitle()); + } + + public String getText() { + return globalizationHelper + .getValueFromLocalizedString(getEvent().getText()); + } + + public String getStartDateTime() { + return DateTimeFormatter.ISO_DATE_TIME + .withZone(ZoneId.systemDefault()) + .format(getEvent().getStartDate().toInstant()); + } + + public String getEndDateTime() { + return DateTimeFormatter.ISO_DATE_TIME + .withZone(ZoneId.systemDefault()) + .format(getEvent().getEndDate().toInstant()); + } + + public String getEventDate() { + return globalizationHelper + .getValueFromLocalizedString(getEvent().getEventDate()); + } + + public String getLocation() { + return globalizationHelper. + getValueFromLocalizedString(getEvent().getLocation()); + } + + protected Event getEvent() { + final ContentItem contentItem = contentItemModel.getContentItem(); + if (contentItem instanceof Event) { + return (Event) contentItem; + } else { + throw new WebApplicationException( + "Current content item is not an event", + Response + .status(Response.Status.INTERNAL_SERVER_ERROR) + .entity("Current content item is not an event.") + .build() + ); + } + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/pages/models/NewsModel.java b/ccm-cms/src/main/java/org/librecms/pages/models/NewsModel.java new file mode 100644 index 000000000..1c3e963cc --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/pages/models/NewsModel.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2021 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.librecms.pages.models; + +import org.libreccm.l10n.GlobalizationHelper; +import org.librecms.contentsection.ContentItem; +import org.librecms.contenttypes.News; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.Date; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.inject.Named; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +@Named("CmsPagesNewsModel") +public class NewsModel { + + @Inject + private ContentItemModel contentItemModel; + + @Inject + private GlobalizationHelper globalizationHelper; + + public String getTitle() { + return globalizationHelper + .getValueFromLocalizedString(getNews().getTitle()); + } + + public String getDescription() { + return globalizationHelper + .getValueFromLocalizedString(getNews().getDescription()); + } + + public String getText() { + return globalizationHelper + .getValueFromLocalizedString(getNews().getText()); + } + + public String getReleaseDateTime() { + return DateTimeFormatter.ISO_DATE_TIME + .withZone(ZoneId.systemDefault()) + .format( + LocalDateTime.from(getNews().getReleaseDate().toInstant()) + ); + } + + public boolean getHomepage() { + return getNews().isHomepage(); + } + + protected News getNews() { + final ContentItem contentItem = contentItemModel.getContentItem(); + if (contentItem instanceof News) { + return (News) contentItem; + } else { + throw new WebApplicationException( + "Current content item is not a news item.", + Response + .status(Response.Status.INTERNAL_SERVER_ERROR) + .entity("Current content item is not a new item.") + .build() + ); + } + } + +}