Project

General

Profile

« Previous | Next » 

Revision 50143

huge update: swagger documentation, alignment of methods with the service trunk

View differences:

ISLookupClient.java
3 3
import java.io.IOException;
4 4
import java.nio.charset.Charset;
5 5
import java.util.List;
6
import java.util.Map;
6 7
import java.util.Queue;
8
import java.util.concurrent.LinkedBlockingQueue;
9
import java.util.function.Function;
10
import java.util.stream.Collectors;
7 11

  
8 12
import com.google.common.collect.Lists;
13
import com.google.common.xml.XmlEscapers;
9 14
import eu.dnetlib.OpenaireExporterConfig;
15
import eu.dnetlib.enabling.datasources.common.DatasourceManagerException;
16
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
10 17
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
11 18
import eu.dnetlib.openaire.exporter.datasource.clients.utils.IndexDsInfo;
19
import eu.dnetlib.openaire.exporter.funders.context.Context;
20
import eu.dnetlib.openaire.exporter.funders.context.MappingUtils;
12 21
import org.apache.commons.io.IOUtils;
22
import org.apache.commons.lang3.StringUtils;
13 23
import org.apache.commons.logging.Log;
14 24
import org.apache.commons.logging.LogFactory;
15 25
import org.springframework.beans.factory.annotation.Autowired;
......
33 43
	private ISLookUpService isLookUpService;
34 44

  
35 45
	@Cacheable("datasources-is-cache")
36
	public IndexDsInfo calculateCurrentIndexDsInfo(final Queue<Throwable> errors) throws IOException {
46
	public IndexDsInfo calculateCurrentIndexDsInfo() throws DatasourceManagerException {
37 47
		log.warn("calculateCurrentIndexDsInfo(): not using cache");
38
		final String[] arr = _isLookUp(_getQuery(config.getFindIndexDsInfo()), errors).split("@@@");
39
		return new IndexDsInfo(
40
				_isLookUp(_getQuery(config.getFindSolrIndexUrl()), errors),
48
		final String[] arr;
49
		try {
50
			arr = _isLookUp(_getQuery(config.getFindIndexDsInfo())).split("@@@");
51
			return new IndexDsInfo(
52
				_isLookUp(_getQuery(config.getFindSolrIndexUrl())),
41 53
				arr[0].trim(), arr[1].trim(), arr[2].trim());
54
		} catch (IOException | ISLookUpException e) {
55
			throw new DatasourceManagerException("unable fetch index DS information from IS");
56
		}
42 57
	}
43 58

  
44 59
	@Cacheable("datasources-is-cache")
45 60
	public String getObjectStoreId(final String dsId, final Queue<Throwable> errors) throws IOException {
46 61
		log.warn(String.format("getObjectStoreId(%s): not using cache", dsId));
47 62
		final String xqueryTemplate = _getQuery(config.getFindObjectStore());
48
		return _isLookUp(String.format(xqueryTemplate, dsId), errors);
63
		try {
64
			return _isLookUp(String.format(xqueryTemplate, dsId));
65
		} catch (ISLookUpException e) {
66
			errors.add(new DatasourceManagerException("unable to find objectStore for datasource " + dsId));
67
			return "";
68
		}
49 69
	}
50 70

  
51
	public List<String> getContextProfiles(final Queue<Throwable> errors) throws IOException {
71
	@Cacheable("datasources-is-cache")
72
	public Map<String, Context> getContextMap() throws IOException {
73
		final Queue<Throwable> errors = new LinkedBlockingQueue<>();
74
		try {
75
			return getContextProfiles(errors).stream()
76
					.filter(StringUtils::isNotBlank)
77
					.map(s -> MappingUtils.parseContext(s, errors))
78
					.collect(Collectors.toMap(
79
							Context::getId,
80
							Function.identity()));
81
		} finally {
82
			if (!errors.isEmpty()) {
83
				log.error(errors);
84
			}
85
		}
86
	}
87

  
88
	private void updateDatasourceField(final String dsId, final String element, final String value, final Queue<Throwable> errors) {
89
		try {
90
			final String xquery = _getQuery(config.getUpdateRepoField());
91
			_isLookUp(
92
				String.format(
93
					xquery,
94
					XmlEscapers.xmlAttributeEscaper().escape(dsId),
95
					XmlEscapers.xmlContentEscaper().escape(element),
96
					XmlEscapers.xmlContentEscaper().escape(value)));
97
		} catch (IOException | ISLookUpException e) {
98
			errors.add(e);
99
		}
100
	}
101

  
102
	/// HELPERS
103

  
104
	private List<String> getContextProfiles(final Queue<Throwable> errors) throws IOException {
52 105
		log.warn("getContextProfiles(): not using cache");
53 106
		final String xqueryTemplate = _getQuery(config.getFindFunderContexts());
54 107
		return _quickSeachProfile(xqueryTemplate, errors);
......
58 111
		return IOUtils.toString(resource.getInputStream(), Charset.defaultCharset());
59 112
	}
60 113

  
61
	private String _isLookUp(final String xquery, final Queue<Throwable> errors) {
62
		try {
63
			log.debug(String.format("running xquery: %s", xquery));
64
			final String res = isLookUpService.getResourceProfileByQuery(xquery);
65
			log.debug(String.format("query result: %s", res));
66
			return res;
67
		} catch (Throwable e) {
68
			errors.add(e);
69
			return "";
70
		}
114
	private String _isLookUp(final String xquery) throws ISLookUpException {
115

  
116
		log.debug(String.format("running xquery: %s", xquery));
117
		final String res = isLookUpService.getResourceProfileByQuery(xquery);
118
		log.debug(String.format("query result: %s", res));
119
		return res;
71 120
	}
72 121

  
73 122
	private List<String> _quickSeachProfile(final String xquery, final Queue<Throwable> errors) {

Also available in: Unified diff