1. Project Clover database Tue Dec 20 2016 21:24:09 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 3
0.9696%
 

Contributing tests

This file is covered by 91 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  1472 toggle @Override
65    public String getName()
66    {
67  1472 return "XObjectEventGeneratorListener";
68    }
69   
 
70  184 toggle @Override
71    public List<Event> getEvents()
72    {
73  184 return EVENTS;
74    }
75   
 
76  4117 toggle @Override
77    public void onEvent(Event event, Object source, Object data)
78    {
79  4117 XWikiDocument doc = (XWikiDocument) source;
80  4117 XWikiDocument originalDoc = doc.getOriginalDocument();
81  4117 XWikiContext context = (XWikiContext) data;
82   
83  4117 if (event instanceof DocumentUpdatedEvent) {
84  458 onDocumentUpdatedEvent(originalDoc, doc, context);
85  3659 } else if (event instanceof DocumentDeletedEvent) {
86  157 onDocumentDeletedEvent(originalDoc, doc, context);
87  3502 } else if (event instanceof DocumentCreatedEvent) {
88  3502 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  3502 toggle private void onDocumentCreatedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
98    {
99  3502 for (List<BaseObject> xobjects : doc.getXObjects().values()) {
100  2776 for (BaseObject xobject : xobjects) {
101  4014 if (xobject != null) {
102  3812 this.observation.notify(new XObjectAddedEvent(xobject.getReference()), doc, context);
103  3812 for (PropertyInterface property : (Collection<PropertyInterface>) xobject.getFieldList()) {
104  13211 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  157 toggle private void onDocumentDeletedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
117    {
118  157 for (List<BaseObject> xobjects : originalDoc.getXObjects().values()) {
119  120 for (BaseObject xobject : xobjects) {
120  130 if (xobject != null) {
121  130 this.observation.notify(new XObjectDeletedEvent(xobject.getReference()), doc, context);
122  130 for (PropertyInterface property : (Collection<PropertyInterface>) xobject.getFieldList()) {
123  540 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  458 toggle private void onDocumentUpdatedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
136    {
137  458 for (List<ObjectDiff> objectChanges : doc.getObjectDiff(originalDoc, doc, context)) {
138  248 boolean modified = false;
139  248 for (ObjectDiff diff : objectChanges) {
140  470 BaseObject xobject = doc.getXObject(diff.getXClassReference(), diff.getNumber());
141  470 BaseObject xobjectOriginal = originalDoc.getXObject(diff.getXClassReference(), diff.getNumber());
142  470 if (ObjectDiff.ACTION_OBJECTREMOVED.equals(diff.getAction())) {
143  13 this.observation.notify(new XObjectDeletedEvent(xobjectOriginal.getReference()), doc, context);
144    } else {
145  457 if (ObjectDiff.ACTION_OBJECTADDED.equals(diff.getAction())) {
146  98 this.observation.notify(new XObjectAddedEvent(xobject.getReference()), doc, context);
147    } else {
148  359 if (!modified && xobject != null && xobjectOriginal != null) {
149  137 this.observation.notify(new XObjectUpdatedEvent(xobject.getReference()), doc, context);
150  137 modified = true;
151    }
152   
153  359 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  359 toggle private void onObjectPropertyModified(XWikiDocument doc, ObjectDiff diff, XWikiContext context)
168    {
169  359 if (ObjectDiff.ACTION_PROPERTYREMOVED.equals(diff.getAction())) {
170  57 BaseObject object = doc.getOriginalDocument().getXObject(diff.getXClassReference(), diff.getNumber());
171  57 PropertyInterface property = object.getField(diff.getPropName());
172  57 this.observation.notify(new XObjectPropertyDeletedEvent(property.getReference()), doc, context);
173    } else {
174  302 BaseObject object = doc.getXObject(diff.getXClassReference(), diff.getNumber());
175  302 PropertyInterface property = object.getField(diff.getPropName());
176  302 if (ObjectDiff.ACTION_PROPERTYADDED.equals(diff.getAction())) {
177  200 this.observation.notify(new XObjectPropertyAddedEvent(property.getReference()), doc, context);
178  102 } else if (ObjectDiff.ACTION_PROPERTYCHANGED.equals(diff.getAction())) {
179  102 this.observation.notify(new XObjectPropertyUpdatedEvent(property.getReference()), doc, context);
180    }
181    }
182    }
183    }