1
|
package eu.dnetlib.functionality.modular.ui.mdstore;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.Iterator;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
import java.util.stream.Collectors;
|
8
|
import javax.annotation.Resource;
|
9
|
import javax.xml.ws.wsaddressing.W3CEndpointReference;
|
10
|
|
11
|
import com.google.common.base.Function;
|
12
|
import com.google.common.collect.Lists;
|
13
|
import com.google.common.collect.Maps;
|
14
|
import eu.dnetlib.data.mdstore.DocumentNotFoundException;
|
15
|
import eu.dnetlib.data.mdstore.MDStoreService;
|
16
|
import eu.dnetlib.data.mdstore.modular.MDFormatDescription;
|
17
|
import eu.dnetlib.data.mdstore.modular.MDStoreUtils;
|
18
|
import eu.dnetlib.data.mdstore.modular.ModularMDStoreService;
|
19
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
20
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
21
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
22
|
import eu.dnetlib.enabling.resultset.client.ResultSetClientFactory;
|
23
|
import eu.dnetlib.functionality.modular.ui.mdstore.model.MDStoreInfo;
|
24
|
import eu.dnetlib.functionality.modular.ui.mdstore.model.MDStoreResult;
|
25
|
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
|
26
|
import org.apache.commons.logging.Log;
|
27
|
import org.apache.commons.logging.LogFactory;
|
28
|
import org.springframework.beans.factory.annotation.Autowired;
|
29
|
import org.springframework.beans.factory.annotation.Value;
|
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.*;
|
34
|
|
35
|
/**
|
36
|
* The Class MDStoreServiceInternalController.
|
37
|
*/
|
38
|
@Controller
|
39
|
public class MDStoreServiceInternalController {
|
40
|
|
41
|
/**
|
42
|
* The Constant log.
|
43
|
*/
|
44
|
private static final Log log = LogFactory.getLog(MDStoreServiceInternalController.class);
|
45
|
|
46
|
@Value("${dnet.modular.ui.mdstore.xslt.record2document}")
|
47
|
private String recordXsltClasspath;
|
48
|
|
49
|
/**
|
50
|
* The lookup locator.
|
51
|
*/
|
52
|
@Resource
|
53
|
private UniqueServiceLocator serviceLocator;
|
54
|
|
55
|
@Autowired
|
56
|
private MDStoreUtils mdStoreUtils;
|
57
|
|
58
|
@Resource
|
59
|
private ResultSetClientFactory resultSetClientFactory;
|
60
|
|
61
|
@Autowired
|
62
|
private ModularMDStoreService mdStoreService;
|
63
|
|
64
|
|
65
|
private Map<String, Info> datasourceCache;
|
66
|
|
67
|
/**
|
68
|
* List md stores.
|
69
|
*
|
70
|
* @param map the map
|
71
|
* @return the list
|
72
|
* @throws Exception the exception
|
73
|
*/
|
74
|
@RequestMapping(value = "/ui/mdstore/listMDStores.do")
|
75
|
@ResponseBody
|
76
|
|
77
|
public List<MDStoreInfo> listMDStores(final ModelMap map) throws Exception {
|
78
|
if (datasourceCache == null) {
|
79
|
datasourceCache = datasourceFromMdStore();
|
80
|
}
|
81
|
|
82
|
|
83
|
final String xquery = "for $x in collection('/db/DRIVER/MDStoreDSResources/MDStoreDSResourceType') let $format := $x//METADATA_FORMAT "
|
84
|
+ "let $layout := $x//METADATA_FORMAT_LAYOUT let $interpretation :=$x//METADATA_FORMAT_INTERPRETATION "
|
85
|
+ "let $uri := $x//RESOURCE_URI/@value/string() let $id := $x//RESOURCE_IDENTIFIER/@value/string() "
|
86
|
+ "let $lastDate := $x//LAST_STORAGE_DATE let $size :=$x//NUMBER_OF_RECORDS "
|
87
|
+ "return concat($uri,'@::@',$id,'@::@',$format,'(-)',$layout, '(-)',$interpretation,'@::@',$lastDate,'@::@',$size) ";
|
88
|
|
89
|
log.debug("Executing lookup query" + xquery);
|
90
|
return Lists.transform(serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery), input -> {
|
91
|
MDStoreInfo mdStoreInfo = MDStoreInfo.fromXqueryResult(input);
|
92
|
Info currentInfo = datasourceCache.get(mdStoreInfo.getId());
|
93
|
if (currentInfo != null) {
|
94
|
Map<String, String> datasourcesInvolved = mdStoreInfo.getDatasourcesInvolved();
|
95
|
if (datasourcesInvolved == null) {
|
96
|
datasourcesInvolved = Maps.newHashMap();
|
97
|
mdStoreInfo.setDatasourcesInvolved(datasourcesInvolved);
|
98
|
}
|
99
|
datasourcesInvolved.put("datasourceId", currentInfo.getDatasourceId());
|
100
|
datasourcesInvolved.put("datasourceName", currentInfo.getDatasourceName());
|
101
|
|
102
|
}
|
103
|
return mdStoreInfo;
|
104
|
});
|
105
|
}
|
106
|
|
107
|
@RequestMapping(value = "/ui/mdstore/reloadInfo")
|
108
|
@ResponseBody
|
109
|
public List<MDStoreInfo> reloadInfo(final ModelMap map) throws Exception {
|
110
|
datasourceCache = null;
|
111
|
return listMDStores(map);
|
112
|
}
|
113
|
|
114
|
@RequestMapping(value = "/ui/mdstore/getMdStoreInfo.do")
|
115
|
@ResponseBody
|
116
|
public MDStoreInfo getMdStoreInfo(final ModelMap map, @RequestParam(value = "id", required = true) final String profileId) throws Exception {
|
117
|
|
118
|
final MDStoreInfo mdStoreInfo = retrieveMDStoreInfo(profileId);
|
119
|
List<MDFormatDescription> indexFields = mdStoreUtils.getField(mdStoreInfo.getFormat(), mdStoreInfo.getLayout());
|
120
|
List<String> idFields;
|
121
|
if (indexFields != null) {
|
122
|
idFields = indexFields.stream().map(MDFormatDescription::getName).collect(Collectors.toList());
|
123
|
mdStoreInfo.setIndexFields(idFields);
|
124
|
}
|
125
|
return mdStoreInfo;
|
126
|
}
|
127
|
|
128
|
|
129
|
@RequestMapping(value = "/ui/mdstore/findRecords", method = RequestMethod.POST)
|
130
|
@ResponseBody
|
131
|
public MDStoreResult findRecords(final ModelMap map,
|
132
|
@RequestBody final Map<String, String> queryParam) throws Exception {
|
133
|
|
134
|
if (!queryParam.containsKey("mdId")) throw new MDStoreUIException("mdId parameters expected");
|
135
|
|
136
|
final String mdId = queryParam.get("mdId");
|
137
|
|
138
|
int offset = 0;
|
139
|
|
140
|
if (queryParam.containsKey("page")) {
|
141
|
try {
|
142
|
offset = Integer.parseInt(queryParam.get("page"));
|
143
|
queryParam.remove("page");
|
144
|
} catch (Exception e) {
|
145
|
log.error("Error on parsing page " + queryParam.get("page"));
|
146
|
}
|
147
|
}
|
148
|
queryParam.remove("mdId");
|
149
|
|
150
|
final ClassPathResource cpr = new ClassPathResource(recordXsltClasspath);
|
151
|
final ApplyXslt xslt = new ApplyXslt(cpr);
|
152
|
|
153
|
final MDStoreResult res = new MDStoreResult();
|
154
|
res.setResult(Lists.transform(mdStoreService.getMDStoreRecords(mdId, 10, offset, queryParam), new Function<String, String>() {
|
155
|
@Override
|
156
|
public String apply(String it) {
|
157
|
return xslt.evaluate(it);
|
158
|
}
|
159
|
}));
|
160
|
|
161
|
|
162
|
//TERRIBLE HACK SHAME SANDRO SHAME!
|
163
|
int count = 0;
|
164
|
try {
|
165
|
count = Integer.parseInt(queryParam.get("count"));
|
166
|
} catch (Exception e) {
|
167
|
log.error(e);
|
168
|
}
|
169
|
|
170
|
res.setCount(count);
|
171
|
|
172
|
return res;
|
173
|
}
|
174
|
|
175
|
@RequestMapping(value = "/ui/mdstore/mdStoreResults")
|
176
|
@ResponseBody
|
177
|
public MDStoreResult getMDStoreResults(final ModelMap map,
|
178
|
@RequestParam(value = "mdId", required = true) final String mdId,
|
179
|
@RequestParam(value = "from", required = true) final int from,
|
180
|
@RequestParam(value = "id_query", required = false) final String idToQuery,
|
181
|
@RequestParam(value = "free_query", required = false) final String freeQuery) throws Exception {
|
182
|
|
183
|
MDStoreService mdService = serviceLocator.getService(MDStoreService.class, mdId);
|
184
|
ClassPathResource cpr = new ClassPathResource(recordXsltClasspath);
|
185
|
ApplyXslt xslt = new ApplyXslt(cpr);
|
186
|
MDStoreResult resultValues = new MDStoreResult();
|
187
|
resultValues.setCount(getMdStoreInfo(map, mdId).getSize());
|
188
|
|
189
|
if (idToQuery != null && !idToQuery.isEmpty()) {
|
190
|
try {
|
191
|
String record = mdService.deliverRecord(mdId, idToQuery);
|
192
|
resultValues.setResult(Lists.newArrayList(xslt.evaluate(record)));
|
193
|
return resultValues;
|
194
|
} catch (DocumentNotFoundException e) {
|
195
|
return null;
|
196
|
}
|
197
|
}
|
198
|
|
199
|
|
200
|
W3CEndpointReference epr = mdService.deliverMDRecords(mdId, null, null, null);
|
201
|
Iterable<String> result = resultSetClientFactory.getClient(epr);
|
202
|
List<String> convertedList = new ArrayList<String>();
|
203
|
Iterator<String> it = result.iterator();
|
204
|
int currentCounter = 0;
|
205
|
while (currentCounter < from) {
|
206
|
if (it.hasNext()) {
|
207
|
it.next();
|
208
|
currentCounter++;
|
209
|
} else {
|
210
|
resultValues.setResult(convertedList);
|
211
|
return resultValues;
|
212
|
}
|
213
|
}
|
214
|
|
215
|
for (int i = 0; i < 10; i++) {
|
216
|
if (it.hasNext()) {
|
217
|
// convertedList.add(xslt.evaluate(it.next()));
|
218
|
convertedList.add(it.next());
|
219
|
} else {
|
220
|
break;
|
221
|
}
|
222
|
}
|
223
|
resultValues.setResult(convertedList);
|
224
|
return resultValues;
|
225
|
}
|
226
|
|
227
|
|
228
|
private Map<String, Info> datasourceFromMdStore() {
|
229
|
final String query = "for $x in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType') where $x[.//PARAM/@category/string()='MDSTORE_ID'] " +
|
230
|
"return concat($x//PARAM[./@name='providerId'][1]/string(), '<::>',$x//PARAM[./@name='providerName'][1]/string(), '<::>', " +
|
231
|
"string-join($x//PARAM[./@category/string()='MDSTORE_ID']/string(), '--'))";
|
232
|
final ISLookUpService lookupService = serviceLocator.getService(ISLookUpService.class);
|
233
|
try {
|
234
|
Map<String, Info> result = Maps.newHashMap();
|
235
|
List<String> results = lookupService.quickSearchProfile(query);
|
236
|
for (String item : results) {
|
237
|
String[] splitted = item.split("<::>");
|
238
|
if (splitted != null && splitted.length == 3) {
|
239
|
final Info currentInfo = new Info();
|
240
|
currentInfo.setDatasourceId(splitted[0]);
|
241
|
currentInfo.setDatasourceName(splitted[1]);
|
242
|
final String[] mdstores = splitted[2].split("--");
|
243
|
if (mdstores != null) {
|
244
|
for (String mdstore : mdstores) {
|
245
|
result.put(mdstore, currentInfo);
|
246
|
}
|
247
|
}
|
248
|
}
|
249
|
}
|
250
|
return result;
|
251
|
} catch (ISLookUpException e) {
|
252
|
log.error(e);
|
253
|
return null;
|
254
|
}
|
255
|
}
|
256
|
|
257
|
private MDStoreInfo retrieveMDStoreInfo(final String identifier) {
|
258
|
|
259
|
MDStoreInfo result = null;
|
260
|
try {
|
261
|
result = null;
|
262
|
final String xquery = " for $x in collection('/db/DRIVER/MDStoreDSResources/MDStoreDSResourceType') "
|
263
|
+ " where $x//RESOURCE_IDENTIFIER/@value/string()=\"%s\" return concat($x//RESOURCE_URI/@value/string() "
|
264
|
+ ", '@::@',$x//RESOURCE_IDENTIFIER/@value/string() ,'@::@',$x//METADATA_FORMAT,'(-)',$x//METADATA_FORMAT_LAYOUT, "
|
265
|
+ " '(-)', $x//METADATA_FORMAT_INTERPRETATION ,'@::@',$x//LAST_STORAGE_DATE,'@::@',$x//NUMBER_OF_RECORDS)";
|
266
|
|
267
|
List<String> results = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(String.format(xquery, identifier));
|
268
|
if (results != null && !results.isEmpty()) {
|
269
|
result = MDStoreInfo.fromXqueryResult(results.get(0));
|
270
|
}
|
271
|
} catch (ISLookUpException e) {
|
272
|
log.error(String.format("Error on getting information for mdstore with identifier: %s", identifier), e);
|
273
|
}
|
274
|
|
275
|
return result;
|
276
|
}
|
277
|
|
278
|
|
279
|
private Map<String, String> findDatasourceNameAssociatedToMdStore(final String mdStoreId) throws ISLookUpException {
|
280
|
final String baseQuery = "for $x in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType') where $x[.//PARAM/string()='%s'] return concat($x//PARAM[./@name='providerId'][1]/string(), '<::>',$x//PARAM[./@name='providerName'][1]/string())";
|
281
|
final ISLookUpService lookupService = serviceLocator.getService(ISLookUpService.class);
|
282
|
List<String> result = lookupService.quickSearchProfile(String.format(baseQuery, mdStoreId));
|
283
|
Map<String, String> datasources = Maps.newHashMap();
|
284
|
for (String item : result) {
|
285
|
String[] splitted = item.split("<::>");
|
286
|
if (splitted != null && splitted.length == 2) {
|
287
|
datasources.put(splitted[0], splitted[1]);
|
288
|
}
|
289
|
}
|
290
|
return datasources;
|
291
|
}
|
292
|
|
293
|
|
294
|
class Info {
|
295
|
private String datasourceId;
|
296
|
private String datasourceName;
|
297
|
private String mdStoreId;
|
298
|
|
299
|
|
300
|
public String getDatasourceName() {
|
301
|
return datasourceName;
|
302
|
}
|
303
|
|
304
|
public void setDatasourceName(String datasourceName) {
|
305
|
this.datasourceName = datasourceName;
|
306
|
}
|
307
|
|
308
|
public String getDatasourceId() {
|
309
|
return datasourceId;
|
310
|
}
|
311
|
|
312
|
public void setDatasourceId(String datasourceId) {
|
313
|
this.datasourceId = datasourceId;
|
314
|
}
|
315
|
|
316
|
public String getMdStoreId() {
|
317
|
return mdStoreId;
|
318
|
}
|
319
|
|
320
|
public void setMdStoreId(String mdStoreId) {
|
321
|
this.mdStoreId = mdStoreId;
|
322
|
}
|
323
|
}
|
324
|
}
|