From fe74176563c2031ff8cef584a833912b5be7c4f1 Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Sun, 26 Feb 2023 09:32:29 +0100 Subject: [PATCH] Fixed missing JSONIdentityInfo for AttachmentList --- ccm-cms/package-lock.json | 4 +- ccm-cms/package.json | 2 +- .../contentsection/AttachmentList.java | 9 +++ .../AttachmentListIdResolver.java | 72 +++++++++++++++++++ .../contentsection/ContentItemIdResolver.java | 1 - .../contentsection/ItemAttachment.java | 3 + 6 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 ccm-cms/src/main/java/org/librecms/contentsection/AttachmentListIdResolver.java diff --git a/ccm-cms/package-lock.json b/ccm-cms/package-lock.json index b9ef9f73f..b1b7a9c37 100644 --- a/ccm-cms/package-lock.json +++ b/ccm-cms/package-lock.json @@ -1,12 +1,12 @@ { "name": "@librecms/ccm-cms", - "version": "7.0.0-SNAPSHOT.2023-02-23T193303", + "version": "7.0.0-SNAPSHOT.2023-02-26T075448", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@librecms/ccm-cms", - "version": "7.0.0-SNAPSHOT.2023-02-23T193303", + "version": "7.0.0-SNAPSHOT.2023-02-26T075448", "license": "LGPL-3.0-or-later", "dependencies": { "@tiptap/core": "^2.0.0-beta.127", diff --git a/ccm-cms/package.json b/ccm-cms/package.json index e91da3956..3eb8b2af7 100644 --- a/ccm-cms/package.json +++ b/ccm-cms/package.json @@ -1,6 +1,6 @@ { "name": "@librecms/ccm-cms", - "version": "7.0.0-SNAPSHOT.2023-02-23T193303", + "version": "7.0.0-SNAPSHOT.2023-02-26T075448", "description": "JavaScript stuff for ccm-cms", "main": "target/generated-resources/assets/@content-sections/cms-admin.js", "types": "target/generated-resources/assets/@content-sections/cms-admin.d.ts", diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/AttachmentList.java b/ccm-cms/src/main/java/org/librecms/contentsection/AttachmentList.java index 59533471f..6e58ab435 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/AttachmentList.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/AttachmentList.java @@ -18,7 +18,10 @@ */ package org.librecms.contentsection; +import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.ObjectIdGenerator; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; import org.hibernate.envers.Audited; import org.libreccm.core.Identifiable; import org.libreccm.imexport.Exportable; @@ -89,6 +92,12 @@ import static org.librecms.CmsConstants.DB_SCHEMA; + "AND l.item = :item " + "ORDER BY l.listOrder") }) +@JsonIdentityInfo( + generator = ObjectIdGenerators.PropertyGenerator.class, + resolver = AttachmentListIdResolver.class, + property = "uuid", + scope = AttachmentList.class +) public class AttachmentList implements Comparable, Identifiable, Exportable, diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/AttachmentListIdResolver.java b/ccm-cms/src/main/java/org/librecms/contentsection/AttachmentListIdResolver.java new file mode 100644 index 000000000..ae21ad16b --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/contentsection/AttachmentListIdResolver.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2023 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.contentsection; + +import com.fasterxml.jackson.annotation.ObjectIdGenerator; +import com.fasterxml.jackson.annotation.ObjectIdResolver; +import org.libreccm.cdi.utils.CdiUtil; + +import java.io.Serializable; + +/** + * + * @author Jens Pelzetter + */ +public class AttachmentListIdResolver + implements Serializable, ObjectIdResolver { + + private static final long serialVersionUID = 1L; + + @Override + public void bindItem( + final ObjectIdGenerator.IdKey id, + final Object object + ) { + // According to the Jackson JavaDoc, this method can be used to keep + // track of objects directly in a resolver implementation. We don't need + // this here therefore this method is empty. + } + + @Override + public Object resolveId(final ObjectIdGenerator.IdKey id) { + return CdiUtil + .createCdiUtil() + .findBean(AttachmentListRepository.class) + .findByUuid(id.key.toString()) + .orElseThrow( + () -> new IllegalArgumentException( + String.format( + "No AttachmentList with UUID %s found in the database.", + id.key.toString() + ) + ) + ); + } + + @Override + public ObjectIdResolver newForDeserialization(final Object context) { + return new AttachmentListIdResolver(); + } + + @Override + public boolean canUseFor(final ObjectIdResolver resolverType) { + return resolverType instanceof AttachmentListIdResolver; + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemIdResolver.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemIdResolver.java index 9c07b92b1..50697dbf1 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemIdResolver.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemIdResolver.java @@ -23,7 +23,6 @@ import com.fasterxml.jackson.annotation.ObjectIdResolver; import org.libreccm.cdi.utils.CdiUtil; import java.io.Serializable; -import java.util.Optional; /** * diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ItemAttachment.java b/ccm-cms/src/main/java/org/librecms/contentsection/ItemAttachment.java index bfc7ad5cb..33c42bf47 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ItemAttachment.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ItemAttachment.java @@ -18,6 +18,8 @@ */ package org.librecms.contentsection; +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.JsonIdentityReference; import org.hibernate.envers.Audited; import org.libreccm.core.Identifiable; import org.libreccm.imexport.Exportable; @@ -97,6 +99,7 @@ public class ItemAttachment */ @ManyToOne(cascade = {CascadeType.PERSIST}) @JoinColumn(name = "ATTACHMENT_LIST_ID") + @JsonIdentityReference(alwaysAsId = true) private AttachmentList attachmentList; /**