001/*
002 * (C) Copyright 2018 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.ExternalEntityConstants.EXTERNAL_ENTITY;
025import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_ID;
026import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_ORIGIN;
027import static org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants.COMMENT_AUTHOR_FIELD;
028import static org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants.COMMENT_CREATION_DATE_FIELD;
029import static org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants.COMMENT_PARENT_ID_FIELD;
030import static org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants.COMMENT_ENTITY_TYPE;
031import static org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants.COMMENT_MODIFICATION_DATE_FIELD;
032import static org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants.COMMENT_TEXT_FIELD;
033
034import java.time.Instant;
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        comment.setParentId(jn.get(COMMENT_PARENT_ID_FIELD).textValue());
064        comment.setAuthor(jn.get(COMMENT_AUTHOR_FIELD).textValue());
065        comment.setText(jn.get(COMMENT_TEXT_FIELD).textValue());
066
067        JsonNode creationDateNode = jn.get(COMMENT_CREATION_DATE_FIELD);
068        Instant creationDate = creationDateNode != null && !creationDateNode.isNull()
069                ? Instant.parse(creationDateNode.textValue())
070                : null;
071        comment.setCreationDate(creationDate);
072
073        JsonNode modificationDateNode = jn.get(COMMENT_MODIFICATION_DATE_FIELD);
074        Instant modificationDate = modificationDateNode != null && !modificationDateNode.isNull()
075                ? Instant.parse(modificationDateNode.textValue())
076                : null;
077        comment.setModificationDate(modificationDate);
078
079        if (jn.has(EXTERNAL_ENTITY_ID)) {
080            ExternalEntity externalEntity = (ExternalEntity) comment;
081            externalEntity.setEntityId(jn.get(EXTERNAL_ENTITY_ID).textValue());
082            externalEntity.setOrigin(jn.get(EXTERNAL_ENTITY_ORIGIN).textValue());
083            externalEntity.setEntity(jn.get(EXTERNAL_ENTITY).textValue());
084        }
085        return comment;
086    }
087}