| 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.LinkedHashMap; |
| 25 |
|
import java.util.List; |
| 26 |
|
import java.util.Map; |
| 27 |
|
|
| 28 |
|
import javax.inject.Inject; |
| 29 |
|
import javax.inject.Named; |
| 30 |
|
import javax.inject.Provider; |
| 31 |
|
|
| 32 |
|
import org.apache.commons.lang3.StringUtils; |
| 33 |
|
import org.xwiki.component.annotation.Component; |
| 34 |
|
import org.xwiki.component.annotation.InstantiationStrategy; |
| 35 |
|
import org.xwiki.component.descriptor.ComponentInstantiationStrategy; |
| 36 |
|
import org.xwiki.component.manager.ComponentLookupException; |
| 37 |
|
import org.xwiki.component.manager.ComponentManager; |
| 38 |
|
import org.xwiki.component.phase.Initializable; |
| 39 |
|
import org.xwiki.component.phase.InitializationException; |
| 40 |
|
import org.xwiki.localization.LocalizationContext; |
| 41 |
|
import org.xwiki.model.EntityType; |
| 42 |
|
import org.xwiki.model.reference.DocumentReference; |
| 43 |
|
import org.xwiki.model.reference.EntityReference; |
| 44 |
|
import org.xwiki.model.reference.SpaceReference; |
| 45 |
|
import org.xwiki.query.Query; |
| 46 |
|
import org.xwiki.query.QueryException; |
| 47 |
|
import org.xwiki.query.QueryFilter; |
| 48 |
|
import org.xwiki.security.authorization.ContextualAuthorizationManager; |
| 49 |
|
import org.xwiki.security.authorization.Right; |
| 50 |
|
import org.xwiki.tree.TreeNode; |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
@version |
| 56 |
|
@since |
| 57 |
|
@since |
| 58 |
|
|
| 59 |
|
@Component |
| 60 |
|
@Named("document") |
| 61 |
|
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP) |
| |
|
| 89.7% |
Uncovered Elements: 12 (117) |
Complexity: 28 |
Complexity Density: 0.38 |
|
| 62 |
|
public class DocumentTreeNode extends AbstractDocumentTreeNode implements Initializable |
| 63 |
|
{ |
| 64 |
|
private static final String FIELD_TITLE = "title"; |
| 65 |
|
|
| 66 |
|
private static final String PARAMETER_LOCALE = "locale"; |
| 67 |
|
|
| 68 |
|
@Inject |
| 69 |
|
@Named("count") |
| 70 |
|
protected QueryFilter countQueryFilter; |
| 71 |
|
|
| 72 |
|
@Inject |
| 73 |
|
@Named("hidden/document") |
| 74 |
|
protected QueryFilter hiddenDocumentQueryFilter; |
| 75 |
|
|
| 76 |
|
@Inject |
| 77 |
|
private LocalizationContext localizationContext; |
| 78 |
|
|
| 79 |
|
@Inject |
| 80 |
|
private ContextualAuthorizationManager authorization; |
| 81 |
|
|
| 82 |
|
@Inject |
| 83 |
|
@Named("context") |
| 84 |
|
private Provider<ComponentManager> contextComponentManagerProvider; |
| 85 |
|
|
| 86 |
|
@Inject |
| 87 |
|
@Named("childPage/nestedPages") |
| 88 |
|
private QueryFilter childPageFilter; |
| 89 |
|
|
| 90 |
|
@Inject |
| 91 |
|
@Named("hiddenPage/nestedPages") |
| 92 |
|
private QueryFilter hiddenPageFilter; |
| 93 |
|
|
| 94 |
|
@Inject |
| 95 |
|
@Named("documentReferenceResolver/nestedPages") |
| 96 |
|
private QueryFilter documentReferenceResolverFilter; |
| 97 |
|
|
| 98 |
|
|
| 99 |
|
@link |
| 100 |
|
|
| 101 |
|
private Map<String, TreeNode> nonLeafChildNodes = new LinkedHashMap<String, TreeNode>(); |
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 106 |
64 |
public DocumentTreeNode()... |
| 107 |
|
{ |
| 108 |
64 |
super("document"); |
| 109 |
|
} |
| 110 |
|
|
| |
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 111 |
64 |
@Override... |
| 112 |
|
public void initialize() throws InitializationException |
| 113 |
|
{ |
| 114 |
64 |
String[] nonLeafChildNodeTypes = new String[] {"translations", "attachments", "classProperties", "objects"}; |
| 115 |
64 |
ComponentManager contextComponentManager = this.contextComponentManagerProvider.get(); |
| 116 |
64 |
try { |
| 117 |
64 |
for (String nonLeafChildNodeType : nonLeafChildNodeTypes) { |
| 118 |
256 |
TreeNode treeNode = contextComponentManager.getInstance(TreeNode.class, nonLeafChildNodeType); |
| 119 |
256 |
this.nonLeafChildNodes.put(nonLeafChildNodeType, treeNode); |
| 120 |
|
} |
| 121 |
|
} catch (ComponentLookupException e) { |
| 122 |
0 |
throw new InitializationException("Failed to lookup the child components.", e); |
| 123 |
|
} |
| 124 |
|
} |
| 125 |
|
|
| |
|
| 81.2% |
Uncovered Elements: 3 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
| 126 |
9 |
@Override... |
| 127 |
|
protected List<String> getChildren(DocumentReference documentReference, int offset, int limit) throws Exception |
| 128 |
|
{ |
| 129 |
9 |
List<String> children = new ArrayList<String>(); |
| 130 |
9 |
String serializedDocRef = this.defaultEntityReferenceSerializer.serialize(documentReference); |
| 131 |
|
|
| 132 |
9 |
if (offset == 0) { |
| 133 |
9 |
for (Map.Entry<String, TreeNode> entry : this.nonLeafChildNodes.entrySet()) { |
| 134 |
36 |
if (hasChild(entry.getKey(), entry.getValue(), documentReference)) { |
| 135 |
1 |
children.add(entry.getKey() + ':' + serializedDocRef); |
| 136 |
|
} |
| 137 |
|
} |
| 138 |
|
|
| 139 |
9 |
if (showAddDocument(documentReference)) { |
| 140 |
0 |
children.add("addDocument:" + serializedDocRef); |
| 141 |
|
} |
| 142 |
|
} |
| 143 |
|
|
| 144 |
9 |
children.addAll(serialize(getChildDocuments(documentReference, offset, limit))); |
| 145 |
|
|
| 146 |
9 |
return children; |
| 147 |
|
} |
| 148 |
|
|
| |
|
| 83.9% |
Uncovered Elements: 5 (31) |
Complexity: 6 |
Complexity Density: 0.29 |
|
| 149 |
9 |
protected List<DocumentReference> getChildDocuments(DocumentReference documentReference, int offset, int limit)... |
| 150 |
|
throws QueryException |
| 151 |
|
{ |
| 152 |
9 |
if (!getDefaultDocumentName().equals(documentReference.getName())) { |
| 153 |
1 |
return Collections.emptyList(); |
| 154 |
|
} |
| 155 |
|
|
| 156 |
8 |
String orderBy = getOrderBy(); |
| 157 |
8 |
Query query; |
| 158 |
8 |
if (areTerminalDocumentsShown()) { |
| 159 |
7 |
if (FIELD_TITLE.equals(orderBy)) { |
| 160 |
7 |
query = this.queryManager.getNamedQuery("nestedPagesOrderedByTitle"); |
| 161 |
7 |
query.bindValue(PARAMETER_LOCALE, this.localizationContext.getCurrentLocale().toString()); |
| 162 |
|
} else { |
| 163 |
0 |
query = this.queryManager.getNamedQuery("nestedPagesOrderedByName"); |
| 164 |
|
} |
| 165 |
|
} else { |
| 166 |
1 |
if (FIELD_TITLE.equals(orderBy)) { |
| 167 |
1 |
query = this.queryManager.getNamedQuery("nonTerminalPagesOrderedByTitle"); |
| 168 |
1 |
query.bindValue(PARAMETER_LOCALE, this.localizationContext.getCurrentLocale().toString()); |
| 169 |
|
} else { |
| 170 |
|
|
| 171 |
0 |
query = this.queryManager.createQuery( |
| 172 |
|
"select reference, 0 as terminal from XWikiSpace page order by lower(name), name", Query.HQL); |
| 173 |
|
} |
| 174 |
|
} |
| 175 |
|
|
| 176 |
8 |
query.setWiki(documentReference.getWikiReference().getName()); |
| 177 |
8 |
query.setOffset(offset); |
| 178 |
8 |
query.setLimit(limit); |
| 179 |
|
|
| 180 |
8 |
query.addFilter(this.childPageFilter); |
| 181 |
8 |
query.bindValue("parent", this.localEntityReferenceSerializer.serialize(documentReference.getParent())); |
| 182 |
|
|
| 183 |
8 |
if (!areHiddenEntitiesShown()) { |
| 184 |
8 |
query.addFilter(this.hiddenPageFilter); |
| 185 |
|
} |
| 186 |
|
|
| 187 |
8 |
return query.addFilter(this.documentReferenceResolverFilter).execute(); |
| 188 |
|
} |
| 189 |
|
|
| |
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 190 |
70 |
@Override... |
| 191 |
|
protected int getChildCount(DocumentReference documentReference) throws Exception |
| 192 |
|
{ |
| 193 |
70 |
int count = 0; |
| 194 |
70 |
for (Map.Entry<String, TreeNode> entry : this.nonLeafChildNodes.entrySet()) { |
| 195 |
280 |
if (hasChild(entry.getKey(), entry.getValue(), documentReference)) { |
| 196 |
1 |
count++; |
| 197 |
|
} |
| 198 |
|
} |
| 199 |
|
|
| 200 |
70 |
if (showAddDocument(documentReference)) { |
| 201 |
0 |
count++; |
| 202 |
|
} |
| 203 |
|
|
| 204 |
70 |
return count + getChildDocumentsCount(documentReference); |
| 205 |
|
} |
| 206 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 207 |
70 |
protected int getChildDocumentsCount(DocumentReference documentReference) throws QueryException... |
| 208 |
|
{ |
| 209 |
70 |
if (!getDefaultDocumentName().equals(documentReference.getName())) { |
| 210 |
3 |
return 0; |
| 211 |
|
} |
| 212 |
|
|
| 213 |
67 |
int count = getChildSpacesCount(documentReference); |
| 214 |
67 |
if (areTerminalDocumentsShown()) { |
| 215 |
63 |
count += getChildTerminalPagesCount(documentReference); |
| 216 |
|
} |
| 217 |
67 |
return count; |
| 218 |
|
} |
| 219 |
|
|
| |
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
| 220 |
63 |
private int getChildTerminalPagesCount(DocumentReference documentReference) throws QueryException... |
| 221 |
|
{ |
| 222 |
63 |
Query query = this.queryManager |
| 223 |
|
.createQuery("where doc.translation = 0 and doc.space = :space and doc.name <> :defaultDocName", Query.HQL); |
| 224 |
63 |
query.addFilter(this.countQueryFilter); |
| 225 |
63 |
if (Boolean.TRUE.equals(getProperties().get("filterHiddenDocuments"))) { |
| 226 |
63 |
query.addFilter(this.hiddenDocumentQueryFilter); |
| 227 |
|
} |
| 228 |
63 |
query.setWiki(documentReference.getWikiReference().getName()); |
| 229 |
63 |
query.bindValue("space", this.localEntityReferenceSerializer.serialize(documentReference.getParent())); |
| 230 |
63 |
query.bindValue("defaultDocName", getDefaultDocumentName()); |
| 231 |
63 |
return ((Long) query.execute().get(0)).intValue(); |
| 232 |
|
} |
| 233 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 234 |
5 |
@Override... |
| 235 |
|
protected EntityReference getParent(DocumentReference documentReference) throws Exception |
| 236 |
|
{ |
| 237 |
5 |
if (getDefaultDocumentName().equals(documentReference.getName())) { |
| 238 |
4 |
EntityReference parentReference = documentReference.getParent().getParent(); |
| 239 |
4 |
if (parentReference.getType() == EntityType.SPACE) { |
| 240 |
3 |
return new DocumentReference(getDefaultDocumentName(), new SpaceReference(parentReference)); |
| 241 |
|
} else { |
| 242 |
1 |
return parentReference; |
| 243 |
|
} |
| 244 |
|
} else { |
| 245 |
1 |
return new DocumentReference(getDefaultDocumentName(), documentReference.getLastSpaceReference()); |
| 246 |
|
} |
| 247 |
|
} |
| 248 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 249 |
79 |
private boolean showAddDocument(DocumentReference documentReference)... |
| 250 |
|
{ |
| 251 |
79 |
return Boolean.TRUE.equals(getProperties().get("showAddDocument")) |
| 252 |
|
&& "reference".equals(getProperties().get("hierarchyMode")) |
| 253 |
|
&& getDefaultDocumentName().equals(documentReference.getName()) |
| 254 |
|
&& this.authorization.hasAccess(Right.EDIT, documentReference.getParent()); |
| 255 |
|
} |
| 256 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 257 |
316 |
private boolean hasChild(String nodeType, TreeNode childNode, DocumentReference documentReference)... |
| 258 |
|
{ |
| 259 |
316 |
return hasChild(nodeType, childNode, this.defaultEntityReferenceSerializer.serialize(documentReference)); |
| 260 |
|
} |
| 261 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 262 |
316 |
private boolean hasChild(String nodeType, TreeNode childNode, String serializedDocumentReference)... |
| 263 |
|
{ |
| 264 |
316 |
String showChild = "show" + StringUtils.capitalize(nodeType); |
| 265 |
316 |
if (Boolean.TRUE.equals(getProperties().get(showChild))) { |
| 266 |
48 |
String nodeId = nodeType + ':' + serializedDocumentReference; |
| 267 |
48 |
childNode.getProperties().putAll(getProperties()); |
| 268 |
48 |
return childNode.getChildCount(nodeId) > 0; |
| 269 |
|
} |
| 270 |
268 |
return false; |
| 271 |
|
} |
| 272 |
|
} |