1. Project Clover database Tue Dec 20 2016 21:24:09 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 89 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  1477 toggle @Override
65    public String getName()
66    {
67  1477 return "XClassPropertyEventGeneratorListener";
68    }
69   
 
70  185 toggle @Override
71    public List<Event> getEvents()
72    {
73  185 return EVENTS;
74    }
75   
 
76  4116 toggle @Override
77    public void onEvent(Event event, Object source, Object data)
78    {
79  4116 XWikiDocument doc = (XWikiDocument) source;
80  4116 XWikiDocument originalDoc = doc.getOriginalDocument();
81  4116 XWikiContext context = (XWikiContext) data;
82   
83  4116 if (event instanceof DocumentUpdatedEvent) {
84  457 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 (PropertyInterface property : (Collection<PropertyInterface>) doc.getXClass().getFieldList()) {
100  8248 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  157 toggle private void onDocumentDeletedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
110    {
111  157 for (PropertyInterface property : (Collection<PropertyInterface>) originalDoc.getXClass().getFieldList()) {
112  349 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  457 toggle private void onDocumentUpdatedEvent(XWikiDocument originalDoc, XWikiDocument doc, XWikiContext context)
122    {
123  457 BaseClass baseClass = doc.getXClass();
124  457 BaseClass baseClassOriginal = originalDoc.getXClass();
125   
126  457 for (List<ObjectDiff> objectChanges : doc.getClassDiff(originalDoc, doc, context)) {
127  14 for (ObjectDiff diff : objectChanges) {
128  19 PropertyInterface property = baseClass.getField(diff.getPropName());
129  19 PropertyInterface propertyOriginal = baseClassOriginal.getField(diff.getPropName());
130   
131  19 if (ObjectDiff.ACTION_PROPERTYREMOVED.equals(diff.getAction())) {
132  1 this.observation.notify(
133    new XClassPropertyDeletedEvent(propertyOriginal.getReference()), doc, context);
134  18 } else if (ObjectDiff.ACTION_PROPERTYADDED.equals(diff.getAction())) {
135  11 this.observation.notify(new XClassPropertyAddedEvent(property.getReference()), doc, context);
136  7 } else if (ObjectDiff.ACTION_PROPERTYCHANGED.equals(diff.getAction())) {
137  7 this.observation.notify(new XClassPropertyUpdatedEvent(property.getReference()), doc, context);
138    }
139    }
140    }
141    }
142    }