| 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.LinkedList; |
| 23 |
|
import java.util.List; |
| 24 |
|
|
| 25 |
|
import javax.inject.Inject; |
| 26 |
|
import javax.inject.Named; |
| 27 |
|
|
| 28 |
|
import org.xwiki.component.annotation.Component; |
| 29 |
|
import org.xwiki.model.EntityType; |
| 30 |
|
import org.xwiki.model.reference.DocumentReference; |
| 31 |
|
import org.xwiki.model.reference.EntityReference; |
| 32 |
|
import org.xwiki.model.reference.SpaceReference; |
| 33 |
|
import org.xwiki.refactoring.internal.LinkRefactoring; |
| 34 |
|
import org.xwiki.refactoring.job.EntityJobStatus; |
| 35 |
|
import org.xwiki.refactoring.job.MoveRequest; |
| 36 |
|
import org.xwiki.refactoring.job.OverwriteQuestion; |
| 37 |
|
import org.xwiki.refactoring.job.RefactoringJobs; |
| 38 |
|
import org.xwiki.security.authorization.Right; |
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
@version |
| 44 |
|
@since |
| 45 |
|
|
| 46 |
|
@Component |
| 47 |
|
@Named(RefactoringJobs.MOVE) |
| |
|
| 85.2% |
Uncovered Elements: 24 (162) |
Complexity: 53 |
Complexity Density: 0.53 |
|
| 48 |
|
public class MoveJob extends AbstractEntityJob<MoveRequest, EntityJobStatus<MoveRequest>> |
| 49 |
|
{ |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
private Boolean overwriteAll; |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
@Inject |
| 61 |
|
private LinkRefactoring linkRefactoring; |
| 62 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 63 |
65 |
@Override... |
| 64 |
|
public String getType() |
| 65 |
|
{ |
| 66 |
65 |
return RefactoringJobs.MOVE; |
| 67 |
|
} |
| 68 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 69 |
21 |
@Override... |
| 70 |
|
protected EntityJobStatus<MoveRequest> createNewStatus(MoveRequest request) |
| 71 |
|
{ |
| 72 |
21 |
return new EntityJobStatus<MoveRequest>(request, this.observationManager, this.loggerManager, null); |
| 73 |
|
} |
| 74 |
|
|
| |
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 75 |
21 |
@Override... |
| 76 |
|
protected void runInternal() throws Exception |
| 77 |
|
{ |
| 78 |
21 |
if (this.request.getDestination() != null) { |
| 79 |
21 |
super.runInternal(); |
| 80 |
|
} |
| 81 |
|
} |
| 82 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 4 |
Complexity Density: 0.31 |
|
| 83 |
19 |
@Override... |
| 84 |
|
protected void process(EntityReference source) |
| 85 |
|
{ |
| 86 |
|
|
| 87 |
|
|
| 88 |
19 |
EntityReference destination = this.request.getDestination(); |
| 89 |
19 |
if (isDescendantOrSelf(destination, source)) { |
| 90 |
1 |
this.logger.error("Cannot make [{}] a descendant of itself.", source); |
| 91 |
1 |
return; |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
18 |
switch (source.getType()) { |
| 97 |
14 |
case DOCUMENT: |
| 98 |
14 |
process(new DocumentReference(source), destination); |
| 99 |
13 |
break; |
| 100 |
3 |
case SPACE: |
| 101 |
3 |
process(new SpaceReference(source), destination); |
| 102 |
2 |
break; |
| 103 |
1 |
default: |
| 104 |
1 |
this.logger.error("Unsupported source entity type [{}].", source.getType()); |
| 105 |
|
} |
| 106 |
|
} |
| 107 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 108 |
19 |
private boolean isDescendantOrSelf(EntityReference alice, EntityReference bob)... |
| 109 |
|
{ |
| 110 |
19 |
EntityReference parent = alice; |
| 111 |
64 |
while (parent != null && !parent.equals(bob)) { |
| 112 |
45 |
parent = parent.getParent(); |
| 113 |
|
} |
| 114 |
19 |
return parent != null; |
| 115 |
|
} |
| 116 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 6 |
Complexity Density: 0.86 |
|
| 117 |
9 |
protected void process(DocumentReference source, EntityReference destination)... |
| 118 |
|
{ |
| 119 |
9 |
if (this.request.isDeep() && isSpaceHomeReference(source)) { |
| 120 |
1 |
process(source.getLastSpaceReference(), destination); |
| 121 |
8 |
} else if (destination.getType() == EntityType.SPACE) { |
| 122 |
5 |
maybeMove(source, new DocumentReference(source.getName(), new SpaceReference(destination))); |
| 123 |
3 |
} else if (destination.getType() == EntityType.DOCUMENT |
| 124 |
|
&& isSpaceHomeReference(new DocumentReference(destination))) { |
| 125 |
1 |
maybeMove(source, new DocumentReference(source.getName(), new SpaceReference(destination.getParent()))); |
| 126 |
|
} else { |
| 127 |
2 |
this.logger.error("Unsupported destination entity type [{}] for a document.", destination.getType()); |
| 128 |
|
} |
| 129 |
|
} |
| 130 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 5 |
Complexity Density: 1 |
|
| 131 |
3 |
protected void process(SpaceReference source, EntityReference destination)... |
| 132 |
|
{ |
| 133 |
3 |
if (destination.getType() == EntityType.SPACE || destination.getType() == EntityType.WIKI) { |
| 134 |
1 |
process(source, new SpaceReference(source.getName(), destination)); |
| 135 |
2 |
} else if (destination.getType() == EntityType.DOCUMENT |
| 136 |
|
&& isSpaceHomeReference(new DocumentReference(destination))) { |
| 137 |
1 |
process(source, new SpaceReference(source.getName(), destination.getParent())); |
| 138 |
|
} else { |
| 139 |
1 |
this.logger.error("Unsupported destination entity type [{}] for a space.", destination.getType()); |
| 140 |
|
} |
| 141 |
|
} |
| 142 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 143 |
5 |
protected void process(final SpaceReference source, final SpaceReference destination)... |
| 144 |
|
{ |
| 145 |
5 |
visitDocuments(source, new Visitor<DocumentReference>() |
| 146 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 147 |
4 |
@Override... |
| 148 |
|
public void visit(DocumentReference oldChildReference) |
| 149 |
|
{ |
| 150 |
4 |
DocumentReference newChildReference = oldChildReference.replaceParent(source, destination); |
| 151 |
4 |
maybeMove(oldChildReference, newChildReference); |
| 152 |
|
} |
| 153 |
|
}); |
| 154 |
|
} |
| 155 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 8 |
Complexity Density: 1.14 |
|
| 156 |
12 |
protected void maybeMove(DocumentReference oldReference, DocumentReference newReference)... |
| 157 |
|
{ |
| 158 |
|
|
| 159 |
|
|
| 160 |
12 |
if (!this.modelBridge.exists(oldReference)) { |
| 161 |
1 |
this.logger.warn("Skipping [{}] because it doesn't exist.", oldReference); |
| 162 |
11 |
} else if (this.request.isDeleteSource() && !hasAccess(Right.DELETE, oldReference)) { |
| 163 |
|
|
| 164 |
1 |
this.logger.error("You are not allowed to delete [{}].", oldReference); |
| 165 |
10 |
} else if (!hasAccess(Right.VIEW, newReference) || !hasAccess(Right.EDIT, newReference) |
| 166 |
|
|| (this.modelBridge.exists(newReference) && !hasAccess(Right.DELETE, newReference))) { |
| 167 |
1 |
this.logger |
| 168 |
|
.error("You don't have sufficient permissions over the destination document [{}].", newReference); |
| 169 |
|
} else { |
| 170 |
9 |
move(oldReference, newReference); |
| 171 |
|
} |
| 172 |
|
} |
| 173 |
|
|
| |
|
| 85.3% |
Uncovered Elements: 5 (34) |
Complexity: 9 |
Complexity Density: 0.41 |
|
| 174 |
9 |
private void move(DocumentReference oldReference, DocumentReference newReference)... |
| 175 |
|
{ |
| 176 |
9 |
this.progressManager.pushLevelProgress(7, this); |
| 177 |
|
|
| 178 |
9 |
try { |
| 179 |
|
|
| 180 |
9 |
this.progressManager.startStep(this); |
| 181 |
9 |
if (this.modelBridge.exists(newReference)) { |
| 182 |
1 |
if (this.request.isInteractive() && !confirmOverwrite(oldReference, newReference)) { |
| 183 |
0 |
this.logger.warn( |
| 184 |
|
"Skipping [{}] because [{}] already exists and the user doesn't want to overwrite it.", |
| 185 |
|
oldReference, newReference); |
| 186 |
0 |
return; |
| 187 |
1 |
} else if (!this.modelBridge.delete(newReference)) { |
| 188 |
0 |
return; |
| 189 |
|
} |
| 190 |
|
} |
| 191 |
|
|
| 192 |
|
|
| 193 |
9 |
this.progressManager.startStep(this); |
| 194 |
9 |
if (!this.modelBridge.copy(oldReference, newReference)) { |
| 195 |
3 |
return; |
| 196 |
|
} |
| 197 |
|
|
| 198 |
|
|
| 199 |
6 |
this.progressManager.startStep(this); |
| 200 |
6 |
this.modelBridge.update(newReference, this.request.getEntityParameters(oldReference)); |
| 201 |
|
|
| 202 |
|
|
| 203 |
6 |
updateDocuments(oldReference, newReference); |
| 204 |
|
|
| 205 |
|
|
| 206 |
6 |
this.progressManager.startStep(this); |
| 207 |
6 |
if (this.request.isDeleteSource()) { |
| 208 |
5 |
this.modelBridge.delete(oldReference); |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
|
| 212 |
6 |
this.progressManager.startStep(this); |
| 213 |
6 |
if (this.request.isDeleteSource() && this.request.isAutoRedirect()) { |
| 214 |
4 |
this.modelBridge.createRedirect(oldReference, newReference); |
| 215 |
|
} |
| 216 |
|
} finally { |
| 217 |
9 |
this.progressManager.popLevelProgress(this); |
| 218 |
|
} |
| 219 |
|
} |
| 220 |
|
|
| |
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 221 |
6 |
private void updateDocuments(DocumentReference oldReference, DocumentReference newReference)... |
| 222 |
|
{ |
| 223 |
|
|
| 224 |
6 |
this.progressManager.startStep(this); |
| 225 |
6 |
if (this.request.isUpdateLinks()) { |
| 226 |
6 |
updateLinks(oldReference, newReference); |
| 227 |
|
} |
| 228 |
|
|
| 229 |
|
|
| 230 |
|
|
| 231 |
6 |
this.progressManager.startStep(this); |
| 232 |
6 |
if (this.request.isUpdateParentField()) { |
| 233 |
6 |
this.modelBridge.updateParentField(oldReference, newReference); |
| 234 |
|
} |
| 235 |
|
} |
| 236 |
|
|
| |
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
| 237 |
0 |
private boolean confirmOverwrite(EntityReference source, EntityReference destination)... |
| 238 |
|
{ |
| 239 |
0 |
if (this.overwriteAll == null) { |
| 240 |
0 |
OverwriteQuestion question = new OverwriteQuestion(source, destination); |
| 241 |
0 |
try { |
| 242 |
0 |
this.status.ask(question); |
| 243 |
0 |
if (!question.isAskAgain()) { |
| 244 |
|
|
| 245 |
0 |
this.overwriteAll = question.isOverwrite(); |
| 246 |
|
} |
| 247 |
0 |
return question.isOverwrite(); |
| 248 |
|
} catch (InterruptedException e) { |
| 249 |
0 |
this.logger.warn("Overwrite question has been interrupted."); |
| 250 |
0 |
return false; |
| 251 |
|
} |
| 252 |
|
} else { |
| 253 |
0 |
return this.overwriteAll; |
| 254 |
|
} |
| 255 |
|
} |
| 256 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
| 257 |
6 |
private void updateLinks(DocumentReference oldReference, DocumentReference newReference)... |
| 258 |
|
{ |
| 259 |
6 |
this.progressManager.pushLevelProgress(2, this); |
| 260 |
|
|
| 261 |
6 |
try { |
| 262 |
|
|
| 263 |
6 |
this.progressManager.startStep(this); |
| 264 |
6 |
if (this.request.isDeleteSource()) { |
| 265 |
5 |
updateBackLinks(oldReference, newReference); |
| 266 |
|
} |
| 267 |
|
|
| 268 |
|
|
| 269 |
6 |
this.progressManager.startStep(this); |
| 270 |
6 |
this.linkRefactoring.updateRelativeLinks(oldReference, newReference); |
| 271 |
|
} finally { |
| 272 |
6 |
this.progressManager.popLevelProgress(this); |
| 273 |
|
} |
| 274 |
|
} |
| 275 |
|
|
| |
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
| 276 |
5 |
private void updateBackLinks(DocumentReference oldReference, DocumentReference newReference)... |
| 277 |
|
{ |
| 278 |
5 |
List<DocumentReference> backlinkDocumentReferences = this.modelBridge.getBackLinkedReferences(oldReference); |
| 279 |
5 |
this.progressManager.pushLevelProgress(backlinkDocumentReferences.size(), this); |
| 280 |
|
|
| 281 |
5 |
try { |
| 282 |
5 |
for (DocumentReference backlinkDocumentReference : backlinkDocumentReferences) { |
| 283 |
2 |
this.progressManager.startStep(this); |
| 284 |
2 |
if (hasAccess(Right.EDIT, backlinkDocumentReference)) { |
| 285 |
2 |
this.linkRefactoring.renameLinks(backlinkDocumentReference, oldReference, newReference); |
| 286 |
|
} |
| 287 |
|
} |
| 288 |
|
} finally { |
| 289 |
5 |
this.progressManager.popLevelProgress(this); |
| 290 |
|
} |
| 291 |
|
} |
| 292 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 293 |
21 |
@Override... |
| 294 |
|
protected String getTargetWiki() |
| 295 |
|
{ |
| 296 |
21 |
List<EntityReference> entityReferences = new LinkedList<>(this.request.getEntityReferences()); |
| 297 |
21 |
entityReferences.add(this.request.getDestination()); |
| 298 |
21 |
return getTargetWiki(entityReferences); |
| 299 |
|
} |
| 300 |
|
} |