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 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.IdRef; 029import org.nuxeo.ecm.core.api.event.DocumentEventTypes; 030import org.nuxeo.ecm.core.event.Event; 031import org.nuxeo.ecm.core.event.EventBundle; 032import org.nuxeo.ecm.core.event.EventContext; 033import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 034import org.nuxeo.ecm.platform.comment.api.CommentManager; 035import org.nuxeo.ecm.platform.comment.service.CommentServiceConfig; 036import org.nuxeo.ecm.platform.comment.service.CommentServiceHelper; 037import org.nuxeo.ecm.platform.relations.api.RelationManager; 038import org.nuxeo.runtime.api.Framework; 039 040public abstract class AbstractCommentListener { 041 042 private static final Log log = LogFactory.getLog(AbstractCommentListener.class); 043 044 public void handleEvent(EventBundle events) { 045 if (events.containsEventName(DocumentEventTypes.DOCUMENT_REMOVED)) { 046 for (Event event : events) { 047 handleEvent(event); 048 } 049 } 050 } 051 052 public void handleEvent(Event event) { 053 if (DocumentEventTypes.DOCUMENT_REMOVED.equals(event.getName())) { 054 EventContext ctx = event.getContext(); 055 if (ctx instanceof DocumentEventContext) { 056 DocumentEventContext docCtx = (DocumentEventContext) ctx; 057 DocumentModel doc = docCtx.getSourceDocument(); 058 CoreSession coreSession = docCtx.getCoreSession(); 059 CommentServiceConfig config = CommentServiceHelper.getCommentService().getConfig(); 060 RelationManager relationManager = Framework.getService(RelationManager.class); 061 doProcess(coreSession, relationManager, config, doc); 062 return; 063 } 064 } 065 } 066 067 protected void deleteCommentChildren(CoreSession coreSession, CommentManager commentManager, 068 DocumentModel documentModel) { 069 commentManager.getComments(coreSession, documentModel.getId()) 070 .forEach(comment -> coreSession.removeDocument(new IdRef(comment.getId()))); 071 } 072 073 protected abstract void doProcess(CoreSession coreSession, RelationManager relationManager, 074 CommentServiceConfig config, DocumentModel docMessage); 075 076}