| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.script.parentchild; |
| 21 |
|
|
| 22 |
|
import java.util.ArrayList; |
| 23 |
|
import java.util.Collections; |
| 24 |
|
import java.util.List; |
| 25 |
|
|
| 26 |
|
import javax.inject.Inject; |
| 27 |
|
import javax.inject.Named; |
| 28 |
|
import javax.inject.Provider; |
| 29 |
|
import javax.inject.Singleton; |
| 30 |
|
|
| 31 |
|
import org.slf4j.Logger; |
| 32 |
|
import org.xwiki.component.annotation.Component; |
| 33 |
|
import org.xwiki.model.reference.DocumentReference; |
| 34 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
| 35 |
|
import org.xwiki.model.reference.EntityReference; |
| 36 |
|
import org.xwiki.model.reference.SpaceReference; |
| 37 |
|
import org.xwiki.script.service.ScriptService; |
| 38 |
|
|
| 39 |
|
import com.xpn.xwiki.XWiki; |
| 40 |
|
import com.xpn.xwiki.XWikiContext; |
| 41 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 42 |
|
import com.xpn.xwiki.internal.parentchild.ParentChildConfiguration; |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
@version |
| 49 |
|
@since |
| 50 |
|
|
| 51 |
|
@Component |
| 52 |
|
@Named("parentchild") |
| 53 |
|
@Singleton |
| |
|
| 56.2% |
Uncovered Elements: 14 (32) |
Complexity: 8 |
Complexity Density: 0.36 |
|
| 54 |
|
public class ParentChildScriptService implements ScriptService |
| 55 |
|
{ |
| 56 |
|
@Inject |
| 57 |
|
private ParentChildConfiguration configuration; |
| 58 |
|
|
| 59 |
|
@Inject |
| 60 |
|
private DocumentReferenceResolver<EntityReference> documentReferenceResolver; |
| 61 |
|
|
| 62 |
|
@Inject |
| 63 |
|
private Provider<XWikiContext> contextProvider; |
| 64 |
|
|
| 65 |
|
@Inject |
| 66 |
|
private Logger logger; |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
@return |
| 70 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 71 |
2621 |
public boolean isParentChildMechanismEnabled()... |
| 72 |
|
{ |
| 73 |
2619 |
return configuration.isParentChildMechanismEnabled(); |
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
|
| 77 |
|
@param |
| 78 |
|
@return |
| 79 |
|
|
| |
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 80 |
0 |
public List<DocumentReference> getParents(DocumentReference docRef)... |
| 81 |
|
{ |
| 82 |
0 |
if (isParentChildMechanismEnabled()) { |
| 83 |
0 |
return getParentsBasedOnParentChildRelationship(docRef); |
| 84 |
|
} else { |
| 85 |
0 |
return getParentsBasedOnReference(docRef); |
| 86 |
|
} |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
@param |
| 92 |
|
@return |
| 93 |
|
|
| |
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 94 |
0 |
public List<DocumentReference> getParentsBasedOnReference(DocumentReference docRef)... |
| 95 |
|
{ |
| 96 |
0 |
List<DocumentReference> parents = new ArrayList<>(); |
| 97 |
|
|
| 98 |
0 |
for (SpaceReference spaceReference : docRef.getSpaceReferences()) { |
| 99 |
0 |
parents.add(documentReferenceResolver.resolve(spaceReference)); |
| 100 |
|
} |
| 101 |
|
|
| 102 |
0 |
return parents; |
| 103 |
|
} |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
@param |
| 108 |
|
@return |
| 109 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 3 (18) |
Complexity: 4 |
Complexity Density: 0.29 |
|
| 110 |
29 |
public List<DocumentReference> getParentsBasedOnParentChildRelationship(DocumentReference docRef)... |
| 111 |
|
{ |
| 112 |
29 |
XWikiContext context = contextProvider.get(); |
| 113 |
29 |
XWiki xwiki = context.getWiki(); |
| 114 |
|
|
| 115 |
29 |
List<DocumentReference> parents = new ArrayList<>(); |
| 116 |
29 |
try { |
| 117 |
29 |
XWikiDocument document = xwiki.getDocument(docRef, context); |
| 118 |
79 |
while (document.getParentReference() != null) { |
| 119 |
50 |
DocumentReference parentReference = document.getParentReference(); |
| 120 |
50 |
if (parents.contains(parentReference)) { |
| 121 |
0 |
throw new Exception("Cyclic references of parent documents"); |
| 122 |
|
} |
| 123 |
50 |
parents.add(document.getParentReference()); |
| 124 |
50 |
document = xwiki.getDocument(document.getParentReference(), context); |
| 125 |
|
} |
| 126 |
|
} catch (Exception e) { |
| 127 |
0 |
logger.error("Failed to get the parents of [{}].", docRef, e); |
| 128 |
|
} |
| 129 |
|
|
| 130 |
29 |
Collections.reverse(parents); |
| 131 |
|
|
| 132 |
29 |
return parents; |
| 133 |
|
} |
| 134 |
|
} |