001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (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 java.util.List;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.event.EventListener;
031import org.nuxeo.ecm.platform.comment.service.CommentServiceConfig;
032import org.nuxeo.ecm.platform.comment.workflow.utils.CommentsConstants;
033import org.nuxeo.ecm.platform.relations.api.Graph;
034import org.nuxeo.ecm.platform.relations.api.RelationManager;
035import org.nuxeo.ecm.platform.relations.api.Resource;
036import org.nuxeo.ecm.platform.relations.api.Statement;
037
038public class CommentRemovedEventListener extends AbstractCommentListener implements EventListener {
039
040    private static final Log log = LogFactory.getLog(CommentRemovedEventListener.class);
041
042    @Override
043    protected void doProcess(CoreSession coreSession, RelationManager relationManager, CommentServiceConfig config,
044            DocumentModel docMessage) {
045        log.debug("Processing relations cleanup on Comment removal");
046        String typeName = docMessage.getType();
047        if (CommentsConstants.COMMENT_DOC_TYPE.equals(typeName) || "Post".equals(typeName)) {
048            onCommentRemoved(relationManager, config, docMessage);
049        }
050    }
051
052    private static void onCommentRemoved(RelationManager relationManager, CommentServiceConfig config,
053            DocumentModel docModel) {
054        Resource commentRes = relationManager.getResource(config.commentNamespace, docModel, null);
055        if (commentRes == null) {
056            log.warn("Could not adapt document model to relation resource; "
057                    + "check the service relation adapters configuration");
058            return;
059        }
060        Graph graph = relationManager.getGraph(config.graphName, docModel.getCoreSession());
061        List<Statement> statementList = graph.getStatements(commentRes, null, null);
062        graph.remove(statementList);
063    }
064
065}