1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.refactoring.internal.job; |
21 |
|
|
22 |
|
import java.util.Collection; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
|
26 |
|
import org.xwiki.job.AbstractJob; |
27 |
|
import org.xwiki.job.GroupedJob; |
28 |
|
import org.xwiki.job.JobGroupPath; |
29 |
|
import org.xwiki.job.Request; |
30 |
|
import org.xwiki.model.EntityType; |
31 |
|
import org.xwiki.model.reference.DocumentReference; |
32 |
|
import org.xwiki.model.reference.EntityReference; |
33 |
|
import org.xwiki.model.reference.EntityReferenceProvider; |
34 |
|
import org.xwiki.model.reference.EntityReferenceTree; |
35 |
|
import org.xwiki.model.reference.EntityReferenceTreeNode; |
36 |
|
import org.xwiki.model.reference.SpaceReference; |
37 |
|
import org.xwiki.refactoring.internal.ModelBridge; |
38 |
|
import org.xwiki.refactoring.job.EntityJobStatus; |
39 |
|
import org.xwiki.refactoring.job.EntityRequest; |
40 |
|
import org.xwiki.refactoring.job.RefactoringJobs; |
41 |
|
import org.xwiki.security.authorization.AuthorizationManager; |
42 |
|
import org.xwiki.security.authorization.Right; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
@param |
48 |
|
@param |
49 |
|
@version |
50 |
|
@since |
51 |
|
|
|
|
| 92.8% |
Uncovered Elements: 7 (97) |
Complexity: 29 |
Complexity Density: 0.51 |
|
52 |
|
public abstract class AbstractEntityJob<R extends EntityRequest, S extends EntityJobStatus<? super R>> extends |
53 |
|
AbstractJob<R, S> implements GroupedJob |
54 |
|
{ |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@param |
59 |
|
@version |
60 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
61 |
|
public interface Visitor<T> |
62 |
|
{ |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
@param |
67 |
|
|
68 |
|
void visit(T node); |
69 |
|
} |
70 |
|
|
71 |
|
private static final JobGroupPath ROOT_GROUP = new JobGroupPath(RefactoringJobs.GROUP, null); |
72 |
|
|
73 |
|
private static final String PREFERENCES_DOCUMENT_NAME = "WebPreferences"; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
@Inject |
79 |
|
protected ModelBridge modelBridge; |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
private JobGroupPath groupPath; |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
@see |
93 |
|
|
94 |
|
@Inject |
95 |
|
private AuthorizationManager authorization; |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
@see |
101 |
|
|
102 |
|
@Inject |
103 |
|
private EntityReferenceProvider defaultEntityReferenceProvider; |
104 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
105 |
41 |
@Override... |
106 |
|
public JobGroupPath getGroupPath() |
107 |
|
{ |
108 |
41 |
return this.groupPath; |
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
111 |
79 |
@Override... |
112 |
|
public void initialize(Request request) |
113 |
|
{ |
114 |
79 |
super.initialize(request); |
115 |
|
|
116 |
|
|
117 |
79 |
String targetWiki = getTargetWiki(); |
118 |
79 |
if (targetWiki != null) { |
119 |
69 |
this.groupPath = new JobGroupPath(targetWiki, ROOT_GROUP); |
120 |
|
} else { |
121 |
10 |
this.groupPath = ROOT_GROUP; |
122 |
|
} |
123 |
|
} |
124 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
125 |
75 |
@Override... |
126 |
|
protected void runInternal() throws Exception |
127 |
|
{ |
128 |
75 |
Collection<EntityReference> entityReferences = this.request.getEntityReferences(); |
129 |
75 |
if (entityReferences != null) { |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
75 |
this.modelBridge.setContextUserReference(this.request.getUserReference()); |
134 |
75 |
process(entityReferences); |
135 |
|
} |
136 |
|
} |
137 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
138 |
67 |
protected void process(Collection<EntityReference> entityReferences)... |
139 |
|
{ |
140 |
67 |
this.progressManager.pushLevelProgress(entityReferences.size(), this); |
141 |
|
|
142 |
67 |
try { |
143 |
67 |
for (EntityReference entityReference : entityReferences) { |
144 |
70 |
if (this.status.isCanceled()) { |
145 |
0 |
break; |
146 |
|
} else { |
147 |
70 |
this.progressManager.startStep(this); |
148 |
70 |
process(entityReference); |
149 |
|
} |
150 |
|
} |
151 |
|
} finally { |
152 |
67 |
this.progressManager.popLevelProgress(this); |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
@param |
160 |
|
|
161 |
|
protected abstract void process(EntityReference entityReference); |
162 |
|
|
163 |
|
|
164 |
|
@return |
165 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
166 |
58 |
protected String getTargetWiki()... |
167 |
|
{ |
168 |
58 |
return getTargetWiki(this.request.getEntityReferences()); |
169 |
|
} |
170 |
|
|
171 |
|
|
172 |
|
@return |
173 |
|
|
|
|
| 90% |
Uncovered Elements: 2 (20) |
Complexity: 5 |
Complexity Density: 0.42 |
|
174 |
79 |
protected String getTargetWiki(Collection<EntityReference> entityReferences)... |
175 |
|
{ |
176 |
79 |
if (entityReferences == null) { |
177 |
1 |
return null; |
178 |
|
} |
179 |
|
|
180 |
78 |
String targetWiki = null; |
181 |
78 |
for (EntityReference entityReference : entityReferences) { |
182 |
106 |
EntityReference wikiReference = entityReference.extractReference(EntityType.WIKI); |
183 |
106 |
if (wikiReference != null) { |
184 |
106 |
if (targetWiki == null) { |
185 |
77 |
targetWiki = wikiReference.getName(); |
186 |
29 |
} else if (!targetWiki.equals(wikiReference.getName())) { |
187 |
8 |
return null; |
188 |
|
} |
189 |
|
} else { |
190 |
0 |
return null; |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
70 |
return targetWiki; |
195 |
|
} |
196 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
197 |
74 |
protected boolean hasAccess(Right right, EntityReference reference)... |
198 |
|
{ |
199 |
74 |
return !this.request.isCheckRights() |
200 |
|
|| this.authorization.hasAccess(right, this.request.getUserReference(), reference); |
201 |
|
} |
202 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
203 |
33 |
protected boolean isSpaceHomeReference(DocumentReference documentReference)... |
204 |
|
{ |
205 |
33 |
return documentReference.getName().equals( |
206 |
|
this.defaultEntityReferenceProvider.getDefaultReference(documentReference.getType()).getName()); |
207 |
|
} |
208 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
209 |
37 |
private boolean isSpacePreferencesReference(EntityReference entityReference)... |
210 |
|
{ |
211 |
37 |
return entityReference.getType() == EntityType.DOCUMENT |
212 |
|
&& PREFERENCES_DOCUMENT_NAME.equals(entityReference.getName()); |
213 |
|
} |
214 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
215 |
18 |
protected void visitDocuments(SpaceReference spaceReference, Visitor<DocumentReference> visitor)... |
216 |
|
{ |
217 |
18 |
visitDocumentNodes(getDocumentReferenceTree(spaceReference), visitor); |
218 |
|
} |
219 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
220 |
18 |
private EntityReferenceTreeNode getDocumentReferenceTree(SpaceReference spaceReference)... |
221 |
|
{ |
222 |
18 |
return new EntityReferenceTree(this.modelBridge.getDocumentReferences(spaceReference)).get(spaceReference); |
223 |
|
} |
224 |
|
|
|
|
| 91.7% |
Uncovered Elements: 1 (12) |
Complexity: 6 |
Complexity Density: 1 |
|
225 |
55 |
private void visitDocumentNodes(EntityReferenceTreeNode node, Visitor<DocumentReference> visitor)... |
226 |
|
{ |
227 |
55 |
EntityReference nodeReference = node.getReference(); |
228 |
47 |
EntityType nodeType = nodeReference != null ? nodeReference.getType() : null; |
229 |
47 |
if (nodeType == EntityType.SPACE || nodeType == EntityType.WIKI || nodeType == null) { |
230 |
|
|
231 |
22 |
visitDocumentAncestor(node, visitor); |
232 |
25 |
} else if (nodeType == EntityType.DOCUMENT) { |
233 |
25 |
visitor.visit((DocumentReference) node.getReference()); |
234 |
|
} |
235 |
|
} |
236 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 3 |
Complexity Density: 0.25 |
|
237 |
22 |
private void visitDocumentAncestor(EntityReferenceTreeNode node, Visitor<DocumentReference> visitor)... |
238 |
|
{ |
239 |
22 |
Collection<EntityReferenceTreeNode> children = node.getChildren(); |
240 |
22 |
this.progressManager.pushLevelProgress(children.size(), this); |
241 |
|
|
242 |
22 |
try { |
243 |
|
|
244 |
22 |
EntityReferenceTreeNode spacePreferencesNode = null; |
245 |
22 |
for (EntityReferenceTreeNode child : children) { |
246 |
37 |
if (isSpacePreferencesReference(child.getReference())) { |
247 |
2 |
spacePreferencesNode = child; |
248 |
2 |
continue; |
249 |
|
} |
250 |
35 |
visitDocumentAncestorStep(child, visitor); |
251 |
|
} |
252 |
|
|
253 |
22 |
if (spacePreferencesNode != null) { |
254 |
2 |
visitDocumentAncestorStep(spacePreferencesNode, visitor); |
255 |
|
} |
256 |
|
} finally { |
257 |
22 |
this.progressManager.popLevelProgress(this); |
258 |
|
} |
259 |
|
} |
260 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
261 |
37 |
private void visitDocumentAncestorStep(EntityReferenceTreeNode node, Visitor<DocumentReference> visitor)... |
262 |
|
{ |
263 |
37 |
this.progressManager.startStep(this); |
264 |
37 |
if (!this.status.isCanceled()) { |
265 |
37 |
visitDocumentNodes(node, visitor); |
266 |
|
} |
267 |
|
} |
268 |
|
} |