Project

General

Profile

1
package eu.dnetlib.parthenos.publisher;
2

    
3
import eu.dnetlib.parthenos.publisher.ParthenosPublisherHelper.ParthenosTargets;
4
import org.apache.commons.lang3.StringUtils;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.scheduling.annotation.Async;
7
import org.springframework.web.bind.annotation.RequestMapping;
8
import org.springframework.web.bind.annotation.RequestMethod;
9
import org.springframework.web.bind.annotation.RequestParam;
10
import org.springframework.web.bind.annotation.RestController;
11

    
12
@RestController
13
public class ParthenosPublisherController {
14

    
15
	//TODO: to nicely handle arrors, follow https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-error-handling
16

    
17
	@Autowired
18
	private ParthenosPublisherHelper parthenosPublisherHelper;
19

    
20
	@RequestMapping(value = "/publish", method = RequestMethod.POST)
21
	@Async
22
	public void publish(@RequestParam final String record, @RequestParam(required = false) String parthenosTarget) throws ParthenosPublisherException {
23
		this.parthenosPublisherHelper.publish(record, getTarget(parthenosTarget));
24
	}
25

    
26
	@RequestMapping(value = "/unpublish", method = RequestMethod.GET)
27
	@Async
28
	public long unpublish(@RequestParam final String datasourceApi, @RequestParam(required = false) String parthenosTarget) throws ParthenosPublisherException {
29
		return this.parthenosPublisherHelper.unpublish(datasourceApi, getTarget(parthenosTarget));
30
	}
31

    
32
	private ParthenosTargets getTarget(String value){
33
		ParthenosTargets target = ParthenosTargets.ALL;
34
		if (StringUtils.isNotBlank(value)) {
35
			target = ParthenosTargets.valueOf(value);
36
		}
37
		return target;
38
	}
39

    
40
	public ParthenosPublisherHelper getParthenosPublisherHelper() {
41
		return parthenosPublisherHelper;
42
	}
43

    
44
	public void setParthenosPublisherHelper(final ParthenosPublisherHelper parthenosPublisherHelper) {
45
		this.parthenosPublisherHelper = parthenosPublisherHelper;
46
	}
47
}
(1-1/4)