1. Project Clover database Tue Dec 20 2016 21:24:09 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: 405b70e96103d6bed899aab13944fb1c2255db44 $
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  504 toggle @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    // Document itself
92  503 result.add(documentReference);
93   
94    // FIXME: Assumption - Only original documents contain objects and attachments, because objects are
95    // not translatable.
96    // http://jira.xwiki.org/browse/XWIKI-69 is the long standing issue on which the second assumption relies.
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    // Document translations
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    // Attachments
121  445 addAttachmentsReferences(document, result);
122   
123    // Objects
124  445 addObjectsReferences(document, result);
125    }
126    }
127   
128  504 return result;
129    }
130   
131    /**
132    * @param document the document
133    * @param result the list to add reference to
134    */
 
135  445 toggle 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 document the document
151    * @param result the list to add reference to
152    */
 
153  445 toggle 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   
 
171  910 toggle @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    // 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  910 result += FieldUtils.USCORE + getLocale(documentReference);
182   
183  910 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  910 toggle 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   
 
210  2 toggle @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    }