001/*
002 * (C) Copyright 2006-2008 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 *     Alexandre Russel
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.repository.core;
021
022import java.net.URI;
023import java.security.Principal;
024import java.util.List;
025
026import org.nuxeo.ecm.core.api.DocumentLocation;
027import org.nuxeo.ecm.core.api.NuxeoPrincipal;
028import org.nuxeo.ecm.platform.annotations.api.Annotation;
029import org.nuxeo.ecm.platform.annotations.repository.DefaultNuxeoUriResolver;
030import org.nuxeo.ecm.platform.annotations.repository.service.AnnotatedDocumentEventListener;
031import org.nuxeo.ecm.platform.annotations.repository.service.AnnotationsRepositoryConfigurationService;
032import org.nuxeo.ecm.platform.annotations.service.EventListener;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * @author Alexandre Russel
037 */
038public class AnnotationEventListener implements EventListener {
039    private List<AnnotatedDocumentEventListener> listeners;
040
041    private final DefaultNuxeoUriResolver resolver = new DefaultNuxeoUriResolver();
042
043    public void afterAnnotationCreated(Principal principal, Annotation annotation) {
044        for (AnnotatedDocumentEventListener listener : getListeners()) {
045            listener.afterAnnotationCreated((NuxeoPrincipal) principal, getDocumentLocation(annotation), annotation);
046        }
047    }
048
049    private DocumentLocation getDocumentLocation(Annotation annotation) {
050        URI annotates = annotation.getAnnotates();
051        return resolver.getDocumentLocation(annotates);
052    }
053
054    private List<AnnotatedDocumentEventListener> getListeners() {
055        if (listeners == null) {
056            AnnotationsRepositoryConfigurationService service = Framework.getService(AnnotationsRepositoryConfigurationService.class);
057            listeners = service.getEventListeners();
058        }
059        return listeners;
060    }
061
062    public void afterAnnotationDeleted(Principal principal, Annotation annotation) {
063        for (AnnotatedDocumentEventListener listener : getListeners()) {
064            listener.afterAnnotationDeleted((NuxeoPrincipal) principal, getDocumentLocation(annotation), annotation);
065        }
066    }
067
068    public void afterAnnotationRead(Principal principal, Annotation annotation) {
069        for (AnnotatedDocumentEventListener listener : getListeners()) {
070            listener.afterAnnotationRead((NuxeoPrincipal) principal, getDocumentLocation(annotation), annotation);
071        }
072    }
073
074    public void afterAnnotationUpdated(Principal principal, Annotation annotation) {
075        for (AnnotatedDocumentEventListener listener : getListeners()) {
076            listener.afterAnnotationUpdated((NuxeoPrincipal) principal, getDocumentLocation(annotation), annotation);
077        }
078    }
079
080    public void beforeAnnotationCreated(Principal principal, Annotation annotation) {
081        for (AnnotatedDocumentEventListener listener : getListeners()) {
082            listener.beforeAnnotationCreated((NuxeoPrincipal) principal, getDocumentLocation(annotation), annotation);
083        }
084    }
085
086    public void beforeAnnotationDeleted(Principal principal, Annotation annotation) {
087        for (AnnotatedDocumentEventListener listener : getListeners()) {
088            listener.beforeAnnotationDeleted((NuxeoPrincipal) principal, getDocumentLocation(annotation), annotation);
089        }
090    }
091
092    public void beforeAnnotationRead(Principal principal, String annotationId) {
093        for (AnnotatedDocumentEventListener listener : getListeners()) {
094            listener.beforeAnnotationRead((NuxeoPrincipal) principal, annotationId);
095        }
096    }
097
098    public void beforeAnnotationUpdated(Principal principal, Annotation annotation) {
099        for (AnnotatedDocumentEventListener listener : getListeners()) {
100            listener.beforeAnnotationUpdated((NuxeoPrincipal) principal, getDocumentLocation(annotation), annotation);
101        }
102    }
103
104}