Bugfixes for models

pull/20/head
Jens Pelzetter 2022-01-26 20:37:26 +01:00
parent 2cd477eae2
commit 56e53e6205
6 changed files with 627 additions and 202 deletions

View File

@ -22,9 +22,12 @@ import org.libreccm.l10n.GlobalizationHelper;
import org.librecms.contentsection.ContentItem; import org.librecms.contentsection.ContentItem;
import org.librecms.contenttypes.Article; import org.librecms.contenttypes.Article;
import java.util.Optional;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.transaction.Transactional;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
@ -43,26 +46,54 @@ public class ArticleModel {
@Inject @Inject
private GlobalizationHelper globalizationHelper; private GlobalizationHelper globalizationHelper;
private String title;
private String description;
private String text;
public String getTitle() { public String getTitle() {
return globalizationHelper if (title == null) {
.getValueFromLocalizedString(getArticle().getTitle()); init();
}
return title;
} }
public String getDescription() { public String getDescription() {
return globalizationHelper if (description == null) {
.getValueFromLocalizedString(getArticle().getDescription()); init();
}
return description;
} }
public String getText() { public String getText() {
return globalizationHelper if (text == null) {
.getValueFromLocalizedString(getArticle().getText()); init();
}
return text;
} }
protected Article getArticle() { @Transactional(Transactional.TxType.REQUIRED)
final ContentItem contentItem = contentItemModel.getContentItem(); protected void init() {
final ContentItem contentItem = contentItemModel
.retrieveContentItem()
.orElse(null);
if (contentItem instanceof Article) { 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 { } else {
throw new WebApplicationException( throw new WebApplicationException(
"Current content item is not an article.", "Current content item is not an article.",

View File

@ -23,23 +23,20 @@ import org.apache.logging.log4j.Logger;
import org.libreccm.categorization.CategoryManager; import org.libreccm.categorization.CategoryManager;
import org.libreccm.categorization.CategoryRepository; import org.libreccm.categorization.CategoryRepository;
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.PagesService; import org.librecms.pages.PagesService;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.persistence.EntityManager; 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 * Retrieves a categorized content item for the current category. To work, the
@ -59,7 +56,7 @@ public class ContentItemModel {
@Inject @Inject
private CategoryManager categoryManager; private CategoryManager categoryManager;
@Inject @Inject
private CategoryRepository categoryRepository; private CategoryRepository categoryRepository;
@ -82,7 +79,8 @@ public class ContentItemModel {
private ContentItemVersion itemVersion; private ContentItemVersion itemVersion;
private Optional<ContentItem> contentItem; // private Optional<ContentItem> contentItem;
private Optional<ContentItemModelData> contentItem;
public String getItemName() { public String getItemName() {
return itemName; return itemName;
@ -100,157 +98,314 @@ public class ContentItemModel {
this.itemVersion = itemVersion; this.itemVersion = itemVersion;
} }
/** public boolean isItemAvailable() {
* Retrieves the current content item. Depending if {@link #itemName} has return getOrRetrieveContentItem().isPresent();
* 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 long getObjectId() { public long getObjectId() {
return getOrRetrieveContentItem() return getOrRetrieveContentItem()
.map(ContentItem::getObjectId) .map(ContentItemModelData::getObjectId)
.orElse(0L); .orElse(0L);
} }
public String getUuid() { public String getUuid() {
return getOrRetrieveContentItem() return getOrRetrieveContentItem()
.map(ContentItem::getUuid) .map(ContentItemModelData::getUuid)
.orElse(""); .orElse("");
} }
public String getDisplayName() { public String getDisplayName() {
return getOrRetrieveContentItem() return getOrRetrieveContentItem()
.map(ContentItem::getDisplayName) .map(ContentItemModelData::getDisplayName)
.orElse(""); .orElse("");
} }
public String getItemUuid() { public String getItemUuid() {
return getOrRetrieveContentItem() return getOrRetrieveContentItem()
.map(ContentItem::getItemUuid) .map(ContentItemModelData::getItemUuid)
.orElse(""); .orElse("");
} }
public String getName() { public String getName() {
return getOrRetrieveContentItem() return getOrRetrieveContentItem()
.map(ContentItem::getName) .map(ContentItemModelData::getName)
.map(globalizationHelper::getValueFromLocalizedString)
.orElse(""); .orElse("");
} }
public String getTitle() { public String getTitle() {
return getOrRetrieveContentItem() return getOrRetrieveContentItem()
.map(ContentItem::getTitle) .map(ContentItemModelData::getTitle)
.map(globalizationHelper::getValueFromLocalizedString)
.orElse(""); .orElse("");
} }
public String getDescription() { public String getDescription() {
return getOrRetrieveContentItem() return getOrRetrieveContentItem()
.map(ContentItem::getDescription) .map(ContentItemModelData::getDescription)
.map(globalizationHelper::getValueFromLocalizedString)
.orElse(""); .orElse("");
} }
public String getVersion() { public String getVersion() {
return getOrRetrieveContentItem() return getOrRetrieveContentItem()
.map(ContentItem::getVersion) .map(ContentItemModelData::getVersion)
.map(ContentItemVersion::toString)
.orElse(""); .orElse("");
} }
public String getCreationDate() { public String getCreationDate() {
return getOrRetrieveContentItem() return getOrRetrieveContentItem()
.map(ContentItem::getCreationDate) .map(ContentItemModelData::getCreationDate)
.map(Date::toInstant)
.map(instant -> instant.atZone(ZoneId.systemDefault()))
.map(ZonedDateTime::toLocalDateTime)
.map(dateTimeFormatter::format)
.orElse(""); .orElse("");
} }
public String getLastModified() { public String getLastModified() {
return getOrRetrieveContentItem() return getOrRetrieveContentItem()
.map(ContentItem::getLastModified) .map(ContentItemModelData::getLastModified)
.map(Date::toInstant)
.map(instant -> instant.atZone(ZoneId.systemDefault()))
.map(ZonedDateTime::toLocalDateTime)
.map(dateTimeFormatter::format)
.orElse(""); .orElse("");
} }
public String getCreationUser() { public String getCreationUser() {
return getOrRetrieveContentItem() return getOrRetrieveContentItem()
.map(ContentItem::getCreationUserName) .map(ContentItemModelData::getCreationUser)
.orElse(""); .orElse("");
} }
public String getLastModifyingUserName() { public String getLastModifyingUserName() {
return getOrRetrieveContentItem() return getOrRetrieveContentItem()
.map(ContentItem::getLastModifyingUserName) .map(ContentItemModelData::getLastModifyingUserName)
.orElse(""); .orElse("");
} }
public List<AttachmentList> getAttachments() { // public List<AttachmentList> getAttachments() {
return getContentItem().getAttachments(); // throw new UnsupportedOperationException("Not implemented yet.");
} // }
// private Optional<ContentItem> getOrRetrieveContentItem() {
private Optional<ContentItem> getOrRetrieveContentItem() { // if (contentItem == null) {
// retrieveContentItem();
// }
// return contentItem;
// }
@Transactional(Transactional.TxType.REQUIRED)
private Optional<ContentItemModelData> getOrRetrieveContentItem() {
if (contentItem == null) { if (contentItem == null) {
retrieveContentItem(); contentItem = retrieveContentItem().map(this::buildModelData);
} }
return contentItem; return contentItem;
} }
private void retrieveContentItem() { @Transactional(Transactional.TxType.REQUIRED)
if (itemName == null) { protected Optional<ContentItem> retrieveContentItem() {
contentItem = pagesService.findIndexItem( final Optional<ContentItem> item;
if (itemName == null || "index".equals(itemName)) {
item = pagesService.findIndexItem(
categoryRepository categoryRepository
.findById(categoryModel.getCategory().getCategoryId()) .findById(categoryModel.getCategory().getCategoryId())
.orElseThrow( .orElseThrow(
() -> new RuntimeException( () -> new RuntimeException(
String.format( String.format(
"The category with the ID %d is set as current " "The category with the ID %d is set as current "
+ "category, but not category with that ID " + "category, but not category with that ID "
+ "can be found in the database.", + "can be found in the database.",
categoryModel.getCategory().getCategoryId() categoryModel.getCategory().getCategoryId()
)
) )
) ),
),
itemVersion itemVersion
); );
} else { } else {
contentItem = pagesService.findCategorizedItem( item = pagesService.findCategorizedItem(
categoryRepository categoryRepository
.findById(categoryModel.getCategory().getCategoryId()) .findById(categoryModel.getCategory().getCategoryId())
.orElseThrow( .orElseThrow(
() -> new RuntimeException( () -> new RuntimeException(
String.format( String.format(
"The category with the ID %d is set as current " "The category with the ID %d is set as current "
+ "category, but not category with that ID " + "category, but not category with that ID "
+ "can be found in the database.", + "can be found in the database.",
categoryModel.getCategory().getCategoryId() categoryModel.getCategory().getCategoryId()
)
) )
) ),
), itemName,
itemName,
itemVersion 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<AttachmentList> getAttachments() {
// throw new UnsupportedOperationException("Not implemented yet.");
// }
}
} }

View File

@ -27,7 +27,7 @@ import java.util.Optional;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; 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 * MVC model for retrieving information about the content type of the current
@ -51,55 +51,124 @@ public class ContentItemTypeModel {
@Inject @Inject
private GlobalizationHelper globalizationHelper; private GlobalizationHelper globalizationHelper;
private Optional<ContentType> contentType; private Optional<ContentItemTypeModelData> contentType;
public ContentType getContentType() {
return getOrRetrieveContentType().orElse(null);
}
public long getContentTypeId() { public long getContentTypeId() {
return getOrRetrieveContentType() return getOrRetrieveContentType()
.map(ContentType::getObjectId) .map(ContentItemTypeModelData::getTypeId)
.orElse(0L); .orElse(0L);
} }
public String getUuid() { public String getUuid() {
return getOrRetrieveContentType() return getOrRetrieveContentType()
.map(ContentType::getUuid) .map(ContentItemTypeModelData::getUuid)
.orElse(""); .orElse("");
} }
public String getDisplayName() { public String getDisplayName() {
return getOrRetrieveContentType() return getOrRetrieveContentType()
.map(ContentType::getDisplayName) .map(ContentItemTypeModelData::getDisplayName)
.orElse(""); .orElse("");
} }
public String getLabel() { public String getLabel() {
return getOrRetrieveContentType() return getOrRetrieveContentType()
.map(ContentType::getLabel) .map(ContentItemTypeModelData::getLabel)
.map(globalizationHelper::getValueFromLocalizedString)
.orElse(""); .orElse("");
} }
public String getDescription() { public String getDescription() {
return getOrRetrieveContentType() return getOrRetrieveContentType()
.map(ContentType::getDescription) .map(ContentItemTypeModelData::getDescription)
.map(globalizationHelper::getValueFromLocalizedString)
.orElse(""); .orElse("");
} }
private Optional<ContentType> getOrRetrieveContentType() { private Optional<ContentItemTypeModelData> getOrRetrieveContentType() {
if (contentType == null) { if (contentType == null) {
retrieveContentType(); retrieveContentType();
} }
return contentType; return contentType;
} }
@Transactional(Transactional.TxType.REQUIRED)
private void retrieveContentType() { private void retrieveContentType() {
contentType = Optional contentType = contentItemModel.retrieveContentItem()
.ofNullable(contentItemModel.getContentItem()) .map(ContentItem::getContentType)
.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;
}
} }

View File

@ -24,10 +24,13 @@ import org.librecms.contenttypes.Event;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Optional;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.transaction.Transactional;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
@ -45,46 +48,120 @@ public class EventModel {
@Inject @Inject
private GlobalizationHelper globalizationHelper; 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() { public String getTitle() {
return globalizationHelper.getValueFromLocalizedString( if (title == null) {
getEvent().getTitle() init();
); }
return title;
} }
public String getText() { public String getText() {
return globalizationHelper.getValueFromLocalizedString( if (text == null) {
getEvent().getText() init();
); }
return text;
} }
public String getStartDateTime() { public String getStartDateTime() {
return DateTimeFormatter.ISO_DATE_TIME if (startDateTime == null) {
.withZone(ZoneId.systemDefault()) init();
.format(getEvent().getStartDate().toInstant()); }
return startDateTime;
} }
public String getEndDateTime() { public String getEndDateTime() {
return DateTimeFormatter.ISO_DATE_TIME if (endDateTime == null) {
.withZone(ZoneId.systemDefault()) init();
.format(getEvent().getEndDate().toInstant()); }
return endDateTime;
} }
public String getEventDate() { public String getEventDate() {
return globalizationHelper.getValueFromLocalizedString( if (eventDate == null) {
getEvent().getEventDate() init();
); }
return eventDate;
} }
public String getLocation() { public String getLocation() {
return globalizationHelper.getValueFromLocalizedString( if (location == null) {
getEvent().getLocation() init();
); }
return location;
} }
protected Event getEvent() { public String getEventType() {
final ContentItem contentItem = contentItemModel.getContentItem(); if (eventType == null) {
init();
}
return eventDate;
}
@Transactional(Transactional.TxType.REQUIRED)
protected void init() {
final ContentItem contentItem = contentItemModel
.retrieveContentItem()
.orElse(null);
if (contentItem instanceof Event) { 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 { } else {
throw new WebApplicationException( throw new WebApplicationException(
"Current content item is not an event", "Current content item is not an event",

View File

@ -23,12 +23,15 @@ import org.librecms.contentsection.ContentItem;
import org.librecms.contenttypes.MultiPartArticle; import org.librecms.contenttypes.MultiPartArticle;
import org.librecms.contenttypes.MultiPartArticleSection; import org.librecms.contenttypes.MultiPartArticleSection;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.transaction.Transactional;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
@ -49,83 +52,123 @@ public class MultiPartArticleModel {
@Inject @Inject
private PageUrlModel pageUrlModel; private PageUrlModel pageUrlModel;
private String title;
private String summary;
private List<String> sectionTitles;
private String currentSectionTitle;
private String currentSectionText;
private List<MultiPartArticleSectionModel> sections;
public String getTitle() { public String getTitle() {
return globalizationHelper.getValueFromLocalizedString( if (title == null) {
getMultiPartArticle().getTitle() init();
); }
return title;
} }
public String getSummary() { public String getSummary() {
return globalizationHelper.getValueFromLocalizedString( if (summary == null) {
getMultiPartArticle().getSummary() return null;
); }
return summary;
} }
public List<String> getSectionTitles() { public List<String> getSectionTitles() {
return getMultiPartArticle() if (sectionTitles == null) {
.getSections() init();
.stream() }
.map(MultiPartArticleSection::getTitle)
.map(globalizationHelper::getValueFromLocalizedString) return Collections.unmodifiableList(sectionTitles);
.collect(Collectors.toList());
} }
public String getCurrentSectionTitle() { public String getCurrentSectionTitle() {
final int currentSection = readCurrentSection(); if (currentSectionTitle == null) {
if (getMultiPartArticle().getSections().size() > currentSection) { init();
throw new WebApplicationException(
Response
.status(Response.Status.NOT_FOUND)
.entity(
String.format(
"MultiPartArticle %s has not section %d.",
getMultiPartArticle().getDisplayName(),
currentSection
)
)
.build()
);
} }
return globalizationHelper.getValueFromLocalizedString( return currentSectionTitle;
getMultiPartArticle().getSections().get(currentSection).getTitle()
);
} }
public String getCurrentSectionText() { public String getCurrentSectionText() {
final int currentSection = readCurrentSection(); if (currentSectionText == null) {
if (getMultiPartArticle().getSections().size() > currentSection) { init();
throw new WebApplicationException(
Response
.status(Response.Status.NOT_FOUND)
.entity(
String.format(
"MultiPartArticle %s has not section %d.",
getMultiPartArticle().getDisplayName(),
currentSection
)
)
.build()
);
} }
return globalizationHelper.getValueFromLocalizedString( return currentSectionText;
getMultiPartArticle().getSections().get(currentSection).getText()
);
} }
public List<MultiPartArticleSectionModel> getSections() { public List<MultiPartArticleSectionModel> getSections() {
return getMultiPartArticle() if (sections == null) {
.getSections() init();
.stream() }
.map(this::buildSectionModel)
.collect(Collectors.toList()); return Collections.unmodifiableList(sections);
} }
protected MultiPartArticle getMultiPartArticle() { @Transactional(Transactional.TxType.REQUIRED)
final ContentItem contentItem = contentItemModel.getContentItem(); protected void init() {
final ContentItem contentItem = contentItemModel
.retrieveContentItem()
.orElse(null);
if (contentItem instanceof MultiPartArticle) { 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 { } else {
throw new WebApplicationException( throw new WebApplicationException(
"Current content item is not an MultiPartArticle", "Current content item is not an MultiPartArticle",
@ -146,6 +189,7 @@ public class MultiPartArticleModel {
} }
} }
@Transactional(Transactional.TxType.REQUIRED)
private MultiPartArticleSectionModel buildSectionModel( private MultiPartArticleSectionModel buildSectionModel(
final MultiPartArticleSection fromSection final MultiPartArticleSection fromSection
) { ) {

View File

@ -26,10 +26,12 @@ import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
import java.util.Optional;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.transaction.Transactional;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
@ -46,38 +48,85 @@ public class NewsModel {
@Inject @Inject
private GlobalizationHelper globalizationHelper; 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() { public String getTitle() {
return globalizationHelper if (title == null) {
.getValueFromLocalizedString(getNews().getTitle()); init();
}
return title;
} }
public String getDescription() { public String getDescription() {
return globalizationHelper if (description == null) {
.getValueFromLocalizedString(getNews().getDescription()); init();
}
return description;
} }
public String getText() { public String getText() {
return globalizationHelper if (text == null) {
.getValueFromLocalizedString(getNews().getText()); init();
}
return text;
} }
public String getReleaseDateTime() { public String getReleaseDateTime() {
return DateTimeFormatter.ISO_DATE_TIME if (releaseDateTime == null) {
.withZone(ZoneId.systemDefault()) init();
.format( }
LocalDateTime.from(getNews().getReleaseDate().toInstant())
); return releaseDateTime;
} }
public boolean getHomepage() { public boolean getHomepage() {
return getNews().isHomepage(); return homepage;
} }
protected News getNews() { @Transactional(Transactional.TxType.REQUIRED)
final ContentItem contentItem = contentItemModel.getContentItem(); protected void init() {
final ContentItem contentItem = contentItemModel
.retrieveContentItem()
.orElse(null);
if (contentItem instanceof News) { 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 { } else {
throw new WebApplicationException( throw new WebApplicationException(
"Current content item is not a news item.", "Current content item is not a news item.",