Get start and end date time of an event formatted using a pattern
parent
b034d7d321
commit
ae2d3b57ea
|
|
@ -18,13 +18,9 @@
|
|||
*/
|
||||
package org.librecms.pages.models;
|
||||
|
||||
import org.libreccm.l10n.GlobalizationHelper;
|
||||
import org.librecms.contentsection.ContentItem;
|
||||
import org.librecms.contenttypes.Event;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
|
|
@ -32,6 +28,10 @@ import javax.inject.Inject;
|
|||
import javax.inject.Named;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import org.libreccm.l10n.GlobalizationHelper;
|
||||
import org.librecms.contentsection.ContentItem;
|
||||
import org.librecms.contenttypes.Event;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||
|
|
@ -48,15 +48,13 @@ public class EventModel implements ProcessesContentItem {
|
|||
|
||||
private boolean initialized;
|
||||
|
||||
private final DateTimeFormatter isoDateTimeFormatter;
|
||||
|
||||
private String title;
|
||||
|
||||
private String text;
|
||||
|
||||
private String startDateTime;
|
||||
private LocalDateTime startDateTime;
|
||||
|
||||
private String endDateTime;
|
||||
private LocalDateTime endDateTime;
|
||||
|
||||
private String eventDate;
|
||||
|
||||
|
|
@ -66,8 +64,6 @@ public class EventModel implements ProcessesContentItem {
|
|||
|
||||
public EventModel() {
|
||||
initialized = false;
|
||||
isoDateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME
|
||||
.withZone(ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
|
|
@ -85,19 +81,97 @@ public class EventModel implements ProcessesContentItem {
|
|||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public String getStartDateTime() {
|
||||
public LocalDateTime getStartDateTime() {
|
||||
contentItemModel.init();
|
||||
|
||||
return startDateTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the start date of the event represented 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 getStartDateTime(final String pattern) {
|
||||
contentItemModel.init();
|
||||
|
||||
return Optional
|
||||
.ofNullable(startDateTime)
|
||||
.map(
|
||||
dateTime -> DateTimeFormatter
|
||||
.ofPattern(
|
||||
pattern,
|
||||
globalizationHelper.getNegotiatedLocale()
|
||||
)
|
||||
.withZone(ZoneId.systemDefault())
|
||||
.format(dateTime)
|
||||
)
|
||||
.orElse("");
|
||||
}
|
||||
|
||||
public String getStartDateTimeAsString() {
|
||||
contentItemModel.init();
|
||||
|
||||
return Optional
|
||||
.ofNullable(endDateTime)
|
||||
.map(
|
||||
dateTime -> DateTimeFormatter.ISO_DATE_TIME
|
||||
.withZone(ZoneId.systemDefault())
|
||||
.format(dateTime)
|
||||
)
|
||||
.orElse("");
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public String getEndDateTime() {
|
||||
public LocalDateTime getEndDateTime() {
|
||||
contentItemModel.init();
|
||||
|
||||
return endDateTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the end date of the event represented 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 getEndDateTime(final String pattern) {
|
||||
contentItemModel.init();
|
||||
|
||||
return Optional
|
||||
.ofNullable(endDateTime)
|
||||
.map(
|
||||
dateTime -> DateTimeFormatter
|
||||
.ofPattern(
|
||||
pattern,
|
||||
globalizationHelper.getNegotiatedLocale()
|
||||
)
|
||||
.withZone(ZoneId.systemDefault())
|
||||
.format(dateTime)
|
||||
)
|
||||
.orElse("");
|
||||
}
|
||||
|
||||
public String getEndDateTimeAsString() {
|
||||
contentItemModel.init();
|
||||
|
||||
return Optional
|
||||
.ofNullable(endDateTime)
|
||||
.map(
|
||||
dateTime -> DateTimeFormatter.ISO_DATE_TIME
|
||||
.withZone(ZoneId.systemDefault())
|
||||
.format(dateTime)
|
||||
)
|
||||
.orElse("");
|
||||
}
|
||||
|
||||
@Transactional(Transactional.TxType.REQUIRED)
|
||||
public String getEventDate() {
|
||||
contentItemModel.init();
|
||||
|
|
@ -139,14 +213,20 @@ public class EventModel implements ProcessesContentItem {
|
|||
.orElse("");
|
||||
startDateTime = Optional
|
||||
.ofNullable(event.getStartDate())
|
||||
.map(Date::toInstant)
|
||||
.map(isoDateTimeFormatter::format)
|
||||
.orElse("");
|
||||
.map(
|
||||
startDate -> LocalDateTime.from(
|
||||
startDate.toInstant().atZone(ZoneId.systemDefault())
|
||||
)
|
||||
)
|
||||
.orElse(null);
|
||||
endDateTime = Optional
|
||||
.ofNullable(event.getEndDate())
|
||||
.map(Date::toInstant)
|
||||
.map(isoDateTimeFormatter::format)
|
||||
.orElse("");
|
||||
.map(
|
||||
endDate -> LocalDateTime.from(
|
||||
endDate.toInstant().atZone(ZoneId.systemDefault())
|
||||
)
|
||||
)
|
||||
.orElse(null);
|
||||
eventDate = Optional
|
||||
.ofNullable(event.getEventDate())
|
||||
.map(globalizationHelper::getValueFromLocalizedString)
|
||||
|
|
|
|||
Loading…
Reference in New Issue