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

File AbstractEntityTreeNode.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

8
40
10
1
166
117
16
0.4
4
10
1.6

Classes

Class Line # Actions
AbstractEntityTreeNode 49 40 0% 16 7
0.8793103787.9%
 

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;
21   
22    import java.util.ArrayList;
23    import java.util.HashMap;
24    import java.util.List;
25    import java.util.Map;
26   
27    import javax.inject.Inject;
28    import javax.inject.Named;
29   
30    import org.apache.commons.lang3.StringUtils;
31    import org.xwiki.configuration.ConfigurationSource;
32    import org.xwiki.model.EntityType;
33    import org.xwiki.model.reference.EntityReference;
34    import org.xwiki.model.reference.EntityReferenceProvider;
35    import org.xwiki.model.reference.EntityReferenceResolver;
36    import org.xwiki.model.reference.EntityReferenceSerializer;
37    import org.xwiki.query.Query;
38    import org.xwiki.query.QueryException;
39    import org.xwiki.query.QueryManager;
40    import org.xwiki.tree.AbstractTreeNode;
41   
42    /**
43    * Base class for tree nodes that represent entities.
44    *
45    * @version $Id: c0e68af086dbc798ac99236a140b6625a2a21702 $
46    * @since 8.3M2
47    * @since 7.4.5
48    */
 
49    public abstract class AbstractEntityTreeNode extends AbstractTreeNode
50    {
51    @Inject
52    protected EntityReferenceSerializer<String> defaultEntityReferenceSerializer;
53   
54    @Inject
55    @Named("local")
56    protected EntityReferenceSerializer<String> localEntityReferenceSerializer;
57   
58    @Inject
59    protected QueryManager queryManager;
60   
61    @Inject
62    @Named("current")
63    private EntityReferenceResolver<String> currentEntityReferenceResolver;
64   
65    @Inject
66    @Named("user")
67    private ConfigurationSource userPreferencesSource;
68   
69    @Inject
70    private EntityReferenceProvider defaultEntityReferenceProvider;
71   
 
72  136 toggle protected EntityReference resolve(String nodeId)
73    {
74  136 String[] parts = StringUtils.split(nodeId, ":", 2);
75  136 if (parts == null || parts.length != 2) {
76  0 return null;
77    }
78   
79  136 try {
80  136 EntityType entityType = EntityType.valueOf(camelCaseToUnderscore(parts[0]).toUpperCase());
81  136 return this.currentEntityReferenceResolver.resolve(parts[1], entityType);
82    } catch (IllegalArgumentException e) {
83  0 return null;
84    }
85    }
86   
 
87  21 toggle protected String serialize(EntityReference entityReference)
88    {
89  21 return underscoreToCamelCase(entityReference.getType().name().toLowerCase()) + ':'
90    + this.defaultEntityReferenceSerializer.serialize(entityReference);
91    }
92   
 
93  12 toggle protected <E extends EntityReference> List<String> serialize(List<E> entityReferences)
94    {
95  12 List<String> nodeIds = new ArrayList<String>();
96  12 for (EntityReference entityReference : entityReferences) {
97  15 nodeIds.add(serialize(entityReference));
98    }
99  12 return nodeIds;
100    }
101   
 
102  136 toggle private String camelCaseToUnderscore(String nodeType)
103    {
104  136 return StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(nodeType), '_');
105    }
106   
 
107  21 toggle private String underscoreToCamelCase(String entityType)
108    {
109  21 StringBuilder result = new StringBuilder();
110  21 for (String part : StringUtils.split(entityType, '_')) {
111  21 result.append(StringUtils.capitalize(part));
112    }
113  21 return StringUtils.uncapitalize(result.toString());
114    }
115   
 
116  78 toggle protected boolean areHiddenEntitiesShown()
117    {
118  78 boolean shown = !Boolean.TRUE.equals(getProperties().get("filterHiddenDocuments"));
119  78 if (!shown) {
120  78 Integer value = this.userPreferencesSource.getProperty("displayHiddenDocuments", Integer.class);
121  78 shown = value != null && value == 1;
122    }
123  78 return shown;
124    }
125   
 
126  67 toggle protected String whereClause(List<String> constraints)
127    {
128  67 return "where " + StringUtils.join(constraints, " and ");
129    }
130   
 
131  67 toggle protected int getChildSpacesCount(EntityReference parentReference) throws QueryException
132    {
133  67 List<String> constraints = new ArrayList<String>();
134  67 Map<String, Object> parameters = new HashMap<String, Object>();
135   
136  67 EntityReference parentSpaceReference = parentReference.extractReference(EntityType.SPACE);
137  67 if (parentSpaceReference != null) {
138  67 constraints.add("parent = :parent");
139  67 parameters.put("parent", this.localEntityReferenceSerializer.serialize(parentSpaceReference));
140    } else {
141  0 constraints.add("parent is null");
142    }
143  67 if (!areHiddenEntitiesShown()) {
144  67 constraints.add("hidden <> true");
145    }
146   
147  67 String statement = "select count(*) from XWikiSpace " + whereClause(constraints);
148  67 Query query = this.queryManager.createQuery(statement, Query.HQL);
149  67 query.setWiki(parentReference.extractReference(EntityType.WIKI).getName());
150  67 for (Map.Entry<String, Object> entry : parameters.entrySet()) {
151  67 query.bindValue(entry.getKey(), entry.getValue());
152    }
153   
154  67 return ((Long) query.execute().get(0)).intValue();
155    }
156   
 
157  151 toggle protected String getDefaultDocumentName()
158    {
159  151 return this.defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT).getName();
160    }
161   
 
162  75 toggle protected boolean areTerminalDocumentsShown()
163    {
164  75 return !Boolean.FALSE.equals(getProperties().get("showTerminalDocuments"));
165    }
166    }