1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.watchlist.internal; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.List; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Named; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.slf4j.Logger; |
30 |
|
import org.xwiki.bridge.event.DocumentCreatedEvent; |
31 |
|
import org.xwiki.bridge.event.DocumentUpdatedEvent; |
32 |
|
import org.xwiki.bridge.event.WikiCreatingEvent; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.job.event.JobEvent; |
35 |
|
import org.xwiki.model.reference.DocumentReference; |
36 |
|
import org.xwiki.observation.AbstractEventListener; |
37 |
|
import org.xwiki.observation.ObservationContext; |
38 |
|
import org.xwiki.observation.event.BeginEvent; |
39 |
|
import org.xwiki.observation.event.Event; |
40 |
|
import org.xwiki.watchlist.internal.api.AutomaticWatchMode; |
41 |
|
import org.xwiki.watchlist.internal.api.WatchListStore; |
42 |
|
import org.xwiki.watchlist.internal.api.WatchedElementType; |
43 |
|
|
44 |
|
import com.xpn.xwiki.XWikiContext; |
45 |
|
import com.xpn.xwiki.XWikiException; |
46 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
47 |
|
import com.xpn.xwiki.internal.event.XARImportingEvent; |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
@version |
53 |
|
|
54 |
|
@Component |
55 |
|
@Named(AutomaticWatchModeListener.LISTENER_NAME) |
56 |
|
@Singleton |
|
|
| 90.2% |
Uncovered Elements: 4 (41) |
Complexity: 13 |
Complexity Density: 0.43 |
|
57 |
|
public class AutomaticWatchModeListener extends AbstractEventListener |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
public static final String LISTENER_NAME = "AutomaticWatchModeListener"; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
public static final BeginEvent SKIPPED_EVENTS = new BeginEvent() |
68 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
69 |
138 |
@Override... |
70 |
|
public boolean matches(Object otherEvent) |
71 |
|
{ |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
138 |
return otherEvent instanceof WikiCreatingEvent || otherEvent instanceof XARImportingEvent |
79 |
|
|| otherEvent instanceof JobEvent; |
80 |
|
} |
81 |
|
}; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
private static final List<Event> LISTENER_EVENTS = new ArrayList<Event>() |
87 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
88 |
2 |
{... |
89 |
2 |
add(new DocumentCreatedEvent()); |
90 |
2 |
add(new DocumentUpdatedEvent()); |
91 |
|
} |
92 |
|
}; |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
@Inject |
98 |
|
private Logger logger; |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
@Inject |
104 |
|
private WatchListStore store; |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
@Inject |
111 |
|
private ObservationContext observationContext; |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
116 |
4 |
public AutomaticWatchModeListener()... |
117 |
|
{ |
118 |
4 |
super(LISTENER_NAME, LISTENER_EVENTS); |
119 |
|
} |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
121 |
74 |
@Override... |
122 |
|
public void onEvent(Event event, Object source, Object data) |
123 |
|
{ |
124 |
74 |
XWikiDocument currentDoc = (XWikiDocument) source; |
125 |
74 |
XWikiContext context = (XWikiContext) data; |
126 |
|
|
127 |
|
|
128 |
74 |
if (!observationContext.isIn(SKIPPED_EVENTS)) { |
129 |
62 |
documentModifiedHandler(event, currentDoc, context); |
130 |
|
} |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
@param |
137 |
|
@param |
138 |
|
@param |
139 |
|
|
|
|
| 84.6% |
Uncovered Elements: 4 (26) |
Complexity: 8 |
Complexity Density: 0.36 |
|
140 |
62 |
private void documentModifiedHandler(Event event, XWikiDocument currentDoc, XWikiContext context)... |
141 |
|
{ |
142 |
62 |
String user = currentDoc.getContentAuthor(); |
143 |
62 |
DocumentReference userReference = currentDoc.getContentAuthorReference(); |
144 |
|
|
145 |
|
|
146 |
62 |
if (userReference == null || !context.getWiki().exists(userReference, context)) { |
147 |
13 |
return; |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
49 |
boolean register = false; |
152 |
|
|
153 |
49 |
AutomaticWatchMode mode = this.store.getAutomaticWatchMode(user); |
154 |
49 |
switch (mode) { |
155 |
1 |
case ALL: |
156 |
1 |
register = true; |
157 |
1 |
break; |
158 |
36 |
case MAJOR: |
159 |
36 |
register = !currentDoc.isMinorEdit(); |
160 |
36 |
break; |
161 |
0 |
case NEW: |
162 |
0 |
register = event instanceof DocumentCreatedEvent; |
163 |
0 |
break; |
164 |
12 |
default: |
165 |
12 |
break; |
166 |
|
} |
167 |
|
|
168 |
49 |
if (register) { |
169 |
12 |
try { |
170 |
12 |
this.store.addWatchedElement(user, currentDoc.getPrefixedFullName(), WatchedElementType.DOCUMENT); |
171 |
|
} catch (XWikiException e) { |
172 |
0 |
logger.warn("Failed to watch document [{}] for user [{}]", currentDoc.getPrefixedFullName(), user, e); |
173 |
|
} |
174 |
|
} |
175 |
|
} |
176 |
|
} |