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; 033 034/** 035 * Extract the text of the body of the annotation to register it as a related text resource on the document for 036 * full-text indexing by the repository. 037 */ 038public class AnnotationFulltextEventListener implements EventListener { 039 040 @Override 041 public void handleEvent(Event event) { 042 AnnotationsFulltextInjector injector = new AnnotationsFulltextInjector(); 043 if (!(event.getContext() instanceof DocumentEventContext)) { 044 return; 045 } 046 DocumentEventContext context = (DocumentEventContext) event.getContext(); 047 CoreSession session = context.getCoreSession(); 048 DocumentModel doc = context.getSourceDocument(); 049 if (doc == null) { 050 // no need to update a deleted document 051 return; 052 } 053 if (!doc.hasFacet(FacetNames.HAS_RELATED_TEXT)) { 054 // no full-text indexing of annotation for this document type 055 return; 056 } 057 String annotationId = (String) context.getProperty(AnnotatedDocumentEventListener.ANNOTATION_ID); 058 String annotationBody = (String) context.getProperty(AnnotatedDocumentEventListener.ANNOTATION_BODY); 059 060 if (AnnotatedDocumentEventListener.ANNOTATION_CREATED.equals(event.getName())) { 061 injector.setAnnotationText(doc, annotationId, annotationBody); 062 session.saveDocument(doc); 063 } else if (AnnotatedDocumentEventListener.ANNOTATION_DELETED.equals(event.getName())) { 064 if (injector.removeAnnotationText(doc, annotationId)) { 065 session.saveDocument(doc); 066 } 067 } else if (AnnotatedDocumentEventListener.ANNOTATION_UPDATED.equals(event.getName())) { 068 injector.removeAnnotationText(doc, annotationId); 069 injector.setAnnotationText(doc, annotationId, annotationBody); 070 session.saveDocument(doc); 071 } else if (DocumentEventTypes.DOCUMENT_CHECKEDIN.equals(event.getName())) { 072 // clean all annotation text before check-in: checked-in versions 073 // handle their own annotations independently of the live version 074 DocumentRef versionRef = (DocumentRef) context.getProperty("checkedInVersionRef"); 075 DocumentModel version = session.getDocument(versionRef); 076 if (injector.removeAnnotationText(version, null)) { 077 session.saveDocument(version); 078 } 079 } else { 080 return; 081 } 082 } 083}