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

File DefaultSolrReferenceResolver.java

 

Coverage histogram

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

Code metrics

14
35
10
2
202
123
21
0.6
3.5
5
2.1

Classes

Class Line # Actions
DefaultSolrReferenceResolver 48 20 0% 10 4
0.8709677587.1%
DefaultSolrReferenceResolver.FarmIterator 55 15 0% 11 4
0.8571428785.7%
 

Contributing tests

This file is covered by 20 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.Iterator;
24    import java.util.List;
25   
26    import javax.inject.Inject;
27    import javax.inject.Singleton;
28   
29    import org.slf4j.Logger;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.component.manager.ComponentLookupException;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.model.EntityType;
34    import org.xwiki.model.reference.EntityReference;
35    import org.xwiki.model.reference.WikiReference;
36    import org.xwiki.search.solr.internal.api.SolrIndexerException;
37    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
38    import org.xwiki.wiki.manager.WikiManagerException;
39   
40    /**
41    * Dispatch to the proper {@link SolrReferenceResolver}.
42    *
43    * @version $Id: e833a867223fe9203b2551919a13c3d6f49ab5dc $
44    * @since 5.1M2
45    */
46    @Component
47    @Singleton
 
48    public class DefaultSolrReferenceResolver implements SolrReferenceResolver
49    {
50    /**
51    * Lazily get reference one wiki at a time.
52    *
53    * @version $Id: e833a867223fe9203b2551919a13c3d6f49ab5dc $
54    */
 
55    class FarmIterator implements Iterator<EntityReference>
56    {
57    /**
58    * The current iterator.
59    */
60    private Iterator<EntityReference> currentIterator;
61   
62    /**
63    * The current wiki.
64    */
65    private final Iterator<String> currentWiki;
66   
67    /**
68    * @param wikis the wikis
69    */
 
70  2 toggle FarmIterator(List<String> wikis)
71    {
72  2 this.currentWiki = wikis.iterator();
73    }
74   
 
75  0 toggle @Override
76    public void remove()
77    {
78  0 throw new UnsupportedOperationException();
79    }
80   
 
81  26 toggle @Override
82    public boolean hasNext()
83    {
84  26 update();
85   
86  26 return currentIterator != null;
87    }
88   
 
89  24 toggle @Override
90    public EntityReference next()
91    {
92  24 update();
93   
94  24 return currentIterator != null ? currentIterator.next() : null;
95    }
96   
97    /**
98    * Make sure to point the caret to the right element.
99    */
 
100  52 toggle private void update()
101    {
102  52 if (currentIterator == null || !currentIterator.hasNext()) {
103  6 if (currentWiki.hasNext()) {
104  4 String wiki = currentWiki.next();
105  4 try {
106  4 currentIterator = getReferences(new WikiReference(wiki)).iterator();
107    } catch (SolrIndexerException e) {
108  0 logger.error("Failed to get references for wiki [" + wiki + "]", e);
109    }
110   
111  4 if (!currentIterator.hasNext()) {
112  2 update();
113    }
114    } else {
115  2 currentIterator = null;
116    }
117    }
118    }
119    }
120   
121    /**
122    * Used to find the {@link SolrReferenceResolver}.
123    */
124    @Inject
125    private ComponentManager componentManager;
126   
127    /**
128    * The logger.
129    */
130    @Inject
131    private Logger logger;
132   
133    /**
134    * Used to get the list of available wikis.
135    */
136    @Inject
137    private WikiDescriptorManager wikiDescriptorManager;
138   
139    /**
140    * @param reference the reference
141    * @return the resolver associated to the reference type
142    * @throws SolrIndexerException when failed to find a resolve associated to the passed reference
143    */
 
144  938 toggle private SolrReferenceResolver getResover(EntityReference reference) throws SolrIndexerException
145    {
146  938 EntityType type = reference.getType();
147   
148  938 SolrReferenceResolver resolver;
149  938 try {
150  938 resolver = this.componentManager.getInstance(SolrReferenceResolver.class, type.getLowerCase());
151    } catch (ComponentLookupException e) {
152  0 throw new SolrIndexerException("Failed to get SolrDocumentReferenceResolver corresponding to entity type ["
153    + type + "]", e);
154    }
155   
156  938 return resolver;
157    }
158   
 
159  498 toggle @Override
160    public Iterable<EntityReference> getReferences(EntityReference reference) throws SolrIndexerException
161    {
162  498 if (reference != null) {
163  497 return getResover(reference).getReferences(reference);
164    } else {
165  1 final List<String> wikis;
166  1 try {
167  1 wikis = new ArrayList<String>(this.wikiDescriptorManager.getAllIds());
168    } catch (WikiManagerException e) {
169  0 throw new SolrIndexerException("Failed to get the list of available wikis.", e);
170    }
171   
172  1 return new Iterable<EntityReference>()
173    {
 
174  2 toggle @Override
175    public Iterator<EntityReference> iterator()
176    {
177  2 return new FarmIterator(wikis);
178    }
179    };
180    }
181    }
182   
 
183  435 toggle @Override
184    public String getId(EntityReference reference) throws SolrIndexerException, IllegalArgumentException
185    {
186  435 if (reference != null) {
187  435 return getResover(reference).getId(reference);
188    } else {
189  0 return null;
190    }
191    }
192   
 
193  9 toggle @Override
194    public String getQuery(EntityReference reference) throws SolrIndexerException
195    {
196  9 if (reference != null) {
197  6 return getResover(reference).getQuery(reference);
198    } else {
199  3 return "*:*";
200    }
201    }
202    }