1. Project Clover database Sat Feb 2 2019 06:45:20 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
15
2
1
99
54
4
0.27
7.5
2
2

Classes

Class Line # Actions
CommentEventGeneratorListener 48 15 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 73 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 javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Singleton;
25   
26    import org.xwiki.component.annotation.Component;
27    import org.xwiki.model.reference.EntityReferenceSerializer;
28    import org.xwiki.model.reference.ObjectReference;
29    import org.xwiki.model.reference.RegexEntityReference;
30    import org.xwiki.observation.AbstractEventListener;
31    import org.xwiki.observation.ObservationManager;
32    import org.xwiki.observation.event.Event;
33   
34    import com.xpn.xwiki.XWikiContext;
35    import com.xpn.xwiki.doc.XWikiDocument;
36    import com.xpn.xwiki.internal.mandatory.XWikiCommentsDocumentInitializer;
37    import com.xpn.xwiki.objects.BaseObject;
38   
39    /**
40    * Produce comments related events based on {@link XObjectEvent object events}.
41    *
42    * @version $Id: db00b7677333300b35308547aecabc08beaa6235 $
43    * @since 3.2M1
44    */
45    @Component
46    @Singleton
47    @Named("CommentEventGeneratorListener")
 
48    public class CommentEventGeneratorListener extends AbstractEventListener
49    {
50    /**
51    * The reference to match class XWiki.Comment on whatever wiki.
52    */
53    private static final RegexEntityReference COMMENTCLASS_REFERENCE =
54    XWikiCommentsDocumentInitializer.OBJECT_REFERENCE;
55   
56    /**
57    * Used to serializer document.
58    */
59    @Inject
60    private EntityReferenceSerializer<String> defaultEntityReferenceSerializer;
61   
62    @Inject
63    private ObservationManager observation;
64   
65    /**
66    * Default constructor.
67    */
 
68  220 toggle public CommentEventGeneratorListener()
69    {
70  220 super("CommentEventGeneratorListener", new XObjectAddedEvent(COMMENTCLASS_REFERENCE),
71    new XObjectDeletedEvent(COMMENTCLASS_REFERENCE), new XObjectUpdatedEvent(COMMENTCLASS_REFERENCE));
72    }
73   
 
74  26 toggle @Override
75    public void onEvent(Event event, Object source, Object data)
76    {
77  26 XWikiDocument doc = (XWikiDocument) source;
78  26 XWikiDocument originalDoc = doc.getOriginalDocument();
79  26 XWikiContext context = (XWikiContext) data;
80   
81  26 XObjectEvent objectEvent = (XObjectEvent) event;
82   
83  26 String reference = this.defaultEntityReferenceSerializer.serialize(doc.getDocumentReference());
84   
85  26 if (event instanceof XObjectDeletedEvent) {
86  7 BaseObject obj = originalDoc.getXObject((ObjectReference) objectEvent.getReference());
87  7 String number = String.valueOf(obj.getNumber());
88  7 this.observation.notify(new CommentDeletedEvent(reference, number), source, context);
89    } else {
90  19 BaseObject obj = doc.getXObject((ObjectReference) objectEvent.getReference());
91  19 String number = String.valueOf(obj.getNumber());
92  19 if (event instanceof XObjectAddedEvent) {
93  17 this.observation.notify(new CommentAddedEvent(reference, number), source, context);
94    } else {
95  2 this.observation.notify(new CommentUpdatedEvent(reference, number), source, context);
96    }
97    }
98    }
99    }