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 *     Funsho David
018 */
019
020package org.nuxeo.ecm.platform.comment.api;
021
022import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_ID_PROPERTY;
023import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_PROPERTY;
024import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_ORIGIN_PROPERTY;
025import static org.nuxeo.ecm.platform.comment.api.AnnotationConstants.ANNOTATION_XPATH_PROPERTY;
026import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_FACET;
027import static org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants.COMMENT_ANCESTOR_IDS;
028import static org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants.COMMENT_AUTHOR;
029import static org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants.COMMENT_CREATION_DATE;
030import static org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants.COMMENT_PARENT_ID;
031import static org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants.COMMENT_MODIFICATION_DATE;
032import static org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants.COMMENT_TEXT;
033import java.io.Serializable;
034import java.time.Instant;
035import java.time.ZoneId;
036import java.time.ZonedDateTime;
037import java.util.Calendar;
038import java.util.Collection;
039import java.util.GregorianCalendar;
040
041import org.nuxeo.ecm.core.api.DocumentModel;
042
043/**
044 * Utility class to convert document model from/to comments, annotations or external entities.
045 * 
046 * @since 10.3
047 */
048public class Comments {
049
050    private Comments() {
051        // no instance allowed
052    }
053
054    public static void commentToDocumentModel(Comment comment, DocumentModel documentModel) {
055        // Do not set ancestor ids as it is computed at document creation
056        documentModel.setPropertyValue(COMMENT_AUTHOR, comment.getAuthor());
057        documentModel.setPropertyValue(COMMENT_TEXT, comment.getText());
058        documentModel.setPropertyValue(COMMENT_PARENT_ID, comment.getParentId());
059        Instant creationDate = comment.getCreationDate();
060        if (creationDate != null) {
061            documentModel.setPropertyValue(COMMENT_CREATION_DATE,
062                    GregorianCalendar.from(ZonedDateTime.ofInstant(creationDate, ZoneId.systemDefault())));
063        }
064        Instant modificationDate = comment.getModificationDate();
065        if (modificationDate != null) {
066            documentModel.setPropertyValue(COMMENT_MODIFICATION_DATE,
067                    GregorianCalendar.from(ZonedDateTime.ofInstant(modificationDate, ZoneId.systemDefault())));
068        }
069    }
070
071    public static void annotationToDocumentModel(Annotation annotation, DocumentModel documentModel) {
072        commentToDocumentModel(annotation, documentModel);
073        documentModel.setPropertyValue(ANNOTATION_XPATH_PROPERTY, annotation.getXpath());
074    }
075
076    public static void externalEntityToDocumentModel(ExternalEntity entity, DocumentModel documentModel) {
077        documentModel.setPropertyValue(EXTERNAL_ENTITY_ID_PROPERTY, entity.getEntityId());
078        documentModel.setPropertyValue(EXTERNAL_ENTITY_ORIGIN_PROPERTY, entity.getOrigin());
079        documentModel.setPropertyValue(EXTERNAL_ENTITY_PROPERTY, entity.getEntity());
080    }
081
082    @SuppressWarnings("unchecked")
083    public static void documentModelToComment(DocumentModel documentModel, Comment comment) {
084        comment.setId(documentModel.getId());
085        comment.setAuthor((String) documentModel.getPropertyValue(COMMENT_AUTHOR));
086        comment.setText((String) documentModel.getPropertyValue(COMMENT_TEXT));
087        Collection<String> ancestorIds = (Collection<String>) documentModel.getPropertyValue(COMMENT_ANCESTOR_IDS);
088        ancestorIds.forEach(comment::addAncestorId);
089        String parentId = (String) documentModel.getPropertyValue(COMMENT_PARENT_ID);
090        comment.setParentId(parentId);
091
092        Calendar creationDate = (Calendar) documentModel.getPropertyValue(COMMENT_CREATION_DATE);
093        if (creationDate != null) {
094            comment.setCreationDate(creationDate.toInstant());
095        }
096        Calendar modificationDate = (Calendar) documentModel.getPropertyValue(COMMENT_MODIFICATION_DATE);
097        if (modificationDate != null) {
098            comment.setModificationDate(modificationDate.toInstant());
099        }
100    }
101
102    public static void documentModelToAnnotation(DocumentModel documentModel, Annotation annotation) {
103        documentModelToComment(documentModel, annotation);
104        annotation.setXpath((String) documentModel.getPropertyValue(ANNOTATION_XPATH_PROPERTY));
105    }
106
107    public static void documentModelToExternalEntity(DocumentModel documentModel, ExternalEntity entity) {
108        if (documentModel.hasFacet(EXTERNAL_ENTITY_FACET)) {
109            entity.setEntityId((String) documentModel.getPropertyValue(EXTERNAL_ENTITY_ID_PROPERTY));
110            entity.setOrigin((String) documentModel.getPropertyValue(EXTERNAL_ENTITY_ORIGIN_PROPERTY));
111            entity.setEntity((String) documentModel.getPropertyValue(EXTERNAL_ENTITY_PROPERTY));
112        }
113    }
114
115    public static Comment newComment(DocumentModel commentModel) {
116        Comment comment = new CommentImpl();
117        documentModelToComment(commentModel, comment);
118        documentModelToExternalEntity(commentModel, (ExternalEntity) comment);
119        return comment;
120    }
121
122    public static Annotation newAnnotation(DocumentModel annotationModel) {
123        Annotation annotation = new AnnotationImpl();
124        documentModelToAnnotation(annotationModel, annotation);
125        documentModelToExternalEntity(annotationModel, (ExternalEntity) annotation);
126        return annotation;
127    }
128}