1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.index.tree.internal.nestedpages; |
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 |
|
|
30 |
|
import org.apache.commons.lang3.StringUtils; |
31 |
|
import org.apache.commons.lang3.exception.ExceptionUtils; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.component.annotation.InstantiationStrategy; |
34 |
|
import org.xwiki.component.descriptor.ComponentInstantiationStrategy; |
35 |
|
import org.xwiki.model.reference.DocumentReference; |
36 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
37 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
38 |
|
import org.xwiki.model.reference.ObjectReference; |
39 |
|
import org.xwiki.tree.AbstractTreeNode; |
40 |
|
|
41 |
|
import com.xpn.xwiki.XWikiContext; |
42 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
43 |
|
import com.xpn.xwiki.objects.BaseObject; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
@version |
49 |
|
@since |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Named("objectsOfType") |
54 |
|
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP) |
|
|
| 0% |
Uncovered Elements: 56 (56) |
Complexity: 16 |
Complexity Density: 0.43 |
|
55 |
|
public class ObjectsOfTypeTreeNode extends AbstractTreeNode |
56 |
|
{ |
57 |
|
@Inject |
58 |
|
@Named("current") |
59 |
|
private DocumentReferenceResolver<String> currentDocumentReferenceResolver; |
60 |
|
|
61 |
|
@Inject |
62 |
|
private EntityReferenceSerializer<String> defaultEntityReferenceSerializer; |
63 |
|
|
64 |
|
@Inject |
65 |
|
private Provider<XWikiContext> xcontextProvider; |
66 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
|
67 |
0 |
@Override... |
68 |
|
public List<String> getChildren(String nodeId, int offset, int limit) |
69 |
|
{ |
70 |
0 |
DocumentReference[] parts = resolve(nodeId); |
71 |
0 |
if (parts != null) { |
72 |
0 |
try { |
73 |
0 |
List<String> children = new ArrayList<String>(); |
74 |
0 |
for (ObjectReference objectReference : subList(getXObjectReferences(parts[0], parts[1]), offset, |
75 |
|
limit)) { |
76 |
0 |
children.add("object:" + this.defaultEntityReferenceSerializer.serialize(objectReference)); |
77 |
|
} |
78 |
0 |
return children; |
79 |
|
} catch (Exception e) { |
80 |
0 |
this.logger.warn("Failed to retrieve the children of [{}]. Root cause is [{}].", nodeId, |
81 |
|
ExceptionUtils.getRootCauseMessage(e)); |
82 |
|
} |
83 |
|
} |
84 |
0 |
return Collections.emptyList(); |
85 |
|
} |
86 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
87 |
0 |
@Override... |
88 |
|
public int getChildCount(String nodeId) |
89 |
|
{ |
90 |
0 |
DocumentReference[] parts = resolve(nodeId); |
91 |
0 |
if (parts != null) { |
92 |
0 |
try { |
93 |
0 |
return getXObjectReferences(parts[0], parts[1]).size(); |
94 |
|
} catch (Exception e) { |
95 |
0 |
this.logger.warn("Failed to count the children of [{}]. Root cause is [{}].", nodeId, |
96 |
|
ExceptionUtils.getRootCauseMessage(e)); |
97 |
|
} |
98 |
|
} |
99 |
0 |
return 0; |
100 |
|
} |
101 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
102 |
0 |
@Override... |
103 |
|
public String getParent(String nodeId) |
104 |
|
{ |
105 |
0 |
DocumentReference[] parts = resolve(nodeId); |
106 |
0 |
if (parts != null) { |
107 |
0 |
return "objects:" + this.defaultEntityReferenceSerializer.serialize(parts[0]); |
108 |
|
} |
109 |
0 |
return null; |
110 |
|
} |
111 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 5 |
Complexity Density: 0.56 |
|
112 |
0 |
private DocumentReference[] resolve(String nodeId)... |
113 |
|
{ |
114 |
0 |
String[] parts = StringUtils.split(nodeId, ":", 2); |
115 |
0 |
if (parts == null || parts.length != 2 || !"objectsOfType".equals(parts[0])) { |
116 |
0 |
return null; |
117 |
|
} |
118 |
|
|
119 |
0 |
int separatorIndex = parts[1].lastIndexOf('/'); |
120 |
0 |
if (separatorIndex < 0) { |
121 |
0 |
return null; |
122 |
|
} |
123 |
|
|
124 |
0 |
String serializedDocRef = parts[1].substring(0, separatorIndex); |
125 |
0 |
String serializedClassRef = parts[1].substring(separatorIndex + 1); |
126 |
0 |
return new DocumentReference[] {this.currentDocumentReferenceResolver.resolve(serializedDocRef), |
127 |
|
this.currentDocumentReferenceResolver.resolve(serializedClassRef)}; |
128 |
|
} |
129 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
130 |
0 |
private List<ObjectReference> getXObjectReferences(DocumentReference documentReference,... |
131 |
|
DocumentReference classReference) throws Exception |
132 |
|
{ |
133 |
0 |
XWikiContext xcontext = this.xcontextProvider.get(); |
134 |
0 |
XWikiDocument document = xcontext.getWiki().getDocument(documentReference, xcontext); |
135 |
0 |
List<ObjectReference> objectReferences = new ArrayList<ObjectReference>(); |
136 |
0 |
List<BaseObject> objects = document.getXObjects(classReference); |
137 |
0 |
if (objects != null) { |
138 |
0 |
for (BaseObject object : objects) { |
139 |
|
|
140 |
0 |
if (object != null) { |
141 |
0 |
objectReferences.add(object.getReference()); |
142 |
|
} |
143 |
|
} |
144 |
|
} |
145 |
0 |
return objectReferences; |
146 |
|
} |
147 |
|
} |