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.job; |
21 |
|
|
22 |
|
import java.util.Collection; |
23 |
|
import java.util.Date; |
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Map; |
27 |
|
|
28 |
|
import org.quartz.Job; |
29 |
|
import org.quartz.JobDataMap; |
30 |
|
import org.quartz.JobExecutionContext; |
31 |
|
import org.quartz.JobExecutionException; |
32 |
|
import org.slf4j.Logger; |
33 |
|
import org.slf4j.LoggerFactory; |
34 |
|
import org.xwiki.watchlist.internal.DefaultWatchListNotifier; |
35 |
|
import org.xwiki.watchlist.internal.WatchListEventMatcher; |
36 |
|
import org.xwiki.watchlist.internal.api.WatchList; |
37 |
|
import org.xwiki.watchlist.internal.api.WatchListEvent; |
38 |
|
import org.xwiki.watchlist.internal.documents.WatchListJobClassDocumentInitializer; |
39 |
|
import org.xwiki.watchlist.internal.notification.WatchListEventMimeMessageFactory; |
40 |
|
|
41 |
|
import com.xpn.xwiki.XWikiException; |
42 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
43 |
|
import com.xpn.xwiki.objects.BaseObject; |
44 |
|
import com.xpn.xwiki.plugin.scheduler.AbstractJob; |
45 |
|
import com.xpn.xwiki.web.Utils; |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
@version |
51 |
|
|
|
|
| 78.7% |
Uncovered Elements: 10 (47) |
Complexity: 11 |
Complexity Density: 0.32 |
|
52 |
|
public class WatchListJob extends AbstractJob implements Job |
53 |
|
{ |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
public static final String DEFAULT_EMAIL_TEMPLATE = "XWiki.WatchListMessage"; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(WatchListJob.class); |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
private BaseObject schedulerJobObject; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
private BaseObject watchListJobObject; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
private WatchList watchlist; |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
@param |
83 |
|
@throws |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
85 |
1 |
public void init(JobExecutionContext jobContext) throws Exception... |
86 |
|
{ |
87 |
1 |
JobDataMap data = jobContext.getJobDetail().getJobDataMap(); |
88 |
|
|
89 |
1 |
this.watchlist = Utils.getComponent(WatchList.class); |
90 |
1 |
this.schedulerJobObject = (BaseObject) data.get("xjob"); |
91 |
1 |
this.watchListJobObject = |
92 |
|
getXWikiContext().getWiki().getDocument(this.schedulerJobObject.getDocumentReference(), getXWikiContext()) |
93 |
|
.getXObject(WatchListJobClassDocumentInitializer.DOCUMENT_REFERENCE); |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
|
@return |
98 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
99 |
0 |
public String getId()... |
100 |
|
{ |
101 |
0 |
String className = this.getClass().getName(); |
102 |
0 |
return className.substring(className.lastIndexOf(".") + 1); |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
|
@return |
107 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
108 |
1 |
private Date getPreviousFireTime()... |
109 |
|
{ |
110 |
1 |
return this.watchListJobObject.getDateValue(WatchListJobClassDocumentInitializer.LAST_FIRE_TIME_FIELD); |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
@throws |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
118 |
1 |
private void setPreviousFireTime() throws XWikiException... |
119 |
|
{ |
120 |
1 |
XWikiDocument doc = |
121 |
|
getXWikiContext().getWiki().getDocument(this.watchListJobObject.getDocumentReference(), getXWikiContext()); |
122 |
|
|
123 |
1 |
this.watchListJobObject.setDateValue(WatchListJobClassDocumentInitializer.LAST_FIRE_TIME_FIELD, new Date()); |
124 |
|
|
125 |
|
|
126 |
1 |
doc.setMetaDataDirty(false); |
127 |
1 |
doc.setContentDirty(false); |
128 |
|
|
129 |
1 |
getXWikiContext().getWiki().saveDocument(doc, "Updated last fire time", true, getXWikiContext()); |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
@return |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
138 |
2 |
private Collection<String> getSubscribers()... |
139 |
|
{ |
140 |
2 |
return this.watchlist.getStore().getSubscribers(this.schedulerJobObject.getName()); |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
@return |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
146 |
1 |
private boolean hasSubscribers()... |
147 |
|
{ |
148 |
1 |
Collection<String> subscribers = getSubscribers(); |
149 |
|
|
150 |
1 |
return !subscribers.isEmpty(); |
151 |
|
} |
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
@param |
157 |
|
@throws |
158 |
|
|
|
|
| 72% |
Uncovered Elements: 7 (25) |
Complexity: 5 |
Complexity Density: 0.26 |
|
159 |
1 |
@Override... |
160 |
|
public void executeJob(JobExecutionContext jobContext) throws JobExecutionException |
161 |
|
{ |
162 |
1 |
try { |
163 |
1 |
init(jobContext); |
164 |
|
|
165 |
1 |
if (this.watchListJobObject == null) { |
166 |
0 |
return; |
167 |
|
} |
168 |
|
|
169 |
1 |
Collection<String> subscribers = getSubscribers(); |
170 |
|
|
171 |
|
|
172 |
1 |
if (!hasSubscribers()) { |
173 |
0 |
return; |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
1 |
Date previousFireTime = getPreviousFireTime(); |
178 |
1 |
WatchListEventMatcher eventMatcher = Utils.getComponent(WatchListEventMatcher.class); |
179 |
1 |
List<WatchListEvent> events = eventMatcher.getEventsSince(previousFireTime); |
180 |
1 |
setPreviousFireTime(); |
181 |
|
|
182 |
|
|
183 |
1 |
if (events.size() == 0) { |
184 |
0 |
return; |
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
|
|
189 |
1 |
Map<String, Object> notificationData = new HashMap<>(); |
190 |
1 |
notificationData.put(DefaultWatchListNotifier.PREVIOUS_FIRE_TIME_VARIABLE, previousFireTime); |
191 |
|
|
192 |
1 |
String mailTemplate = |
193 |
|
this.watchListJobObject.getStringValue(WatchListJobClassDocumentInitializer.TEMPLATE_FIELD); |
194 |
1 |
notificationData.put(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER, mailTemplate); |
195 |
|
|
196 |
|
|
197 |
1 |
this.watchlist.getNotifier().sendNotification(subscribers, events, notificationData); |
198 |
|
} catch (Exception e) { |
199 |
|
|
200 |
0 |
LOGGER.error("Exception while running job", e); |
201 |
|
} |
202 |
|
} |
203 |
|
} |