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