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

File SolrIndexAvailableLocalesListener.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart4.png
78% of files have more coverage

Code metrics

6
31
6
1
200
118
11
0.35
5.17
6
1.83

Classes

Class Line # Actions
SolrIndexAvailableLocalesListener 68 31 0% 11 26
0.3953488539.5%
 

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.search.solr.internal;
21   
22    import java.util.Arrays;
23    import java.util.Collection;
24    import java.util.HashSet;
25    import java.util.List;
26    import java.util.Locale;
27    import java.util.Map;
28    import java.util.Set;
29    import java.util.concurrent.ConcurrentHashMap;
30   
31    import javax.inject.Inject;
32    import javax.inject.Named;
33    import javax.inject.Provider;
34    import javax.inject.Singleton;
35   
36    import org.apache.commons.collections4.CollectionUtils;
37    import org.apache.commons.lang3.LocaleUtils;
38    import org.apache.solr.client.solrj.SolrQuery;
39    import org.apache.solr.client.solrj.StreamingResponseCallback;
40    import org.apache.solr.common.SolrDocument;
41    import org.slf4j.Logger;
42    import org.xwiki.bridge.event.AbstractDocumentEvent;
43    import org.xwiki.bridge.event.ApplicationReadyEvent;
44    import org.xwiki.bridge.event.DocumentCreatedEvent;
45    import org.xwiki.bridge.event.DocumentUpdatedEvent;
46    import org.xwiki.bridge.event.WikiReadyEvent;
47    import org.xwiki.component.annotation.Component;
48    import org.xwiki.model.EntityType;
49    import org.xwiki.model.reference.DocumentReferenceResolver;
50    import org.xwiki.observation.EventListener;
51    import org.xwiki.observation.event.Event;
52    import org.xwiki.observation.event.filter.RegexEventFilter;
53    import org.xwiki.search.solr.internal.api.FieldUtils;
54    import org.xwiki.search.solr.internal.api.SolrIndexer;
55    import org.xwiki.search.solr.internal.api.SolrInstance;
56   
57    import com.xpn.xwiki.XWikiContext;
58   
59    /**
60    * Update already indexed entries when new available locales are added.
61    *
62    * @version $Id: cd2cdb528ed34da48b06b05eea162b9308dff321 $
63    * @since 5.1RC1
64    */
65    @Component
66    @Named("solr.availablelocales")
67    @Singleton
 
68    public class SolrIndexAvailableLocalesListener implements EventListener
69    {
70    /**
71    * The regex used to match preferences documents.
72    */
73    private static final String PREFERENCEDOCUMENT_REGEX = ".*:XWiki.XWikiPreferences";
74   
75    /**
76    * The events to listen to that trigger the index update.
77    */
78    private static final List<Event> EVENTS =
79    Arrays.<Event>asList(new DocumentUpdatedEvent(new RegexEventFilter(PREFERENCEDOCUMENT_REGEX)),
80    new DocumentCreatedEvent(new RegexEventFilter(PREFERENCEDOCUMENT_REGEX)), new ApplicationReadyEvent(),
81    new WikiReadyEvent(), new ApplicationReadyEvent());
82   
83    /**
84    * The currently available locales for each running wiki.
85    */
86    private Map<String, Set<Locale>> localesCache = new ConcurrentHashMap<String, Set<Locale>>();
87   
88    /**
89    * Logging framework.
90    */
91    @Inject
92    private Logger logger;
93   
94    /**
95    * Provider for the {@link SolrInstance} that allows communication with the Solr server.
96    */
97    @Inject
98    private Provider<SolrInstance> solrInstanceProvider;
99   
100    /**
101    * The solr index.
102    * <p>
103    * Lazily initialize the {@link SolrIndexer} to not initialize it too early.
104    */
105    @Inject
106    private Provider<SolrIndexer> solrIndexer;
107   
108    /**
109    * Used to extract a document reference from a {@link SolrDocument}.
110    */
111    @Inject
112    private DocumentReferenceResolver<SolrDocument> solrDocumentReferenceResolver;
113   
 
114  3 toggle @Override
115    public List<Event> getEvents()
116    {
117  3 return EVENTS;
118    }
119   
 
120  33 toggle @Override
121    public String getName()
122    {
123  33 return this.getClass().getName();
124    }
125   
 
126  76 toggle @Override
127    public void onEvent(Event event, Object source, Object data)
128    {
129  76 XWikiContext xcontext = (XWikiContext) data;
130   
131  76 String wiki = xcontext.getWikiId();
132   
133  76 Set<Locale> oldLocales = this.localesCache.get(wiki);
134  76 List<Locale> availableLocales = xcontext.getWiki().getAvailableLocales(xcontext);
135   
136    // Update the cache
137  76 this.localesCache.put(wiki, new HashSet<Locale>(availableLocales));
138   
139  76 try {
140    // oldLocales may be null in case the XWikiPreferences has been modified as part of a mandatory document
141    // initialization
142  76 if (oldLocales != null && event instanceof AbstractDocumentEvent) {
143  72 Collection<Locale> newLocales = CollectionUtils.subtract(availableLocales, oldLocales);
144   
145  72 if (!newLocales.isEmpty()) {
146  0 StringBuilder builder = new StringBuilder();
147   
148  0 for (Locale newLocale : newLocales) {
149  0 for (Locale locale : getParentLocales(newLocale)) {
150  0 if (builder.length() > 0) {
151  0 builder.append(" OR ");
152    }
153  0 builder.append(FieldUtils.DOCUMENT_LOCALE);
154  0 builder.append(':');
155  0 builder.append('"');
156  0 builder.append(locale.toString());
157  0 builder.append('"');
158    }
159    }
160   
161  0 SolrQuery solrQuery = new SolrQuery(builder.toString());
162  0 solrQuery
163    .setFields(FieldUtils.WIKI, FieldUtils.SPACES, FieldUtils.NAME, FieldUtils.DOCUMENT_LOCALE);
164  0 solrQuery.addFilterQuery(FieldUtils.TYPE + ':' + EntityType.DOCUMENT.name());
165   
166  0 StreamingResponseCallback callback = new StreamingResponseCallback()
167    {
 
168  0 toggle @Override
169    public void streamSolrDocument(SolrDocument doc)
170    {
171  0 solrIndexer.get().index(solrDocumentReferenceResolver.resolve(doc), true);
172    }
173   
 
174  0 toggle @Override
175    public void streamDocListInfo(long numFound, long start, Float maxScore)
176    {
177    // Do nothing.
178    }
179    };
180  0 this.solrInstanceProvider.get().queryAndStreamResponse(solrQuery, callback);
181    }
182    }
183    } catch (Exception e) {
184  0 this.logger.error("Failed to handle event [{}] with source [{}]", event, source, e);
185    }
186    }
187   
188    /**
189    * @param locale the locale
190    * @return the parents of the locale
191    */
 
192  0 toggle private Set<Locale> getParentLocales(Locale locale)
193    {
194  0 Set<Locale> parentLocales = new HashSet<Locale>(LocaleUtils.localeLookupList(locale, Locale.ROOT));
195   
196  0 parentLocales.remove(locale);
197   
198  0 return parentLocales;
199    }
200    }