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

File ObjectSolrReferenceResolver.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

2
29
2
1
142
82
5
0.17
14.5
2
2.5

Classes

Class Line # Actions
ObjectSolrReferenceResolver 55 29 0% 5 3
0.9090909490.9%
 

Contributing tests

This file is covered by 6 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   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Provider;
28    import javax.inject.Singleton;
29   
30    import org.apache.solr.client.solrj.util.ClientUtils;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.model.EntityType;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.EntityReference;
35    import org.xwiki.model.reference.EntityReferenceSerializer;
36    import org.xwiki.model.reference.ObjectPropertyReference;
37    import org.xwiki.search.solr.internal.api.FieldUtils;
38    import org.xwiki.search.solr.internal.api.SolrIndexerException;
39   
40    import com.google.common.collect.Iterables;
41    import com.xpn.xwiki.doc.XWikiDocument;
42    import com.xpn.xwiki.objects.BaseObject;
43    import com.xpn.xwiki.objects.BaseObjectReference;
44    import com.xpn.xwiki.objects.BaseProperty;
45   
46    /**
47    * Resolve object references.
48    *
49    * @version $Id: a8ba6b2911f86f3b95a9164d70d4a0b871c8b16f $
50    * @since 5.1M2
51    */
52    @Component
53    @Named("object")
54    @Singleton
 
55    public class ObjectSolrReferenceResolver extends AbstractSolrReferenceResolver
56    {
57    /**
58    * Used to resolve object property references.
59    */
60    @Inject
61    @Named("object_property")
62    private Provider<SolrReferenceResolver> objectPropertyResolverProvider;
63   
64    /**
65    * Used to resolve document references.
66    */
67    @Inject
68    @Named("document")
69    private Provider<SolrReferenceResolver> documentResolverProvider;
70   
71    /**
72    * Reference to String serializer. Used for fields such as class and fullname that are relative to their wiki and
73    * are stored without the wiki name.
74    */
75    @Inject
76    @Named("local")
77    private EntityReferenceSerializer<String> localSerializer;
78   
 
79  512 toggle @Override
80    public List<EntityReference> getReferences(EntityReference objectReference) throws SolrIndexerException
81    {
82  512 List<EntityReference> result = new ArrayList<>();
83   
84    // Object itself
85  512 result.add(objectReference);
86   
87    // Object properties
88  512 DocumentReference documentReference = new DocumentReference(objectReference.getParent());
89   
90  512 XWikiDocument document;
91  512 try {
92  512 document = getDocument(documentReference);
93    } catch (Exception e) {
94  0 throw new SolrIndexerException("Failed to get document for object [" + objectReference + "]", e);
95    }
96   
97  512 BaseObject object = document.getXObject(objectReference);
98  512 if (object != null) {
99  512 for (Object field : object.getFieldList()) {
100  1898 BaseProperty<ObjectPropertyReference> objectProperty = (BaseProperty<ObjectPropertyReference>) field;
101   
102  1898 ObjectPropertyReference objectPropertyReference = objectProperty.getReference();
103   
104  1898 try {
105  1898 Iterables.addAll(result,
106    this.objectPropertyResolverProvider.get().getReferences(objectPropertyReference));
107    } catch (Exception e) {
108  0 this.logger.error("Failed to resolve references for object property [" + objectPropertyReference
109    + "]", e);
110    }
111    }
112    }
113   
114  512 return result;
115    }
116   
 
117  1 toggle @Override
118    public String getQuery(EntityReference reference) throws SolrIndexerException
119    {
120  1 BaseObjectReference objectReference = new BaseObjectReference(reference);
121   
122  1 StringBuilder builder = new StringBuilder();
123   
124  1 EntityReference documentReference = reference.extractReference(EntityType.DOCUMENT);
125  1 builder.append(this.documentResolverProvider.get().getQuery(documentReference));
126   
127  1 builder.append(QUERY_AND);
128   
129  1 builder.append(FieldUtils.CLASS);
130  1 builder.append(':');
131  1 builder
132    .append(ClientUtils.escapeQueryChars(this.localSerializer.serialize(objectReference.getXClassReference())));
133   
134  1 builder.append(QUERY_AND);
135   
136  1 builder.append(FieldUtils.NUMBER);
137  1 builder.append(':');
138  1 builder.append(ClientUtils.escapeQueryChars(String.valueOf(objectReference.getObjectNumber())));
139   
140  1 return builder.toString();
141    }
142    }