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.metadata; |
21 |
|
|
22 |
|
import java.util.Locale; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Named; |
26 |
|
import javax.inject.Singleton; |
27 |
|
|
28 |
|
import org.apache.solr.common.SolrInputDocument; |
29 |
|
import org.xwiki.component.annotation.Component; |
30 |
|
import org.xwiki.model.reference.AttachmentReference; |
31 |
|
import org.xwiki.model.reference.EntityReference; |
32 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
33 |
|
import org.xwiki.search.solr.internal.api.FieldUtils; |
34 |
|
|
35 |
|
import com.xpn.xwiki.XWikiContext; |
36 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
37 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
45 |
|
@Component |
46 |
|
@Named("attachment") |
47 |
|
@Singleton |
|
|
| 96.9% |
Uncovered Elements: 1 (32) |
Complexity: 4 |
Complexity Density: 0.14 |
|
48 |
|
public class AttachmentSolrMetadataExtractor extends AbstractSolrMetadataExtractor |
49 |
|
{ |
50 |
|
@Inject |
51 |
|
private EntityReferenceSerializer<String> entityReferenceSerializer; |
52 |
|
|
|
|
| 96% |
Uncovered Elements: 1 (25) |
Complexity: 3 |
Complexity Density: 0.13 |
|
53 |
92 |
@Override... |
54 |
|
public boolean setFieldsInternal(LengthSolrInputDocument solrDocument, EntityReference entityReference) |
55 |
|
throws Exception |
56 |
|
{ |
57 |
92 |
AttachmentReference attachmentReference = new AttachmentReference(entityReference); |
58 |
|
|
59 |
92 |
XWikiDocument document = getDocument(attachmentReference.getDocumentReference()); |
60 |
92 |
XWikiAttachment attachment = document.getAttachment(attachmentReference.getName()); |
61 |
92 |
if (attachment == null) { |
62 |
1 |
return false; |
63 |
|
} |
64 |
|
|
65 |
91 |
XWikiContext xcontext = xcontextProvider.get(); |
66 |
|
|
67 |
91 |
solrDocument.setField(FieldUtils.FILENAME, attachment.getFilename()); |
68 |
91 |
solrDocument.setField(FieldUtils.FILENAME_SORT, attachment.getFilename()); |
69 |
91 |
solrDocument.setField(FieldUtils.MIME_TYPE, attachment.getMimeType(xcontext)); |
70 |
91 |
solrDocument.setField(FieldUtils.ATTACHMENT_DATE, attachment.getDate()); |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
91 |
solrDocument.setField(FieldUtils.ATTACHMENT_DATE_SORT, attachment.getDate()); |
75 |
91 |
solrDocument.setField(FieldUtils.ATTACHMENT_SIZE, attachment.getFilesize()); |
76 |
91 |
solrDocument.setField(FieldUtils.ATTACHMENT_SIZE_SORT, attachment.getFilesize()); |
77 |
|
|
78 |
|
|
79 |
91 |
solrDocument.setField(FieldUtils.ATTACHMENT_VERSION, attachment.getVersion()); |
80 |
|
|
81 |
|
|
82 |
91 |
String authorStringReference = entityReferenceSerializer.serialize(attachment.getAuthorReference()); |
83 |
91 |
solrDocument.setField(FieldUtils.ATTACHMENT_AUTHOR, authorStringReference); |
84 |
91 |
try { |
85 |
|
|
86 |
91 |
String authorDisplayName = xcontext.getWiki().getPlainUserName(attachment.getAuthorReference(), xcontext); |
87 |
91 |
solrDocument.setField(FieldUtils.ATTACHMENT_AUTHOR_DISPLAY, authorDisplayName); |
88 |
91 |
solrDocument.setField(FieldUtils.ATTACHMENT_AUTHOR_DISPLAY_SORT, authorDisplayName); |
89 |
|
} catch (Exception e) { |
90 |
0 |
this.logger.error("Failed to get author display name for attachment [{}]", attachment.getReference(), e); |
91 |
|
} |
92 |
|
|
93 |
91 |
setLocaleAndContentFields(attachment, solrDocument); |
94 |
|
|
95 |
91 |
return true; |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
@param |
105 |
|
@param |
106 |
|
@throws |
107 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
108 |
91 |
protected void setLocaleAndContentFields(XWikiAttachment attachment, SolrInputDocument solrDocument)... |
109 |
|
throws Exception |
110 |
|
{ |
111 |
91 |
String attachmentTextContent = getContentAsText(attachment); |
112 |
|
|
113 |
|
|
114 |
91 |
for (Locale documentLocale : getLocales(attachment.getDoc(), null)) { |
115 |
181 |
solrDocument.addField(FieldUtils.LOCALES, documentLocale.toString()); |
116 |
|
|
117 |
181 |
solrDocument.setField(FieldUtils.getFieldName(FieldUtils.ATTACHMENT_CONTENT, documentLocale), |
118 |
|
attachmentTextContent); |
119 |
|
} |
120 |
|
|
121 |
|
|
122 |
|
|
123 |
91 |
solrDocument.setField(FieldUtils.getFieldName(FieldUtils.ATTACHMENT_CONTENT, null), attachmentTextContent); |
124 |
|
} |
125 |
|
} |