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