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

File AttachmentSolrMetadataExtractor.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
28
2
1
125
64
4
0.14
14
2
2

Classes

Class Line # Actions
AttachmentSolrMetadataExtractor 48 28 0% 4 1
0.9687596.9%
 

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.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    * Extract the metadata to be indexed from attachments.
41    *
42    * @version $Id: 2db0b3880055b2a15de66a244f5a84020c39039b $
43    * @since 4.3M2
44    */
45    @Component
46    @Named("attachment")
47    @Singleton
 
48    public class AttachmentSolrMetadataExtractor extends AbstractSolrMetadataExtractor
49    {
50    @Inject
51    private EntityReferenceSerializer<String> entityReferenceSerializer;
52   
 
53  92 toggle @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    // We need to add a dedicated sort field because the corresponding field is multiValued and thus cannot be used
72    // for sorting (the reason it is multiValued is because it is 'reused' on document rows and documents can have
73    // multiple attachments).
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    // We need to index the attachment version (revision) to be able to detect when the search index is out of date
78    // (not in sync with the database).
79  91 solrDocument.setField(FieldUtils.ATTACHMENT_VERSION, attachment.getVersion());
80   
81    // Index the full author reference for exact matching (faceting).
82  91 String authorStringReference = entityReferenceSerializer.serialize(attachment.getAuthorReference());
83  91 solrDocument.setField(FieldUtils.ATTACHMENT_AUTHOR, authorStringReference);
84  91 try {
85    // Index the author display name for free text search and results sorting.
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    * Set the locale to all the translations that the owning document has. This ensures that this entity is found for
100    * all the translations of a document, not just the original document.
101    * <p>
102    * Also, index the content with each locale so that the right analyzer is used.
103    *
104    * @param attachment the attachment.
105    * @param solrDocument the Solr document where to add the fields.
106    * @throws Exception if problems occur.
107    */
 
108  91 toggle protected void setLocaleAndContentFields(XWikiAttachment attachment, SolrInputDocument solrDocument)
109    throws Exception
110    {
111  91 String attachmentTextContent = getContentAsText(attachment);
112   
113    // Do the work for each locale.
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    // We can't rely on the schema's copyField here because we would trigger it for each language. Doing the copy to
122    // the text_general field manually.
123  91 solrDocument.setField(FieldUtils.getFieldName(FieldUtils.ATTACHMENT_CONTENT, null), attachmentTextContent);
124    }
125    }