001/*
002 * (C) Copyright 2006-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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.comment.listener;
023
024import static org.nuxeo.ecm.platform.comment.api.CommentConstants.COMMENT_DOC_TYPE;
025import static org.nuxeo.ecm.platform.comment.api.CommentManager.Feature.COMMENTS_LINKED_WITH_PROPERTY;
026
027import java.util.List;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.event.EventListener;
034import org.nuxeo.ecm.platform.comment.api.CommentManager;
035import org.nuxeo.ecm.platform.comment.service.CommentServiceConfig;
036import org.nuxeo.ecm.platform.relations.api.Graph;
037import org.nuxeo.ecm.platform.relations.api.RelationManager;
038import org.nuxeo.ecm.platform.relations.api.Resource;
039import org.nuxeo.ecm.platform.relations.api.Statement;
040import org.nuxeo.runtime.api.Framework;
041
042public class CommentRemovedEventListener extends AbstractCommentListener implements EventListener {
043
044    private static final Log log = LogFactory.getLog(CommentRemovedEventListener.class);
045
046    @Override
047    protected void doProcess(CoreSession coreSession, RelationManager relationManager, CommentServiceConfig config,
048            DocumentModel docMessage) {
049        log.debug("Processing relations cleanup on Comment removal");
050        String typeName = docMessage.getType();
051        if (COMMENT_DOC_TYPE.equals(typeName) || "Post".equals(typeName)) {
052            CommentManager commentManager = Framework.getService(CommentManager.class);
053            if (commentManager.hasFeature(COMMENTS_LINKED_WITH_PROPERTY)) {
054                deleteCommentChildren(coreSession, commentManager, docMessage);
055                coreSession.save();
056            } else if (relationManager != null) {
057                onCommentRemoved(relationManager, config, docMessage);
058            } else {
059                log.info("Migration in progress or relation manager is missing");
060            }
061        }
062    }
063
064    private static void onCommentRemoved(RelationManager relationManager, CommentServiceConfig config,
065            DocumentModel docModel) {
066        Resource commentRes = relationManager.getResource(config.commentNamespace, docModel, null);
067        if (commentRes == null) {
068            log.warn("Could not adapt document model to relation resource; "
069                    + "check the service relation adapters configuration");
070            return;
071        }
072        Graph graph = relationManager.getGraph(config.graphName, docModel.getCoreSession());
073        List<Statement> statementList = graph.getStatements(commentRes, null, null);
074        graph.remove(statementList);
075    }
076}