Project

General

Profile

1
package eu.dnetlib.functionality.modular.ui.workflows.values;
2

    
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Map;
6
import java.util.concurrent.*;
7

    
8
import com.google.common.base.Function;
9
import com.google.common.collect.Lists;
10
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
11
import eu.dnetlib.msro.workflows.util.ValidNodeValuesFetcher;
12
import eu.dnetlib.rmi.data.CollectorService;
13
import eu.dnetlib.rmi.data.ProtocolParameterValue;
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.springframework.beans.factory.annotation.Autowired;
17

    
18
public class ListProtocolFieldValues extends ValidNodeValuesFetcher {
19

    
20
	private static final Log log = LogFactory.getLog(ListProtocolFieldValues.class);
21
	private final ExecutorService pool = Executors.newFixedThreadPool(10);
22
	@Autowired
23
	private UniqueServiceLocator serviceLocator;
24

    
25
	@Override
26
	protected List<DnetParamValue> obtainValues(final Map<String, String> params) throws Exception {
27
		verifyParams(params, "baseUrl", "protocol", "field");
28

    
29
		final String protocol = params.get("protocol");
30
		final String baseUrl = params.get("baseUrl");
31
		final String field = params.get("field");
32

    
33
		final Future<List<DnetParamValue>> futureList = this.pool.submit(new Callable<List<DnetParamValue>>() {
34

    
35
			@Override
36
			public List<DnetParamValue> call() throws Exception {
37
				final List<ProtocolParameterValue> list =
38
						getServiceLocator().getService(CollectorService.class).listValidValuesForParam(protocol, baseUrl, field, null);
39

    
40
				return Lists.newArrayList(Lists.transform(list, new Function<ProtocolParameterValue, ValidNodeValuesFetcher.DnetParamValue>() {
41

    
42
					@Override
43
					public DnetParamValue apply(final ProtocolParameterValue ppv) {
44
						return new DnetParamValue(ppv.getId(), ppv.getName());
45
					}
46
				}));
47
			}
48
		});
49

    
50
		try {
51
			return futureList.get(10, TimeUnit.SECONDS);
52
		} catch (final Exception e) {
53
			log.error("Error obtaining values of protocol: " + protocol);
54
			futureList.cancel(true);
55
			return new ArrayList<DnetParamValue>();
56
		}
57
	}
58

    
59
	public UniqueServiceLocator getServiceLocator() {
60
		return this.serviceLocator;
61
	}
62

    
63
	public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
64
		this.serviceLocator = serviceLocator;
65
	}
66

    
67
}
(3-3/3)