Project

General

Profile

1
package eu.dnetlib.parthenos.publisher;
2

    
3
import java.util.concurrent.ExecutorService;
4
import java.util.concurrent.Executors;
5

    
6
import eu.dnetlib.parthenos.publisher.ParthenosPublisherHelper.ParthenosTargets;
7
import org.apache.commons.lang3.StringUtils;
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.scheduling.annotation.Async;
12
import org.springframework.web.bind.annotation.RequestMapping;
13
import org.springframework.web.bind.annotation.RequestMethod;
14
import org.springframework.web.bind.annotation.RequestParam;
15
import org.springframework.web.bind.annotation.RestController;
16

    
17
@RestController
18
public class ParthenosPublisherController {
19

    
20
	private static final Log log = LogFactory.getLog(ParthenosPublisherController.class);
21
	//TODO: to nicely handle arrors, follow https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-error-handling
22

    
23
	@Autowired
24
	private ParthenosPublisherHelper parthenosPublisherHelper;
25
	private ExecutorService executorService = Executors.newCachedThreadPool();
26

    
27
	@RequestMapping(value = "/publish", method = RequestMethod.POST)
28
	@Async
29
	public void publish(@RequestParam final String record, @RequestParam(required = false) String parthenosTarget)
30
			throws ParthenosPublisherException, InterruptedException {
31
		parthenosPublisherHelper.publish(record, getTarget(parthenosTarget));
32
	}
33

    
34
	@RequestMapping(value = "/unpublish", method = RequestMethod.GET)
35
	@Async
36
	public void unpublish(@RequestParam final String datasourceApi, @RequestParam(required = false) String parthenosTarget) throws ParthenosPublisherException {
37
		this.parthenosPublisherHelper.unpublish(datasourceApi, getTarget(parthenosTarget));
38
	}
39

    
40
	@RequestMapping(value = "/dropRegistry", method = RequestMethod.GET)
41
	@Async
42
	public void unpublish() throws ParthenosPublisherException {
43
		this.parthenosPublisherHelper.dropRegistry();
44
	}
45

    
46
	private ParthenosTargets getTarget(String value){
47
		ParthenosTargets target = ParthenosTargets.ALL;
48
		if (StringUtils.isNotBlank(value)) {
49
			target = ParthenosTargets.valueOf(value);
50
		}
51
		return target;
52
	}
53

    
54
	public ParthenosPublisherHelper getParthenosPublisherHelper() {
55
		return parthenosPublisherHelper;
56
	}
57

    
58
	public void setParthenosPublisherHelper(final ParthenosPublisherHelper parthenosPublisherHelper) {
59
		this.parthenosPublisherHelper = parthenosPublisherHelper;
60
	}
61
}
(1-1/4)