Project

General

Profile

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.lang3.StringUtils;
13
import org.apache.commons.logging.Log;
14
import org.apache.commons.logging.LogFactory;
15
import org.dom4j.DocumentException;
16
import org.springframework.beans.factory.annotation.Value;
17
import org.springframework.stereotype.Controller;
18
import org.springframework.ui.ModelMap;
19
import org.springframework.util.AntPathMatcher;
20
import org.springframework.web.bind.annotation.*;
21
import org.springframework.web.servlet.HandlerMapping;
22

    
23
@Controller
24
@DNetRestDocumentation
25
@CrossOrigin(origins = { "*" })
26
public class VocabularyPublisherController {
27

    
28
	private static final Log log = LogFactory.getLog(VocabularyPublisherController.class);
29

    
30
	@Resource
31
	private VocabularyRetriever vocabularyRetriever;
32
	@Value("${vocabulary.publisher.title}")
33
	private String title;
34
	@Value("${vocabulary.publisher.baseURL}")
35
	private String baseURL;
36
	@Value("${container.context}")
37
	private String context;
38

    
39
	@RequestMapping(value = "/vocabularies", method = RequestMethod.GET, produces = { "application/json" })
40
	public @ResponseBody
41
	List<Vocabulary> listVocabularies() {
42
		log.debug("listVocabularies()");
43
		return vocabularyRetriever.listVocabularies();
44
	}
45

    
46
	@RequestMapping(value = "/vocabularies/{code}", method = RequestMethod.GET, produces = { "application/json" })
47
	public @ResponseBody
48
	Vocabulary getVocabulary(@PathVariable("code") final String code) throws VocabularyNotFoundException {
49
		log.debug("getVocabulary with code = " + code);
50
		return this.vocabularyRetriever.getVocabularyByCode(code);
51
	}
52

    
53
	@RequestMapping(value = "/vocabularies/{code}/**", method = RequestMethod.GET, produces = { "application/json" })
54
	public @ResponseBody
55
	VocabularyTerm getTermSynonyms(
56
			@PathVariable("code") final String code,
57
			final HttpServletRequest request) throws VocabularyNotFoundException {
58
		final String term = extractTerm(request);
59
		log.debug(String.format("getVocabulary with code = %s, term = %s", code, term));
60
		return this.vocabularyRetriever.getTermSynonyms(code, term);
61
	}
62

    
63

    
64
	private String extractTerm(HttpServletRequest request) {
65

    
66
		String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); // /elements/CATEGORY1/CATEGORY1_1/ID
67
		String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); // /elements/**
68

    
69
		return StringUtils.substringBeforeLast(new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path), ".json"); // CATEGORY1/CATEGORY1_1/ID
70
	}
71

    
72
	// View-based methods for html responses
73
	@RequestMapping(value = "/vocabularies", method = RequestMethod.GET, produces = { "text/html" })
74
	public void listVocabularies(final ModelMap map) {
75
		map.addAttribute("vocabularies", listVocabularies());
76
		map.addAttribute("title", title);
77
		map.addAttribute("baseURL", baseURL);
78
		map.addAttribute("context", context);
79
	}
80

    
81
	@RequestMapping(value = "/vocabularies/{code}", method = RequestMethod.GET, produces = { "text/html" })
82
	public String getVocabulary(
83
			@PathVariable("code") final String code,
84
			final ModelMap map) throws VocabularyNotFoundException {
85

    
86
		map.addAttribute("vocabulary", getVocabulary(code));
87
		map.addAttribute("baseURL", baseURL);
88
		map.addAttribute("context", context);
89
		return "displayVocabulary";
90
	}
91

    
92
	@RequestMapping(value = "/vocabularies/{code}/**", method = RequestMethod.GET, produces = { "text/html" })
93
	public String getTermSynonyms(
94
			@PathVariable("code") final String code,
95
			final HttpServletRequest request,
96
			final ModelMap map) throws VocabularyNotFoundException {
97
		map.addAttribute("vocabulary", getVocabulary(code));
98
		map.addAttribute("term", getTermSynonyms(code, request));
99
		map.addAttribute("baseURL", baseURL);
100
		map.addAttribute("context", context);
101
		return "displaySynonyms";
102
	}
103

    
104
}
(2-2/2)