1
|
package eu.dnetlib.functionality.modular.ui.index;
|
2
|
|
3
|
import java.util.List;
|
4
|
import java.util.Set;
|
5
|
import javax.annotation.Resource;
|
6
|
import javax.xml.transform.TransformerFactory;
|
7
|
|
8
|
import com.google.common.base.Function;
|
9
|
import com.google.common.collect.Lists;
|
10
|
import com.google.common.collect.Sets;
|
11
|
import com.google.gson.Gson;
|
12
|
import com.google.gson.reflect.TypeToken;
|
13
|
import eu.dnetlib.clients.index.client.IndexClient;
|
14
|
import eu.dnetlib.clients.index.client.IndexClientException;
|
15
|
import eu.dnetlib.clients.index.client.ResolvingIndexClientFactory;
|
16
|
import eu.dnetlib.clients.index.client.response.BrowseEntry;
|
17
|
import eu.dnetlib.clients.index.client.response.LookupResponse;
|
18
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
19
|
import eu.dnetlib.functionality.modular.ui.AbstractAjaxController;
|
20
|
import eu.dnetlib.functionality.modular.ui.index.models.IndexInfo;
|
21
|
import eu.dnetlib.functionality.modular.ui.index.models.MdFormatInfo;
|
22
|
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
|
23
|
import eu.dnetlib.rmi.enabling.ISLookUpException;
|
24
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
25
|
import eu.dnetlib.rmi.provision.IndexServiceException;
|
26
|
import org.apache.commons.lang3.StringUtils;
|
27
|
import org.apache.commons.logging.Log;
|
28
|
import org.apache.commons.logging.LogFactory;
|
29
|
import org.springframework.beans.factory.annotation.Autowired;
|
30
|
import org.springframework.beans.factory.annotation.Value;
|
31
|
import org.springframework.core.io.ClassPathResource;
|
32
|
import org.springframework.stereotype.Controller;
|
33
|
import org.springframework.ui.ModelMap;
|
34
|
import org.springframework.web.bind.annotation.RequestMapping;
|
35
|
import org.springframework.web.bind.annotation.RequestMethod;
|
36
|
import org.springframework.web.bind.annotation.RequestParam;
|
37
|
import org.springframework.web.bind.annotation.ResponseBody;
|
38
|
|
39
|
/**
|
40
|
* The Class IndexServiceInternalController.
|
41
|
*/
|
42
|
@Controller
|
43
|
public class IndexServiceInternalController extends AbstractAjaxController {
|
44
|
|
45
|
/**
|
46
|
* The Constant log.
|
47
|
*/
|
48
|
private static final Log log = LogFactory.getLog(IndexServiceInternalController.class);
|
49
|
|
50
|
/**
|
51
|
* The lookup locator.
|
52
|
*/
|
53
|
@Resource
|
54
|
private UniqueServiceLocator serviceLocator;
|
55
|
|
56
|
/**
|
57
|
* The index client factory.
|
58
|
*/
|
59
|
@Autowired
|
60
|
private ResolvingIndexClientFactory indexClientFactory;
|
61
|
|
62
|
@Autowired
|
63
|
private IndexClientMap clientMap;
|
64
|
|
65
|
@Value("${dnet.modular.ui.index.gmf.xslt}")
|
66
|
private String gmf2documentXslt;
|
67
|
|
68
|
/**
|
69
|
* Index metadata formats.
|
70
|
*
|
71
|
* @param map the map
|
72
|
* @return the list< md format info>
|
73
|
* @throws Exception the exception
|
74
|
*/
|
75
|
@RequestMapping(value = "/ui/index/indexMetadataFormats.do")
|
76
|
@ResponseBody
|
77
|
public List<MdFormatInfo> indexMetadataFormats(final ModelMap map) throws Exception {
|
78
|
final String xquery =
|
79
|
"for $x in //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='MDFormatDSResourceType'] "
|
80
|
+ "let $format:= $x//CONFIGURATION/NAME/string() for $y in $x//LAYOUTS/LAYOUT let $layout:= $y/@name/string() "
|
81
|
+ "let $interpretation:= $x//CONFIGURATION/INTERPRETATION/text() let $id:=$x//RESOURCE_IDENTIFIER/@value/string() "
|
82
|
+ " return concat($format,'-',$layout,'-',$interpretation,'::', $id) ";
|
83
|
log.debug("Executing lookup query " + xquery);
|
84
|
|
85
|
return Lists.transform(quickSearchProfile(xquery), input -> MdFormatInfo.initFromXqueryResult(input));
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Index datastructures.
|
90
|
*
|
91
|
* @param backend the backend
|
92
|
* @return the list< index info>
|
93
|
* @throws Exception the exception
|
94
|
*/
|
95
|
@RequestMapping(value = "/ui/index/indexDatastructures.do")
|
96
|
@ResponseBody
|
97
|
public List<IndexInfo> indexDatastructures(@RequestParam(value = "backend", required = true) final String backend) throws Exception {
|
98
|
final String xquery =
|
99
|
"for $x in //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='IndexDSResourceType' and .//BACKEND/@ID='%s'] let $format := $x//METADATA_FORMAT "
|
100
|
+ "let $layout := $x//METADATA_FORMAT_LAYOUT let $interpretation :=$x//METADATA_FORMAT_INTERPRETATION "
|
101
|
+ "let $id :=$x//RESOURCE_IDENTIFIER/@value let $backendid := $x//BACKEND/@ID let $size := $x//INDEX_SIZE "
|
102
|
+ "return concat($format, ':-:',$layout,':-:',$interpretation,':-:',$id,':-:',$backendid,':-:',$size)";
|
103
|
log.debug("Executing lookup query " + String.format(xquery, backend));
|
104
|
|
105
|
Function<String, IndexInfo> function = input -> IndexInfo.newInstanceFromString(input);
|
106
|
return Lists.transform(quickSearchProfile(String.format(xquery, backend)), function);
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* Md format info.
|
111
|
*
|
112
|
* @param id the id
|
113
|
* @param layout the layout
|
114
|
* @return the list< string>
|
115
|
* @throws Exception the exception
|
116
|
*/
|
117
|
@RequestMapping(value = "/ui/index/mdFormatInfo.do")
|
118
|
@ResponseBody
|
119
|
public List<String> mdFormatInfo(@RequestParam(value = "id", required = true) final String id,
|
120
|
@RequestParam(value = "layout", required = true) final String layout) throws Exception {
|
121
|
|
122
|
String xqueryTemplate =
|
123
|
"//RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value='%s']" + "//LAYOUT[./@name/string()='%s'] "
|
124
|
+ "//FIELD[./@tokenizable/string()='false' or ./@type = 'int' or ./@type = 'date']/@name/string()";
|
125
|
log.debug("executing query: " + String.format(xqueryTemplate, id, layout));
|
126
|
return quickSearchProfile(String.format(xqueryTemplate, id, layout));
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Gets the backend available.
|
131
|
*
|
132
|
* @return the ids of the available backends
|
133
|
* @throws Exception the exception
|
134
|
*/
|
135
|
@RequestMapping(value = "/ui/index/backend.do")
|
136
|
@ResponseBody
|
137
|
public Set<String> getBackendAvailable() throws Exception {
|
138
|
|
139
|
String xquery = "for $x in //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='IndexDSResourceType'] return distinct-values($x//BACKEND/@ID/string())";
|
140
|
log.debug("executing query: " + xquery);
|
141
|
return Sets.newHashSet(quickSearchProfile(xquery));
|
142
|
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* Browse.
|
147
|
*
|
148
|
* @param map the map
|
149
|
* @param backend the backend
|
150
|
* @param format the format
|
151
|
* @param layout the layout
|
152
|
* @param interpretation the interpretation
|
153
|
* @return the list< string>
|
154
|
* @throws IndexClientException the index client exception
|
155
|
*/
|
156
|
@RequestMapping(value = "/ui/index/browse.do", method = RequestMethod.POST)
|
157
|
@ResponseBody
|
158
|
public List<BrowseEntry> browse(final ModelMap map,
|
159
|
@RequestParam(value = "backend", required = true) final String backend,
|
160
|
@RequestParam(value = "format", required = true) final String format,
|
161
|
@RequestParam(value = "layout", required = true) final String layout,
|
162
|
@RequestParam(value = "interpretation", required = true) final String interpretation,
|
163
|
@RequestParam(value = "fields", required = true) final String fields,
|
164
|
@RequestParam(value = "query", required = true) final String query) throws IndexClientException {
|
165
|
|
166
|
List<String> browseFields = new Gson().fromJson(fields, new TypeToken<List<String>>() {
|
167
|
}.getType());
|
168
|
|
169
|
if (browseFields != null) {
|
170
|
for (String s : browseFields) {
|
171
|
log.debug("Browse field " + s);
|
172
|
}
|
173
|
}
|
174
|
|
175
|
String indexClientKeys = backend + "-" + format + "-" + layout + "-" + interpretation;
|
176
|
|
177
|
IndexClient client = null;
|
178
|
if (clientMap.getMap().containsKey(indexClientKeys)) {
|
179
|
client = clientMap.getMap().get(indexClientKeys);
|
180
|
} else {
|
181
|
client = indexClientFactory.getClient(format, layout, interpretation, backend);
|
182
|
clientMap.getMap().put(indexClientKeys, client);
|
183
|
}
|
184
|
|
185
|
// LookupResponse result = client.lookup("*=*", null, 0, 10);
|
186
|
|
187
|
List<BrowseEntry> result = client.browse(query, browseFields, 99);
|
188
|
return result;
|
189
|
}
|
190
|
|
191
|
@RequestMapping(value = "/ui/index/delete.do", method = RequestMethod.POST)
|
192
|
@ResponseBody
|
193
|
public long delete(final ModelMap map,
|
194
|
@RequestParam(value = "backend", required = true) final String backend,
|
195
|
@RequestParam(value = "format", required = true) final String format,
|
196
|
@RequestParam(value = "layout", required = true) final String layout,
|
197
|
@RequestParam(value = "interpretation", required = true) final String interpretation,
|
198
|
@RequestParam(value = "query", required = true) final String query,
|
199
|
@RequestParam(value = "indexidentifier", required = false) final String indexId) throws IndexServiceException {
|
200
|
|
201
|
String indexClientKeys = backend + "-" + format + "-" + layout + "-" + interpretation;
|
202
|
|
203
|
IndexClient client = null;
|
204
|
if (clientMap.getMap().containsKey(indexClientKeys)) {
|
205
|
client = clientMap.getMap().get(indexClientKeys);
|
206
|
} else {
|
207
|
client = indexClientFactory.getClient(format, layout, interpretation, backend);
|
208
|
clientMap.getMap().put(indexClientKeys, client);
|
209
|
}
|
210
|
String mquery = query;
|
211
|
|
212
|
if (!StringUtils.isEmpty(indexId)) {
|
213
|
mquery = query + " and __dsid exact \"" + indexId + "\"";
|
214
|
}
|
215
|
|
216
|
return client.delete(mquery);
|
217
|
}
|
218
|
|
219
|
@RequestMapping(value = "/ui/index/search.do", method = RequestMethod.POST)
|
220
|
@ResponseBody
|
221
|
public LookupResponse search(final ModelMap map,
|
222
|
@RequestParam(value = "backend", required = true) final String backend,
|
223
|
@RequestParam(value = "format", required = true) final String format,
|
224
|
@RequestParam(value = "layout", required = true) final String layout,
|
225
|
@RequestParam(value = "interpretation", required = true) final String interpretation,
|
226
|
@RequestParam(value = "query", required = true) final String query,
|
227
|
@RequestParam(value = "from", required = true) final int from,
|
228
|
@RequestParam(value = "number", required = true) final int number,
|
229
|
@RequestParam(value = "indexidentifier", required = false) final String indexId
|
230
|
|
231
|
) throws IndexClientException {
|
232
|
|
233
|
String indexClientKeys = backend + "-" + format + "-" + layout + "-" + interpretation;
|
234
|
|
235
|
log.debug(indexClientKeys);
|
236
|
|
237
|
IndexClient client = null;
|
238
|
|
239
|
if (!clientMap.getMap().containsKey(indexClientKeys)) {
|
240
|
clientMap.getMap().put(indexClientKeys, indexClientFactory.getClient(format, layout, interpretation, backend));
|
241
|
}
|
242
|
client = clientMap.getMap().get(indexClientKeys);
|
243
|
|
244
|
List<String> filterId = null;
|
245
|
if (indexId != null && !indexId.isEmpty()) {
|
246
|
filterId = Lists.newArrayList("__dsid:\"" + indexId + "\"");
|
247
|
}
|
248
|
|
249
|
log.debug(String.format("query: '%s', filter: '%s', from: '%d', number: '%d'", query, filterId, from, number));
|
250
|
|
251
|
LookupResponse result = client.lookup(query, filterId, from, number);
|
252
|
|
253
|
ClassPathResource cpr = new ClassPathResource(gmf2documentXslt);
|
254
|
ApplyXslt xslt = new ApplyXslt(cpr);
|
255
|
List<String> convertedList = Lists.newArrayList();
|
256
|
|
257
|
for (String s : result.getRecords()) {
|
258
|
log.debug("response record: \n" + s);
|
259
|
convertedList.add(xslt.apply(s));
|
260
|
}
|
261
|
result.setRecords(convertedList);
|
262
|
return result;
|
263
|
}
|
264
|
|
265
|
/**
|
266
|
* Quick search profile.
|
267
|
*
|
268
|
* @param xquery the xquery
|
269
|
* @return the list< string>
|
270
|
* @throws Exception the exception
|
271
|
*/
|
272
|
private List<String> quickSearchProfile(final String xquery) throws Exception {
|
273
|
try {
|
274
|
return serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery);
|
275
|
} catch (ISLookUpException e) {
|
276
|
throw new Exception(e);
|
277
|
}
|
278
|
}
|
279
|
}
|