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

File AttachmentEventGeneratorListener.java

 

Coverage histogram

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

Code metrics

10
27
6
1
149
86
11
0.41
4.5
6
1.83

Classes

Class Line # Actions
AttachmentEventGeneratorListener 54 27 0% 11 1
0.976744297.7%
 

Contributing tests

This file is covered by 83 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   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.apache.commons.lang3.StringUtils;
30    import org.xwiki.bridge.event.DocumentCreatedEvent;
31    import org.xwiki.bridge.event.DocumentDeletedEvent;
32    import org.xwiki.bridge.event.DocumentUpdatedEvent;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.model.reference.EntityReferenceSerializer;
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.AttachmentDiff;
41    import com.xpn.xwiki.doc.XWikiAttachment;
42    import com.xpn.xwiki.doc.XWikiDocument;
43    import com.xpn.xwiki.web.Utils;
44   
45    /**
46    * Produce attachment events based on document events.
47    *
48    * @version $Id: e96dba61bbf10075c5e8e311cefc076c9f4915df $
49    * @since 3.2M1
50    */
51    @Component
52    @Singleton
53    @Named("AttachmentEventGeneratorListener")
 
54    public class AttachmentEventGeneratorListener implements EventListener
55    {
56    /**
57    * The events to match.
58    */
59    private static final List<Event> EVENTS = Arrays.<Event>asList(new DocumentDeletedEvent(),
60    new DocumentCreatedEvent(), new DocumentUpdatedEvent());
61   
62    /**
63    * Used to serializer document.
64    */
65    @Inject
66    private EntityReferenceSerializer<String> defaultEntityReferenceSerializer;
67   
 
68  1199 toggle @Override
69    public String getName()
70    {
71  1199 return "AttachmentEventGeneratorListener";
72    }
73   
 
74  184 toggle @Override
75    public List<Event> getEvents()
76    {
77  184 return EVENTS;
78    }
79   
 
80  4109 toggle @Override
81    public void onEvent(Event event, Object source, Object data)
82    {
83  4109 XWikiDocument doc = (XWikiDocument) source;
84  4109 XWikiDocument originalDoc = doc.getOriginalDocument();
85  4109 XWikiContext context = (XWikiContext) data;
86   
87  4109 if (event instanceof DocumentUpdatedEvent) {
88  452 onDocumentUpdatedEvent(originalDoc, doc, context);
89  3657 } else if (event instanceof DocumentDeletedEvent) {
90  156 onDocumentDeletedEvent(originalDoc, doc, context);
91  3501 } else if (event instanceof DocumentCreatedEvent) {
92  3501 onDocumentCreatedEvent(originalDoc, doc, context);
93    }
94    }
95   
96    /**
97    * @param originalDoc the previous version of the document
98    * @param doc the new version of the document
99    * @param context the XWiki context
100    */
 
101  3501 toggle private void onDocumentCreatedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
102    {
103  3501 ObservationManager observation = Utils.getComponent(ObservationManager.class);
104   
105  3501 for (XWikiAttachment attachment : doc.getAttachmentList()) {
106  368 String reference =
107    this.defaultEntityReferenceSerializer.serialize(attachment.getDoc().getDocumentReference());
108  368 observation.notify(new AttachmentAddedEvent(reference, attachment.getFilename()), doc, context);
109    }
110    }
111   
112    /**
113    * @param originalDoc the previous version of the document
114    * @param doc the new version of the document
115    * @param context the XWiki context
116    */
 
117  156 toggle private void onDocumentDeletedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
118    {
119  156 ObservationManager observation = Utils.getComponent(ObservationManager.class);
120   
121  156 for (XWikiAttachment attachment : originalDoc.getAttachmentList()) {
122  17 String reference =
123    this.defaultEntityReferenceSerializer.serialize(attachment.getDoc().getDocumentReference());
124  17 observation.notify(new AttachmentDeletedEvent(reference, attachment.getFilename()), doc, context);
125    }
126    }
127   
128    /**
129    * @param originalDoc the previous version of the document
130    * @param doc the new version of the document
131    * @param context the XWiki context
132    */
 
133  452 toggle private void onDocumentUpdatedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
134    {
135  452 ObservationManager observation = Utils.getComponent(ObservationManager.class);
136   
137  452 String reference = this.defaultEntityReferenceSerializer.serialize(doc.getDocumentReference());
138   
139  452 for (AttachmentDiff diff : doc.getAttachmentDiff(originalDoc, doc, context)) {
140  61 if (StringUtils.isEmpty(diff.getOrigVersion())) {
141  51 observation.notify(new AttachmentAddedEvent(reference, diff.getFileName()), doc, context);
142  10 } else if (StringUtils.isEmpty(diff.getNewVersion())) {
143  7 observation.notify(new AttachmentDeletedEvent(reference, diff.getFileName()), doc, context);
144    } else {
145  3 observation.notify(new AttachmentUpdatedEvent(reference, diff.getFileName()), doc, context);
146    }
147    }
148    }
149    }