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.reference; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.List; |
24 |
|
import java.util.Locale; |
25 |
|
import java.util.Map.Entry; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Provider; |
30 |
|
import javax.inject.Singleton; |
31 |
|
|
32 |
|
import org.apache.solr.client.solrj.util.ClientUtils; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.model.EntityType; |
35 |
|
import org.xwiki.model.reference.AttachmentReference; |
36 |
|
import org.xwiki.model.reference.DocumentReference; |
37 |
|
import org.xwiki.model.reference.EntityReference; |
38 |
|
import org.xwiki.search.solr.internal.api.FieldUtils; |
39 |
|
import org.xwiki.search.solr.internal.api.SolrIndexerException; |
40 |
|
|
41 |
|
import com.google.common.collect.Iterables; |
42 |
|
import com.xpn.xwiki.XWikiContext; |
43 |
|
import com.xpn.xwiki.XWikiException; |
44 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
45 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
46 |
|
import com.xpn.xwiki.objects.BaseObject; |
47 |
|
import com.xpn.xwiki.objects.BaseObjectReference; |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
@version |
53 |
|
@since |
54 |
|
|
55 |
|
@Component |
56 |
|
@Named("document") |
57 |
|
@Singleton |
|
|
| 92.6% |
Uncovered Elements: 5 (68) |
Complexity: 17 |
Complexity Density: 0.31 |
|
58 |
|
public class DocumentSolrReferenceResolver extends AbstractSolrReferenceResolver |
59 |
|
{ |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
@Inject |
64 |
|
@Named("object") |
65 |
|
private Provider<SolrReferenceResolver> objectResolverProvider; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
@Inject |
71 |
|
@Named("space") |
72 |
|
private Provider<SolrReferenceResolver> spaceResolverProvider; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@Inject |
78 |
|
@Named("attachment") |
79 |
|
private Provider<SolrReferenceResolver> attachmentResolverProvider; |
80 |
|
|
|
|
| 91.7% |
Uncovered Elements: 2 (24) |
Complexity: 6 |
Complexity Density: 0.3 |
|
81 |
504 |
@Override... |
82 |
|
public List<EntityReference> getReferences(EntityReference reference) throws SolrIndexerException |
83 |
|
{ |
84 |
504 |
List<EntityReference> result = new ArrayList<EntityReference>(); |
85 |
|
|
86 |
504 |
XWikiContext xcontext = this.xcontextProvider.get(); |
87 |
|
|
88 |
504 |
DocumentReference documentReference = new DocumentReference(reference); |
89 |
|
|
90 |
504 |
if (xcontext.getWiki().exists(documentReference, xcontext)) { |
91 |
|
|
92 |
503 |
result.add(documentReference); |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
503 |
if (documentReference.getLocale() == null || documentReference.getLocale().equals(Locale.ROOT)) { |
98 |
445 |
XWikiDocument document; |
99 |
445 |
try { |
100 |
445 |
document = getDocument(documentReference); |
101 |
|
} catch (Exception e) { |
102 |
0 |
throw new SolrIndexerException(String.format("Failed to get document [%s]", documentReference), e); |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
445 |
List<Locale> translatedLocales; |
107 |
445 |
try { |
108 |
445 |
translatedLocales = document.getTranslationLocales(xcontext); |
109 |
|
} catch (XWikiException e) { |
110 |
0 |
throw new SolrIndexerException(String.format("Failed to get document [%s] translations", |
111 |
|
documentReference), e); |
112 |
|
} |
113 |
|
|
114 |
445 |
for (Locale translatedLocale : translatedLocales) { |
115 |
64 |
DocumentReference translatedDocumentReference = |
116 |
|
new DocumentReference(documentReference, translatedLocale); |
117 |
64 |
result.add(translatedDocumentReference); |
118 |
|
} |
119 |
|
|
120 |
|
|
121 |
445 |
addAttachmentsReferences(document, result); |
122 |
|
|
123 |
|
|
124 |
445 |
addObjectsReferences(document, result); |
125 |
|
} |
126 |
|
} |
127 |
|
|
128 |
504 |
return result; |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
|
@param |
133 |
|
@param |
134 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
135 |
445 |
private void addAttachmentsReferences(XWikiDocument document, List<EntityReference> result)... |
136 |
|
{ |
137 |
445 |
List<XWikiAttachment> attachments = document.getAttachmentList(); |
138 |
445 |
for (XWikiAttachment attachment : attachments) { |
139 |
60 |
AttachmentReference attachmentReference = attachment.getReference(); |
140 |
|
|
141 |
60 |
try { |
142 |
60 |
Iterables.addAll(result, this.attachmentResolverProvider.get().getReferences(attachmentReference)); |
143 |
|
} catch (Exception e) { |
144 |
0 |
this.logger.error("Failed to resolve references for attachment [" + attachmentReference + "]", e); |
145 |
|
} |
146 |
|
} |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
|
@param |
151 |
|
@param |
152 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
153 |
445 |
private void addObjectsReferences(XWikiDocument document, List<EntityReference> result)... |
154 |
|
{ |
155 |
445 |
for (Entry<DocumentReference, List<BaseObject>> entry : document.getXObjects().entrySet()) { |
156 |
382 |
List<BaseObject> objects = entry.getValue(); |
157 |
382 |
for (BaseObject object : objects) { |
158 |
538 |
if (object != null) { |
159 |
510 |
BaseObjectReference objectReference = object.getReference(); |
160 |
|
|
161 |
510 |
try { |
162 |
510 |
Iterables.addAll(result, this.objectResolverProvider.get().getReferences(objectReference)); |
163 |
|
} catch (Exception e) { |
164 |
0 |
this.logger.error("Failed to resolve references for object [" + objectReference + "]", e); |
165 |
|
} |
166 |
|
} |
167 |
|
} |
168 |
|
} |
169 |
|
} |
170 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
171 |
910 |
@Override... |
172 |
|
public String getId(EntityReference reference) throws SolrIndexerException |
173 |
|
{ |
174 |
910 |
DocumentReference documentReference = new DocumentReference(reference); |
175 |
|
|
176 |
910 |
String result = super.getId(reference); |
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
910 |
result += FieldUtils.USCORE + getLocale(documentReference); |
182 |
|
|
183 |
910 |
return result; |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
@param |
188 |
|
@return |
189 |
|
@throws |
190 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 4 |
Complexity Density: 0.5 |
|
191 |
910 |
protected Locale getLocale(DocumentReference documentReference) throws SolrIndexerException... |
192 |
|
{ |
193 |
910 |
Locale locale = null; |
194 |
|
|
195 |
910 |
try { |
196 |
910 |
if (documentReference.getLocale() != null && !documentReference.getLocale().equals(Locale.ROOT)) { |
197 |
119 |
locale = documentReference.getLocale(); |
198 |
|
} else { |
199 |
791 |
XWikiContext xcontext = this.xcontextProvider.get(); |
200 |
791 |
locale = xcontext.getWiki().getDocument(documentReference, xcontext).getRealLocale(); |
201 |
|
} |
202 |
|
} catch (Exception e) { |
203 |
0 |
throw new SolrIndexerException(String.format("Exception while fetching the locale of the document '%s'", |
204 |
|
documentReference), e); |
205 |
|
} |
206 |
|
|
207 |
910 |
return locale; |
208 |
|
} |
209 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
210 |
2 |
@Override... |
211 |
|
public String getQuery(EntityReference reference) throws SolrIndexerException |
212 |
|
{ |
213 |
2 |
StringBuilder builder = new StringBuilder(); |
214 |
|
|
215 |
2 |
EntityReference spaceReference = reference.extractReference(EntityType.SPACE); |
216 |
2 |
builder.append(spaceResolverProvider.get().getQuery(spaceReference)); |
217 |
|
|
218 |
2 |
builder.append(QUERY_AND); |
219 |
|
|
220 |
2 |
builder.append(FieldUtils.NAME_EXACT); |
221 |
2 |
builder.append(':'); |
222 |
2 |
builder.append(ClientUtils.escapeQueryChars(reference.getName())); |
223 |
|
|
224 |
2 |
return builder.toString(); |
225 |
|
} |
226 |
|
} |