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.notification; |
21 |
|
|
22 |
|
import java.util.HashSet; |
23 |
|
import java.util.List; |
24 |
|
import java.util.Map; |
25 |
|
import java.util.Set; |
26 |
|
|
27 |
|
import javax.mail.Address; |
28 |
|
|
29 |
|
import org.slf4j.Logger; |
30 |
|
import org.slf4j.LoggerFactory; |
31 |
|
import org.xwiki.context.Execution; |
32 |
|
import org.xwiki.mail.internal.factory.usersandgroups.AddressUserDataExtractor; |
33 |
|
import org.xwiki.model.reference.DocumentReference; |
34 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
35 |
|
import org.xwiki.model.reference.WikiReference; |
36 |
|
import org.xwiki.watchlist.internal.DefaultWatchListStore; |
37 |
|
import org.xwiki.watchlist.internal.WatchListEventMatcher; |
38 |
|
import org.xwiki.watchlist.internal.api.WatchListEvent; |
39 |
|
import org.xwiki.watchlist.internal.job.WatchListJob; |
40 |
|
|
41 |
|
import com.xpn.xwiki.XWiki; |
42 |
|
import com.xpn.xwiki.XWikiContext; |
43 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
44 |
|
import com.xpn.xwiki.internal.plugin.rightsmanager.UserDataExtractor; |
45 |
|
import com.xpn.xwiki.objects.BaseObject; |
46 |
|
|
47 |
|
|
48 |
|
@link |
49 |
|
|
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
|
|
| 90.5% |
Uncovered Elements: 7 (74) |
Complexity: 18 |
Complexity Density: 0.35 |
|
54 |
|
public class WatchListMessageDataExtractor implements UserDataExtractor<WatchListMessageData> |
55 |
|
{ |
56 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(AddressUserDataExtractor.class); |
57 |
|
|
58 |
|
private EventsAndSubscribersSource source; |
59 |
|
|
60 |
|
private Set<Address> processedAddresses; |
61 |
|
|
62 |
|
private WatchListEventMatcher eventMatcher; |
63 |
|
|
64 |
|
private Execution execution; |
65 |
|
|
66 |
|
private AddressUserDataExtractor addressExtractor; |
67 |
|
|
68 |
|
private DocumentReferenceResolver<String> explicitDocumentReferenceResolver; |
69 |
|
|
70 |
|
private Map<String, Object> parameters; |
71 |
|
|
72 |
|
private boolean skipContextUser; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@param |
78 |
|
@param |
79 |
|
@param |
80 |
|
@param |
81 |
|
@param |
82 |
|
@link |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
84 |
41 |
public WatchListMessageDataExtractor(EventsAndSubscribersSource source, Map<String, Object> parameters,... |
85 |
|
WatchListEventMatcher eventMatcher, Execution execution, |
86 |
|
DocumentReferenceResolver<String> explicitDocumentReferenceResolver) |
87 |
|
{ |
88 |
41 |
this.source = source; |
89 |
41 |
this.parameters = parameters; |
90 |
41 |
this.eventMatcher = eventMatcher; |
91 |
41 |
this.execution = execution; |
92 |
41 |
this.explicitDocumentReferenceResolver = explicitDocumentReferenceResolver; |
93 |
|
|
94 |
|
|
95 |
41 |
this.addressExtractor = new AddressUserDataExtractor(); |
96 |
|
|
97 |
|
|
98 |
41 |
this.processedAddresses = new HashSet<>(); |
99 |
|
|
100 |
|
|
101 |
41 |
Boolean skipContextUserValue = |
102 |
|
(Boolean) parameters.get(WatchListEventMimeMessageFactory.SKIP_CONTEXT_USER_PARAMETER); |
103 |
41 |
if (skipContextUserValue != null && Boolean.TRUE.equals(skipContextUserValue)) { |
104 |
30 |
skipContextUser = true; |
105 |
|
} |
106 |
|
} |
107 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
108 |
0 |
@Override... |
109 |
|
public WatchListMessageData extractFromSuperadmin(DocumentReference reference) |
110 |
|
{ |
111 |
0 |
return null; |
112 |
|
} |
113 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
114 |
0 |
@Override... |
115 |
|
public WatchListMessageData extractFromGuest(DocumentReference reference) |
116 |
|
{ |
117 |
0 |
return null; |
118 |
|
} |
119 |
|
|
|
|
| 95.7% |
Uncovered Elements: 1 (23) |
Complexity: 7 |
Complexity Density: 0.41 |
|
120 |
41 |
@Override... |
121 |
|
public WatchListMessageData extract(DocumentReference subscriberReference, XWikiDocument document, |
122 |
|
BaseObject userObject) |
123 |
|
{ |
124 |
41 |
WatchListMessageData result = null; |
125 |
|
|
126 |
41 |
try { |
127 |
41 |
if (skipContextUser && subscriberReference.equals(getXWikiContext().getUserReference())) { |
128 |
|
|
129 |
6 |
return null; |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
35 |
List<WatchListEvent> matchingEvents = |
134 |
|
eventMatcher.getMatchingVisibleEvents(source.getEvents(), document.getPrefixedFullName()); |
135 |
|
|
136 |
35 |
if (matchingEvents.size() == 0) { |
137 |
|
|
138 |
24 |
return null; |
139 |
|
} |
140 |
|
|
141 |
11 |
String firstName = userObject.getStringValue("first_name"); |
142 |
11 |
String lastName = userObject.getStringValue("last_name"); |
143 |
|
|
144 |
11 |
Address address = addressExtractor.extract(subscriberReference, document, userObject); |
145 |
11 |
if (address == null || processedAddresses.contains(address)) { |
146 |
|
|
147 |
3 |
return null; |
148 |
|
} |
149 |
|
|
150 |
8 |
processedAddresses.add(address); |
151 |
|
|
152 |
8 |
DocumentReference templateReference = getTemplateReference(subscriberReference); |
153 |
|
|
154 |
8 |
result = |
155 |
|
new WatchListMessageData(subscriberReference, templateReference, firstName, lastName, address, |
156 |
|
matchingEvents); |
157 |
|
|
158 |
|
} catch (Exception e) { |
159 |
0 |
LOGGER.error("Failed to retrieve information for user [{}]", subscriberReference, e); |
160 |
|
} |
161 |
|
|
162 |
8 |
return result; |
163 |
|
} |
164 |
|
|
|
|
| 93.1% |
Uncovered Elements: 2 (29) |
Complexity: 5 |
Complexity Density: 0.24 |
|
165 |
8 |
private DocumentReference getTemplateReference(DocumentReference subscriberReference)... |
166 |
|
{ |
167 |
8 |
DocumentReference result = null; |
168 |
|
|
169 |
|
|
170 |
|
|
171 |
8 |
String templateStringReference = (String) parameters.get(WatchListEventMimeMessageFactory.TEMPLATE_PARAMETER); |
172 |
|
|
173 |
8 |
if (templateStringReference.contains(DefaultWatchListStore.WIKI_SPACE_SEP)) { |
174 |
|
|
175 |
2 |
result = explicitDocumentReferenceResolver.resolve(templateStringReference); |
176 |
2 |
return result; |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
6 |
WikiReference userWikiReference = subscriberReference.getWikiReference(); |
181 |
6 |
result = explicitDocumentReferenceResolver.resolve(templateStringReference, userWikiReference); |
182 |
6 |
XWikiContext context = getXWikiContext(); |
183 |
6 |
XWiki wiki = context.getWiki(); |
184 |
6 |
if (wiki.exists(result, context)) { |
185 |
3 |
return result; |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
3 |
WikiReference currentWikiReference = new WikiReference(context.getWikiId()); |
190 |
3 |
result = explicitDocumentReferenceResolver.resolve(templateStringReference, currentWikiReference); |
191 |
3 |
if (wiki.exists(result, context)) { |
192 |
2 |
return result; |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
1 |
result = explicitDocumentReferenceResolver.resolve(WatchListJob.DEFAULT_EMAIL_TEMPLATE, currentWikiReference); |
199 |
1 |
if (wiki.exists(result, context)) { |
200 |
0 |
return result; |
201 |
|
} |
202 |
|
|
203 |
|
|
204 |
1 |
WikiReference mainWikiReference = new WikiReference(context.getMainXWiki()); |
205 |
1 |
result = explicitDocumentReferenceResolver.resolve(WatchListJob.DEFAULT_EMAIL_TEMPLATE, mainWikiReference); |
206 |
|
|
207 |
1 |
return result; |
208 |
|
} |
209 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
210 |
36 |
private XWikiContext getXWikiContext()... |
211 |
|
{ |
212 |
36 |
XWikiContext context = |
213 |
|
(XWikiContext) this.execution.getContext().getProperty(XWikiContext.EXECUTIONCONTEXT_KEY); |
214 |
|
|
215 |
36 |
return context; |
216 |
|
} |
217 |
|
|
218 |
|
} |