001/* 002 * (C) Copyright 2018-2020 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Kevin Leturc <kleturc@nuxeo.com> 018 * Nuno Cunha <ncunha@nuxeo.com> 019 */ 020package org.nuxeo.ecm.platform.comment.impl; 021 022import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON; 023import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE; 024import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_CREATION_DATE_FIELD; 025import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_ENTITY_TYPE; 026import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_MODIFICATION_DATE_FIELD; 027import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_PARENT_ID_FIELD; 028import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_TEXT_FIELD; 029import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY; 030import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_ID_FIELD; 031import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_ORIGIN_FIELD; 032 033import java.time.Instant; 034import java.util.function.Consumer; 035 036import org.nuxeo.ecm.core.io.marshallers.json.EntityJsonReader; 037import org.nuxeo.ecm.core.io.registry.reflect.Setup; 038import org.nuxeo.ecm.platform.comment.api.Comment; 039import org.nuxeo.ecm.platform.comment.api.CommentImpl; 040import org.nuxeo.ecm.platform.comment.api.ExternalEntity; 041 042import com.fasterxml.jackson.databind.JsonNode; 043 044/** 045 * @since 10.3 046 */ 047@Setup(mode = SINGLETON, priority = REFERENCE) 048public class CommentJsonReader extends EntityJsonReader<Comment> { 049 050 public CommentJsonReader() { 051 super(COMMENT_ENTITY_TYPE); 052 } 053 054 @Override 055 protected Comment readEntity(JsonNode jn) { 056 Comment comment = new CommentImpl(); 057 return fillCommentEntity(jn, comment); 058 059 } 060 061 protected static Comment fillCommentEntity(JsonNode jn, Comment comment) { 062 // don't read id from given JsonNode, if needed it is read from path 063 setIfExist(jn, COMMENT_PARENT_ID_FIELD, comment::setParentId); 064 setIfExist(jn, COMMENT_TEXT_FIELD, comment::setText); 065 setIfExist(jn, COMMENT_CREATION_DATE_FIELD, s -> comment.setCreationDate(s == null ? null : Instant.parse(s))); 066 setIfExist(jn, COMMENT_MODIFICATION_DATE_FIELD, 067 s -> comment.setModificationDate(s == null ? null : Instant.parse(s))); 068 if (jn.has(EXTERNAL_ENTITY_ID_FIELD) || jn.has(EXTERNAL_ENTITY_ORIGIN_FIELD) || jn.has(EXTERNAL_ENTITY)) { 069 ExternalEntity externalEntity = (ExternalEntity) comment; 070 setIfExist(jn, EXTERNAL_ENTITY_ID_FIELD, externalEntity::setEntityId); 071 setIfExist(jn, EXTERNAL_ENTITY_ORIGIN_FIELD, externalEntity::setOrigin); 072 setIfExist(jn, EXTERNAL_ENTITY, externalEntity::setEntity); 073 } 074 return comment; 075 } 076 077 protected static void setIfExist(JsonNode jn, String key, Consumer<String> consumer) { 078 if (jn.has(key)) { 079 consumer.accept(jn.get(key).textValue()); 080 } 081 } 082}