1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.annotation.maintainer.internal

File DocumentContentAnnotationUpdateListener.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

2
12
3
1
119
58
6
0.5
4
3
2

Classes

Class Line # Actions
DocumentContentAnnotationUpdateListener 50 12 0% 6 17
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.annotation.maintainer.internal;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.List;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
30    import org.slf4j.Logger;
31    import org.xwiki.annotation.maintainer.AnnotationMaintainer;
32    import org.xwiki.annotation.maintainer.MaintainerServiceException;
33    import org.xwiki.bridge.DocumentModelBridge;
34    import org.xwiki.bridge.event.DocumentUpdatedEvent;
35    import org.xwiki.component.annotation.Component;
36    import org.xwiki.model.reference.EntityReferenceSerializer;
37    import org.xwiki.observation.EventListener;
38    import org.xwiki.observation.event.Event;
39   
40    /**
41    * Event listener to listen to documents update events and update the annotations that are impacted by the document
42    * <strong>content</strong> change, to update the selection and context to match the new document content. <br>
43    *
44    * @version $Id: a1360751a7852813c894b39aee045a4f9b490914 $
45    * @since 2.3M1
46    */
47    @Component
48    @Named("document-content-annotation-updater")
49    @Singleton
 
50    public class DocumentContentAnnotationUpdateListener implements EventListener
51    {
52    /**
53    * Entity reference serializer, to serialize the modified document reference to send to the annotations service.
54    */
55    @Inject
56    private EntityReferenceSerializer<String> serializer;
57   
58    /**
59    * Annotations maintainer, to be invoked for the document content update.
60    */
61    @Inject
62    private AnnotationMaintainer maintainer;
63   
64    /**
65    * The logger to log.
66    */
67    @Inject
68    private Logger logger;
69   
70    /**
71    * Marks that there is currently an annotations update in progress so all the saves should not trigger a new update.
72    * All document edits that take place because of updating the annotations for the current document shouldn't be
73    * considered.
74    */
75    private volatile boolean isUpdating;
76   
77    /**
78    * The events observed by this observation manager.
79    */
80    private final List<Event> eventsList = new ArrayList<Event>(Arrays.asList(new DocumentUpdatedEvent()));
81   
 
82  0 toggle @Override
83    public List<Event> getEvents()
84    {
85  0 return eventsList;
86    }
87   
 
88  0 toggle @Override
89    public String getName()
90    {
91  0 return "DocumentContentAnnotationUpdateListener";
92    }
93   
 
94  0 toggle @Override
95    public void onEvent(Event event, Object source, Object data)
96    {
97  0 DocumentModelBridge currentDocument = (DocumentModelBridge) source;
98   
99  0 DocumentModelBridge previousDocument = currentDocument.getOriginalDocument();
100   
101    // if it's not a modification triggered by the updates of the annotations while running the same annotation
102    // maintainer, and the difference is in the content of the document
103  0 if (!isUpdating && !previousDocument.getContent().equals(currentDocument.getContent())) {
104  0 isUpdating = true;
105  0 String content = currentDocument.getContent();
106  0 String previousContent = previousDocument.getContent();
107   
108    // maintain the document annotations
109  0 try {
110  0 maintainer.updateAnnotations(this.serializer.serialize(currentDocument.getDocumentReference()),
111    previousContent, content);
112    } catch (MaintainerServiceException e) {
113  0 this.logger.warn(e.getMessage(), e);
114    // nothing else, just go further
115    }
116  0 isUpdating = false;
117    }
118    }
119    }