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