1
|
package eu.dnetlib.vocabularies.publisher.controller;
|
2
|
|
3
|
import java.util.List;
|
4
|
import javax.annotation.Resource;
|
5
|
import javax.servlet.http.HttpServletRequest;
|
6
|
|
7
|
import eu.dnetlib.common.rmi.DNetRestDocumentation;
|
8
|
import eu.dnetlib.vocabularies.publisher.VocabularyNotFoundException;
|
9
|
import eu.dnetlib.vocabularies.publisher.VocabularyRetriever;
|
10
|
import eu.dnetlib.vocabularies.publisher.model.Vocabulary;
|
11
|
import eu.dnetlib.vocabularies.publisher.model.VocabularyTerm;
|
12
|
import org.apache.commons.logging.Log;
|
13
|
import org.apache.commons.logging.LogFactory;
|
14
|
import org.dom4j.DocumentException;
|
15
|
import org.springframework.beans.factory.annotation.Value;
|
16
|
import org.springframework.stereotype.Controller;
|
17
|
import org.springframework.ui.ModelMap;
|
18
|
import org.springframework.util.AntPathMatcher;
|
19
|
import org.springframework.web.bind.annotation.*;
|
20
|
import org.springframework.web.servlet.HandlerMapping;
|
21
|
|
22
|
@Controller
|
23
|
@DNetRestDocumentation
|
24
|
@CrossOrigin(origins = { "*" })
|
25
|
public class VocabularyPublisherController {
|
26
|
|
27
|
private static final Log log = LogFactory.getLog(VocabularyPublisherController.class);
|
28
|
|
29
|
@Resource
|
30
|
private VocabularyRetriever vocabularyRetriever;
|
31
|
@Value("${vocabulary.publisher.title}")
|
32
|
private String title;
|
33
|
@Value("${vocabulary.publisher.baseURL}")
|
34
|
private String baseURL;
|
35
|
@Value("${container.context}")
|
36
|
private String context;
|
37
|
|
38
|
@RequestMapping(value = "/vocabularies", method = RequestMethod.GET, produces = { "application/json" })
|
39
|
public @ResponseBody
|
40
|
List<Vocabulary> listVocabularies() {
|
41
|
log.debug("listVocabularies()");
|
42
|
return vocabularyRetriever.listVocabularies();
|
43
|
}
|
44
|
|
45
|
@RequestMapping(value = "/vocabularies/{code}", method = RequestMethod.GET, produces = { "application/json" })
|
46
|
public @ResponseBody
|
47
|
Vocabulary getVocabulary(@PathVariable("code") final String code) throws VocabularyNotFoundException {
|
48
|
log.debug("getVocabulary with code = " + code);
|
49
|
return this.vocabularyRetriever.getVocabularyByCode(code);
|
50
|
}
|
51
|
|
52
|
@RequestMapping(value = "/vocabularies/{code}/**", method = RequestMethod.GET, produces = { "application/json" })
|
53
|
public @ResponseBody
|
54
|
VocabularyTerm getTermSynonyms(
|
55
|
@PathVariable("code") final String code,
|
56
|
final HttpServletRequest request) throws VocabularyNotFoundException {
|
57
|
final String term = extractTerm(request);
|
58
|
log.debug(String.format("getVocabulary with code = %s, term = %s", code, term));
|
59
|
return this.vocabularyRetriever.getTermSynonyms(code, term);
|
60
|
}
|
61
|
|
62
|
|
63
|
private String extractTerm(HttpServletRequest request) {
|
64
|
|
65
|
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); // /elements/CATEGORY1/CATEGORY1_1/ID
|
66
|
String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); // /elements/**
|
67
|
|
68
|
return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path); // CATEGORY1/CATEGORY1_1/ID
|
69
|
}
|
70
|
|
71
|
// View-based methods for html responses
|
72
|
@RequestMapping(value = "/vocabularies", method = RequestMethod.GET, produces = { "text/html" })
|
73
|
public void listVocabularies(final ModelMap map) {
|
74
|
map.addAttribute("vocabularies", listVocabularies());
|
75
|
map.addAttribute("title", title);
|
76
|
map.addAttribute("baseURL", baseURL);
|
77
|
map.addAttribute("context", context);
|
78
|
}
|
79
|
|
80
|
@RequestMapping(value = "/vocabularies/{code}", method = RequestMethod.GET, produces = { "text/html" })
|
81
|
public String getVocabulary(
|
82
|
@PathVariable("code") final String code,
|
83
|
final ModelMap map) throws VocabularyNotFoundException {
|
84
|
|
85
|
map.addAttribute("vocabulary", getVocabulary(code));
|
86
|
map.addAttribute("baseURL", baseURL);
|
87
|
map.addAttribute("context", context);
|
88
|
return "displayVocabulary";
|
89
|
}
|
90
|
|
91
|
@RequestMapping(value = "/vocabularies/{code}/**", method = RequestMethod.GET, produces = { "text/html" })
|
92
|
public String getTermSynonyms(
|
93
|
@PathVariable("code") final String code,
|
94
|
final HttpServletRequest request,
|
95
|
final ModelMap map) throws VocabularyNotFoundException {
|
96
|
map.addAttribute("vocabulary", getVocabulary(code));
|
97
|
map.addAttribute("term", getTermSynonyms(code, request));
|
98
|
map.addAttribute("baseURL", baseURL);
|
99
|
map.addAttribute("context", context);
|
100
|
return "displaySynonyms";
|
101
|
}
|
102
|
|
103
|
}
|