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

File TemplateListener.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart2.png
81% of files have more coverage

Code metrics

14
16
2
1
96
59
9
0.56
8
2
4.5

Classes

Class Line # Actions
TemplateListener 53 16 0% 9 26
0.187518.8%
 

Contributing tests

This file is covered by 59 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.template;
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.AttachmentReference;
28    import org.xwiki.model.reference.EntityReferenceSerializer;
29    import org.xwiki.observation.AbstractEventListener;
30    import org.xwiki.observation.ObservationManager;
31    import org.xwiki.observation.event.Event;
32    import org.xwiki.template.event.TemplateDeletedEvent;
33    import org.xwiki.template.event.TemplateUpdatedEvent;
34   
35    import com.xpn.xwiki.doc.XWikiDocument;
36    import com.xpn.xwiki.internal.event.AbstractAttachmentEvent;
37    import com.xpn.xwiki.internal.event.AttachmentDeletedEvent;
38    import com.xpn.xwiki.internal.event.AttachmentUpdatedEvent;
39    import com.xpn.xwiki.internal.event.XObjectPropertyDeletedEvent;
40    import com.xpn.xwiki.internal.event.XObjectPropertyEvent;
41    import com.xpn.xwiki.internal.event.XObjectPropertyUpdatedEvent;
42    import com.xpn.xwiki.internal.skin.WikiSkinUtils;
43   
44    /**
45    * Listener to modification to wiki based template and generate related {@link org.xwiki.template.event.TemplateEvent}s.
46    *
47    * @version $Id: 7cfa18ef5ea107216181d725d6a71a380753cc41 $
48    */
49    @Component
50    @Named("templates")
51    @Singleton
52    // TODO: add support for TemplateAddedEvent
 
53    public class TemplateListener extends AbstractEventListener
54    {
55    @Inject
56    private EntityReferenceSerializer<String> referenceSerializer;
57   
58    @Inject
59    private ObservationManager observation;
60   
61    /**
62    * Default constructor.
63    */
 
64  185 toggle public TemplateListener()
65    {
66  185 super("templates", new XObjectPropertyUpdatedEvent(), new XObjectPropertyDeletedEvent(),
67    new AttachmentDeletedEvent(), new AttachmentUpdatedEvent());
68    }
69   
 
70  724 toggle @Override
71    public void onEvent(Event event, Object source, Object data)
72    {
73  724 XWikiDocument document = (XWikiDocument) source;
74   
75    // Is this a skin document
76  724 if (document.getXObject(WikiSkinUtils.SKINCLASS_REFERENCE) != null) {
77  0 if (event instanceof AbstractAttachmentEvent) {
78  0 AttachmentReference attachment = new AttachmentReference(((AbstractAttachmentEvent) event).getName(),
79    document.getDocumentReference());
80  0 String id = this.referenceSerializer.serialize(attachment);
81  0 if (event instanceof AttachmentDeletedEvent) {
82  0 this.observation.notify(new TemplateDeletedEvent(id), this);
83  0 } else if (event instanceof AttachmentUpdatedEvent) {
84  0 this.observation.notify(new TemplateUpdatedEvent(id), this);
85    }
86  0 } else if (event instanceof XObjectPropertyEvent) {
87  0 String id = this.referenceSerializer.serialize(((XObjectPropertyEvent) event).getReference());
88  0 if (event instanceof XObjectPropertyDeletedEvent) {
89  0 this.observation.notify(new TemplateDeletedEvent(id), this);
90  0 } else if (event instanceof XObjectPropertyUpdatedEvent) {
91  0 this.observation.notify(new TemplateUpdatedEvent(id), this);
92    }
93    }
94    }
95    }
96    }