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