001/*
002 * (C) Copyright 2011 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 *     Olivier Grisel
016 *
017 * $Id$
018 */
019package org.nuxeo.ecm.platform.annotations.repository.listener;
020
021import org.nuxeo.ecm.core.api.CoreSession;
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.api.DocumentRef;
024import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
025import org.nuxeo.ecm.core.event.Event;
026import org.nuxeo.ecm.core.event.EventListener;
027import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
028import org.nuxeo.ecm.core.schema.FacetNames;
029import org.nuxeo.ecm.platform.annotations.repository.service.AnnotatedDocumentEventListener;
030import org.nuxeo.ecm.platform.annotations.repository.service.AnnotationsFulltextInjector;
031import org.nuxeo.ecm.platform.annotations.repository.service.AnnotationsRepositoryComponent;
032
033/**
034 * Extract the text of the body of the annotation to register it as a related text resource on the document for
035 * full-text indexing by the repository.
036 */
037public class AnnotationFulltextEventListener implements EventListener {
038
039    @Override
040    public void handleEvent(Event event) {
041        AnnotationsFulltextInjector injector = AnnotationsRepositoryComponent.instance.getFulltextInjector();
042        if (!(event.getContext() instanceof DocumentEventContext)) {
043            return;
044        }
045        DocumentEventContext context = (DocumentEventContext) event.getContext();
046        CoreSession session = context.getCoreSession();
047        DocumentModel doc = context.getSourceDocument();
048        if (doc == null) {
049            // no need to update a deleted document
050            return;
051        }
052        if (!doc.hasFacet(FacetNames.HAS_RELATED_TEXT)) {
053            // no full-text indexing of annotation for this document type
054            return;
055        }
056        String annotationId = (String) context.getProperty(AnnotatedDocumentEventListener.ANNOTATION_ID);
057        String annotationBody = (String) context.getProperty(AnnotatedDocumentEventListener.ANNOTATION_BODY);
058
059        if (AnnotatedDocumentEventListener.ANNOTATION_CREATED.equals(event.getName())) {
060            injector.setAnnotationText(doc, annotationId, annotationBody);
061            session.saveDocument(doc);
062        } else if (AnnotatedDocumentEventListener.ANNOTATION_DELETED.equals(event.getName())) {
063            if (injector.removeAnnotationText(doc, annotationId)) {
064                session.saveDocument(doc);
065            }
066        } else if (AnnotatedDocumentEventListener.ANNOTATION_UPDATED.equals(event.getName())) {
067            injector.removeAnnotationText(doc, annotationId);
068            injector.setAnnotationText(doc, annotationId, annotationBody);
069            session.saveDocument(doc);
070        } else if (DocumentEventTypes.DOCUMENT_CHECKEDIN.equals(event.getName())) {
071            // clean all annotation text before check-in: checked-in versions
072            // handle their own annotations independently of the live version
073            DocumentRef versionRef = (DocumentRef) context.getProperty("checkedInVersionRef");
074            DocumentModel version = session.getDocument(versionRef);
075            if (injector.removeAnnotationText(version, null)) {
076                session.saveDocument(version);
077            }
078        } else {
079            return;
080        }
081    }
082}