1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.search.solr.internal.reference

File SpaceSolrReferenceResolver.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
22
2
1
126
71
4
0.18
11
2
2

Classes

Class Line # Actions
SpaceSolrReferenceResolver 51 22 0% 4 2
0.916666791.7%
 

Contributing tests

This file is covered by 6 tests. .

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.search.solr.internal.reference;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Provider;
28    import javax.inject.Singleton;
29   
30    import org.apache.solr.client.solrj.util.ClientUtils;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.model.EntityType;
33    import org.xwiki.model.reference.EntityReference;
34    import org.xwiki.model.reference.EntityReferenceSerializer;
35    import org.xwiki.query.QueryException;
36    import org.xwiki.query.QueryManager;
37    import org.xwiki.search.solr.internal.api.FieldUtils;
38    import org.xwiki.search.solr.internal.api.SolrIndexerException;
39   
40    import com.google.common.collect.Iterables;
41   
42    /**
43    * Resolve space references.
44    *
45    * @version $Id: 9308c1c2666a24dc96b1b3fa940a0b87087ab3ab $
46    * @since 5.1M2
47    */
48    @Component
49    @Named("space")
50    @Singleton
 
51    public class SpaceSolrReferenceResolver extends AbstractSolrReferenceResolver
52    {
53    /**
54    * Used to resolve document references.
55    */
56    @Inject
57    @Named("document")
58    private Provider<SolrReferenceResolver> documentResolverProvider;
59   
60    /**
61    * Used to resolve document references.
62    */
63    @Inject
64    @Named("wiki")
65    private Provider<SolrReferenceResolver> wikiResolverProvider;
66   
67    /**
68    * Query manager used to perform queries on the XWiki model.
69    */
70    @Inject
71    private QueryManager queryManager;
72   
73    @Inject
74    @Named("local")
75    private EntityReferenceSerializer<String> localEntityReferenceSerializer;
76   
 
77  10 toggle @Override
78    public List<EntityReference> getReferences(EntityReference spaceReference) throws SolrIndexerException
79    {
80  10 List<EntityReference> result = new ArrayList<EntityReference>();
81  10 EntityReference wikiReference = spaceReference.extractReference(EntityType.WIKI);
82  10 String localSpaceReference = this.localEntityReferenceSerializer.serialize(spaceReference);
83   
84    // Ignore the space reference because it is not indexable.
85   
86    // Make sure the list of spaces is from the requested wiki
87  10 List<String> documentNames;
88  10 try {
89  10 documentNames =
90    this.queryManager.getNamedQuery("getSpaceDocsName").setWiki(wikiReference.getName())
91    .bindValue("space", localSpaceReference).execute();
92    } catch (QueryException e) {
93  0 throw new SolrIndexerException("Failed to query space [" + spaceReference + "] documents", e);
94    }
95   
96  10 for (String documentName : documentNames) {
97  18 EntityReference documentReference = new EntityReference(documentName, EntityType.DOCUMENT, spaceReference);
98   
99  18 try {
100  18 Iterables.addAll(result, this.documentResolverProvider.get().getReferences(documentReference));
101    } catch (Exception e) {
102  0 this.logger.error("Failed to resolve references for document [" + documentReference + "]", e);
103    }
104    }
105   
106  10 return result;
107    }
108   
 
109  3 toggle @Override
110    public String getQuery(EntityReference reference) throws SolrIndexerException
111    {
112  3 StringBuilder builder = new StringBuilder();
113   
114  3 EntityReference wikiReference = reference.extractReference(EntityType.WIKI);
115  3 builder.append(wikiResolverProvider.get().getQuery(wikiReference));
116   
117  3 builder.append(QUERY_AND);
118   
119  3 builder.append(FieldUtils.SPACE_EXACT);
120  3 builder.append(':');
121  3 String localSpaceReference = this.localEntityReferenceSerializer.serialize(reference);
122  3 builder.append(ClientUtils.escapeQueryChars(localSpaceReference));
123   
124  3 return builder.toString();
125    }
126    }