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.documents; |
21 |
|
|
22 |
|
import java.util.Date; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Provider; |
26 |
|
|
27 |
|
import org.apache.commons.lang3.StringUtils; |
28 |
|
import org.slf4j.Logger; |
29 |
|
import org.xwiki.model.reference.DocumentReference; |
30 |
|
import org.xwiki.watchlist.internal.DefaultWatchListNotifier; |
31 |
|
import org.xwiki.watchlist.internal.job.WatchListJob; |
32 |
|
|
33 |
|
import com.xpn.xwiki.XWikiContext; |
34 |
|
import com.xpn.xwiki.XWikiException; |
35 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
36 |
|
import com.xpn.xwiki.internal.mandatory.AbstractMandatoryDocumentInitializer; |
37 |
|
import com.xpn.xwiki.objects.BaseObject; |
38 |
|
import com.xpn.xwiki.plugin.scheduler.JobState; |
39 |
|
import com.xpn.xwiki.plugin.scheduler.SchedulerPlugin; |
40 |
|
import com.xpn.xwiki.user.api.XWikiRightService; |
41 |
|
|
42 |
|
|
43 |
|
@value |
44 |
|
|
45 |
|
|
46 |
|
@version |
47 |
|
|
|
|
| 84% |
Uncovered Elements: 13 (81) |
Complexity: 15 |
Complexity Density: 0.25 |
|
48 |
|
public abstract class AbstractWatchListJobDocumentInitializer extends AbstractMandatoryDocumentInitializer |
49 |
|
{ |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
protected static final String XWIKI_RIGHTS_CLASS_GROUPS_PROPERTY = "groups"; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
protected static final String XWIKI_RIGHTS_CLASS_LEVELS_PROPERTY = "levels"; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
protected static final String XWIKI_RIGHTS_CLASS_ALLOW_PROPERTY = "allow"; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
protected static final String SCHEDULER_SPACE = "Scheduler"; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
protected static final String SCHEDULER_JOB_CLASS_PROPERTY = "jobClass"; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
@Inject |
79 |
|
private Provider<XWikiContext> contextProvider; |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
@Inject |
85 |
|
private Logger logger; |
86 |
|
|
87 |
|
|
88 |
|
@param |
89 |
|
@param |
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
91 |
3 |
public AbstractWatchListJobDocumentInitializer(String spaceName, String documentName)... |
92 |
|
{ |
93 |
3 |
super(spaceName, documentName); |
94 |
|
} |
95 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
|
96 |
3 |
@Override... |
97 |
|
public boolean updateDocument(XWikiDocument document) |
98 |
|
{ |
99 |
3 |
XWikiContext context = contextProvider.get(); |
100 |
3 |
boolean needsUpdate = false; |
101 |
|
|
102 |
3 |
try { |
103 |
|
|
104 |
3 |
needsUpdate |= createSchedulerJobObject(document, getJobName(), getCron(), context); |
105 |
|
|
106 |
3 |
needsUpdate |= createWatchListJobRightsObject(document, context); |
107 |
|
|
108 |
3 |
needsUpdate |= createWatchListJobObject(document, getMessageTemplateDocument(), context); |
109 |
|
|
110 |
|
|
111 |
3 |
needsUpdate |= setDocumentFields(document, getDocumentTitle()); |
112 |
|
} catch (Exception e) { |
113 |
0 |
logger.error("Failed to initialize document [{}]", getDocumentReference(), e); |
114 |
|
} |
115 |
|
|
116 |
3 |
return needsUpdate; |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
@return |
121 |
|
|
122 |
|
protected abstract String getCron(); |
123 |
|
|
124 |
|
|
125 |
|
@return |
126 |
|
|
127 |
|
protected abstract String getJobName(); |
128 |
|
|
129 |
|
|
130 |
|
@return |
131 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
132 |
3 |
protected String getDocumentTitle()... |
133 |
|
{ |
134 |
3 |
String title = String.format("$services.localization.render('%s')", getDocumentTitleTranslationKey()); |
135 |
3 |
return title; |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
|
@return |
140 |
|
|
141 |
|
protected abstract String getDocumentTitleTranslationKey(); |
142 |
|
|
143 |
|
|
144 |
|
@return |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
146 |
3 |
protected String getMessageTemplateDocument()... |
147 |
|
{ |
148 |
3 |
return DefaultWatchListNotifier.DEFAULT_EMAIL_TEMPLATE; |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
|
@param |
153 |
|
@param |
154 |
|
@param |
155 |
|
@param |
156 |
|
@return |
157 |
|
@throws |
158 |
|
|
|
|
| 66.7% |
Uncovered Elements: 7 (21) |
Complexity: 3 |
Complexity Density: 0.18 |
|
159 |
3 |
private static boolean createSchedulerJobObject(XWikiDocument document, String jobName, String cron,... |
160 |
|
XWikiContext context) throws XWikiException |
161 |
|
{ |
162 |
3 |
boolean needsUpdate = false; |
163 |
|
|
164 |
3 |
BaseObject job = document.getXObject(SchedulerPlugin.XWIKI_JOB_CLASSREFERENCE); |
165 |
3 |
if (job == null) { |
166 |
3 |
needsUpdate = true; |
167 |
3 |
job = document.newXObject(SchedulerPlugin.XWIKI_JOB_CLASSREFERENCE, context); |
168 |
3 |
job.setStringValue("jobName", jobName); |
169 |
3 |
job.setStringValue(SCHEDULER_JOB_CLASS_PROPERTY, WatchListJob.class.getName()); |
170 |
3 |
job.setStringValue("cron", cron); |
171 |
3 |
job.setStringValue("contextUser", XWikiRightService.SUPERADMIN_USER_FULLNAME); |
172 |
3 |
job.setStringValue("contextLang", "en"); |
173 |
3 |
job.setStringValue("contextDatabase", "xwiki"); |
174 |
3 |
job.setStringValue("status", JobState.STATE_NORMAL); |
175 |
|
} else { |
176 |
|
|
177 |
0 |
String jobClass = job.getStringValue(SCHEDULER_JOB_CLASS_PROPERTY); |
178 |
0 |
if (!WatchListJob.class.getName().equals(jobClass)) { |
179 |
0 |
job.setStringValue(SCHEDULER_JOB_CLASS_PROPERTY, WatchListJob.class.getName()); |
180 |
|
|
181 |
0 |
needsUpdate = true; |
182 |
|
} |
183 |
|
} |
184 |
|
|
185 |
3 |
return needsUpdate; |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
@param |
192 |
|
@param |
193 |
|
@return |
194 |
|
@throws |
195 |
|
|
|
|
| 90.5% |
Uncovered Elements: 2 (21) |
Complexity: 3 |
Complexity Density: 0.18 |
|
196 |
3 |
private static boolean createWatchListJobRightsObject(XWikiDocument doc, XWikiContext context)... |
197 |
|
throws XWikiException |
198 |
|
{ |
199 |
3 |
boolean needsUpdate = false; |
200 |
3 |
DocumentReference righsClassReference = context.getWiki().getRightsClass(context).getDocumentReference(); |
201 |
|
|
202 |
3 |
BaseObject editRights = doc.getXObject(righsClassReference, 0); |
203 |
3 |
BaseObject viewRights = doc.getXObject(righsClassReference, 1); |
204 |
|
|
205 |
3 |
if (editRights == null) { |
206 |
3 |
editRights = doc.newXObject(righsClassReference, context); |
207 |
3 |
editRights.setLargeStringValue(XWIKI_RIGHTS_CLASS_GROUPS_PROPERTY, "XWiki.XWikiAdminGroup"); |
208 |
3 |
editRights.setStringValue(XWIKI_RIGHTS_CLASS_LEVELS_PROPERTY, "edit,delete"); |
209 |
3 |
editRights.setIntValue(XWIKI_RIGHTS_CLASS_ALLOW_PROPERTY, 1); |
210 |
3 |
needsUpdate = true; |
211 |
|
} |
212 |
|
|
213 |
3 |
if (viewRights == null) { |
214 |
3 |
viewRights = doc.newXObject(righsClassReference, context); |
215 |
3 |
viewRights.setLargeStringValue(XWIKI_RIGHTS_CLASS_GROUPS_PROPERTY, "XWiki.XWikiAllGroup"); |
216 |
3 |
viewRights.setStringValue(XWIKI_RIGHTS_CLASS_LEVELS_PROPERTY, "view"); |
217 |
3 |
viewRights.setIntValue(XWIKI_RIGHTS_CLASS_ALLOW_PROPERTY, 1); |
218 |
3 |
needsUpdate = true; |
219 |
|
} |
220 |
|
|
221 |
3 |
return needsUpdate; |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
@param |
228 |
|
@param |
229 |
|
@param |
230 |
|
@return |
231 |
|
@throws |
232 |
|
|
|
|
| 84.2% |
Uncovered Elements: 3 (19) |
Complexity: 4 |
Complexity Density: 0.31 |
|
233 |
3 |
private static boolean createWatchListJobObject(XWikiDocument doc, String emailTemplate, XWikiContext context)... |
234 |
|
throws XWikiException |
235 |
|
{ |
236 |
3 |
BaseObject obj = null; |
237 |
3 |
boolean needsupdate = false; |
238 |
|
|
239 |
3 |
obj = doc.getXObject(WatchListJobClassDocumentInitializer.DOCUMENT_REFERENCE); |
240 |
3 |
if (obj == null) { |
241 |
3 |
obj = doc.newXObject(WatchListJobClassDocumentInitializer.DOCUMENT_REFERENCE, context); |
242 |
3 |
needsupdate = true; |
243 |
|
} |
244 |
|
|
245 |
3 |
if (StringUtils.isBlank(obj.getStringValue(WatchListJobClassDocumentInitializer.TEMPLATE_FIELD))) { |
246 |
3 |
obj.setStringValue(WatchListJobClassDocumentInitializer.TEMPLATE_FIELD, emailTemplate); |
247 |
3 |
needsupdate = true; |
248 |
|
} |
249 |
|
|
250 |
3 |
if (obj.getDateValue(WatchListJobClassDocumentInitializer.LAST_FIRE_TIME_FIELD) == null) { |
251 |
3 |
obj.setDateValue(WatchListJobClassDocumentInitializer.LAST_FIRE_TIME_FIELD, new Date()); |
252 |
3 |
needsupdate = true; |
253 |
|
} |
254 |
|
|
255 |
3 |
return needsupdate; |
256 |
|
} |
257 |
|
} |