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.CloseableCoreSession; 031import org.nuxeo.ecm.core.api.CoreInstance; 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 @Override 049 public void beforeAnnotationCreated(NuxeoPrincipal principal, DocumentLocation documentLoc, Annotation annotation) { 050 // NOP 051 } 052 053 @Override 054 public void beforeAnnotationDeleted(NuxeoPrincipal principal, DocumentLocation documentLoc, Annotation annotation) { 055 // NOP 056 } 057 058 @Override 059 public void beforeAnnotationRead(NuxeoPrincipal principal, String annotationId) { 060 // NOP 061 } 062 063 @Override 064 public void beforeAnnotationUpdated(NuxeoPrincipal principal, DocumentLocation documentLoc, Annotation annotation) { 065 // NOP 066 } 067 068 @Override 069 public void afterAnnotationCreated(NuxeoPrincipal principal, DocumentLocation documentLoc, Annotation annotation) { 070 notifyEvent(ANNOTATION_CREATED, annotation, documentLoc, principal); 071 } 072 073 @Override 074 public void afterAnnotationDeleted(NuxeoPrincipal principal, DocumentLocation documentLoc, Annotation annotation) { 075 notifyEvent(ANNOTATION_DELETED, annotation, documentLoc, principal); 076 } 077 078 @Override 079 public void afterAnnotationRead(NuxeoPrincipal principal, DocumentLocation documentLoc, Annotation annotation) { 080 // NOP for now 081 } 082 083 @Override 084 public void afterAnnotationUpdated(NuxeoPrincipal principal, DocumentLocation documentLoc, Annotation annotation) { 085 notifyEvent(ANNOTATION_UPDATED, annotation, documentLoc, principal); 086 } 087 088 protected void notifyEvent(String eventId, Annotation annotation, DocumentLocation documentLocation, 089 NuxeoPrincipal principal) { 090 if (documentLocation == null) { 091 return; 092 } 093 try (CloseableCoreSession session = CoreInstance.openCoreSessionSystem(documentLocation.getServerName())) { 094 DocumentModel doc = null; 095 if (session.exists(documentLocation.getDocRef())) { 096 doc = session.getDocument(documentLocation.getDocRef()); 097 } 098 099 Map<String, Serializable> properties = new HashMap<String, Serializable>(); 100 properties.put(AnnotatedDocumentEventListener.ANNOTATION_ID, annotation.getId()); 101 properties.put(AnnotatedDocumentEventListener.ANNOTATION_SUBJECT, annotation.getSubject()); 102 properties.put(AnnotatedDocumentEventListener.ANNOTATION_BODY, annotation.getBodyAsText()); 103 104 EventContext ctx = null; 105 if (doc != null) { 106 DocumentEventContext docCtx = new DocumentEventContext(session, principal, doc); 107 docCtx.setCategory(DocumentEventCategories.EVENT_DOCUMENT_CATEGORY); 108 ctx = docCtx; 109 } else { 110 ctx = new EventContextImpl(session, principal); 111 } 112 ctx.setRepositoryName(documentLocation.getServerName()); 113 ctx.setProperties(properties); 114 115 Event event = ctx.newEvent(eventId); 116 Framework.getService(EventProducer.class).fireEvent(event); 117 session.save(); 118 } 119 } 120 121}