1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.extension.distribution.internal; |
21 |
|
|
22 |
|
import java.util.HashMap; |
23 |
|
import java.util.Map; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Named; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.apache.commons.lang3.StringUtils; |
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.context.Execution; |
35 |
|
import org.xwiki.context.ExecutionContext; |
36 |
|
import org.xwiki.extension.Extension; |
37 |
|
import org.xwiki.extension.LocalExtension; |
38 |
|
import org.xwiki.extension.distribution.internal.DocumentsModifiedDuringDistributionListener.DocumentStatus.Action; |
39 |
|
import org.xwiki.extension.xar.internal.handler.XarExtensionPlan; |
40 |
|
import org.xwiki.job.JobContext; |
41 |
|
import org.xwiki.job.Request; |
42 |
|
import org.xwiki.model.reference.DocumentReference; |
43 |
|
import org.xwiki.observation.AbstractEventListener; |
44 |
|
import org.xwiki.observation.event.Event; |
45 |
|
|
46 |
|
import com.xpn.xwiki.XWikiContext; |
47 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
@version |
53 |
|
@since |
54 |
|
|
55 |
|
@Component |
56 |
|
@Named(DocumentsModifiedDuringDistributionListener.NAME) |
57 |
|
@Singleton |
58 |
|
|
|
|
| 21.8% |
Uncovered Elements: 68 (87) |
Complexity: 22 |
Complexity Density: 0.44 |
|
59 |
|
public class DocumentsModifiedDuringDistributionListener extends AbstractEventListener |
60 |
|
{ |
61 |
|
public static final String NAME = "distribution.DocumentsModifiedDuringDistributionListener"; |
62 |
|
|
63 |
|
@Inject |
64 |
|
private Execution execution; |
65 |
|
|
66 |
|
@Inject |
67 |
|
private JobContext jobContext; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
private Map<String, Map<DocumentReference, DocumentStatus>> documents = |
73 |
|
new HashMap<String, Map<DocumentReference, DocumentStatus>>(); |
74 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 7 |
Complexity Density: 0.64 |
|
75 |
|
public static class DocumentStatus |
76 |
|
{ |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
77 |
|
public enum Action |
78 |
|
{ |
79 |
|
CREATED, |
80 |
|
DELETED, |
81 |
|
MODIFIED |
82 |
|
} |
83 |
|
|
84 |
|
private final DocumentReference reference; |
85 |
|
|
86 |
|
private final String previousVersion; |
87 |
|
|
88 |
|
private final Action action; |
89 |
|
|
90 |
|
private final Extension previousExtension; |
91 |
|
|
92 |
|
private final Extension nextExtension; |
93 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
94 |
0 |
public DocumentStatus(DocumentReference reference, String version, Action action)... |
95 |
|
{ |
96 |
0 |
this(reference, version, action, null, null); |
97 |
|
} |
98 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
99 |
0 |
public DocumentStatus(DocumentReference reference, String previousVersion, Action action,... |
100 |
|
Extension previousExtension, Extension nextExtension) |
101 |
|
{ |
102 |
0 |
this.reference = reference; |
103 |
0 |
this.previousVersion = previousVersion; |
104 |
0 |
this.action = action; |
105 |
0 |
this.previousExtension = previousExtension; |
106 |
0 |
this.nextExtension = nextExtension; |
107 |
|
} |
108 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
109 |
0 |
public DocumentReference getReference()... |
110 |
|
{ |
111 |
0 |
return this.reference; |
112 |
|
} |
113 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
114 |
0 |
public String getPreviousVersion()... |
115 |
|
{ |
116 |
0 |
return this.previousVersion; |
117 |
|
} |
118 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
119 |
0 |
public Action getAction()... |
120 |
|
{ |
121 |
0 |
return this.action; |
122 |
|
} |
123 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
124 |
0 |
public Extension getNextExtension()... |
125 |
|
{ |
126 |
0 |
return this.nextExtension; |
127 |
|
} |
128 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
129 |
0 |
public Extension getPreviousExtension()... |
130 |
|
{ |
131 |
0 |
return this.previousExtension; |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
138 |
1 |
public DocumentsModifiedDuringDistributionListener()... |
139 |
|
{ |
140 |
1 |
super("DocumentsModifiedDuringDistributionListener", new DocumentCreatedEvent(), new DocumentDeletedEvent(), |
141 |
|
new DocumentUpdatedEvent()); |
142 |
|
} |
143 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
144 |
310 |
@Override... |
145 |
|
public void onEvent(Event event, Object source, Object data) |
146 |
|
{ |
147 |
310 |
checkXARHandler(event, (XWikiDocument) source, (XWikiContext) data); |
148 |
310 |
checkDistributionAction(event, (XWikiDocument) source, (XWikiContext) data); |
149 |
|
} |
150 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
151 |
0 |
private static Action toAction(Event event)... |
152 |
|
{ |
153 |
0 |
DocumentStatus.Action action; |
154 |
0 |
if (event instanceof DocumentCreatedEvent) { |
155 |
0 |
action = Action.CREATED; |
156 |
0 |
} else if (event instanceof DocumentDeletedEvent) { |
157 |
0 |
action = Action.DELETED; |
158 |
|
} else { |
159 |
0 |
action = Action.MODIFIED; |
160 |
|
} |
161 |
|
|
162 |
0 |
return action; |
163 |
|
} |
164 |
|
|
|
|
| 47.6% |
Uncovered Elements: 11 (21) |
Complexity: 5 |
Complexity Density: 0.38 |
|
165 |
310 |
private void checkXARHandler(Event event, XWikiDocument document, XWikiContext xcontext)... |
166 |
|
{ |
167 |
310 |
ExecutionContext context = this.execution.getContext(); |
168 |
|
|
169 |
310 |
if (context != null) { |
170 |
310 |
XarExtensionPlan xarExtensionPlan = |
171 |
|
(XarExtensionPlan) context.getProperty(XarExtensionPlan.CONTEXTKEY_XARINSTALLPLAN); |
172 |
|
|
173 |
310 |
if (xarExtensionPlan != null) { |
174 |
25 |
Request request = this.jobContext.getCurrentJob().getRequest(); |
175 |
|
|
176 |
|
|
177 |
25 |
if (StringUtils.equals(request.<String> getProperty("context.action"), "distribution")) { |
178 |
0 |
String distributionWiki = request.getProperty("context.wiki"); |
179 |
|
|
180 |
0 |
if (distributionWiki != null) { |
181 |
0 |
DocumentReference reference = document.getDocumentReferenceWithLocale(); |
182 |
|
|
183 |
0 |
DocumentStatus.Action action = toAction(event); |
184 |
0 |
LocalExtension previousExtension = xarExtensionPlan.getPreviousXarExtension(reference); |
185 |
0 |
LocalExtension nextExtension = xarExtensionPlan.getNextXarExtension(reference); |
186 |
|
|
187 |
0 |
addDocument(distributionWiki, document, action, previousExtension, nextExtension); |
188 |
|
} |
189 |
|
} |
190 |
|
} |
191 |
|
} |
192 |
|
} |
193 |
|
|
|
|
| 33.3% |
Uncovered Elements: 4 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
194 |
310 |
private void checkDistributionAction(Event event, XWikiDocument document, XWikiContext xcontext)... |
195 |
|
{ |
196 |
310 |
if (DistributionAction.DISTRIBUTION_ACTION.equals(xcontext.getAction())) { |
197 |
0 |
String distributionWiki = xcontext.getOriginalWikiId(); |
198 |
|
|
199 |
0 |
DocumentStatus.Action action = toAction(event); |
200 |
|
|
201 |
0 |
addDocument(distributionWiki, document, action, null, null); |
202 |
|
} |
203 |
|
} |
204 |
|
|
|
|
| 0% |
Uncovered Elements: 38 (38) |
Complexity: 9 |
Complexity Density: 0.41 |
|
205 |
0 |
private void addDocument(String distributionWiki, XWikiDocument document, Action action,... |
206 |
|
LocalExtension previousExtension, LocalExtension nextExtension) |
207 |
|
{ |
208 |
0 |
Map<DocumentReference, DocumentStatus> wikiDocuments = this.documents.get(distributionWiki); |
209 |
|
|
210 |
0 |
if (wikiDocuments == null) { |
211 |
0 |
wikiDocuments = |
212 |
|
new HashMap<DocumentReference, DocumentsModifiedDuringDistributionListener.DocumentStatus>(); |
213 |
0 |
this.documents.put(distributionWiki, wikiDocuments); |
214 |
|
} |
215 |
|
|
216 |
0 |
DocumentReference reference = document.getDocumentReferenceWithLocale(); |
217 |
|
|
218 |
0 |
DocumentStatus currentStatus = wikiDocuments.get(reference); |
219 |
|
|
220 |
0 |
String previousVersion; |
221 |
0 |
if (currentStatus != null) { |
222 |
0 |
previousVersion = currentStatus.getPreviousVersion(); |
223 |
|
|
224 |
0 |
if (action == Action.CREATED) { |
225 |
0 |
if (previousVersion != null) { |
226 |
0 |
action = Action.MODIFIED; |
227 |
|
} |
228 |
0 |
} else if (action == Action.DELETED) { |
229 |
0 |
if (previousVersion == null) { |
230 |
|
|
231 |
0 |
wikiDocuments.remove(reference); |
232 |
|
|
233 |
0 |
return; |
234 |
|
} |
235 |
0 |
} else if (action == Action.MODIFIED) { |
236 |
0 |
action = currentStatus.getAction(); |
237 |
|
} |
238 |
|
} else { |
239 |
0 |
if (action != Action.CREATED) { |
240 |
0 |
previousVersion = document.getOriginalDocument().getVersion(); |
241 |
|
} else { |
242 |
0 |
previousVersion = null; |
243 |
|
} |
244 |
|
} |
245 |
|
|
246 |
0 |
wikiDocuments.put(reference, new DocumentStatus(reference, previousVersion, action, previousExtension, |
247 |
|
nextExtension)); |
248 |
|
} |
249 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
250 |
0 |
public Map<String, Map<DocumentReference, DocumentStatus>> getDocuments()... |
251 |
|
{ |
252 |
0 |
return this.documents; |
253 |
|
} |
254 |
|
} |