| 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.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.bridge.event.DocumentCreatedEvent; |
| 32 |
|
import org.xwiki.bridge.event.DocumentDeletedEvent; |
| 33 |
|
import org.xwiki.bridge.event.DocumentUpdatedEvent; |
| 34 |
|
import org.xwiki.component.annotation.Component; |
| 35 |
|
import org.xwiki.observation.AbstractEventListener; |
| 36 |
|
import org.xwiki.observation.event.Event; |
| 37 |
|
import org.xwiki.watchlist.internal.documents.WatchListClassDocumentInitializer; |
| 38 |
|
import org.xwiki.watchlist.internal.documents.WatchListJobClassDocumentInitializer; |
| 39 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
| 40 |
|
|
| 41 |
|
import com.xpn.xwiki.XWikiContext; |
| 42 |
|
import com.xpn.xwiki.XWikiException; |
| 43 |
|
import com.xpn.xwiki.doc.MandatoryDocumentInitializer; |
| 44 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 45 |
|
import com.xpn.xwiki.objects.BaseObject; |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
@link |
| 49 |
|
|
| 50 |
|
@version |
| 51 |
|
|
| 52 |
|
@Component |
| 53 |
|
@Named(WatchListNotificationCacheListener.LISTENER_NAME) |
| 54 |
|
@Singleton |
| |
|
| 90.3% |
Uncovered Elements: 6 (62) |
Complexity: 19 |
Complexity Density: 0.46 |
|
| 55 |
|
public class WatchListNotificationCacheListener extends AbstractEventListener |
| 56 |
|
{ |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
public static final String LISTENER_NAME = "WatchListNotificationCacheListener"; |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
private static final List<Event> LISTENER_EVENTS = Arrays.<Event>asList(new DocumentCreatedEvent(), |
| 66 |
|
new DocumentUpdatedEvent(), new DocumentDeletedEvent()); |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
@Inject |
| 74 |
|
private Provider<WatchListNotificationCache> notificationCacheProvider; |
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
@Inject |
| 80 |
|
@Named(WatchListClassDocumentInitializer.DOCUMENT_FULL_NAME) |
| 81 |
|
private MandatoryDocumentInitializer watchListClassInitializer; |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
@Inject |
| 87 |
|
private WikiDescriptorManager wikiDescriptorManager; |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
@Inject |
| 93 |
|
private Logger logger; |
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 98 |
1 |
public WatchListNotificationCacheListener()... |
| 99 |
|
{ |
| 100 |
1 |
super(LISTENER_NAME, LISTENER_EVENTS); |
| 101 |
|
} |
| 102 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 103 |
73 |
@Override... |
| 104 |
|
public void onEvent(Event event, Object source, Object data) |
| 105 |
|
{ |
| 106 |
73 |
XWikiDocument currentDoc = (XWikiDocument) source; |
| 107 |
73 |
XWikiDocument originalDoc = currentDoc.getOriginalDocument(); |
| 108 |
73 |
XWikiContext context = (XWikiContext) data; |
| 109 |
|
|
| 110 |
73 |
watchListJobObjectsEventHandler(originalDoc, currentDoc, context); |
| 111 |
73 |
watchListObjectsEventHandler(originalDoc, currentDoc, context); |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
@param |
| 118 |
|
@param |
| 119 |
|
@param |
| 120 |
|
|
| |
|
| 82.4% |
Uncovered Elements: 3 (17) |
Complexity: 6 |
Complexity Density: 0.55 |
|
| 121 |
73 |
private void watchListJobObjectsEventHandler(XWikiDocument originalDoc, XWikiDocument currentDoc,... |
| 122 |
|
XWikiContext context) |
| 123 |
|
{ |
| 124 |
73 |
BaseObject originalJob = originalDoc.getXObject(WatchListJobClassDocumentInitializer.DOCUMENT_REFERENCE); |
| 125 |
73 |
BaseObject currentJob = currentDoc.getXObject(WatchListJobClassDocumentInitializer.DOCUMENT_REFERENCE); |
| 126 |
|
|
| 127 |
73 |
boolean reinitWatchListClass = false; |
| 128 |
|
|
| 129 |
|
|
| 130 |
73 |
if (originalJob != null && currentJob == null) { |
| 131 |
0 |
String deletedJobDocument = originalDoc.getFullName(); |
| 132 |
|
|
| 133 |
0 |
reinitWatchListClass |= notificationCacheProvider.get().removeInterval(deletedJobDocument); |
| 134 |
|
} |
| 135 |
|
|
| 136 |
|
|
| 137 |
73 |
if (originalJob == null && currentJob != null) { |
| 138 |
3 |
String newJobDocument = currentDoc.getFullName(); |
| 139 |
|
|
| 140 |
3 |
reinitWatchListClass |= notificationCacheProvider.get().addInterval(newJobDocument); |
| 141 |
|
} |
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
73 |
if (reinitWatchListClass) { |
| 146 |
|
|
| 147 |
|
|
| 148 |
3 |
reinitializeWatchListClass(context); |
| 149 |
|
} |
| 150 |
|
} |
| 151 |
|
|
| 152 |
|
|
| 153 |
|
@param |
| 154 |
|
|
| 155 |
|
|
| |
|
| 76.9% |
Uncovered Elements: 3 (13) |
Complexity: 4 |
Complexity Density: 0.36 |
|
| 156 |
3 |
private void reinitializeWatchListClass(XWikiContext context)... |
| 157 |
|
{ |
| 158 |
3 |
String currentWikiId = context.getWikiId(); |
| 159 |
3 |
try { |
| 160 |
|
|
| 161 |
3 |
for (String wikiId : wikiDescriptorManager.getAllIds()) { |
| 162 |
3 |
try { |
| 163 |
3 |
context.setWikiId(wikiId); |
| 164 |
|
|
| 165 |
3 |
XWikiDocument document = |
| 166 |
|
context.getWiki().getDocument(watchListClassInitializer.getDocumentReference(), context); |
| 167 |
|
|
| 168 |
3 |
if (watchListClassInitializer.updateDocument(document)) { |
| 169 |
3 |
context.getWiki().saveDocument(document, context); |
| 170 |
|
} |
| 171 |
|
} catch (XWikiException e) { |
| 172 |
0 |
logger.error("Failed to re-initialize mandatory document [{}] on wiki [{}]", |
| 173 |
|
WatchListClassDocumentInitializer.DOCUMENT_FULL_NAME, wikiId, e); |
| 174 |
|
} |
| 175 |
|
} |
| 176 |
|
} catch (Exception e) { |
| 177 |
0 |
logger.error("Failed to re-initialize mandatory document [{}]", |
| 178 |
|
WatchListClassDocumentInitializer.DOCUMENT_FULL_NAME, e); |
| 179 |
|
} finally { |
| 180 |
|
|
| 181 |
3 |
context.setWikiId(currentWikiId); |
| 182 |
|
} |
| 183 |
|
} |
| 184 |
|
|
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
@param |
| 189 |
|
@param |
| 190 |
|
@param |
| 191 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 7 |
Complexity Density: 0.54 |
|
| 192 |
73 |
private void watchListObjectsEventHandler(XWikiDocument originalDoc, XWikiDocument currentDoc, XWikiContext context)... |
| 193 |
|
{ |
| 194 |
73 |
BaseObject originalWatchListObj = originalDoc.getXObject(WatchListClassDocumentInitializer.DOCUMENT_REFERENCE); |
| 195 |
73 |
BaseObject currentWatchListObj = currentDoc.getXObject(WatchListClassDocumentInitializer.DOCUMENT_REFERENCE); |
| 196 |
|
|
| 197 |
73 |
if (originalWatchListObj != null) { |
| 198 |
|
|
| 199 |
|
|
| 200 |
29 |
String oriInterval = |
| 201 |
|
originalWatchListObj.getStringValue(WatchListClassDocumentInitializer.INTERVAL_PROPERTY); |
| 202 |
|
|
| 203 |
|
|
| 204 |
29 |
if (currentWatchListObj == null) { |
| 205 |
2 |
notificationCacheProvider.get().removeSubscriber(oriInterval, originalDoc.getPrefixedFullName()); |
| 206 |
2 |
return; |
| 207 |
|
} |
| 208 |
|
|
| 209 |
|
|
| 210 |
27 |
String newInterval = |
| 211 |
|
currentWatchListObj.getStringValue(WatchListClassDocumentInitializer.INTERVAL_PROPERTY); |
| 212 |
|
|
| 213 |
27 |
if (!newInterval.equals(oriInterval)) { |
| 214 |
2 |
notificationCacheProvider.get().moveSubscriber(oriInterval, newInterval, |
| 215 |
|
originalDoc.getPrefixedFullName()); |
| 216 |
|
} |
| 217 |
|
} |
| 218 |
|
|
| 219 |
71 |
if ((originalWatchListObj == null || originalDoc == null) && currentWatchListObj != null) { |
| 220 |
|
|
| 221 |
6 |
String newInterval = |
| 222 |
|
currentWatchListObj.getStringValue(WatchListClassDocumentInitializer.INTERVAL_PROPERTY); |
| 223 |
|
|
| 224 |
6 |
notificationCacheProvider.get().addSubscriber(newInterval, currentDoc.getPrefixedFullName()); |
| 225 |
|
} |
| 226 |
|
} |
| 227 |
|
} |