Project

General

Profile

1
package eu.dnetlib.msro.controllers;
2

    
3
import java.util.HashMap;
4
import java.util.Map;
5

    
6
import org.apache.commons.logging.Log;
7
import org.apache.commons.logging.LogFactory;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.web.bind.annotation.RequestMapping;
10
import org.springframework.web.bind.annotation.RequestParam;
11
import org.springframework.web.bind.annotation.RestController;
12

    
13
import com.fasterxml.jackson.databind.ObjectMapper;
14

    
15
import eu.dnetlib.clients.msro.Workflow;
16
import eu.dnetlib.clients.msro.WorkflowTemplate;
17
import eu.dnetlib.enabling.annotations.DnetService;
18
import eu.dnetlib.enabling.annotations.DnetServiceType;
19
import eu.dnetlib.msro.exceptions.MSROException;
20
import eu.dnetlib.msro.workflows.WorkflowRef;
21
import eu.dnetlib.msro.workflows.util.WorkflowDispatcher;
22
import eu.dnetlib.services.BaseService;
23
import eu.dnetlib.services.async.AsyncMethodException;
24
import eu.dnetlib.services.async.AsyncRunnable;
25
import eu.dnetlib.services.async.AsyncServerCallback;
26
import eu.dnetlib.services.async.HasAsyncMethods;
27

    
28
@RestController
29
@RequestMapping("/msro")
30
@DnetService(DnetServiceType.msro)
31
public class MsroController extends BaseService implements HasAsyncMethods {
32

    
33
	private static final Log log = LogFactory.getLog(MsroController.class);
34

    
35
	@Autowired
36
	private WorkflowDispatcher dispatcher;
37

    
38
	@Override
39
	public AsyncRunnable prepareThread(final String method, final String jsonParams, final AsyncServerCallback callback) throws AsyncMethodException {
40
		final ObjectMapper mapper = new ObjectMapper();
41

    
42
		return new AsyncRunnable() {
43

    
44
			@Override
45
			public Map<String, String> prepare() {
46
				return new HashMap<>();
47
			}
48

    
49
			@Override
50
			public void execute() throws AsyncMethodException {
51
				final WorkflowRef ref;
52
				try {
53
					switch (method) {
54
					case "startWorkflow":
55
						dispatcher.startWorkflow(mapper.readValue(jsonParams, Workflow.class), getBaseUrl());
56
						break;
57
					case "startWorkflowTemplate":
58
						dispatcher.startWorkflowTemplate(mapper.readValue(jsonParams, WorkflowTemplate.class), getBaseUrl());
59
						break;
60
					default:
61
						log.warn("Invalid method: " + method);
62
						throw new AsyncMethodException("Invalid method: " + method);
63
					}
64
				} catch (final Throwable e) {
65
					log.warn("Error executing method: " + method, e);
66
					throw new AsyncMethodException("Error executing method: " + method, e);
67
				}
68
			}
69
		};
70

    
71
	}
72

    
73
	@RequestMapping("startWorkflow")
74
	public WorkflowRef startWorkflow(
75
			@RequestParam final String wfId,
76
			@RequestParam(required = false) final String parent,
77
			@RequestParam(required = false) final String dsId,
78
			@RequestParam(required = false) final String ifaceId) throws MSROException {
79

    
80
		return dispatcher.startWorkflow(wfId, parent, dsId, ifaceId, null);
81

    
82
	}
83

    
84
}
    (1-1/1)