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