1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.internal.observation.remote.converter; |
21 |
|
|
22 |
|
import java.io.Serializable; |
23 |
|
import java.util.HashSet; |
24 |
|
import java.util.Map; |
25 |
|
import java.util.Set; |
26 |
|
|
27 |
|
import javax.inject.Named; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.xwiki.bridge.event.DocumentCreatedEvent; |
31 |
|
import org.xwiki.bridge.event.DocumentDeletedEvent; |
32 |
|
import org.xwiki.bridge.event.DocumentUpdatedEvent; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.model.reference.DocumentReference; |
35 |
|
import org.xwiki.observation.event.Event; |
36 |
|
import org.xwiki.observation.remote.LocalEventData; |
37 |
|
import org.xwiki.observation.remote.RemoteEventData; |
38 |
|
|
39 |
|
import com.xpn.xwiki.XWiki; |
40 |
|
import com.xpn.xwiki.XWikiContext; |
41 |
|
import com.xpn.xwiki.XWikiException; |
42 |
|
import com.xpn.xwiki.doc.XWikiDeletedDocument; |
43 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
44 |
|
import com.xpn.xwiki.store.XWikiRecycleBinStoreInterface; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
54 |
|
@Component |
55 |
|
@Singleton |
56 |
|
@Named("document") |
|
|
| 89.4% |
Uncovered Elements: 5 (47) |
Complexity: 11 |
Complexity Density: 0.33 |
|
57 |
|
public class DocumentEventConverter extends AbstractXWikiEventConverter |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private static final Set<Class<? extends Event>> EVENTS = new HashSet<Class<? extends Event>>() |
63 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
64 |
4 |
{... |
65 |
4 |
add(DocumentDeletedEvent.class); |
66 |
4 |
add(DocumentCreatedEvent.class); |
67 |
4 |
add(DocumentUpdatedEvent.class); |
68 |
|
} |
69 |
|
}; |
70 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
71 |
1043 |
@Override... |
72 |
|
public boolean toRemote(LocalEventData localEvent, RemoteEventData remoteEvent) |
73 |
|
{ |
74 |
1041 |
if (EVENTS.contains(localEvent.getEvent().getClass())) { |
75 |
|
|
76 |
34 |
remoteEvent.setEvent((Serializable) localEvent.getEvent()); |
77 |
34 |
remoteEvent.setSource(serializeXWikiDocument((XWikiDocument) localEvent.getSource())); |
78 |
34 |
remoteEvent.setData(serializeXWikiContext((XWikiContext) localEvent.getData())); |
79 |
|
|
80 |
34 |
return true; |
81 |
|
} |
82 |
|
|
83 |
1007 |
return false; |
84 |
|
} |
85 |
|
|
|
|
| 77.8% |
Uncovered Elements: 4 (18) |
Complexity: 5 |
Complexity Density: 0.42 |
|
86 |
9 |
@Override... |
87 |
|
public boolean fromRemote(RemoteEventData remoteEvent, LocalEventData localEvent) |
88 |
|
{ |
89 |
9 |
if (EVENTS.contains(remoteEvent.getEvent().getClass())) { |
90 |
|
|
91 |
9 |
XWikiContext xcontext = unserializeXWikiContext(remoteEvent.getData()); |
92 |
|
|
93 |
9 |
try { |
94 |
9 |
if (xcontext != null) { |
95 |
9 |
localEvent.setData(xcontext); |
96 |
9 |
localEvent.setEvent((Event) remoteEvent.getEvent()); |
97 |
|
|
98 |
9 |
if (remoteEvent.getEvent() instanceof DocumentDeletedEvent) { |
99 |
1 |
localEvent.setSource(unserializeDeletdDocument(remoteEvent.getSource(), xcontext)); |
100 |
|
} else { |
101 |
8 |
localEvent.setSource(unserializeDocument(remoteEvent.getSource())); |
102 |
|
} |
103 |
|
} |
104 |
|
} catch (XWikiException e) { |
105 |
0 |
this.logger.error("Failed to convert remote event [{}]", remoteEvent, e); |
106 |
|
} |
107 |
|
|
108 |
9 |
return true; |
109 |
|
} |
110 |
|
|
111 |
0 |
return false; |
112 |
|
} |
113 |
|
|
|
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 3 |
Complexity Density: 0.25 |
|
114 |
1 |
private XWikiDocument unserializeDeletdDocument(Serializable remoteData, XWikiContext xcontext)... |
115 |
|
throws XWikiException |
116 |
|
{ |
117 |
1 |
Map<String, Serializable> remoteDataMap = (Map<String, Serializable>) remoteData; |
118 |
|
|
119 |
1 |
DocumentReference docReference = (DocumentReference) remoteDataMap.get(DOC_NAME); |
120 |
|
|
121 |
1 |
XWikiDocument doc = new XWikiDocument(docReference); |
122 |
|
|
123 |
1 |
XWikiDocument origDoc = new XWikiDocument(docReference); |
124 |
|
|
125 |
|
|
126 |
1 |
XWiki xwiki = xcontext.getWiki(); |
127 |
1 |
XWikiRecycleBinStoreInterface store = xwiki.getRecycleBinStore(); |
128 |
1 |
XWikiDeletedDocument[] deletedDocuments = store.getAllDeletedDocuments(origDoc, xcontext, true); |
129 |
1 |
if (deletedDocuments != null && deletedDocuments.length > 0) { |
130 |
1 |
long index = deletedDocuments[0].getId(); |
131 |
1 |
origDoc = store.restoreFromRecycleBin(doc, index, xcontext, true); |
132 |
|
} |
133 |
|
|
134 |
1 |
doc.setOriginalDocument(origDoc); |
135 |
|
|
136 |
1 |
return doc; |
137 |
|
} |
138 |
|
} |