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.annotations.repository.service;
021
022import java.io.Serializable;
023import java.util.HashMap;
024import java.util.Map;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.core.api.CoreInstance;
029import org.nuxeo.ecm.core.api.CoreSession;
030import org.nuxeo.ecm.core.api.DocumentLocation;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.NuxeoPrincipal;
033import org.nuxeo.ecm.core.api.event.DocumentEventCategories;
034import org.nuxeo.ecm.core.event.Event;
035import org.nuxeo.ecm.core.event.EventContext;
036import org.nuxeo.ecm.core.event.EventProducer;
037import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
038import org.nuxeo.ecm.core.event.impl.EventContextImpl;
039import org.nuxeo.ecm.platform.annotations.api.Annotation;
040import org.nuxeo.runtime.api.Framework;
041
042public class AnnotatedDocumentEventListenerImpl implements AnnotatedDocumentEventListener {
043
044    private static final Log log = LogFactory.getLog(AnnotatedDocumentEventListenerImpl.class);
045
046    public void beforeAnnotationCreated(NuxeoPrincipal principal, DocumentLocation documentLoc, Annotation annotation) {
047        // NOP
048    }
049
050    public void beforeAnnotationDeleted(NuxeoPrincipal principal, DocumentLocation documentLoc, Annotation annotation) {
051        // NOP
052    }
053
054    public void beforeAnnotationRead(NuxeoPrincipal principal, String annotationId) {
055        // NOP
056    }
057
058    public void beforeAnnotationUpdated(NuxeoPrincipal principal, DocumentLocation documentLoc, Annotation annotation) {
059        // NOP
060    }
061
062    public void afterAnnotationCreated(NuxeoPrincipal principal, DocumentLocation documentLoc, Annotation annotation) {
063        notifyEvent(ANNOTATION_CREATED, annotation, documentLoc, principal);
064    }
065
066    public void afterAnnotationDeleted(NuxeoPrincipal principal, DocumentLocation documentLoc, Annotation annotation) {
067        notifyEvent(ANNOTATION_DELETED, annotation, documentLoc, principal);
068    }
069
070    public void afterAnnotationRead(NuxeoPrincipal principal, DocumentLocation documentLoc, Annotation annotation) {
071        // NOP for now
072    }
073
074    public void afterAnnotationUpdated(NuxeoPrincipal principal, DocumentLocation documentLoc, Annotation annotation) {
075        notifyEvent(ANNOTATION_UPDATED, annotation, documentLoc, principal);
076    }
077
078    protected void notifyEvent(String eventId, Annotation annotation, DocumentLocation documentLocation,
079            NuxeoPrincipal principal) {
080        if (documentLocation == null) {
081            return;
082        }
083        try (CoreSession session = CoreInstance.openCoreSessionSystem(documentLocation.getServerName())) {
084            DocumentModel doc = null;
085            if (session.exists(documentLocation.getDocRef())) {
086                doc = session.getDocument(documentLocation.getDocRef());
087            }
088
089            Map<String, Serializable> properties = new HashMap<String, Serializable>();
090            properties.put(AnnotatedDocumentEventListener.ANNOTATION_ID, annotation.getId());
091            properties.put(AnnotatedDocumentEventListener.ANNOTATION_SUBJECT, annotation.getSubject());
092            properties.put(AnnotatedDocumentEventListener.ANNOTATION_BODY, annotation.getBodyAsText());
093
094            EventContext ctx = null;
095            if (doc != null) {
096                DocumentEventContext docCtx = new DocumentEventContext(session, principal, doc);
097                docCtx.setCategory(DocumentEventCategories.EVENT_DOCUMENT_CATEGORY);
098                ctx = docCtx;
099            } else {
100                ctx = new EventContextImpl(session, principal);
101            }
102            ctx.setRepositoryName(documentLocation.getServerName());
103            ctx.setProperties(properties);
104
105            Event event = ctx.newEvent(eventId);
106            Framework.getService(EventProducer.class).fireEvent(event);
107            session.save();
108        }
109    }
110
111}