1. Project Clover database Sat Feb 2 2019 06:45:20 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 3
0.930232693%
 

Contributing tests

This file is covered by 95 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  1323 toggle @Override
69    public String getName()
70    {
71  1323 return "AttachmentEventGeneratorListener";
72    }
73   
 
74  215 toggle @Override
75    public List<Event> getEvents()
76    {
77  215 return EVENTS;
78    }
79   
 
80  8448 toggle @Override
81    public void onEvent(Event event, Object source, Object data)
82    {
83  8448 XWikiDocument doc = (XWikiDocument) source;
84  8448 XWikiDocument originalDoc = doc.getOriginalDocument();
85  8448 XWikiContext context = (XWikiContext) data;
86   
87  8448 if (event instanceof DocumentUpdatedEvent) {
88  2379 onDocumentUpdatedEvent(originalDoc, doc, context);
89  6069 } else if (event instanceof DocumentDeletedEvent) {
90  290 onDocumentDeletedEvent(originalDoc, doc, context);
91  5779 } else if (event instanceof DocumentCreatedEvent) {
92  5779 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  5779 toggle private void onDocumentCreatedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
102    {
103  5779 ObservationManager observation = Utils.getComponent(ObservationManager.class);
104   
105  5779 for (XWikiAttachment attachment : doc.getAttachmentList()) {
106  566 String reference =
107    this.defaultEntityReferenceSerializer.serialize(attachment.getDoc().getDocumentReference());
108  566 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  290 toggle private void onDocumentDeletedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
118    {
119  290 ObservationManager observation = Utils.getComponent(ObservationManager.class);
120   
121  290 for (XWikiAttachment attachment : originalDoc.getAttachmentList()) {
122  25 String reference =
123    this.defaultEntityReferenceSerializer.serialize(attachment.getDoc().getDocumentReference());
124  25 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  2379 toggle private void onDocumentUpdatedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
134    {
135  2379 ObservationManager observation = Utils.getComponent(ObservationManager.class);
136   
137  2379 String reference = this.defaultEntityReferenceSerializer.serialize(doc.getDocumentReference());
138   
139  2379 for (AttachmentDiff diff : doc.getAttachmentDiff(originalDoc, doc, context)) {
140  75 if (StringUtils.isEmpty(diff.getOrigVersion())) {
141  65 observation.notify(new AttachmentAddedEvent(reference, diff.getFileName()), doc, context);
142  10 } else if (StringUtils.isEmpty(diff.getNewVersion())) {
143  10 observation.notify(new AttachmentDeletedEvent(reference, diff.getFileName()), doc, context);
144    } else {
145  0 observation.notify(new AttachmentUpdatedEvent(reference, diff.getFileName()), doc, context);
146    }
147    }
148    }
149    }