1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.internal.event

File XObjectEventGeneratorListener.java

 

Coverage histogram

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

Code metrics

22
46
7
1
183
118
20
0.43
6.57
7
2.86

Classes

Class Line # Actions
XObjectEventGeneratorListener 53 46 0% 20 2
0.9733333697.3%
 

Contributing tests

This file is covered by 103 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.Collection;
24    import java.util.List;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
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.observation.EventListener;
35    import org.xwiki.observation.ObservationManager;
36    import org.xwiki.observation.event.Event;
37   
38    import com.xpn.xwiki.XWikiContext;
39    import com.xpn.xwiki.doc.XWikiDocument;
40    import com.xpn.xwiki.objects.BaseObject;
41    import com.xpn.xwiki.objects.ObjectDiff;
42    import com.xpn.xwiki.objects.PropertyInterface;
43   
44    /**
45    * Produce {@link XObjectEvent} based on document events.
46    *
47    * @version $Id: 3baf932e76b28d20ef0f0baca064faebcab76d25 $
48    * @since 3.2M1
49    */
50    @Component
51    @Singleton
52    @Named("XObjectEventGeneratorListener")
 
53    public class XObjectEventGeneratorListener implements EventListener
54    {
55    /**
56    * The events to match.
57    */
58    private static final List<Event> EVENTS = Arrays.<Event>asList(new DocumentDeletedEvent(),
59    new DocumentCreatedEvent(), new DocumentUpdatedEvent());
60   
61    @Inject
62    private ObservationManager observation;
63   
 
64  1720 toggle @Override
65    public String getName()
66    {
67  1720 return "XObjectEventGeneratorListener";
68    }
69   
 
70  215 toggle @Override
71    public List<Event> getEvents()
72    {
73  215 return EVENTS;
74    }
75   
 
76  8456 toggle @Override
77    public void onEvent(Event event, Object source, Object data)
78    {
79  8456 XWikiDocument doc = (XWikiDocument) source;
80  8456 XWikiDocument originalDoc = doc.getOriginalDocument();
81  8456 XWikiContext context = (XWikiContext) data;
82   
83  8456 if (event instanceof DocumentUpdatedEvent) {
84  2385 onDocumentUpdatedEvent(originalDoc, doc, context);
85  6071 } else if (event instanceof DocumentDeletedEvent) {
86  291 onDocumentDeletedEvent(originalDoc, doc, context);
87  5780 } else if (event instanceof DocumentCreatedEvent) {
88  5780 onDocumentCreatedEvent(originalDoc, doc, context);
89    }
90    }
91   
92    /**
93    * @param originalDoc the previous version of the document
94    * @param doc the new version of the document
95    * @param context the XWiki context
96    */
 
97  5780 toggle private void onDocumentCreatedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
98    {
99  5780 for (List<BaseObject> xobjects : doc.getXObjects().values()) {
100  4382 for (BaseObject xobject : xobjects) {
101  6146 if (xobject != null) {
102  5925 this.observation.notify(new XObjectAddedEvent(xobject.getReference()), doc, context);
103  5925 for (PropertyInterface property : (Collection<PropertyInterface>) xobject.getFieldList()) {
104  30932 this.observation.notify(new XObjectPropertyAddedEvent(property.getReference()), doc, context);
105    }
106    }
107    }
108    }
109    }
110   
111    /**
112    * @param originalDoc the previous version of the document
113    * @param doc the new version of the document
114    * @param context the XWiki context
115    */
 
116  291 toggle private void onDocumentDeletedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
117    {
118  291 for (List<BaseObject> xobjects : originalDoc.getXObjects().values()) {
119  198 for (BaseObject xobject : xobjects) {
120  254 if (xobject != null) {
121  253 this.observation.notify(new XObjectDeletedEvent(xobject.getReference()), doc, context);
122  253 for (PropertyInterface property : (Collection<PropertyInterface>) xobject.getFieldList()) {
123  1829 this.observation.notify(new XObjectPropertyDeletedEvent(property.getReference()), doc, context);
124    }
125    }
126    }
127    }
128    }
129   
130    /**
131    * @param originalDoc the previous version of the document
132    * @param doc the new version of the document
133    * @param context the XWiki context
134    */
 
135  2385 toggle private void onDocumentUpdatedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
136    {
137  2385 for (List<ObjectDiff> objectChanges : doc.getObjectDiff(originalDoc, doc, context)) {
138  924 boolean modified = false;
139  924 for (ObjectDiff diff : objectChanges) {
140  2092 BaseObject xobject = doc.getXObject(diff.getXClassReference(), diff.getNumber());
141  2092 BaseObject xobjectOriginal = originalDoc.getXObject(diff.getXClassReference(), diff.getNumber());
142  2092 if (ObjectDiff.ACTION_OBJECTREMOVED.equals(diff.getAction())) {
143  45 this.observation.notify(new XObjectDeletedEvent(xobjectOriginal.getReference()), doc, context);
144    } else {
145  2047 if (ObjectDiff.ACTION_OBJECTADDED.equals(diff.getAction())) {
146  334 this.observation.notify(new XObjectAddedEvent(xobject.getReference()), doc, context);
147    } else {
148  1713 if (!modified && xobject != null && xobjectOriginal != null) {
149  545 this.observation.notify(new XObjectUpdatedEvent(xobject.getReference()), doc, context);
150  545 modified = true;
151    }
152   
153  1713 onObjectPropertyModified(doc, diff, context);
154    }
155    }
156    }
157    }
158    }
159   
160    /**
161    * Generate object property related events.
162    *
163    * @param doc the new version of the document
164    * @param diff the diff entry
165    * @param context the XWiki context
166    */
 
167  1713 toggle private void onObjectPropertyModified(XWikiDocument doc, ObjectDiff diff, XWikiContext context)
168    {
169  1713 if (ObjectDiff.ACTION_PROPERTYREMOVED.equals(diff.getAction())) {
170  111 BaseObject object = doc.getOriginalDocument().getXObject(diff.getXClassReference(), diff.getNumber());
171  111 PropertyInterface property = object.getField(diff.getPropName());
172  111 this.observation.notify(new XObjectPropertyDeletedEvent(property.getReference()), doc, context);
173    } else {
174  1602 BaseObject object = doc.getXObject(diff.getXClassReference(), diff.getNumber());
175  1602 PropertyInterface property = object.getField(diff.getPropName());
176  1602 if (ObjectDiff.ACTION_PROPERTYADDED.equals(diff.getAction())) {
177  807 this.observation.notify(new XObjectPropertyAddedEvent(property.getReference()), doc, context);
178  795 } else if (ObjectDiff.ACTION_PROPERTYCHANGED.equals(diff.getAction())) {
179  795 this.observation.notify(new XObjectPropertyUpdatedEvent(property.getReference()), doc, context);
180    }
181    }
182    }
183    }