Project

General

Profile

1
package eu.dnetlib.enabling.inspector;
2

    
3
import java.util.*;
4
import java.util.stream.Collectors;
5
import java.util.stream.StreamSupport;
6

    
7
import javax.annotation.Resource;
8

    
9
import com.google.common.base.Function;
10
import com.google.common.collect.Iterators;
11
import com.google.common.collect.Lists;
12
import org.apache.commons.lang.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
import eu.dnetlib.xml.database.XMLDatabase;
22

    
23
/**
24
 * This controller offers a simple way to run arbitrary queries on the xmldb.
25
 *
26
 * @author marko
27
 *
28
 */
29
@Controller
30
public class QueryController extends AbstractInspectorController {
31

    
32
	/**
33
	 * logger.
34
	 */
35
	private static final Log log = LogFactory.getLog(QueryController.class); // NOPMD by marko on 11/24/08 5:02 PM
36

    
37
	/**
38
	 * xmldb.
39
	 */
40
	@Resource(name = "existDatabase")
41
	private transient XMLDatabase xmlDatabase;
42
	
43
	/**
44
	 * utility to parse resource ids and allows to navigate them.
45
	 */
46
	@Resource(name = "resourcelinkTool")
47
	private ResourceLinkTool linkTool;	
48

    
49
	/**
50
	 * run a query.
51
	 *
52
	 * @param model
53
	 *            mvc model
54
	 * @param query
55
	 *            query (optional)
56
	 * @throws
57
	 */
58
	@RequestMapping(value = "/inspector/query.do")
59
	void query(final Model model, @RequestParam(value = "query", required = false) final String query) {
60
		if (query != null) {
61
			log.info("running query: " + query);
62

    
63
			try {
64
				final Iterator<String> it = xmlDatabase.xquery(query);
65
				final List<String> res = StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.IMMUTABLE), false)
66
						.map(StringEscapeUtils::escapeHtml)
67
						.map(linkTool::linkfyToHtml)
68
						.collect(Collectors.toList());
69

    
70
				model.addAttribute("size", res.size());
71
				model.addAttribute("results", res);
72
			} catch (XMLDBException e) {
73
				model.addAttribute("message", "failed: " + e.getMessage());
74
			}
75
		}
76

    
77
		model.addAttribute("query", query);
78

    
79
	}
80

    
81
}
(3-3/9)