1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.search.solr.internal.reference

File DocumentSolrReferenceResolver.java

 

Coverage histogram

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

Code metrics

8
54
6
1
226
136
17
0.31
9
6
2.83

Classes

Class Line # Actions
DocumentSolrReferenceResolver 58 54 0% 17 5
0.926470692.6%
 

Contributing tests

This file is covered by 11 tests. .

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.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    * Resolve document references.
51    *
52    * @version $Id: 25aaa72288aad6f30d3ac0b704e45ef014712db6 $
53    * @since 5.1M2
54    */
55    @Component
56    @Named("document")
57    @Singleton
 
58    public class DocumentSolrReferenceResolver extends AbstractSolrReferenceResolver
59    {
60    /**
61    * Used to resolve object references.
62    */
63    @Inject
64    @Named("object")
65    private Provider<SolrReferenceResolver> objectResolverProvider;
66   
67    /**
68    * Used to resolve space references.
69    */
70    @Inject
71    @Named("space")
72    private Provider<SolrReferenceResolver> spaceResolverProvider;
73   
74    /**
75    * Used to resolve attachment references.
76    */
77    @Inject
78    @Named("attachment")
79    private Provider<SolrReferenceResolver> attachmentResolverProvider;
80   
 
81  8638 toggle @Override
82    public List<EntityReference> getReferences(EntityReference reference) throws SolrIndexerException
83    {
84  8638 List<EntityReference> result = new ArrayList<EntityReference>();
85   
86  8638 XWikiContext xcontext = this.xcontextProvider.get();
87   
88  8638 DocumentReference documentReference = new DocumentReference(reference);
89   
90  8638 if (xcontext.getWiki().exists(documentReference, xcontext)) {
91    // Document itself
92  8598 result.add(documentReference);
93   
94    // FIXME: Assumption - Only original documents contain objects and attachments, because objects are
95    // not translatable.
96    // https://jira.xwiki.org/browse/XWIKI-69 is the long standing issue on which the second assumption relies.
97  8598 if (documentReference.getLocale() == null || documentReference.getLocale().equals(Locale.ROOT)) {
98  5608 XWikiDocument document;
99  5608 try {
100  5608 document = getDocument(documentReference);
101    } catch (Exception e) {
102  0 throw new SolrIndexerException(String.format("Failed to get document [%s]", documentReference), e);
103    }
104   
105    // Document translations
106  5608 List<Locale> translatedLocales;
107  5608 try {
108  5608 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  5608 for (Locale translatedLocale : translatedLocales) {
115  2995 DocumentReference translatedDocumentReference =
116    new DocumentReference(documentReference, translatedLocale);
117  2995 result.add(translatedDocumentReference);
118    }
119   
120    // Attachments
121  5608 addAttachmentsReferences(document, result);
122   
123    // Objects
124  5608 addObjectsReferences(document, result);
125    }
126    }
127   
128  8638 return result;
129    }
130   
131    /**
132    * @param document the document
133    * @param result the list to add reference to
134    */
 
135  5608 toggle private void addAttachmentsReferences(XWikiDocument document, List<EntityReference> result)
136    {
137  5608 List<XWikiAttachment> attachments = document.getAttachmentList();
138  5608 for (XWikiAttachment attachment : attachments) {
139  1035 AttachmentReference attachmentReference = attachment.getReference();
140   
141  1035 try {
142  1035 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 document the document
151    * @param result the list to add reference to
152    */
 
153  5608 toggle private void addObjectsReferences(XWikiDocument document, List<EntityReference> result)
154    {
155  5608 for (Entry<DocumentReference, List<BaseObject>> entry : document.getXObjects().entrySet()) {
156  5534 List<BaseObject> objects = entry.getValue();
157  5534 for (BaseObject object : objects) {
158  7342 if (object != null) {
159  7095 BaseObjectReference objectReference = object.getReference();
160   
161  7095 try {
162  7095 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   
 
171  7875 toggle @Override
172    public String getId(EntityReference reference) throws SolrIndexerException
173    {
174  7875 DocumentReference documentReference = new DocumentReference(reference);
175   
176  7875 String result = super.getId(reference);
177   
178    // Document IDs also contain the locale code to differentiate between them.
179    // Objects, attachments, etc. don`t need this because the only thing that is translated in an XWiki document
180    // right now is the document title and content. Objects and attachments are not translated.
181  7875 result += FieldUtils.USCORE + getLocale(documentReference);
182   
183  7875 return result;
184    }
185   
186    /**
187    * @param documentReference reference to the document.
188    * @return the locale code of the referenced document.
189    * @throws SolrIndexerException if problems occur.
190    */
 
191  7875 toggle protected Locale getLocale(DocumentReference documentReference) throws SolrIndexerException
192    {
193  7875 Locale locale = null;
194   
195  7875 try {
196  7875 if (documentReference.getLocale() != null && !documentReference.getLocale().equals(Locale.ROOT)) {
197  3058 locale = documentReference.getLocale();
198    } else {
199  4817 XWikiContext xcontext = this.xcontextProvider.get();
200  4817 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  7875 return locale;
208    }
209   
 
210  46 toggle @Override
211    public String getQuery(EntityReference reference) throws SolrIndexerException
212    {
213  46 StringBuilder builder = new StringBuilder();
214   
215  46 EntityReference spaceReference = reference.extractReference(EntityType.SPACE);
216  46 builder.append(spaceResolverProvider.get().getQuery(spaceReference));
217   
218  46 builder.append(QUERY_AND);
219   
220  46 builder.append(FieldUtils.NAME_EXACT);
221  46 builder.append(':');
222  46 builder.append(ClientUtils.escapeQueryChars(reference.getName()));
223   
224  46 return builder.toString();
225    }
226    }