1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
61 |
|
|
62 |
|
@version |
63 |
|
@since |
64 |
|
|
65 |
|
@Component |
66 |
|
@Named("solr.availablelocales") |
67 |
|
@Singleton |
|
|
| 39.5% |
Uncovered Elements: 26 (43) |
Complexity: 11 |
Complexity Density: 0.35 |
|
68 |
|
public class SolrIndexAvailableLocalesListener implements EventListener |
69 |
|
{ |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
private static final String PREFERENCEDOCUMENT_REGEX = ".*:XWiki.XWikiPreferences"; |
74 |
|
|
75 |
|
|
76 |
|
|
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 |
|
|
85 |
|
|
86 |
|
private Map<String, Set<Locale>> localesCache = new ConcurrentHashMap<String, Set<Locale>>(); |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
@Inject |
92 |
|
private Logger logger; |
93 |
|
|
94 |
|
|
95 |
|
@link |
96 |
|
|
97 |
|
@Inject |
98 |
|
private Provider<SolrInstance> solrInstanceProvider; |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
@link |
104 |
|
|
105 |
|
@Inject |
106 |
|
private Provider<SolrIndexer> solrIndexer; |
107 |
|
|
108 |
|
|
109 |
|
@link |
110 |
|
|
111 |
|
@Inject |
112 |
|
private DocumentReferenceResolver<SolrDocument> solrDocumentReferenceResolver; |
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
114 |
3 |
@Override... |
115 |
|
public List<Event> getEvents() |
116 |
|
{ |
117 |
3 |
return EVENTS; |
118 |
|
} |
119 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
120 |
33 |
@Override... |
121 |
|
public String getName() |
122 |
|
{ |
123 |
33 |
return this.getClass().getName(); |
124 |
|
} |
125 |
|
|
|
|
| 38.7% |
Uncovered Elements: 19 (31) |
Complexity: 6 |
Complexity Density: 0.24 |
|
126 |
76 |
@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 |
|
|
137 |
76 |
this.localesCache.put(wiki, new HashSet<Locale>(availableLocales)); |
138 |
|
|
139 |
76 |
try { |
140 |
|
|
141 |
|
|
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 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
168 |
0 |
@Override... |
169 |
|
public void streamSolrDocument(SolrDocument doc) |
170 |
|
{ |
171 |
0 |
solrIndexer.get().index(solrDocumentReferenceResolver.resolve(doc), true); |
172 |
|
} |
173 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
174 |
0 |
@Override... |
175 |
|
public void streamDocListInfo(long numFound, long start, Float maxScore) |
176 |
|
{ |
177 |
|
|
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 |
190 |
|
@return |
191 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
192 |
0 |
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 |
|
} |