Added reference to negotiated locale to models, used to format dates.

pull/20/head
Jens Pelzetter 2022-02-19 11:51:21 +01:00
parent 47e26b5878
commit 2f8abff8f4
6 changed files with 55 additions and 1 deletions

View File

@ -18,6 +18,8 @@
*/ */
package org.librecms.pages.models; package org.librecms.pages.models;
import java.util.Locale;
/** /**
* A simplified representation of a content item for use in a list of content * A simplified representation of a content item for use in a list of content
* items.Base class for other more specific models. * items.Base class for other more specific models.
@ -26,6 +28,12 @@ package org.librecms.pages.models;
*/ */
public abstract class AbstractContentItemListItemModel { public abstract class AbstractContentItemListItemModel {
/**
* The locale negotiated with the user agent. Useful for example for
* formatting date. Only for internal use.
*/
private Locale locale;
private String uuid; private String uuid;
private String displayName; private String displayName;
@ -35,6 +43,14 @@ public abstract class AbstractContentItemListItemModel {
private String title; private String title;
private String description; private String description;
protected Locale getLocale() {
return locale;
}
protected void setLocale(final Locale locale) {
this.locale = locale;
}
public String getUuid() { public String getUuid() {
return uuid; return uuid;

View File

@ -39,6 +39,8 @@ public abstract class AbstractContentItemListItemModelBuilder<T extends ContentI
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public M buildListItemModel(final ContentItem contentItem) { public M buildListItemModel(final ContentItem contentItem) {
final M model = buildModel(); final M model = buildModel();
model.setLocale(globalizationHelper.getNegotiatedLocale());
model.setDescription( model.setDescription(
globalizationHelper.getValueFromLocalizedString( globalizationHelper.getValueFromLocalizedString(
contentItem.getDescription() contentItem.getDescription()

View File

@ -21,6 +21,8 @@ package org.librecms.pages.models;
import org.librecms.contenttypes.Event; import org.librecms.contenttypes.Event;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
/** /**
* *
@ -43,6 +45,13 @@ public class EventListItemModel extends AbstractContentItemListItemModel {
return startDate; return startDate;
} }
public String getStartDate(final String pattern) {
return DateTimeFormatter
.ofPattern(pattern)
.withZone(ZoneId.systemDefault())
.format(startDate);
}
public void setStartDate(final LocalDateTime startDate) { public void setStartDate(final LocalDateTime startDate) {
this.startDate = startDate; this.startDate = startDate;
} }
@ -50,6 +59,13 @@ public class EventListItemModel extends AbstractContentItemListItemModel {
public LocalDateTime getEndDate() { public LocalDateTime getEndDate() {
return endDate; return endDate;
} }
public String getEndDate(final String pattern) {
return DateTimeFormatter
.ofPattern(pattern)
.withZone(ZoneId.systemDefault())
.format(endDate);
}
public void setEndDate(final LocalDateTime endDate) { public void setEndDate(final LocalDateTime endDate) {
this.endDate = endDate; this.endDate = endDate;

View File

@ -186,7 +186,8 @@ public class ItemListModel {
public List<? extends AbstractContentItemListItemModel> getItems( public List<? extends AbstractContentItemListItemModel> getItems(
final String listName final String listName
) { ) {
return Collections.unmodifiableList( final List<? extends AbstractContentItemListItemModel> items
= Collections.unmodifiableList(
buildList( buildList(
buildLimitToType(getLimitToTypeSetting(listName)), buildLimitToType(getLimitToTypeSetting(listName)),
collectCategories( collectCategories(
@ -208,6 +209,8 @@ public class ItemListModel {
getOffset(listName, getPageSizeSetting(listName)) getOffset(listName, getPageSizeSetting(listName))
) )
); );
return items;
} }
private List<? extends AbstractContentItemListItemModel> buildList( private List<? extends AbstractContentItemListItemModel> buildList(

View File

@ -36,6 +36,22 @@ public class NewsListItemModel extends AbstractContentItemListItemModel {
return releaseDate; return releaseDate;
} }
/**
* Returns the release date of the news represeted by this model formatted
* using the provided pattern. The pattern MUST be a valid pattern for
* {@link DateTimeFormatter#ofPattern(java.lang.String) }.
*
* @param pattern The pattern to use for formatting the release date.
*
* @return The formatted release date.
*/
public String getReleaseDate(final String pattern) {
return DateTimeFormatter
.ofPattern(pattern, getLocale())
.withZone(ZoneId.systemDefault())
.format(releaseDate);
}
public String getReleaseDateAsString() { public String getReleaseDateAsString() {
return DateTimeFormatter.ISO_DATE_TIME return DateTimeFormatter.ISO_DATE_TIME
.withZone(ZoneId.systemDefault()) .withZone(ZoneId.systemDefault())

View File

@ -18,6 +18,7 @@
*/ */
package org.librecms.pages.models; package org.librecms.pages.models;
import org.libreccm.l10n.GlobalizationHelper;
import org.librecms.contenttypes.News; import org.librecms.contenttypes.News;
import java.time.LocalDateTime; import java.time.LocalDateTime;