Models for content item lists

pull/10/head
Jens Pelzetter 2021-10-28 19:47:32 +02:00
parent 4305242079
commit 7cebc9a2bd
5 changed files with 321 additions and 0 deletions

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
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();
}

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
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();
}
}

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
public class ArticleListItemModelBuilder
implements ContentItemListItemModelBuilder<Article, ArticleListItemModel> {
@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<Article> buildsListItemModelFor() {
return Article.class;
}
}

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
* @param <T>
* @param <M>
*/
public interface ContentItemListItemModelBuilder<T extends ContentItem, M extends AbstractContentItemListItemModel> {
M buildListItemModel(T contentItem);
Class<T> buildsListItemModelFor();
}

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
public class ContentItemListItemModelBuilders {
@Inject
private Instance<ContentItemListItemModelBuilder<?, ?>> modelBuilders;
public Optional<ContentItemListItemModelBuilder<?, ?>> getModelBuilderFor(
final Class<? extends ContentItem> itemType
) {
final Optional<ContentItemListItemModelBuilder<?, ?>> exactMatch
= findExactType(itemType);
if (exactMatch.isPresent()) {
return exactMatch;
} else {
return findForParentType(itemType);
}
}
private Optional<ContentItemListItemModelBuilder<?, ?>> findExactType(
final Class<? extends ContentItem> itemType
) {
return modelBuilders
.stream()
.filter(modelBuilder -> modelBuilder.buildsListItemModelFor()
.equals(itemType))
.findAny();
}
private Optional<ContentItemListItemModelBuilder<?, ?>> findForParentType(
final Class<? extends ContentItem> itemType
) {
final Class<?> superClass = itemType.getSuperclass();
if (ContentItem.class.isAssignableFrom(superClass)) {
@SuppressWarnings("unchecked")
final Class<? extends ContentItem> superType
= (Class<? extends ContentItem>) superClass;
final Optional<ContentItemListItemModelBuilder<?, ?>> builderForSuperType
= findExactType(superType);
if (builderForSuperType.isPresent()) {
return builderForSuperType;
} else {
return findForParentType(superType);
}
} else {
return Optional.empty();
}
}
}