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

File XClassPropertyEventGeneratorListener.java

 

Coverage histogram

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

Code metrics

12
27
6
1
142
84
12
0.44
4.5
6
2

Classes

Class Line # Actions
XClassPropertyEventGeneratorListener 53 27 0% 12 2
0.9555555695.6%
 

Contributing tests

This file is covered by 102 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.ObjectDiff;
41    import com.xpn.xwiki.objects.PropertyInterface;
42    import com.xpn.xwiki.objects.classes.BaseClass;
43   
44    /**
45    * Produce {@link XClassPropertyEvent property events} based on document events.
46    *
47    * @version $Id: eab79f7328aae73324791e427910fe88e3f234e0 $
48    * @since 3.2M1
49    */
50    @Component
51    @Singleton
52    @Named("XClassPropertyEventGeneratorListener")
 
53    public class XClassPropertyEventGeneratorListener 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  1730 toggle @Override
65    public String getName()
66    {
67  1730 return "XClassPropertyEventGeneratorListener";
68    }
69   
 
70  217 toggle @Override
71    public List<Event> getEvents()
72    {
73  217 return EVENTS;
74    }
75   
 
76  8460 toggle @Override
77    public void onEvent(Event event, Object source, Object data)
78    {
79  8460 XWikiDocument doc = (XWikiDocument) source;
80  8460 XWikiDocument originalDoc = doc.getOriginalDocument();
81  8460 XWikiContext context = (XWikiContext) data;
82   
83  8460 if (event instanceof DocumentUpdatedEvent) {
84  2385 onDocumentUpdatedEvent(originalDoc, doc, context);
85  6075 } else if (event instanceof DocumentDeletedEvent) {
86  291 onDocumentDeletedEvent(originalDoc, doc, context);
87  5784 } else if (event instanceof DocumentCreatedEvent) {
88  5784 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  5784 toggle private void onDocumentCreatedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
98    {
99  5784 for (PropertyInterface property : (Collection<PropertyInterface>) doc.getXClass().getFieldList()) {
100  9605 this.observation.notify(new XClassPropertyAddedEvent(property.getReference()), doc, context);
101    }
102    }
103   
104    /**
105    * @param originalDoc the previous version of the document
106    * @param doc the new version of the document
107    * @param context the XWiki context
108    */
 
109  291 toggle private void onDocumentDeletedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
110    {
111  291 for (PropertyInterface property : (Collection<PropertyInterface>) originalDoc.getXClass().getFieldList()) {
112  64 this.observation.notify(new XClassPropertyDeletedEvent(property.getReference()), doc, context);
113    }
114    }
115   
116    /**
117    * @param originalDoc the previous version of the document
118    * @param doc the new version of the document
119    * @param context the XWiki context
120    */
 
121  2385 toggle private void onDocumentUpdatedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
122    {
123  2385 BaseClass baseClass = doc.getXClass();
124  2385 BaseClass baseClassOriginal = originalDoc.getXClass();
125   
126  2385 for (List<ObjectDiff> objectChanges : doc.getClassDiff(originalDoc, doc, context)) {
127  147 for (ObjectDiff diff : objectChanges) {
128  499 PropertyInterface property = baseClass.getField(diff.getPropName());
129  499 PropertyInterface propertyOriginal = baseClassOriginal.getField(diff.getPropName());
130   
131  499 if (ObjectDiff.ACTION_PROPERTYREMOVED.equals(diff.getAction())) {
132  7 this.observation.notify(
133    new XClassPropertyDeletedEvent(propertyOriginal.getReference()), doc, context);
134  492 } else if (ObjectDiff.ACTION_PROPERTYADDED.equals(diff.getAction())) {
135  96 this.observation.notify(new XClassPropertyAddedEvent(property.getReference()), doc, context);
136  396 } else if (ObjectDiff.ACTION_PROPERTYCHANGED.equals(diff.getAction())) {
137  396 this.observation.notify(new XClassPropertyUpdatedEvent(property.getReference()), doc, context);
138    }
139    }
140    }
141    }
142    }