Some more models for MVC based pages application.

pull/10/head
Jens Pelzetter 2021-10-27 20:09:53 +02:00
parent d5cc0370bc
commit 4305242079
5 changed files with 210 additions and 36 deletions

View File

@ -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() {

View File

@ -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("");
}
@ -208,6 +200,10 @@ public class ContentItemModel {
.orElse("");
}
public List<AttachmentList> getAttachments() {
return getContentItem().getAttachments();
}
private Optional<ContentItem> getOrRetrieveContentItem() {
if (contentItem == null) {
retrieveContentItem();

View File

@ -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("");
}

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@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()
);
}
}
}

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@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()
);
}
}
}