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

File SolrEntityReferenceResolver.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

34
70
13
1
220
164
35
0.5
5.38
13
2.69

Classes

Class Line # Actions
SolrEntityReferenceResolver 50 70 0% 35 29
0.7521367775.2%
 

Contributing tests

This file is covered by 1 test. .

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.lang.reflect.Type;
23    import java.util.Collection;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.apache.commons.lang.LocaleUtils;
30    import org.apache.commons.lang3.StringUtils;
31    import org.apache.lucene.document.Field;
32    import org.apache.lucene.index.IndexableField;
33    import org.apache.solr.common.SolrDocument;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.component.util.DefaultParameterizedType;
36    import org.xwiki.model.EntityType;
37    import org.xwiki.model.reference.DocumentReference;
38    import org.xwiki.model.reference.EntityReference;
39    import org.xwiki.model.reference.EntityReferenceResolver;
40    import org.xwiki.search.solr.internal.api.FieldUtils;
41   
42    /**
43    * Component used to extract an {@link EntityReference} from a {@link SolrDocument}.
44    *
45    * @version $Id: 149d372f107c1800721aa9351f17a1f7ab1881a4 $
46    * @since 7.2M2
47    */
48    @Component
49    @Singleton
 
50    public class SolrEntityReferenceResolver implements EntityReferenceResolver<SolrDocument>
51    {
52    /**
53    * Helper for unit tests.
54    */
55    public static final Type TYPE =
56    new DefaultParameterizedType(null, EntityReferenceResolver.class, SolrDocument.class);
57   
58    @Inject
59    @Named("explicit")
60    private EntityReferenceResolver<EntityReference> explicitReferenceEntityReferenceResolver;
61   
 
62  23 toggle @Override
63    public EntityReference resolve(SolrDocument solrDocument, EntityType type, Object... parameters)
64    {
65  23 EntityReference solrEntityReference = getEntityReference(solrDocument, type, parameters);
66  23 return this.explicitReferenceEntityReferenceResolver.resolve(solrEntityReference, type, parameters);
67    }
68   
 
69  23 toggle private EntityReference getEntityReference(SolrDocument solrDocument, EntityType expectedEntityType,
70    Object... parameters)
71    {
72  23 EntityReference wikiReference = getWikiReference(solrDocument, parameters);
73  23 EntityReference spaceReference = getSpaceReference(solrDocument, wikiReference, parameters);
74  23 EntityReference documentReference = getDocumentReferenceWithLocale(solrDocument, spaceReference, parameters);
75   
76  23 String indexedEntityType = getFieldStringValue(solrDocument, FieldUtils.TYPE);
77  23 EntityType actualEntityType =
78  23 StringUtils.isEmpty(indexedEntityType) ? expectedEntityType : EntityType.valueOf(indexedEntityType);
79   
80  23 switch (actualEntityType) {
81  1 case ATTACHMENT:
82  1 return getAttachmentReference(solrDocument, documentReference, parameters);
83  1 case OBJECT:
84  1 return getObjectReference(solrDocument, documentReference, parameters);
85  1 case OBJECT_PROPERTY:
86  1 EntityReference objectReference = getObjectReference(solrDocument, documentReference, parameters);
87  1 return getObjectPropertyReference(solrDocument, objectReference, parameters);
88  20 default:
89  20 return documentReference;
90    }
91    }
92   
 
93  23 toggle private EntityReference getWikiReference(SolrDocument solrDocument, Object... parameters)
94    {
95  23 String wikiName = getFieldStringValue(solrDocument, FieldUtils.WIKI);
96  23 if (!StringUtils.isEmpty(wikiName)) {
97  23 return new EntityReference(wikiName, EntityType.WIKI);
98    } else {
99  0 return resolveMissingReference(EntityType.WIKI, null, parameters);
100    }
101    }
102   
 
103  23 toggle private EntityReference getSpaceReference(SolrDocument solrDocument, EntityReference parent, Object... parameters)
104    {
105  23 Collection<Object> spaceNames = solrDocument.getFieldValues(FieldUtils.SPACES);
106  23 if (spaceNames != null && !spaceNames.isEmpty()) {
107  23 EntityReference spaceReference = parent;
108  23 for (Object spaceName : spaceNames) {
109  48 spaceReference = new EntityReference(String.valueOf(spaceName), EntityType.SPACE, spaceReference);
110    }
111  23 return spaceReference;
112    } else {
113  0 return resolveMissingReference(EntityType.SPACE, parent, parameters);
114    }
115    }
116   
 
117  23 toggle private EntityReference getDocumentReferenceWithLocale(SolrDocument solrDocument, EntityReference parent,
118    Object... parameters)
119    {
120  23 EntityReference documentReference = getDocumentReference(solrDocument, parent, parameters);
121  23 String localeString = getFieldStringValue(solrDocument, FieldUtils.DOCUMENT_LOCALE);
122  23 if (!StringUtils.isEmpty(localeString)) {
123  6 documentReference = new DocumentReference(documentReference, LocaleUtils.toLocale(localeString));
124    }
125  23 return documentReference;
126    }
127   
 
128  23 toggle private EntityReference getDocumentReference(SolrDocument solrDocument, EntityReference parent,
129    Object... parameters)
130    {
131  23 String documentName = getFieldStringValue(solrDocument, FieldUtils.NAME);
132  23 if (!StringUtils.isEmpty(documentName)) {
133  23 return new EntityReference(documentName, EntityType.DOCUMENT, parent);
134    } else {
135  0 return resolveMissingReference(EntityType.DOCUMENT, parent, parameters);
136    }
137    }
138   
 
139  1 toggle private EntityReference getAttachmentReference(SolrDocument solrDocument, EntityReference parent,
140    Object... parameters)
141    {
142  1 String fileName = getFieldFirstStringValue(solrDocument, FieldUtils.FILENAME);
143  1 if (!StringUtils.isEmpty(fileName)) {
144  1 return new EntityReference(fileName, EntityType.ATTACHMENT, parent);
145    } else {
146  0 return resolveMissingReference(EntityType.ATTACHMENT, parent, parameters);
147    }
148    }
149   
 
150  2 toggle private EntityReference getObjectReference(SolrDocument solrDocument, EntityReference parent, Object... parameters)
151    {
152  2 String classReference = getFieldFirstStringValue(solrDocument, FieldUtils.CLASS);
153  2 Number objectNumber = getFieldNumberValue(solrDocument, FieldUtils.NUMBER);
154  2 if (!StringUtils.isEmpty(classReference) && objectNumber != null) {
155  2 return new EntityReference(String.format("%s[%s]", classReference, objectNumber), EntityType.OBJECT,
156    parent);
157    } else {
158  0 return resolveMissingReference(EntityType.OBJECT, parent, parameters);
159    }
160    }
161   
 
162  1 toggle private EntityReference getObjectPropertyReference(SolrDocument solrDocument, EntityReference parent,
163    Object... parameters)
164    {
165  1 String propertyName = getFieldStringValue(solrDocument, FieldUtils.PROPERTY_NAME);
166  1 if (!StringUtils.isEmpty(propertyName)) {
167  1 return new EntityReference(propertyName, EntityType.OBJECT_PROPERTY, parent);
168    } else {
169  0 return resolveMissingReference(EntityType.OBJECT_PROPERTY, parent, parameters);
170    }
171    }
172   
 
173  0 toggle private EntityReference resolveMissingReference(EntityType entityType, EntityReference parent, Object... parameters)
174    {
175  0 EntityReference entityReference =
176    this.explicitReferenceEntityReferenceResolver.resolve(null, entityType, parameters);
177  0 return entityReference.replaceParent(entityReference.getParent(), parent);
178    }
179   
180    // Tools to workaround a change in Solr 5.3 which caused SolrDocument to return Field instead of value in some cases
181   
 
182  93 toggle private String getFieldStringValue(SolrDocument solrDocument, String fieldName)
183    {
184  93 Object field = solrDocument.get(fieldName);
185   
186  93 if (field instanceof Field) {
187  0 return ((Field) field).stringValue();
188  93 } else if (field instanceof IndexableField) {
189  0 return ((IndexableField) field).stringValue();
190    }
191   
192  93 return field != null ? field.toString() : null;
193    }
194   
 
195  3 toggle private String getFieldFirstStringValue(SolrDocument solrDocument, String fieldName)
196    {
197  3 Object field = solrDocument.getFirstValue(fieldName);
198   
199  3 if (field instanceof Field) {
200  0 return ((Field) field).stringValue();
201  3 } else if (field instanceof IndexableField) {
202  0 return ((IndexableField) field).stringValue();
203    }
204   
205  3 return field != null ? field.toString() : null;
206    }
207   
 
208  2 toggle private Number getFieldNumberValue(SolrDocument solrDocument, String fieldName)
209    {
210  2 Object field = solrDocument.get(fieldName);
211   
212  2 if (field instanceof Field) {
213  0 return ((Field) field).numericValue();
214  2 } else if (field instanceof IndexableField) {
215  0 return ((IndexableField) field).numericValue();
216    }
217   
218  2 return field != null ? (Number) field : null;
219    }
220    }