1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rest.internal.resources.search; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Calendar; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.List; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Provider; |
30 |
|
import javax.ws.rs.core.UriInfo; |
31 |
|
|
32 |
|
import org.apache.commons.lang3.StringUtils; |
33 |
|
import org.xwiki.model.reference.DocumentReference; |
34 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
35 |
|
import org.xwiki.model.reference.WikiReference; |
36 |
|
import org.xwiki.query.Query; |
37 |
|
import org.xwiki.query.QueryFilter; |
38 |
|
import org.xwiki.query.QueryManager; |
39 |
|
import org.xwiki.query.internal.HiddenDocumentFilter; |
40 |
|
import org.xwiki.rest.Relations; |
41 |
|
import org.xwiki.rest.internal.ModelFactory; |
42 |
|
import org.xwiki.rest.internal.Utils; |
43 |
|
import org.xwiki.rest.model.jaxb.Link; |
44 |
|
import org.xwiki.rest.model.jaxb.SearchResult; |
45 |
|
import org.xwiki.rest.resources.pages.PageResource; |
46 |
|
import org.xwiki.rest.resources.pages.PageTranslationResource; |
47 |
|
import org.xwiki.security.authorization.ContextualAuthorizationManager; |
48 |
|
import org.xwiki.security.authorization.Right; |
49 |
|
|
50 |
|
import com.xpn.xwiki.XWikiContext; |
51 |
|
import com.xpn.xwiki.api.Document; |
52 |
|
import com.xpn.xwiki.api.XWiki; |
53 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
54 |
|
import com.xpn.xwiki.objects.BaseObject; |
55 |
|
|
56 |
|
|
57 |
|
@version |
58 |
|
@since |
59 |
|
|
|
|
| 86.2% |
Uncovered Elements: 9 (65) |
Complexity: 10 |
Complexity Density: 0.2 |
|
60 |
|
public abstract class AbstractDatabaseSearchSource extends AbstractSearchSource |
61 |
|
{ |
62 |
|
@Inject |
63 |
|
protected Provider<XWikiContext> xcontextProvider; |
64 |
|
|
65 |
|
@Inject |
66 |
|
protected QueryManager queryManager; |
67 |
|
|
68 |
|
@Inject |
69 |
|
@Named(HiddenDocumentFilter.HINT) |
70 |
|
protected QueryFilter hiddenFilter; |
71 |
|
|
72 |
|
@Inject |
73 |
|
protected ModelFactory modelFactory; |
74 |
|
|
75 |
|
@Inject |
76 |
|
protected DocumentReferenceResolver<String> resolver; |
77 |
|
|
78 |
|
@Inject |
79 |
|
protected ContextualAuthorizationManager authorization; |
80 |
|
|
81 |
|
protected final String queryLanguage; |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
1 |
public AbstractDatabaseSearchSource(String queryLanguage)... |
84 |
|
{ |
85 |
1 |
this.queryLanguage = queryLanguage; |
86 |
|
} |
87 |
|
|
88 |
|
protected abstract String resolveQuery(boolean distinct, String query); |
89 |
|
|
|
|
| 85.5% |
Uncovered Elements: 9 (62) |
Complexity: 9 |
Complexity Density: 0.18 |
|
90 |
3 |
@Override... |
91 |
|
public List<SearchResult> search(String partialQueryString, String wikiName, String wikis, |
92 |
|
boolean hasProgrammingRights, String orderField, String order, boolean distinct, int number, int start, |
93 |
|
Boolean withPrettyNames, String className, UriInfo uriInfo) throws Exception |
94 |
|
{ |
95 |
3 |
XWikiContext xwikiContext = this.xcontextProvider.get(); |
96 |
3 |
XWiki xwikiApi = new XWiki(xwikiContext.getWiki(), xwikiContext); |
97 |
|
|
98 |
3 |
if (partialQueryString == null || StringUtils.startsWithIgnoreCase(partialQueryString, "select")) { |
99 |
0 |
return Collections.emptyList(); |
100 |
|
} |
101 |
|
|
102 |
3 |
String queryString = resolveQuery(distinct, partialQueryString); |
103 |
|
|
104 |
3 |
Query query = this.queryManager.createQuery(queryString, this.queryLanguage); |
105 |
|
|
106 |
3 |
query.setLimit(number).setOffset(start); |
107 |
3 |
query.setWiki(wikiName); |
108 |
|
|
109 |
3 |
List<Object> queryResult = query.execute(); |
110 |
|
|
111 |
3 |
WikiReference wikiReference = new WikiReference(wikiName); |
112 |
|
|
113 |
|
|
114 |
3 |
List<SearchResult> result = new ArrayList<>(); |
115 |
3 |
for (Object object : queryResult) { |
116 |
3 |
Object[] fields = (Object[]) object; |
117 |
|
|
118 |
3 |
String fullName = (String) fields[0]; |
119 |
3 |
String language = (String) fields[3]; |
120 |
|
|
121 |
3 |
DocumentReference documentReference = this.resolver.resolve(fullName, wikiReference); |
122 |
|
|
123 |
|
|
124 |
3 |
if (this.authorization.hasAccess(Right.VIEW, documentReference)) { |
125 |
3 |
Document doc = xwikiApi.getDocument(documentReference); |
126 |
3 |
String title = doc.getDisplayTitle(); |
127 |
|
|
128 |
3 |
SearchResult searchResult = this.objectFactory.createSearchResult(); |
129 |
3 |
searchResult.setType("page"); |
130 |
3 |
searchResult.setId(doc.getPrefixedFullName()); |
131 |
3 |
searchResult.setPageFullName(doc.getFullName()); |
132 |
3 |
searchResult.setTitle(title); |
133 |
3 |
searchResult.setWiki(wikiName); |
134 |
3 |
searchResult.setSpace(doc.getSpace()); |
135 |
3 |
searchResult.setPageName(doc.getName()); |
136 |
3 |
searchResult.setVersion(doc.getVersion()); |
137 |
3 |
searchResult.setAuthor(doc.getAuthor()); |
138 |
3 |
Calendar calendar = Calendar.getInstance(); |
139 |
3 |
calendar.setTime(doc.getDate()); |
140 |
3 |
searchResult.setModified(calendar); |
141 |
|
|
142 |
3 |
if (withPrettyNames) { |
143 |
0 |
searchResult.setAuthorName(xwikiApi.getUserName(doc.getAuthor(), false)); |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
3 |
if (StringUtils.isNotEmpty(className) && xwikiContext.getUserReference() != null) { |
151 |
1 |
XWikiDocument xdocument = |
152 |
|
xwikiContext.getWiki().getDocument(doc.getDocumentReference(), xwikiContext); |
153 |
1 |
BaseObject baseObject = xdocument.getObject(className); |
154 |
1 |
if (baseObject != null) { |
155 |
1 |
searchResult.setObject( |
156 |
|
this.modelFactory.toRestObject(uriInfo.getBaseUri(), doc, baseObject, false, false)); |
157 |
|
} |
158 |
|
} |
159 |
|
|
160 |
3 |
String pageUri; |
161 |
3 |
if (StringUtils.isBlank(language)) { |
162 |
3 |
pageUri = Utils.createURI(uriInfo.getBaseUri(), PageResource.class, wikiName, |
163 |
|
Utils.getSpacesHierarchy(documentReference.getLastSpaceReference()), |
164 |
|
documentReference.getName()).toString(); |
165 |
|
} else { |
166 |
0 |
searchResult.setLanguage(language); |
167 |
0 |
pageUri = Utils.createURI(uriInfo.getBaseUri(), PageTranslationResource.class, wikiName, |
168 |
|
Utils.getSpacesHierarchy(documentReference.getLastSpaceReference()), |
169 |
|
documentReference.getName(), language).toString(); |
170 |
|
} |
171 |
|
|
172 |
3 |
Link pageLink = new Link(); |
173 |
3 |
pageLink.setHref(pageUri); |
174 |
3 |
pageLink.setRel(Relations.PAGE); |
175 |
3 |
searchResult.getLinks().add(pageLink); |
176 |
|
|
177 |
3 |
result.add(searchResult); |
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
3 |
return result; |
182 |
|
} |
183 |
|
} |