1
|
package eu.dnetlib.enabling.inspector;
|
2
|
|
3
|
import java.util.Iterator;
|
4
|
import java.util.List;
|
5
|
import java.util.Spliterator;
|
6
|
import java.util.Spliterators;
|
7
|
import java.util.stream.Collectors;
|
8
|
import java.util.stream.StreamSupport;
|
9
|
import javax.annotation.Resource;
|
10
|
|
11
|
import eu.dnetlib.xml.database.XMLDatabase;
|
12
|
import org.apache.commons.lang3.StringEscapeUtils;
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.springframework.stereotype.Controller;
|
16
|
import org.springframework.ui.Model;
|
17
|
import org.springframework.web.bind.annotation.RequestMapping;
|
18
|
import org.springframework.web.bind.annotation.RequestParam;
|
19
|
import org.xmldb.api.base.XMLDBException;
|
20
|
|
21
|
/**
|
22
|
* This controller offers a simple way to run arbitrary queries on the xmldb.
|
23
|
*
|
24
|
* @author marko
|
25
|
*
|
26
|
*/
|
27
|
@Controller
|
28
|
public class QueryController extends AbstractInspectorController {
|
29
|
|
30
|
/**
|
31
|
* logger.
|
32
|
*/
|
33
|
private static final Log log = LogFactory.getLog(QueryController.class); // NOPMD by marko on 11/24/08 5:02 PM
|
34
|
|
35
|
/**
|
36
|
* xmldb.
|
37
|
*/
|
38
|
@Resource(name = "existDatabase")
|
39
|
private transient XMLDatabase xmlDatabase;
|
40
|
|
41
|
/**
|
42
|
* utility to parse resource ids and allows to navigate them.
|
43
|
*/
|
44
|
@Resource(name = "resourcelinkTool")
|
45
|
private ResourceLinkTool linkTool;
|
46
|
|
47
|
/**
|
48
|
* run a query.
|
49
|
*
|
50
|
* @param model
|
51
|
* mvc model
|
52
|
* @param query
|
53
|
* query (optional)
|
54
|
* @throws
|
55
|
*/
|
56
|
@RequestMapping(value = "/inspector/query.do")
|
57
|
void query(final Model model, @RequestParam(value = "query", required = false) final String query) {
|
58
|
if (query != null) {
|
59
|
log.info("running query: " + query);
|
60
|
|
61
|
try {
|
62
|
final Iterator<String> it = xmlDatabase.xquery(query);
|
63
|
final List<String> res = StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.IMMUTABLE), false)
|
64
|
.map(StringEscapeUtils::escapeHtml4)
|
65
|
.map(linkTool::linkfyToHtml)
|
66
|
.collect(Collectors.toList());
|
67
|
|
68
|
model.addAttribute("size", res.size());
|
69
|
model.addAttribute("results", res);
|
70
|
} catch (XMLDBException e) {
|
71
|
model.addAttribute("message", "failed: " + e.getMessage());
|
72
|
}
|
73
|
}
|
74
|
|
75
|
model.addAttribute("query", query);
|
76
|
|
77
|
}
|
78
|
|
79
|
}
|