Include attachments into ContentItemModel

pull/20/head
Jens Pelzetter 2022-01-29 17:12:14 +01:00
parent 661c70073f
commit 79c85df4a3
6 changed files with 305 additions and 18 deletions

View File

@ -30,14 +30,23 @@ import javax.inject.Inject;
* @param <M>
*/
public abstract class AbstractAssetModelBuilder<T extends Asset, M extends AbstractAssetModel>
implements AssetModelBuilder<T, M> {
implements AssetModelBuilder<T, M> {
@Inject
private GlobalizationHelper globalizationHelper;
@Override
@SuppressWarnings("unchecked")
public M buildAssetModel(final T asset) {
public M buildAssetModel(final Asset asset) {
if (!asset.getClass().isAssignableFrom(buildsAssetModelFor())) {
throw new IllegalArgumentException(
String.format(
"This builder can only process Assets of type %s.",
buildsAssetModelFor().getName()
)
);
}
final M model = buildModel();
model.setDisplayName(asset.getDisplayName());
model.setTitle(
@ -45,7 +54,7 @@ implements AssetModelBuilder<T, M> {
);
model.setUuid(asset.getUuid());
addProperties(asset, model);
addProperties((T) asset, model);
return model;
}

View File

@ -22,7 +22,6 @@ import org.libreccm.l10n.GlobalizationHelper;
import org.librecms.assets.BinaryAsset;
import org.librecms.contentsection.AssetManager;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
@ -32,7 +31,6 @@ import javax.transaction.Transactional;
* @param <T>
* @param <M>
*/
@RequestScoped
public abstract class AbstractBinaryAssetModelBuilder<T extends BinaryAsset, M extends BinaryAssetModel>
extends AbstractAssetModelBuilder<T, M> {

View File

@ -28,7 +28,7 @@ import org.librecms.contentsection.Asset;
*/
public interface AssetModelBuilder<T extends Asset, M extends AbstractAssetModel> {
M buildAssetModel(T asset);
M buildAssetModel(Asset asset);
Class<T> buildsAssetModelFor();

View File

@ -0,0 +1,112 @@
/*
* Copyright (C) 2022 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 java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class AttachmentListModel implements Comparable<AttachmentListModel>{
private long listId;
private String uuid;
private String name;
private long listOrder;
private String title;
private String description;
private List<AttachmentModel> attachments;
public long getListId() {
return listId;
}
public void setListId(final long listId) {
this.listId = listId;
}
public String getUuid() {
return uuid;
}
public void setUuid(final String uuid) {
this.uuid = uuid;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public long getListOrder() {
return listOrder;
}
public void setListOrder(final long listOrder) {
this.listOrder = listOrder;
}
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 List<AttachmentModel> getAttachments() {
return Collections.unmodifiableList(attachments);
}
public void setAttachments(final List<AttachmentModel> attachments) {
this.attachments = new ArrayList<>(attachments);
}
@Override
public int compareTo(final AttachmentListModel other) {
return Comparator
.comparing(AttachmentListModel::getListOrder)
.thenComparing(
AttachmentListModel::getName, String::compareToIgnoreCase
)
.compare(this, other);
}
}

View File

@ -0,0 +1,77 @@
/*
* Copyright (C) 2022 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 java.util.Comparator;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
public class AttachmentModel implements Comparable<AttachmentModel>{
private long attachmentId;
private String uuid;
private long sortKey;
private AbstractAssetModel asset;
public long getAttachmentId() {
return attachmentId;
}
public void setAttachmentId(final long attachmentId) {
this.attachmentId = attachmentId;
}
public String getUuid() {
return uuid;
}
public void setUuid(final String uuid) {
this.uuid = uuid;
}
public long getSortKey() {
return sortKey;
}
public void setSortKey(final long sortKey) {
this.sortKey = sortKey;
}
public AbstractAssetModel getAsset() {
return asset;
}
public void setAsset(final AbstractAssetModel asset) {
this.asset = asset;
}
@Override
public int compareTo(final AttachmentModel other) {
return Comparator
.comparing(AttachmentModel::getSortKey)
.thenComparing(AttachmentModel::getAttachmentId)
.compare(this, other);
}
}

View File

@ -21,22 +21,31 @@ package org.librecms.pages.models;
import org.libreccm.categorization.CategoryRepository;
import org.libreccm.core.CcmObject;
import org.libreccm.l10n.GlobalizationHelper;
import org.librecms.contentsection.Asset;
import org.librecms.contentsection.AttachmentList;
import org.librecms.contentsection.ContentItem;
import org.librecms.contentsection.ContentItemVersion;
import org.librecms.contentsection.ItemAttachment;
import org.librecms.pages.PagesController;
import org.librecms.pages.PagesRouter;
import org.librecms.pages.PagesService;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.inject.Named;
import javax.transaction.Transactional;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
/**
* Retrieves a categorized content item for the current category. To work, the
@ -50,6 +59,11 @@ import javax.transaction.Transactional;
@Named("CmsPagesCategorizedItemModel")
public class ContentItemModel {
/**
* Provides access to the builders for asset models.
*/
private AssetModelBuilders assetModelBuilders;
/**
* Category repository used to retrieve the current category.
*/
@ -483,10 +497,79 @@ public class ContentItemModel {
data.setCreationUser(item.getCreationUserName());
data.setLastModifyingUserName(item.getLastModifyingUserName());
//ToDo Attachments
data.setAttachmentLists(
item
.getAttachments()
.stream()
.map(this::buildAttachmentListModel)
.sorted()
.collect(Collectors.toList())
);
return data;
}
private AttachmentListModel buildAttachmentListModel(
final AttachmentList attachmentList
) {
final AttachmentListModel model = new AttachmentListModel();
model.setAttachments(
attachmentList
.getAttachments()
.stream()
.map(this::buildAttachmentModel)
.sorted()
.collect(Collectors.toList())
);
model.setDescription(
globalizationHelper.getValueFromLocalizedString(
attachmentList.getDescription()
)
);
model.setListId(attachmentList.getListId());
model.setListOrder(attachmentList.getListOrder());
model.setName(attachmentList.getName());
model.setTitle(
globalizationHelper.getValueFromLocalizedString(
attachmentList.getTitle()
)
);
model.setUuid(attachmentList.getUuid());
return model;
}
private AttachmentModel buildAttachmentModel(
final ItemAttachment<?> attachment
) {
final AttachmentModel model = new AttachmentModel();
final Asset asset = attachment.getAsset();
final AssetModelBuilder<?, ?> assetModelBuilder
= assetModelBuilders
.getModelBuilderFor(asset.getClass())
.orElseThrow(
() -> new WebApplicationException(
Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(
String.format(
"Unknown asset type %s.",
asset.getClass().getName()
)
)
.build()
)
);
model.setAsset(
assetModelBuilder.buildAssetModel(attachment.getAsset())
);
model.setAttachmentId(attachment.getAttachmentId());
model.setSortKey(attachment.getSortKey());
model.setUuid(attachment.getUuid());
return model;
}
/**
* Encapsulates the data of content item provided by this model. To avoid to
* have to many fields in this model class we use an internal class to
@ -519,6 +602,8 @@ public class ContentItemModel {
private String lastModifyingUserName;
private List<AttachmentListModel> attachmentLists;
public long getObjectId() {
return objectId;
}
@ -617,9 +702,15 @@ public class ContentItemModel {
this.lastModifyingUserName = lastModifyingUserName;
}
// private List<AttachmentList> getAttachments() {
// throw new UnsupportedOperationException("Not implemented yet.");
// }
public List<AttachmentListModel> getAttachmentLists() {
return Collections.unmodifiableList(attachmentLists);
}
public void setAttachmentLists(
final List<AttachmentListModel> attachmentLists) {
this.attachmentLists = new ArrayList<>(attachmentLists);
}
}
}