1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.index.tree.internal.nestedpages

File ObjectsOfTypeTreeNode.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

14
37
5
1
147
106
16
0.43
7.4
5
3.2

Classes

Class Line # Actions
ObjectsOfTypeTreeNode 55 37 0% 16 56
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * The "objects of type" tree node.
47    *
48    * @version $Id: b8c08469de3387052553fce4625c124bafc137a7 $
49    * @since 8.3M2
50    * @since 7.4.5
51    */
52    @Component
53    @Named("objectsOfType")
54    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
 
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   
 
67  0 toggle @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   
 
87  0 toggle @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   
 
102  0 toggle @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   
 
112  0 toggle 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   
 
130  0 toggle 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    // Yes, the list of objects can contain null values..
140  0 if (object != null) {
141  0 objectReferences.add(object.getReference());
142    }
143    }
144    }
145  0 return objectReferences;
146    }
147    }