001/*
002 *  Copyright 2025 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.tasks.datapolicy;
017
018import javax.jcr.Node;
019import javax.jcr.RepositoryException;
020
021import org.apache.commons.lang3.tuple.Pair;
022
023import org.ametys.cms.repository.comment.Comment;
024import org.ametys.core.user.population.UserPopulationDAO;
025import org.ametys.plugins.repository.AmetysRepositoryException;
026import org.ametys.plugins.workspaces.datapolicy.AbstractCommentAndReactionDataPolicy;
027import org.ametys.plugins.workspaces.tasks.jcr.JCRTask;
028import org.ametys.plugins.workspaces.tasks.jcr.JCRTaskFactory;
029
030/**
031 * Data policy that anonymize task's comments made by an unknown user
032 */
033public class TaskCommentAndReactionDataPolicy extends AbstractCommentAndReactionDataPolicy
034{
035    @Override
036    protected String getLogCategory()
037    {
038        return "task";
039    }
040    
041    @Override
042    protected String getObjectPrimaryType()
043    {
044        return JCRTaskFactory.TASK_NODETYPE;
045    }
046    
047    @Override
048    protected boolean handleComment(Node commentNode)
049    {
050        try
051        {
052            Pair<Node, String> holderAndCommentId = getObjectNodeAndCommentId(commentNode);
053            if (holderAndCommentId != null)
054            {
055                JCRTask task = _resolver.resolve(holderAndCommentId.getLeft(), false);
056                Comment comment = task.getComment(holderAndCommentId.getRight());
057                
058                comment.setAuthor(UserPopulationDAO.UNKNOWN_USER_IDENTITY);
059                comment.setAuthorEmail(null);
060                comment.setAuthorName(null);
061                task.saveChanges();
062                
063                // No specific observation event  at this point
064                
065                return true;
066            }
067        }
068        catch (RepositoryException | AmetysRepositoryException e)
069        {
070            getLogger().error("Failed to retrieve file and comment from comment node '{}'", commentNode,  e);
071        }
072        
073        return false;
074    }
075}