1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.event

File CommentEventGeneratorListener.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

4
16
3
1
112
64
5
0.31
5.33
3
1.67

Classes

Class Line # Actions
CommentEventGeneratorListener 52 16 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 61 tests. .

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 com.xpn.xwiki.internal.event;
21   
22    import java.util.Arrays;
23    import java.util.List;
24    import java.util.regex.Pattern;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.model.EntityType;
32    import org.xwiki.model.reference.EntityReferenceSerializer;
33    import org.xwiki.model.reference.ObjectReference;
34    import org.xwiki.model.reference.RegexEntityReference;
35    import org.xwiki.observation.EventListener;
36    import org.xwiki.observation.ObservationManager;
37    import org.xwiki.observation.event.Event;
38   
39    import com.xpn.xwiki.XWikiContext;
40    import com.xpn.xwiki.doc.XWikiDocument;
41    import com.xpn.xwiki.objects.BaseObject;
42   
43    /**
44    * Produce comments related events based on {@link XObjectEvent object events}.
45    *
46    * @version $Id: 711fed2ca012fbbbab721fec115ad35fd9c2c629 $
47    * @since 3.2M1
48    */
49    @Component
50    @Singleton
51    @Named("CommentEventGeneratorListener")
 
52    public class CommentEventGeneratorListener implements EventListener
53    {
54    /**
55    * The reference to match class XWiki.Comment on whatever wiki.
56    */
57    private static final RegexEntityReference COMMENTCLASS_REFERENCE = new RegexEntityReference(
58    Pattern.compile(".*:XWiki.XWikiComments\\[\\d*\\]"), EntityType.OBJECT);
59   
60    /**
61    * The matched events.
62    */
63    private static final List<Event> EVENTS = Arrays.<Event>asList(new XObjectAddedEvent(COMMENTCLASS_REFERENCE),
64    new XObjectDeletedEvent(COMMENTCLASS_REFERENCE), new XObjectUpdatedEvent(COMMENTCLASS_REFERENCE));
65   
66    /**
67    * Used to serializer document.
68    */
69    @Inject
70    private EntityReferenceSerializer<String> defaultEntityReferenceSerializer;
71   
72    @Inject
73    private ObservationManager observation;
74   
 
75  1105 toggle @Override
76    public String getName()
77    {
78  1105 return "CommentEventGeneratorListener";
79    }
80   
 
81  185 toggle @Override
82    public List<Event> getEvents()
83    {
84  185 return EVENTS;
85    }
86   
 
87  16 toggle @Override
88    public void onEvent(Event event, Object source, Object data)
89    {
90  16 XWikiDocument doc = (XWikiDocument) source;
91  16 XWikiDocument originalDoc = doc.getOriginalDocument();
92  16 XWikiContext context = (XWikiContext) data;
93   
94  16 XObjectEvent objectEvent = (XObjectEvent) event;
95   
96  16 String reference = this.defaultEntityReferenceSerializer.serialize(doc.getDocumentReference());
97   
98  16 if (event instanceof XObjectDeletedEvent) {
99  6 BaseObject obj = originalDoc.getXObject((ObjectReference) objectEvent.getReference());
100  6 String number = String.valueOf(obj.getNumber());
101  6 this.observation.notify(new CommentDeletedEvent(reference, number), source, context);
102    } else {
103  10 BaseObject obj = doc.getXObject((ObjectReference) objectEvent.getReference());
104  10 String number = String.valueOf(obj.getNumber());
105  10 if (event instanceof XObjectAddedEvent) {
106  9 this.observation.notify(new CommentAddedEvent(reference, number), source, context);
107    } else {
108  1 this.observation.notify(new CommentUpdatedEvent(reference, number), source, context);
109    }
110    }
111    }
112    }