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