001/*
002 *  Copyright 2024 Anyware Services
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 */
016package org.ametys.plugins.workspaces.documents.jcr;
017
018import java.time.ZonedDateTime;
019import java.util.List;
020
021import javax.jcr.Node;
022import javax.jcr.RepositoryException;
023
024import org.ametys.cms.repository.CommentableAmetysObject;
025import org.ametys.cms.repository.comment.Comment;
026import org.ametys.plugins.explorer.resources.Resource;
027import org.ametys.plugins.explorer.resources.jcr.JCRResource;
028import org.ametys.plugins.repository.AmetysObject;
029import org.ametys.plugins.repository.AmetysRepositoryException;
030import org.ametys.plugins.repository.data.holder.ModifiableModelLessDataHolder;
031import org.ametys.plugins.repository.data.holder.impl.DefaultModifiableModelLessDataHolder;
032import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
033import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
034
035/**
036 * Implementation of {@link Resource} used for document module of workspaces and can handle comments, backed by a JCR node.<br>
037 */
038public class File extends JCRResource<FileFactory> implements CommentableAmetysObject<Comment>
039{
040
041    /**
042     * Creates an {@link File}.
043     * @param node the node backing this {@link AmetysObject}
044     * @param parentPath the parentPath in the Ametys hierarchy
045     * @param factory the DefaultAmetysObjectFactory which created the AmetysObject
046     */
047    public File(Node node, String parentPath, FileFactory factory)
048    {
049        super(node, parentPath, factory);
050    }
051
052    public Comment createComment()
053    {
054        return new Comment(_getCommentsDataHolder());
055    }
056    
057    public Comment createComment(String commentId, ZonedDateTime creationDate)
058    {
059        return new Comment(_getCommentsDataHolder(), commentId, creationDate);
060    }
061    
062    public Comment getComment(String commentId) throws AmetysRepositoryException
063    {
064        return Comment.getComment(_getCommentsDataHolder(), commentId);
065    }
066    
067    public List<Comment> getComments(boolean includeNotValidatedComments, boolean includeValidatedComments) throws AmetysRepositoryException
068    {
069        return Comment.getComments(_getCommentsDataHolder(), includeNotValidatedComments, includeValidatedComments);
070    }
071
072    private ModifiableModelLessDataHolder _getCommentsDataHolder()
073    {
074        try
075        {
076            Node baseNode = getBaseNode();
077            if (!baseNode.hasNode("ametys:comments"))
078            {
079                baseNode.addNode("ametys:comments", "ametys:compositeMetadata");
080            }
081            Node commentsNode = baseNode.getNode("ametys:comments");
082            
083            baseNode.getSession().save();
084            
085            ModifiableRepositoryData repositoryData = new JCRRepositoryData(commentsNode);
086            return new DefaultModifiableModelLessDataHolder(_getFactory().getUnversionedDataTypeExtensionPoint(), repositoryData);
087        }
088        catch (RepositoryException e)
089        {
090            throw new AmetysRepositoryException(e);
091        }
092    }
093}