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 88a45d662..d3de25a72 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 @@ -22,9 +22,12 @@ import org.libreccm.l10n.GlobalizationHelper; import org.librecms.contentsection.ContentItem; import org.librecms.contenttypes.Article; +import java.util.Optional; + import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.inject.Named; +import javax.transaction.Transactional; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; @@ -43,26 +46,54 @@ public class ArticleModel { @Inject private GlobalizationHelper globalizationHelper; + + private String title; + + private String description; + + private String text; public String getTitle() { - return globalizationHelper - .getValueFromLocalizedString(getArticle().getTitle()); + if (title == null) { + init(); + } + return title; } public String getDescription() { - return globalizationHelper - .getValueFromLocalizedString(getArticle().getDescription()); + if (description == null) { + init(); + } + return description; } public String getText() { - return globalizationHelper - .getValueFromLocalizedString(getArticle().getText()); + if (text == null) { + init(); + } + return text; } - - protected Article getArticle() { - final ContentItem contentItem = contentItemModel.getContentItem(); + + @Transactional(Transactional.TxType.REQUIRED) + protected void init() { + final ContentItem contentItem = contentItemModel + .retrieveContentItem() + .orElse(null); if (contentItem instanceof Article) { - return (Article) contentItem; + final Article article = (Article) contentItem; + + title = Optional + .ofNullable(article.getText()) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(""); + description = Optional + .ofNullable(article.getDescription()) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(""); + text = Optional + .ofNullable(article.getText()) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(""); } else { throw new WebApplicationException( "Current content item is not an article.", 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 1a475683d..370cb0c58 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 @@ -23,23 +23,20 @@ import org.apache.logging.log4j.Logger; import org.libreccm.categorization.CategoryManager; import org.libreccm.categorization.CategoryRepository; 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; import org.librecms.pages.PagesService; import java.time.ZoneId; -import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; -import java.util.Date; -import java.util.List; import java.util.Optional; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.inject.Named; import javax.persistence.EntityManager; -import javax.ws.rs.NotFoundException; +import javax.transaction.Transactional; /** * Retrieves a categorized content item for the current category. To work, the @@ -59,7 +56,7 @@ public class ContentItemModel { @Inject private CategoryManager categoryManager; - + @Inject private CategoryRepository categoryRepository; @@ -82,7 +79,8 @@ public class ContentItemModel { private ContentItemVersion itemVersion; - private Optional contentItem; +// private Optional contentItem; + private Optional contentItem; public String getItemName() { return itemName; @@ -100,157 +98,314 @@ public class ContentItemModel { this.itemVersion = itemVersion; } - /** - * Retrieves the current content item. Depending if {@link #itemName} has - * been initalized with a value either the index item of the current - * category or the item in the category identified by {@link #itemName} will - * be retrieved. The item is only received once per request. The method will - * retrieve the item on the first call and store the result in - * {@link #contentItem}. Subsequent calls will return the value of - * {@link #contentItem}. If {@link #itemName} is not {@code null} and there - * is no content item with the requested name in the category this method - * throws a {@link NotFoundException}. - * - * @return The requested categorized item. If {@link #itemName} is - * {@code null}, and the current category has not index item, the - * method will return {@code null}. - * - * @throws NotFoundException If there is no item identified by the name in - * {@link #itemName}. - */ - public ContentItem getContentItem() { - return getOrRetrieveContentItem().orElse(null); + public boolean isItemAvailable() { + return getOrRetrieveContentItem().isPresent(); } - + public long getObjectId() { return getOrRetrieveContentItem() - .map(ContentItem::getObjectId) + .map(ContentItemModelData::getObjectId) .orElse(0L); } public String getUuid() { return getOrRetrieveContentItem() - .map(ContentItem::getUuid) + .map(ContentItemModelData::getUuid) .orElse(""); } public String getDisplayName() { return getOrRetrieveContentItem() - .map(ContentItem::getDisplayName) + .map(ContentItemModelData::getDisplayName) .orElse(""); } public String getItemUuid() { return getOrRetrieveContentItem() - .map(ContentItem::getItemUuid) + .map(ContentItemModelData::getItemUuid) .orElse(""); } public String getName() { return getOrRetrieveContentItem() - .map(ContentItem::getName) - .map(globalizationHelper::getValueFromLocalizedString) + .map(ContentItemModelData::getName) .orElse(""); } public String getTitle() { return getOrRetrieveContentItem() - .map(ContentItem::getTitle) - .map(globalizationHelper::getValueFromLocalizedString) + .map(ContentItemModelData::getTitle) .orElse(""); } public String getDescription() { return getOrRetrieveContentItem() - .map(ContentItem::getDescription) - .map(globalizationHelper::getValueFromLocalizedString) + .map(ContentItemModelData::getDescription) .orElse(""); } public String getVersion() { return getOrRetrieveContentItem() - .map(ContentItem::getVersion) - .map(ContentItemVersion::toString) + .map(ContentItemModelData::getVersion) .orElse(""); } public String getCreationDate() { return getOrRetrieveContentItem() - .map(ContentItem::getCreationDate) - .map(Date::toInstant) - .map(instant -> instant.atZone(ZoneId.systemDefault())) - .map(ZonedDateTime::toLocalDateTime) - .map(dateTimeFormatter::format) + .map(ContentItemModelData::getCreationDate) .orElse(""); } public String getLastModified() { return getOrRetrieveContentItem() - .map(ContentItem::getLastModified) - .map(Date::toInstant) - .map(instant -> instant.atZone(ZoneId.systemDefault())) - .map(ZonedDateTime::toLocalDateTime) - .map(dateTimeFormatter::format) + .map(ContentItemModelData::getLastModified) .orElse(""); } public String getCreationUser() { return getOrRetrieveContentItem() - .map(ContentItem::getCreationUserName) + .map(ContentItemModelData::getCreationUser) .orElse(""); } public String getLastModifyingUserName() { return getOrRetrieveContentItem() - .map(ContentItem::getLastModifyingUserName) + .map(ContentItemModelData::getLastModifyingUserName) .orElse(""); } - public List getAttachments() { - return getContentItem().getAttachments(); - } - - private Optional getOrRetrieveContentItem() { +// public List getAttachments() { +// throw new UnsupportedOperationException("Not implemented yet."); +// } +// private Optional getOrRetrieveContentItem() { +// if (contentItem == null) { +// retrieveContentItem(); +// } +// return contentItem; +// } + @Transactional(Transactional.TxType.REQUIRED) + private Optional getOrRetrieveContentItem() { if (contentItem == null) { - retrieveContentItem(); + contentItem = retrieveContentItem().map(this::buildModelData); } return contentItem; } - private void retrieveContentItem() { - if (itemName == null) { - contentItem = pagesService.findIndexItem( + @Transactional(Transactional.TxType.REQUIRED) + protected Optional retrieveContentItem() { + final Optional item; + if (itemName == null || "index".equals(itemName)) { + item = pagesService.findIndexItem( categoryRepository - .findById(categoryModel.getCategory().getCategoryId()) - .orElseThrow( - () -> new RuntimeException( - String.format( - "The category with the ID %d is set as current " - + "category, but not category with that ID " - + "can be found in the database.", - categoryModel.getCategory().getCategoryId() + .findById(categoryModel.getCategory().getCategoryId()) + .orElseThrow( + () -> new RuntimeException( + String.format( + "The category with the ID %d is set as current " + + "category, but not category with that ID " + + "can be found in the database.", + categoryModel.getCategory().getCategoryId() + ) ) - ) - ), + ), itemVersion ); } else { - contentItem = pagesService.findCategorizedItem( + item = pagesService.findCategorizedItem( categoryRepository - .findById(categoryModel.getCategory().getCategoryId()) - .orElseThrow( - () -> new RuntimeException( - String.format( - "The category with the ID %d is set as current " - + "category, but not category with that ID " - + "can be found in the database.", - categoryModel.getCategory().getCategoryId() + .findById(categoryModel.getCategory().getCategoryId()) + .orElseThrow( + () -> new RuntimeException( + String.format( + "The category with the ID %d is set as current " + + "category, but not category with that ID " + + "can be found in the database.", + categoryModel.getCategory().getCategoryId() + ) ) - ) - ), - itemName, + ), + itemName, itemVersion ); } + + return item; } + + private ContentItemModelData buildModelData(final ContentItem item) { + final ContentItemModelData data = new ContentItemModelData(); + data.setObjectId(item.getObjectId()); + data.setUuid(item.getUuid()); + data.setDisplayName(item.getDisplayName()); + data.setItemUuid(item.getItemUuid()); + data.setName( + globalizationHelper.getValueFromLocalizedString( + item.getName() + ) + ); + data.setTitle( + globalizationHelper.getValueFromLocalizedString( + item.getTitle() + ) + ); + data.setDescription( + globalizationHelper.getValueFromLocalizedString( + item.getDescription() + ) + ); + data.setVersion(item.getVersion().toString()); + data.setCreationDate( + dateTimeFormatter.format( + item + .getCreationDate() + .toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDateTime() + ) + ); + data.setLastModified( + dateTimeFormatter.format( + item + .getLastModified() + .toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDateTime() + ) + ); + data.setCreationUser(item.getCreationUserName()); + data.setLastModifyingUserName(item.getLastModifyingUserName()); + + //ToDo Attachments + return data; + } + + private class ContentItemModelData { + + private long objectId; + + private String uuid; + + private String displayName; + + private String itemUuid; + + private String name; + + private String title; + + private String description; + + private String version; + + private String creationDate; + + private String lastModified; + + private String creationUser; + + private String lastModifyingUserName; + + public long getObjectId() { + return objectId; + } + + public void setObjectId(final long objectId) { + this.objectId = objectId; + } + + public String getUuid() { + return uuid; + } + + public void setUuid(final String uuid) { + this.uuid = uuid; + } + + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(final String displayName) { + this.displayName = displayName; + } + + public String getItemUuid() { + return itemUuid; + } + + public void setItemUuid(final String itemUuid) { + this.itemUuid = itemUuid; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public String getTitle() { + return title; + } + + public void setTitle(final String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(final String description) { + this.description = description; + } + + public String getVersion() { + return version; + } + + public void setVersion(final String version) { + this.version = version; + } + + public String getCreationDate() { + return creationDate; + } + + public void setCreationDate(final String creationDate) { + this.creationDate = creationDate; + } + + public String getLastModified() { + return lastModified; + } + + public void setLastModified(final String lastModified) { + this.lastModified = lastModified; + } + + public String getCreationUser() { + return creationUser; + } + + public void setCreationUser(final String creationUser) { + this.creationUser = creationUser; + } + + public String getLastModifyingUserName() { + return lastModifyingUserName; + } + + public void setLastModifyingUserName( + final String lastModifyingUserName + ) { + this.lastModifyingUserName = lastModifyingUserName; + } + +// private List getAttachments() { +// throw new UnsupportedOperationException("Not implemented yet."); +// } + } + } 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 e7d6914aa..81b4d10b9 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 @@ -27,7 +27,7 @@ import java.util.Optional; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.inject.Named; -import javax.swing.text.AbstractDocument.Content; +import javax.transaction.Transactional; /** * MVC model for retrieving information about the content type of the current @@ -51,55 +51,124 @@ public class ContentItemTypeModel { @Inject private GlobalizationHelper globalizationHelper; - private Optional contentType; - - public ContentType getContentType() { - return getOrRetrieveContentType().orElse(null); - } + private Optional contentType; public long getContentTypeId() { return getOrRetrieveContentType() - .map(ContentType::getObjectId) + .map(ContentItemTypeModelData::getTypeId) .orElse(0L); } public String getUuid() { return getOrRetrieveContentType() - .map(ContentType::getUuid) + .map(ContentItemTypeModelData::getUuid) .orElse(""); } public String getDisplayName() { return getOrRetrieveContentType() - .map(ContentType::getDisplayName) + .map(ContentItemTypeModelData::getDisplayName) .orElse(""); } public String getLabel() { return getOrRetrieveContentType() - .map(ContentType::getLabel) - .map(globalizationHelper::getValueFromLocalizedString) + .map(ContentItemTypeModelData::getLabel) .orElse(""); } public String getDescription() { return getOrRetrieveContentType() - .map(ContentType::getDescription) - .map(globalizationHelper::getValueFromLocalizedString) + .map(ContentItemTypeModelData::getDescription) .orElse(""); } - private Optional getOrRetrieveContentType() { + private Optional getOrRetrieveContentType() { if (contentType == null) { retrieveContentType(); } return contentType; } + @Transactional(Transactional.TxType.REQUIRED) private void retrieveContentType() { - contentType = Optional - .ofNullable(contentItemModel.getContentItem()) - .map(ContentItem::getContentType); + contentType = contentItemModel.retrieveContentItem() + .map(ContentItem::getContentType) + .map(this::buildModelData); + + } + + @Transactional(Transactional.TxType.REQUIRED) + private ContentItemTypeModelData buildModelData(final ContentType type) { + final ContentItemTypeModelData data = new ContentItemTypeModelData(); + data.setDescription( + globalizationHelper.getValueFromLocalizedString( + type.getDescription() + ) + ); + data.setDisplayName(type.getDisplayName()); + data.setLabel( + globalizationHelper.getValueFromLocalizedString( + type.getLabel() + ) + ); + data.setTypeId(type.getObjectId()); + data.setUuid(type.getUuid()); + + return data; + } + + private class ContentItemTypeModelData { + + private long typeId; + + private String uuid; + + private String displayName; + + private String label; + + private String description; + + public long getTypeId() { + return typeId; + } + + public void setTypeId(final long typeId) { + this.typeId = typeId; + } + + public String getUuid() { + return uuid; + } + + public void setUuid(final String uuid) { + this.uuid = uuid; + } + + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(final String displayName) { + this.displayName = displayName; + } + + public String getLabel() { + return label; + } + + public void setLabel(final String label) { + this.label = label; + } + + public String getDescription() { + return description; + } + + public void setDescription(final String description) { + this.description = description; + } } 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 index 0c7f2cde6..50916ba79 100644 --- a/ccm-cms/src/main/java/org/librecms/pages/models/EventModel.java +++ b/ccm-cms/src/main/java/org/librecms/pages/models/EventModel.java @@ -24,10 +24,13 @@ import org.librecms.contenttypes.Event; import java.time.ZoneId; import java.time.format.DateTimeFormatter; +import java.util.Date; +import java.util.Optional; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.inject.Named; +import javax.transaction.Transactional; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; @@ -45,46 +48,120 @@ public class EventModel { @Inject private GlobalizationHelper globalizationHelper; + private final DateTimeFormatter isoDateTimeFormatter; + + private String title; + + private String text; + + private String startDateTime; + + private String endDateTime; + + private String eventDate; + + private String location; + + private String eventType; + + public EventModel() { + isoDateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME + .withZone(ZoneId.systemDefault()); + } + public String getTitle() { - return globalizationHelper.getValueFromLocalizedString( - getEvent().getTitle() - ); + if (title == null) { + init(); + } + + return title; } public String getText() { - return globalizationHelper.getValueFromLocalizedString( - getEvent().getText() - ); + if (text == null) { + init(); + } + + return text; } public String getStartDateTime() { - return DateTimeFormatter.ISO_DATE_TIME - .withZone(ZoneId.systemDefault()) - .format(getEvent().getStartDate().toInstant()); + if (startDateTime == null) { + init(); + } + + return startDateTime; } public String getEndDateTime() { - return DateTimeFormatter.ISO_DATE_TIME - .withZone(ZoneId.systemDefault()) - .format(getEvent().getEndDate().toInstant()); + if (endDateTime == null) { + init(); + } + + return endDateTime; } public String getEventDate() { - return globalizationHelper.getValueFromLocalizedString( - getEvent().getEventDate() - ); + if (eventDate == null) { + init(); + } + return eventDate; } public String getLocation() { - return globalizationHelper.getValueFromLocalizedString( - getEvent().getLocation() - ); + if (location == null) { + init(); + } + + return location; } - protected Event getEvent() { - final ContentItem contentItem = contentItemModel.getContentItem(); + public String getEventType() { + if (eventType == null) { + init(); + } + + return eventDate; + } + + @Transactional(Transactional.TxType.REQUIRED) + protected void init() { + final ContentItem contentItem = contentItemModel + .retrieveContentItem() + .orElse(null); if (contentItem instanceof Event) { - return (Event) contentItem; + final Event event = (Event) contentItem; + + title = Optional + .ofNullable(event.getTitle()) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(""); + text = Optional + .ofNullable(event.getText()) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(""); + startDateTime = Optional + .ofNullable(event.getStartDate()) + .map(Date::toInstant) + .map(isoDateTimeFormatter::format) + .orElse(""); + endDateTime = Optional + .ofNullable(event.getEndDate()) + .map(Date::toInstant) + .map(isoDateTimeFormatter::format) + .orElse(""); + eventDate = Optional + .ofNullable(event.getEventDate()) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(""); + location = Optional + .ofNullable(event.getLocation()) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(""); + eventType = Optional + .ofNullable(event.getEventType()) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(""); } else { throw new WebApplicationException( "Current content item is not an event", diff --git a/ccm-cms/src/main/java/org/librecms/pages/models/MultiPartArticleModel.java b/ccm-cms/src/main/java/org/librecms/pages/models/MultiPartArticleModel.java index 5d0ad91fe..c8247588d 100644 --- a/ccm-cms/src/main/java/org/librecms/pages/models/MultiPartArticleModel.java +++ b/ccm-cms/src/main/java/org/librecms/pages/models/MultiPartArticleModel.java @@ -23,12 +23,15 @@ import org.librecms.contentsection.ContentItem; import org.librecms.contenttypes.MultiPartArticle; import org.librecms.contenttypes.MultiPartArticleSection; +import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.inject.Named; +import javax.transaction.Transactional; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; @@ -49,83 +52,123 @@ public class MultiPartArticleModel { @Inject private PageUrlModel pageUrlModel; + private String title; + + private String summary; + + private List sectionTitles; + + private String currentSectionTitle; + + private String currentSectionText; + + private List sections; + public String getTitle() { - return globalizationHelper.getValueFromLocalizedString( - getMultiPartArticle().getTitle() - ); + if (title == null) { + init(); + } + + return title; } public String getSummary() { - return globalizationHelper.getValueFromLocalizedString( - getMultiPartArticle().getSummary() - ); + if (summary == null) { + return null; + } + + return summary; } public List getSectionTitles() { - return getMultiPartArticle() - .getSections() - .stream() - .map(MultiPartArticleSection::getTitle) - .map(globalizationHelper::getValueFromLocalizedString) - .collect(Collectors.toList()); + if (sectionTitles == null) { + init(); + } + + return Collections.unmodifiableList(sectionTitles); } public String getCurrentSectionTitle() { - final int currentSection = readCurrentSection(); - if (getMultiPartArticle().getSections().size() > currentSection) { - throw new WebApplicationException( - Response - .status(Response.Status.NOT_FOUND) - .entity( - String.format( - "MultiPartArticle %s has not section %d.", - getMultiPartArticle().getDisplayName(), - currentSection - ) - ) - .build() - ); + if (currentSectionTitle == null) { + init(); } - - return globalizationHelper.getValueFromLocalizedString( - getMultiPartArticle().getSections().get(currentSection).getTitle() - ); + + return currentSectionTitle; } public String getCurrentSectionText() { - final int currentSection = readCurrentSection(); - if (getMultiPartArticle().getSections().size() > currentSection) { - throw new WebApplicationException( - Response - .status(Response.Status.NOT_FOUND) - .entity( - String.format( - "MultiPartArticle %s has not section %d.", - getMultiPartArticle().getDisplayName(), - currentSection - ) - ) - .build() - ); + if (currentSectionText == null) { + init(); } - - return globalizationHelper.getValueFromLocalizedString( - getMultiPartArticle().getSections().get(currentSection).getText() - ); + + return currentSectionText; } public List getSections() { - return getMultiPartArticle() - .getSections() - .stream() - .map(this::buildSectionModel) - .collect(Collectors.toList()); + if (sections == null) { + init(); + } + + return Collections.unmodifiableList(sections); } - protected MultiPartArticle getMultiPartArticle() { - final ContentItem contentItem = contentItemModel.getContentItem(); + @Transactional(Transactional.TxType.REQUIRED) + protected void init() { + final ContentItem contentItem = contentItemModel + .retrieveContentItem() + .orElse(null); if (contentItem instanceof MultiPartArticle) { - return (MultiPartArticle) contentItem; + final MultiPartArticle mpa = (MultiPartArticle) contentItem; + + title = Optional + .ofNullable(mpa.getTitle()) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(""); + summary = Optional + .ofNullable(mpa.getSummary()) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(null); + sectionTitles = Optional + .ofNullable(mpa.getSections()) + .map( + mpaSections -> mpaSections + .stream() + .map(MultiPartArticleSection::getTitle) + .map(globalizationHelper::getValueFromLocalizedString) + .collect(Collectors.toList()) + ) + .orElse(Collections.emptyList()); + + final int currentSection = readCurrentSection(); + if (mpa.getSections().size() > currentSection) { + throw new WebApplicationException( + Response + .status(Response.Status.NOT_FOUND) + .entity( + String.format( + "MultiPartArticle %s has not section %d.", + mpa.getDisplayName(), + currentSection + ) + ) + .build() + ); + } + currentSectionTitle = Optional + .ofNullable(mpa.getSections().get(currentSection).getTitle()) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(""); + + currentSectionText = Optional + .ofNullable(mpa.getSections().get(currentSection).getText()) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(""); + + sections = mpa + .getSections() + .stream() + .map(this::buildSectionModel) + .collect(Collectors.toList()); } else { throw new WebApplicationException( "Current content item is not an MultiPartArticle", @@ -146,6 +189,7 @@ public class MultiPartArticleModel { } } + @Transactional(Transactional.TxType.REQUIRED) private MultiPartArticleSectionModel buildSectionModel( final MultiPartArticleSection fromSection ) { 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 index 1c3e963cc..97fd81fcd 100644 --- a/ccm-cms/src/main/java/org/librecms/pages/models/NewsModel.java +++ b/ccm-cms/src/main/java/org/librecms/pages/models/NewsModel.java @@ -26,10 +26,12 @@ 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; import javax.inject.Inject; import javax.inject.Named; +import javax.transaction.Transactional; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; @@ -46,38 +48,85 @@ public class NewsModel { @Inject private GlobalizationHelper globalizationHelper; + + private final DateTimeFormatter isoDateTimeFormatter; + + private String title; + + private String description; + + private String text; + + private String releaseDateTime; + + private boolean homepage; + + public NewsModel() { + isoDateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME + .withZone(ZoneId.systemDefault()); + } public String getTitle() { - return globalizationHelper - .getValueFromLocalizedString(getNews().getTitle()); + if (title == null) { + init(); + } + + return title; } public String getDescription() { - return globalizationHelper - .getValueFromLocalizedString(getNews().getDescription()); + if (description == null) { + init(); + } + + return description; } public String getText() { - return globalizationHelper - .getValueFromLocalizedString(getNews().getText()); + if (text == null) { + init(); + } + return text; } public String getReleaseDateTime() { - return DateTimeFormatter.ISO_DATE_TIME - .withZone(ZoneId.systemDefault()) - .format( - LocalDateTime.from(getNews().getReleaseDate().toInstant()) - ); + if (releaseDateTime == null) { + init(); + } + + return releaseDateTime; } public boolean getHomepage() { - return getNews().isHomepage(); + return homepage; } - protected News getNews() { - final ContentItem contentItem = contentItemModel.getContentItem(); + @Transactional(Transactional.TxType.REQUIRED) + protected void init() { + final ContentItem contentItem = contentItemModel + .retrieveContentItem() + .orElse(null); if (contentItem instanceof News) { - return (News) contentItem; + final News news = (News) contentItem; + + title = Optional + .ofNullable(news.getTitle()) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(""); + description = Optional + .ofNullable(news.getDescription()) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(""); + text = Optional + .ofNullable(news.getText()) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse(""); + releaseDateTime = Optional + .ofNullable(news.getReleaseDate()) + .map(Date::toInstant) + .map(isoDateTimeFormatter::format) + .orElse(""); + homepage = news.isHomepage(); } else { throw new WebApplicationException( "Current content item is not a news item.",