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