Some more models for MVC based pages application.
parent
d5cc0370bc
commit
4305242079
|
|
@ -45,21 +45,18 @@ public class ArticleModel {
|
||||||
private GlobalizationHelper globalizationHelper;
|
private GlobalizationHelper globalizationHelper;
|
||||||
|
|
||||||
public String getTitle() {
|
public String getTitle() {
|
||||||
return getArticle()
|
return globalizationHelper
|
||||||
.getTitle()
|
.getValueFromLocalizedString(getArticle().getTitle());
|
||||||
.getValue(globalizationHelper.getNegotiatedLocale());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
return getArticle()
|
return globalizationHelper
|
||||||
.getDescription()
|
.getValueFromLocalizedString(getArticle().getDescription());
|
||||||
.getValue(globalizationHelper.getNegotiatedLocale());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getText() {
|
public String getText() {
|
||||||
return getArticle()
|
return globalizationHelper
|
||||||
.getText()
|
.getValueFromLocalizedString(getArticle().getText());
|
||||||
.getValue(globalizationHelper.getNegotiatedLocale());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Article getArticle() {
|
protected Article getArticle() {
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ import org.libreccm.categorization.Categorization;
|
||||||
import org.libreccm.categorization.Category;
|
import org.libreccm.categorization.Category;
|
||||||
import org.libreccm.categorization.CategoryManager;
|
import org.libreccm.categorization.CategoryManager;
|
||||||
import org.libreccm.l10n.GlobalizationHelper;
|
import org.libreccm.l10n.GlobalizationHelper;
|
||||||
|
import org.librecms.contentsection.AttachmentList;
|
||||||
import org.librecms.contentsection.ContentItem;
|
import org.librecms.contentsection.ContentItem;
|
||||||
import org.librecms.contentsection.ContentItemVersion;
|
import org.librecms.contentsection.ContentItemVersion;
|
||||||
import org.librecms.pages.PagesRouter;
|
import org.librecms.pages.PagesRouter;
|
||||||
|
|
@ -32,6 +33,7 @@ import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
|
@ -59,7 +61,8 @@ import javax.ws.rs.NotFoundException;
|
||||||
@Named("CmsPagesCategorizedItemModel")
|
@Named("CmsPagesCategorizedItemModel")
|
||||||
public class ContentItemModel {
|
public class ContentItemModel {
|
||||||
|
|
||||||
private static final Logger LOGGER = LogManager.getLogger(ContentItemModel.class
|
private static final Logger LOGGER = LogManager.getLogger(
|
||||||
|
ContentItemModel.class
|
||||||
);
|
);
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
|
|
@ -74,7 +77,7 @@ public class ContentItemModel {
|
||||||
@Inject
|
@Inject
|
||||||
private GlobalizationHelper globalizationHelper;
|
private GlobalizationHelper globalizationHelper;
|
||||||
|
|
||||||
private DateTimeFormatter dateTimeFormatter
|
private final DateTimeFormatter dateTimeFormatter
|
||||||
= DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.systemDefault());
|
= DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.systemDefault());
|
||||||
|
|
||||||
private String itemName;
|
private String itemName;
|
||||||
|
|
@ -140,32 +143,21 @@ public class ContentItemModel {
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return getOrRetrieveContentItem()
|
return getOrRetrieveContentItem()
|
||||||
.map(ContentItem::getName)
|
.map(ContentItem::getName)
|
||||||
.map(
|
.map(globalizationHelper::getValueFromLocalizedString)
|
||||||
localized -> localized.getValue(
|
|
||||||
globalizationHelper.getNegotiatedLocale()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.orElse("");
|
.orElse("");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTitle() {
|
public String getTitle() {
|
||||||
return getOrRetrieveContentItem()
|
return getOrRetrieveContentItem()
|
||||||
.map(ContentItem::getTitle)
|
.map(ContentItem::getTitle)
|
||||||
.map(
|
.map(globalizationHelper::getValueFromLocalizedString)
|
||||||
localized -> localized.getValue(
|
.orElse("");
|
||||||
globalizationHelper.getNegotiatedLocale()
|
|
||||||
)
|
|
||||||
).orElse("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
return getOrRetrieveContentItem()
|
return getOrRetrieveContentItem()
|
||||||
.map(ContentItem::getDescription)
|
.map(ContentItem::getDescription)
|
||||||
.map(
|
.map(globalizationHelper::getValueFromLocalizedString)
|
||||||
localized -> localized.getValue(
|
|
||||||
globalizationHelper.getNegotiatedLocale()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.orElse("");
|
.orElse("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -208,6 +200,10 @@ public class ContentItemModel {
|
||||||
.orElse("");
|
.orElse("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<AttachmentList> getAttachments() {
|
||||||
|
return getContentItem().getAttachments();
|
||||||
|
}
|
||||||
|
|
||||||
private Optional<ContentItem> getOrRetrieveContentItem() {
|
private Optional<ContentItem> getOrRetrieveContentItem() {
|
||||||
if (contentItem == null) {
|
if (contentItem == null) {
|
||||||
retrieveContentItem();
|
retrieveContentItem();
|
||||||
|
|
|
||||||
|
|
@ -78,21 +78,14 @@ public class ContentItemTypeModel {
|
||||||
public String getLabel() {
|
public String getLabel() {
|
||||||
return getOrRetrieveContentType()
|
return getOrRetrieveContentType()
|
||||||
.map(ContentType::getLabel)
|
.map(ContentType::getLabel)
|
||||||
.map(
|
.map(globalizationHelper::getValueFromLocalizedString)
|
||||||
label -> label.getValue(
|
|
||||||
globalizationHelper.getNegotiatedLocale())
|
|
||||||
)
|
|
||||||
.orElse("");
|
.orElse("");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
return getOrRetrieveContentType()
|
return getOrRetrieveContentType()
|
||||||
.map(ContentType::getDescription)
|
.map(ContentType::getDescription)
|
||||||
.map(
|
.map(globalizationHelper::getValueFromLocalizedString)
|
||||||
description -> description.getValue(
|
|
||||||
globalizationHelper.getNegotiatedLocale()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.orElse("");
|
.orElse("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -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()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue