diff --git a/ccm-cms/package-lock.json b/ccm-cms/package-lock.json index 5d9cdef31..367c02bef 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.2022-11-26T122815", + "version": "7.0.0-SNAPSHOT.2022-12-01T192513", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@librecms/ccm-cms", - "version": "7.0.0-SNAPSHOT.2022-11-26T122815", + "version": "7.0.0-SNAPSHOT.2022-12-01T192513", "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 c8ab2d24f..e2725f386 100644 --- a/ccm-cms/package.json +++ b/ccm-cms/package.json @@ -1,6 +1,6 @@ { "name": "@librecms/ccm-cms", - "version": "7.0.0-SNAPSHOT.2022-11-26T122815", + "version": "7.0.0-SNAPSHOT.2022-12-01T192513", "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/assets/ContactEntry.java b/ccm-cms/src/main/java/org/librecms/assets/ContactEntry.java index 8f09807dc..4bab11830 100644 --- a/ccm-cms/src/main/java/org/librecms/assets/ContactEntry.java +++ b/ccm-cms/src/main/java/org/librecms/assets/ContactEntry.java @@ -18,6 +18,8 @@ */ package org.librecms.assets; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; import org.hibernate.envers.Audited; import java.io.Serializable; @@ -63,6 +65,8 @@ public class ContactEntry implements Serializable { // @Column(name = "ENTRY_KEY", length = 255, nullable = false) @OneToOne @JoinColumn(name = "CONTACT_ENTRY_KEY_ID") + @JsonSerialize(using = ContactEntryKeyJsonSerializer.class) + @JsonDeserialize(using = ContactEntryKeyJsonDeserializer.class) private ContactEntryKey key; /** diff --git a/ccm-cms/src/main/java/org/librecms/assets/ContactEntryKeyJsonDeserializer.java b/ccm-cms/src/main/java/org/librecms/assets/ContactEntryKeyJsonDeserializer.java new file mode 100644 index 000000000..3738014dc --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/assets/ContactEntryKeyJsonDeserializer.java @@ -0,0 +1,61 @@ +/* + * 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.assets; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import org.libreccm.cdi.utils.CdiUtil; + +import java.io.IOException; +import java.util.Optional; + +/** + * + * @author Jens Pelzetter + */ +public class ContactEntryKeyJsonDeserializer + extends JsonDeserializer { + + @Override + public ContactEntryKey deserialize( + final JsonParser parser, + final DeserializationContext ctxt + ) throws IOException, JsonProcessingException { + final String key = parser.getText(); + + final ContactEntryKeyRepository entryKeyRepo = CdiUtil + .createCdiUtil() + .findBean(ContactEntryKeyRepository.class); + final Optional result = entryKeyRepo + .findByEntryKey(key); + + if (result.isPresent()) { + return result.get(); + } else { + final ContactEntryKey entryKey = new ContactEntryKey(); + entryKey.setEntryKey(key); + entryKeyRepo.save(entryKey); + + return entryKey; + } + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/assets/ContactEntryKeyJsonSerializer.java b/ccm-cms/src/main/java/org/librecms/assets/ContactEntryKeyJsonSerializer.java new file mode 100644 index 000000000..2b0768a7e --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/assets/ContactEntryKeyJsonSerializer.java @@ -0,0 +1,43 @@ +/* + * 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.assets; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; + +import java.io.IOException; + +/** + * + * @author Jens Pelzetter + */ +public class ContactEntryKeyJsonSerializer + extends JsonSerializer { + + @Override + public void serialize( + final ContactEntryKey value, + final JsonGenerator generator, + final SerializerProvider serializers + ) throws IOException { + generator.writeStringField("key", value.getEntryKey()); + } + +}