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 *     Funsho David
018 */
019
020package org.nuxeo.ecm.platform.comment.api;
021
022import static org.nuxeo.ecm.platform.comment.api.AnnotationConstants.ANNOTATION_XPATH_PROPERTY;
023import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_ANCESTOR_IDS_PROPERTY;
024import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_AUTHOR_PROPERTY;
025import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_CREATION_DATE_PROPERTY;
026import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_MODIFICATION_DATE_PROPERTY;
027import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_PARENT_ID_PROPERTY;
028import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_TEXT_PROPERTY;
029import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_FACET;
030import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_ID_PROPERTY;
031import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_ORIGIN_PROPERTY;
032import static org.nuxeo.ecm.platform.comment.api.ExternalEntityConstants.EXTERNAL_ENTITY_PROPERTY;
033
034import java.time.Instant;
035import java.util.Calendar;
036import java.util.Collection;
037import java.util.Date;
038
039import org.nuxeo.ecm.core.api.DocumentModel;
040
041/**
042 * Utility class to convert document model from/to comments, annotations or external entities.
043 *
044 * @since 10.3
045 * @deprecated since 11.1, use {@link Comment#getDocument()} or {@link DocumentModel#getAdapter(Class)} instead
046 */
047@Deprecated(since = "11.1")
048public class Comments {
049
050    private Comments() {
051        // no instance allowed
052    }
053
054    /**
055     * @deprecated since 11.1, use {@link Comment#getDocument()} with {@code DocumentModelJsonReader} instead
056     */
057    @Deprecated(since = "11.1")
058    public static void commentToDocumentModel(Comment comment, DocumentModel documentModel) {
059        // Do not set ancestor ids as it is computed at document creation
060        documentModel.setPropertyValue(COMMENT_AUTHOR_PROPERTY, comment.getAuthor());
061        documentModel.setPropertyValue(COMMENT_TEXT_PROPERTY, comment.getText());
062        documentModel.setPropertyValue(COMMENT_PARENT_ID_PROPERTY, comment.getParentId());
063        Instant creationDate = comment.getCreationDate();
064        if (creationDate != null) {
065            documentModel.setPropertyValue(COMMENT_CREATION_DATE_PROPERTY, Date.from(creationDate));
066        }
067        Instant modificationDate = comment.getModificationDate();
068        if (modificationDate != null) {
069            documentModel.setPropertyValue(COMMENT_MODIFICATION_DATE_PROPERTY, Date.from(modificationDate));
070        }
071    }
072
073    /**
074     * @deprecated since 11.1, unused
075     */
076    @Deprecated(since = "11.1")
077    public static void annotationToDocumentModel(Annotation annotation, DocumentModel documentModel) {
078        commentToDocumentModel(annotation, documentModel);
079        documentModel.setPropertyValue(ANNOTATION_XPATH_PROPERTY, annotation.getXpath());
080    }
081
082    /**
083     * @deprecated since 11.1, unused
084     */
085    @Deprecated(since = "11.1")
086    public static void externalEntityToDocumentModel(ExternalEntity entity, DocumentModel documentModel) {
087        documentModel.setPropertyValue(EXTERNAL_ENTITY_ID_PROPERTY, entity.getEntityId());
088        documentModel.setPropertyValue(EXTERNAL_ENTITY_ORIGIN_PROPERTY, entity.getOrigin());
089        documentModel.setPropertyValue(EXTERNAL_ENTITY_PROPERTY, entity.getEntity());
090    }
091
092    /**
093     * @deprecated since 11.1, unused
094     */
095    @Deprecated(since = "11.1")
096    @SuppressWarnings("unchecked")
097    public static void documentModelToComment(DocumentModel documentModel, Comment comment) {
098        comment.setId(documentModel.getId());
099        comment.setAuthor((String) documentModel.getPropertyValue(COMMENT_AUTHOR_PROPERTY));
100        comment.setText((String) documentModel.getPropertyValue(COMMENT_TEXT_PROPERTY));
101        Collection<String> ancestorIds = (Collection<String>) documentModel.getPropertyValue(
102                COMMENT_ANCESTOR_IDS_PROPERTY);
103        ancestorIds.forEach(comment::addAncestorId);
104        String parentId = (String) documentModel.getPropertyValue(COMMENT_PARENT_ID_PROPERTY);
105        comment.setParentId(parentId);
106
107        Calendar creationDate = (Calendar) documentModel.getPropertyValue(COMMENT_CREATION_DATE_PROPERTY);
108        if (creationDate != null) {
109            comment.setCreationDate(creationDate.toInstant());
110        }
111        Calendar modificationDate = (Calendar) documentModel.getPropertyValue(COMMENT_MODIFICATION_DATE_PROPERTY);
112        if (modificationDate != null) {
113            comment.setModificationDate(modificationDate.toInstant());
114        }
115    }
116
117    /**
118     * @deprecated since 11.1, unused
119     */
120    @Deprecated(since = "11.1")
121    public static void documentModelToAnnotation(DocumentModel documentModel, Annotation annotation) {
122        documentModelToComment(documentModel, annotation);
123        annotation.setXpath((String) documentModel.getPropertyValue(ANNOTATION_XPATH_PROPERTY));
124    }
125
126    /**
127     * @deprecated since 11.1, unused
128     */
129    @Deprecated(since = "11.1")
130    public static void documentModelToExternalEntity(DocumentModel documentModel, ExternalEntity entity) {
131        if (documentModel.hasFacet(EXTERNAL_ENTITY_FACET)) {
132            entity.setEntityId((String) documentModel.getPropertyValue(EXTERNAL_ENTITY_ID_PROPERTY));
133            entity.setOrigin((String) documentModel.getPropertyValue(EXTERNAL_ENTITY_ORIGIN_PROPERTY));
134            entity.setEntity((String) documentModel.getPropertyValue(EXTERNAL_ENTITY_PROPERTY));
135        }
136    }
137
138    /**
139     * @deprecated since 11.1, use {@link DocumentModel#getAdapter(Class)} with {@link Comment} class instead
140     */
141    @Deprecated(since = "11.1")
142    public static Comment newComment(DocumentModel commentModel) {
143        Comment comment = new CommentImpl();
144        documentModelToComment(commentModel, comment);
145        documentModelToExternalEntity(commentModel, (ExternalEntity) comment);
146        return comment;
147    }
148
149    /**
150     * @deprecated since 11.1, use {@link DocumentModel#getAdapter(Class)} with {@link Annotation} class instead
151     */
152    @Deprecated(since = "11.1")
153    public static Annotation newAnnotation(DocumentModel annotationModel) {
154        Annotation annotation = new AnnotationImpl();
155        documentModelToAnnotation(annotationModel, annotation);
156        documentModelToExternalEntity(annotationModel, (ExternalEntity) annotation);
157        return annotation;
158    }
159}