From 7cebc9a2bd9ef1bdcf7341c72b6d76f8e955f36d Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Thu, 28 Oct 2021 19:47:32 +0200 Subject: [PATCH] Models for content item lists --- .../AbstractContentItemListItemModel.java | 87 +++++++++++++++++++ .../pages/models/ArticleListItemModel.java | 43 +++++++++ .../models/ArticleListItemModelBuilder.java | 75 ++++++++++++++++ .../ContentItemListItemModelBuilder.java | 35 ++++++++ .../ContentItemListItemModelBuilders.java | 81 +++++++++++++++++ 5 files changed, 321 insertions(+) create mode 100644 ccm-cms/src/main/java/org/librecms/pages/models/AbstractContentItemListItemModel.java create mode 100644 ccm-cms/src/main/java/org/librecms/pages/models/ArticleListItemModel.java create mode 100644 ccm-cms/src/main/java/org/librecms/pages/models/ArticleListItemModelBuilder.java create mode 100644 ccm-cms/src/main/java/org/librecms/pages/models/ContentItemListItemModelBuilder.java create mode 100644 ccm-cms/src/main/java/org/librecms/pages/models/ContentItemListItemModelBuilders.java diff --git a/ccm-cms/src/main/java/org/librecms/pages/models/AbstractContentItemListItemModel.java b/ccm-cms/src/main/java/org/librecms/pages/models/AbstractContentItemListItemModel.java new file mode 100644 index 000000000..f2af5b253 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/pages/models/AbstractContentItemListItemModel.java @@ -0,0 +1,87 @@ +/* + * 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; + +/** + * A simplified representation of a content item for use in a list of content + * items.Base class for other more specific models. + * + * @author Jens Pelzetter + */ +public abstract class AbstractContentItemListItemModel { + + private String uuid; + + private String displayName; + + private String name; + + private String title; + + private String description; + + public String getUuid() { + return uuid; + } + + public void setUuid(final String uuid) { + this.uuid = uuid; + } + + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + 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; + } + + /** + * Returns the type of the content item. In most cases implementations + * should return the fully qualified name of the content item class here. + * + * @return The type of the content item. + */ + public abstract String getType(); + +} diff --git a/ccm-cms/src/main/java/org/librecms/pages/models/ArticleListItemModel.java b/ccm-cms/src/main/java/org/librecms/pages/models/ArticleListItemModel.java new file mode 100644 index 000000000..b4d82a222 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/pages/models/ArticleListItemModel.java @@ -0,0 +1,43 @@ +/* + * 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.librecms.contenttypes.Article; + +/** + * + * @author Jens Pelzetter + */ +public class ArticleListItemModel extends AbstractContentItemListItemModel { + + private String text; + + public String getText() { + return text; + } + + public void setText(final String text) { + this.text = text; + } + + public String getType() { + return Article.class.getName(); + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/pages/models/ArticleListItemModelBuilder.java b/ccm-cms/src/main/java/org/librecms/pages/models/ArticleListItemModelBuilder.java new file mode 100644 index 000000000..8b68a8b10 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/pages/models/ArticleListItemModelBuilder.java @@ -0,0 +1,75 @@ +/* + * 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.contenttypes.Article; + +import java.util.Objects; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +public class ArticleListItemModelBuilder + implements ContentItemListItemModelBuilder { + + @Inject + private GlobalizationHelper globalizationHelper; + + @Override + public ArticleListItemModel buildListItemModel(final Article article) { + Objects.requireNonNull(article); + + final ArticleListItemModel model = new ArticleListItemModel(); + model.setDescription( + globalizationHelper.getValueFromLocalizedString( + article.getDescription() + ) + ); + model.setName( + globalizationHelper.getValueFromLocalizedString( + article.getName() + ) + ); + model.setDisplayName(article.getDisplayName()); + model.setText( + globalizationHelper.getValueFromLocalizedString( + article.getText() + ) + ); + model.setTitle( + globalizationHelper.getValueFromLocalizedString( + article.getTitle() + ) + ); + + return model; + } + + @Override + public Class
buildsListItemModelFor() { + return Article.class; + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/pages/models/ContentItemListItemModelBuilder.java b/ccm-cms/src/main/java/org/librecms/pages/models/ContentItemListItemModelBuilder.java new file mode 100644 index 000000000..0a2ee683b --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/pages/models/ContentItemListItemModelBuilder.java @@ -0,0 +1,35 @@ +/* + * 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.librecms.contentsection.ContentItem; + +/** + * + * @author Jens Pelzetter + * @param + * @param + */ +public interface ContentItemListItemModelBuilder { + + M buildListItemModel(T contentItem); + + Class buildsListItemModelFor(); + +} diff --git a/ccm-cms/src/main/java/org/librecms/pages/models/ContentItemListItemModelBuilders.java b/ccm-cms/src/main/java/org/librecms/pages/models/ContentItemListItemModelBuilders.java new file mode 100644 index 000000000..6beb7c131 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/pages/models/ContentItemListItemModelBuilders.java @@ -0,0 +1,81 @@ +/* + * 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.librecms.contentsection.ContentItem; + +import java.util.Optional; + +import javax.enterprise.context.RequestScoped; +import javax.enterprise.inject.Instance; +import javax.inject.Inject; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +public class ContentItemListItemModelBuilders { + + @Inject + private Instance> modelBuilders; + + public Optional> getModelBuilderFor( + final Class itemType + ) { + final Optional> exactMatch + = findExactType(itemType); + if (exactMatch.isPresent()) { + return exactMatch; + } else { + return findForParentType(itemType); + } + } + + private Optional> findExactType( + final Class itemType + ) { + return modelBuilders + .stream() + .filter(modelBuilder -> modelBuilder.buildsListItemModelFor() + .equals(itemType)) + .findAny(); + } + + private Optional> findForParentType( + final Class itemType + ) { + final Class superClass = itemType.getSuperclass(); + if (ContentItem.class.isAssignableFrom(superClass)) { + @SuppressWarnings("unchecked") + final Class superType + = (Class) superClass; + final Optional> builderForSuperType + = findExactType(superType); + if (builderForSuperType.isPresent()) { + return builderForSuperType; + } else { + return findForParentType(superType); + } + } else { + return Optional.empty(); + } + } + +}