1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.annotation.internal; |
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.Provider; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.slf4j.Logger; |
31 |
|
import org.xwiki.annotation.Annotation; |
32 |
|
import org.xwiki.annotation.AnnotationConfiguration; |
33 |
|
import org.xwiki.annotation.event.AnnotationAddedEvent; |
34 |
|
import org.xwiki.annotation.event.AnnotationDeletedEvent; |
35 |
|
import org.xwiki.annotation.event.AnnotationUpdatedEvent; |
36 |
|
import org.xwiki.component.annotation.Role; |
37 |
|
import org.xwiki.context.Execution; |
38 |
|
import org.xwiki.model.reference.DocumentReference; |
39 |
|
import org.xwiki.model.reference.EntityReference; |
40 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
41 |
|
import org.xwiki.observation.EventListener; |
42 |
|
import org.xwiki.observation.ObservationManager; |
43 |
|
import org.xwiki.observation.event.Event; |
44 |
|
|
45 |
|
import com.xpn.xwiki.XWikiContext; |
46 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
47 |
|
import com.xpn.xwiki.internal.event.XObjectAddedEvent; |
48 |
|
import com.xpn.xwiki.internal.event.XObjectDeletedEvent; |
49 |
|
import com.xpn.xwiki.internal.event.XObjectEvent; |
50 |
|
import com.xpn.xwiki.internal.event.XObjectUpdatedEvent; |
51 |
|
import com.xpn.xwiki.objects.BaseObject; |
52 |
|
import com.xpn.xwiki.objects.BaseObjectReference; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
@link |
57 |
|
|
58 |
|
@version |
59 |
|
@since |
60 |
|
|
61 |
|
@Role |
62 |
|
@Named("AnnotationEventGeneratorEventListener") |
63 |
|
@Singleton |
|
|
| 78% |
Uncovered Elements: 13 (59) |
Complexity: 14 |
Complexity Density: 0.36 |
|
64 |
|
public class AnnotationEventGeneratorEventListener implements EventListener |
65 |
|
{ |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
private static final List<Event> EVENTS = Arrays.<Event> asList(new XObjectAddedEvent(), new XObjectUpdatedEvent(), |
70 |
|
new XObjectDeletedEvent()); |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
@Inject |
76 |
|
protected AnnotationConfiguration annotationConfiguration; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
@Inject |
82 |
|
protected Provider<ObservationManager> observationManager; |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
@Inject |
88 |
|
protected Execution execution; |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
@Inject |
94 |
|
protected EntityReferenceSerializer<String> defaultEntityReferenceSerializer; |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
@Inject |
100 |
|
protected Logger logger; |
101 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
102 |
8 |
@Override... |
103 |
|
public String getName() |
104 |
|
{ |
105 |
8 |
return this.getClass().getAnnotation(Named.class).value(); |
106 |
|
} |
107 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
108 |
1 |
@Override... |
109 |
|
public List<Event> getEvents() |
110 |
|
{ |
111 |
1 |
return EVENTS; |
112 |
|
} |
113 |
|
|
|
|
| 75.6% |
Uncovered Elements: 11 (45) |
Complexity: 10 |
Complexity Density: 0.32 |
|
114 |
8 |
@Override... |
115 |
|
public void onEvent(Event event, Object source, Object data) |
116 |
|
{ |
117 |
|
|
118 |
8 |
XWikiContext context = (XWikiContext) execution.getContext().getProperty("xwikicontext"); |
119 |
8 |
String currentWiki = context.getWikiId(); |
120 |
|
|
121 |
8 |
try { |
122 |
8 |
XWikiDocument document = (XWikiDocument) source; |
123 |
8 |
String wikiOfAffectedDocument = document.getDocumentReference().getWikiReference().getName(); |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
8 |
context.setWikiId(wikiOfAffectedDocument); |
129 |
|
|
130 |
|
|
131 |
8 |
if (!annotationConfiguration.isInstalled()) { |
132 |
0 |
return; |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
8 |
BaseObjectReference objectReference = getBaseObjectReference((XObjectEvent) event); |
137 |
8 |
DocumentReference objectClassReference = objectReference.getXClassReference(); |
138 |
|
|
139 |
|
|
140 |
8 |
if (!objectClassReference.equals(annotationConfiguration.getAnnotationClassReference())) { |
141 |
0 |
return; |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
8 |
BaseObject object = document.getXObject(objectReference); |
146 |
|
|
147 |
|
|
148 |
8 |
Event newEvent = null; |
149 |
8 |
String documentReference = defaultEntityReferenceSerializer.serialize(document.getDocumentReference()); |
150 |
8 |
String number = String.valueOf(objectReference.getObjectNumber()); |
151 |
8 |
if (event instanceof XObjectAddedEvent) { |
152 |
4 |
newEvent = new AnnotationAddedEvent(documentReference, number); |
153 |
4 |
} else if (event instanceof XObjectUpdatedEvent) { |
154 |
0 |
newEvent = new AnnotationUpdatedEvent(documentReference, number); |
155 |
4 |
} else if (event instanceof XObjectDeletedEvent) { |
156 |
|
|
157 |
4 |
object = document.getOriginalDocument().getXObject(objectReference); |
158 |
4 |
newEvent = new AnnotationDeletedEvent(documentReference, number); |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
8 |
DocumentReference defaultCommentsClassReference = |
164 |
|
context.getWiki().getCommentsClass(context).getDocumentReference(); |
165 |
8 |
if (defaultCommentsClassReference.equals(object.getXClassReference())) { |
166 |
|
|
167 |
8 |
String selection = object.getStringValue(Annotation.SELECTION_FIELD); |
168 |
8 |
if (selection == null || selection.trim().length() == 0) { |
169 |
|
|
170 |
0 |
return; |
171 |
|
} |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
8 |
observationManager.get().notify(newEvent, source, context); |
176 |
|
} catch (Exception e) { |
177 |
0 |
logger.error("Failed to handle event of type [{}]", event.getClass().getName(), e); |
178 |
|
} finally { |
179 |
|
|
180 |
8 |
context.setWikiId(currentWiki); |
181 |
|
} |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
|
@param |
186 |
|
@return@link |
187 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
188 |
8 |
private BaseObjectReference getBaseObjectReference(XObjectEvent objectEvent)... |
189 |
|
{ |
190 |
8 |
EntityReference objectReference = objectEvent.getReference(); |
191 |
8 |
BaseObjectReference baseObjectReference = null; |
192 |
8 |
if (objectReference instanceof BaseObjectReference) { |
193 |
8 |
baseObjectReference = (BaseObjectReference) objectEvent.getReference(); |
194 |
|
} else { |
195 |
0 |
baseObjectReference = new BaseObjectReference(objectEvent.getReference()); |
196 |
|
} |
197 |
8 |
return baseObjectReference; |
198 |
|
} |
199 |
|
} |