001/*
002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.comment.listener;
021
022import java.util.List;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentNotFoundException;
029import org.nuxeo.ecm.core.event.PostCommitEventListener;
030import org.nuxeo.ecm.platform.comment.service.CommentServiceConfig;
031import org.nuxeo.ecm.platform.relations.api.Graph;
032import org.nuxeo.ecm.platform.relations.api.QNameResource;
033import org.nuxeo.ecm.platform.relations.api.RelationManager;
034import org.nuxeo.ecm.platform.relations.api.Resource;
035import org.nuxeo.ecm.platform.relations.api.Statement;
036
037public class DocumentRemovedCommentEventListener extends AbstractCommentListener implements PostCommitEventListener {
038
039    private static final Log log = LogFactory.getLog(DocumentRemovedCommentEventListener.class);
040
041    @Override
042    protected void doProcess(CoreSession coreSession, RelationManager relationManager, CommentServiceConfig config,
043            DocumentModel docMessage) {
044        log.debug("Processing relations cleanup on Document removal");
045        onDocumentRemoved(coreSession, relationManager, config, docMessage);
046    }
047
048    private static void onDocumentRemoved(CoreSession coreSession, RelationManager relationManager,
049            CommentServiceConfig config, DocumentModel docMessage) {
050
051        Resource documentRes = relationManager.getResource(config.documentNamespace, docMessage, null);
052        if (documentRes == null) {
053            log.error("Could not adapt document model to relation resource ; "
054                    + "check the service relation adapters configuration");
055            return;
056        }
057        Graph graph = relationManager.getGraphByName(config.graphName);
058        List<Statement> statementList = graph.getStatements(null, null, documentRes);
059
060        // remove comments
061        for (Statement stmt : statementList) {
062            QNameResource resource = (QNameResource) stmt.getSubject();
063            String commentId = resource.getLocalName();
064            DocumentModel docModel = (DocumentModel) relationManager.getResourceRepresentation(config.commentNamespace,
065                    resource, null);
066
067            if (docModel != null) {
068                try {
069                    coreSession.removeDocument(docModel.getRef());
070                    log.debug("comment removal succeded for id: " + commentId);
071                } catch (DocumentNotFoundException e) {
072                    log.error("comment removal failed", e);
073                }
074            } else {
075                log.warn("comment not found: id=" + commentId);
076            }
077        }
078        coreSession.save();
079        // remove relations
080        graph.remove(statementList);
081    }
082
083}