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.HashMap; |
24 |
|
import java.util.Map; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
|
28 |
|
import org.slf4j.Logger; |
29 |
|
import org.xwiki.context.Execution; |
30 |
|
import org.xwiki.context.ExecutionContext; |
31 |
|
import org.xwiki.model.reference.DocumentReference; |
32 |
|
import org.xwiki.observation.remote.converter.AbstractEventConverter; |
33 |
|
|
34 |
|
import com.xpn.xwiki.XWikiContext; |
35 |
|
import com.xpn.xwiki.XWikiException; |
36 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
37 |
|
import com.xpn.xwiki.util.XWikiStubContextProvider; |
38 |
|
|
39 |
|
|
40 |
|
@link@link |
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
|
|
| 88.4% |
Uncovered Elements: 8 (69) |
Complexity: 15 |
Complexity Density: 0.32 |
|
45 |
|
public abstract class AbstractXWikiEventConverter extends AbstractEventConverter |
46 |
|
{ |
47 |
|
protected static final String CONTEXT_WIKI = "contextwiki"; |
48 |
|
|
49 |
|
protected static final String CONTEXT_USER = "contextuser"; |
50 |
|
|
51 |
|
protected static final String DOC_NAME = "docname"; |
52 |
|
|
53 |
|
protected static final String DOC_VERSION = "docversion"; |
54 |
|
|
55 |
|
protected static final String DOC_LANGUAGE = "doclanguage"; |
56 |
|
|
57 |
|
protected static final String ORIGDOC_VERSION = "origdocversion"; |
58 |
|
|
59 |
|
protected static final String ORIGDOC_LANGUAGE = "origdoclanguage"; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
@Inject |
65 |
|
protected Logger logger; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
@Inject |
71 |
|
private Execution execution; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@Inject |
77 |
|
private XWikiStubContextProvider stubContextProvider; |
78 |
|
|
79 |
|
|
80 |
|
@param |
81 |
|
@return |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
83 |
34 |
protected Serializable serializeXWikiContext(XWikiContext context)... |
84 |
|
{ |
85 |
34 |
HashMap<String, Serializable> remoteDataMap = new HashMap<String, Serializable>(); |
86 |
|
|
87 |
34 |
remoteDataMap.put(CONTEXT_WIKI, context.getWikiId()); |
88 |
34 |
remoteDataMap.put(CONTEXT_USER, context.getUser()); |
89 |
|
|
90 |
34 |
return remoteDataMap; |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
|
@return |
95 |
|
|
|
|
| 45.5% |
Uncovered Elements: 6 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
96 |
21 |
private XWikiContext getXWikiStubContext()... |
97 |
|
{ |
98 |
21 |
ExecutionContext context = this.execution.getContext(); |
99 |
21 |
XWikiContext xcontext = (XWikiContext) context.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY); |
100 |
|
|
101 |
21 |
if (xcontext == null) { |
102 |
0 |
xcontext = this.stubContextProvider.createStubContext(); |
103 |
|
|
104 |
0 |
if (xcontext != null) { |
105 |
0 |
xcontext.declareInExecutionContext(context); |
106 |
|
} |
107 |
|
} |
108 |
|
|
109 |
21 |
return xcontext; |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
@param |
114 |
|
@return |
115 |
|
|
|
|
| 77.8% |
Uncovered Elements: 2 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
116 |
9 |
protected XWikiContext unserializeXWikiContext(Serializable remoteData)... |
117 |
|
{ |
118 |
9 |
XWikiContext xcontext = getXWikiStubContext(); |
119 |
|
|
120 |
9 |
Map<String, Serializable> remoteDataMap = (Map<String, Serializable>) remoteData; |
121 |
|
|
122 |
9 |
if (xcontext != null) { |
123 |
9 |
xcontext.setWikiId((String) remoteDataMap.get(CONTEXT_WIKI)); |
124 |
9 |
xcontext.setUser((String) remoteDataMap.get(CONTEXT_USER)); |
125 |
|
} else { |
126 |
0 |
this.logger.warn("Can't get a proper XWikiContext." |
127 |
|
+ " It generally mean that the wiki has never been fully initialized," |
128 |
|
+ " i.e. has never been accesses at least once"); |
129 |
|
} |
130 |
|
|
131 |
9 |
return xcontext; |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
|
@param |
136 |
|
@return |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
138 |
34 |
protected Serializable serializeXWikiDocument(XWikiDocument document)... |
139 |
|
{ |
140 |
34 |
HashMap<String, Serializable> remoteDataMap = new HashMap<String, Serializable>(); |
141 |
|
|
142 |
34 |
remoteDataMap.put(DOC_NAME, document.getDocumentReference()); |
143 |
|
|
144 |
34 |
if (!document.isNew()) { |
145 |
32 |
remoteDataMap.put(DOC_VERSION, document.getVersion()); |
146 |
32 |
remoteDataMap.put(DOC_LANGUAGE, document.getLanguage()); |
147 |
|
} |
148 |
|
|
149 |
34 |
XWikiDocument originalDocument = document.getOriginalDocument(); |
150 |
|
|
151 |
34 |
if (originalDocument != null && !originalDocument.isNew()) { |
152 |
6 |
remoteDataMap.put(ORIGDOC_VERSION, originalDocument.getVersion()); |
153 |
6 |
remoteDataMap.put(ORIGDOC_LANGUAGE, originalDocument.getLanguage()); |
154 |
|
} |
155 |
|
|
156 |
34 |
return remoteDataMap; |
157 |
|
} |
158 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
159 |
12 |
protected XWikiDocument getDocument(DocumentReference documentReference, String language, String version)... |
160 |
|
throws XWikiException |
161 |
|
{ |
162 |
12 |
XWikiContext xcontext = getXWikiStubContext(); |
163 |
|
|
164 |
12 |
XWikiDocument document = new XWikiDocument(documentReference); |
165 |
12 |
document.setLanguage(language); |
166 |
|
|
167 |
|
|
168 |
12 |
XWikiDocument targetDocument = xcontext.getWiki().getNotCacheStore().loadXWikiDoc(document, xcontext); |
169 |
|
|
170 |
12 |
if (!targetDocument.getVersion().equals(version)) { |
171 |
|
|
172 |
3 |
targetDocument = xcontext.getWiki().getVersioningStore().loadXWikiDoc(document, version, xcontext); |
173 |
|
} |
174 |
|
|
175 |
12 |
return targetDocument; |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
|
@param |
180 |
|
@return |
181 |
|
@throws |
182 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 3 |
Complexity Density: 0.25 |
|
183 |
8 |
protected XWikiDocument unserializeDocument(Serializable remoteData) throws XWikiException... |
184 |
|
{ |
185 |
8 |
Map<String, Serializable> remoteDataMap = (Map<String, Serializable>) remoteData; |
186 |
|
|
187 |
8 |
DocumentReference docReference = (DocumentReference) remoteDataMap.get(DOC_NAME); |
188 |
|
|
189 |
8 |
XWikiDocument doc; |
190 |
8 |
if (remoteDataMap.get(DOC_VERSION) == null) { |
191 |
1 |
doc = new XWikiDocument(docReference); |
192 |
|
} else { |
193 |
7 |
doc = |
194 |
|
getDocument(docReference, (String) remoteDataMap.get(DOC_LANGUAGE), |
195 |
|
(String) remoteDataMap.get(DOC_VERSION)); |
196 |
|
} |
197 |
|
|
198 |
8 |
XWikiDocument origDoc; |
199 |
8 |
if (remoteDataMap.get(ORIGDOC_VERSION) == null) { |
200 |
3 |
origDoc = new XWikiDocument(docReference); |
201 |
|
} else { |
202 |
5 |
origDoc = |
203 |
|
getDocument(docReference, (String) remoteDataMap.get(ORIGDOC_LANGUAGE), |
204 |
|
(String) remoteDataMap.get(ORIGDOC_VERSION)); |
205 |
|
} |
206 |
|
|
207 |
8 |
doc.setOriginalDocument(origDoc); |
208 |
|
|
209 |
8 |
return doc; |
210 |
|
} |
211 |
|
} |