Project

General

Profile

1
package eu.dnetlib.openaire.exporter.community;
2

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

    
12
import com.google.common.collect.Lists;
13
import com.google.common.escape.Escaper;
14
import com.google.common.xml.XmlEscapers;
15
import eu.dnetlib.OpenaireExporterConfig;
16
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
17
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
18
import eu.dnetlib.openaire.exporter.context.Context;
19
import eu.dnetlib.openaire.exporter.context.ContextMappingUtils;
20
import org.apache.commons.io.IOUtils;
21
import org.apache.commons.lang3.StringUtils;
22
import org.apache.commons.logging.Log;
23
import org.apache.commons.logging.LogFactory;
24
import org.springframework.beans.factory.annotation.Autowired;
25
import org.springframework.cache.annotation.CacheEvict;
26
import org.springframework.cache.annotation.Cacheable;
27
import org.springframework.core.io.ClassPathResource;
28
import org.springframework.stereotype.Component;
29

    
30
/**
31
 * Created by claudio on 20/10/2016.
32
 */
33
@Component
34
public class ISClientImpl implements ISClient {
35

    
36
 	private static final Log log = LogFactory.getLog(ISClientImpl.class);
37

    
38
	@Autowired
39
	private OpenaireExporterConfig config;
40

    
41
	@Autowired
42
	private ISLookUpService isLookUpService;
43

    
44
	@Override
45
	@Cacheable("context-cache")
46
	public Map<String, Context> getFunderContextMap() throws IOException {
47
		return _processContext(
48
				new LinkedBlockingQueue<>(),
49
				_getQuery(config.getFindFunderContexts()));
50
	}
51

    
52
	@Override
53
	@Cacheable("context-cache")
54
	public Map<String, Context> getCommunityContextMap() throws IOException {
55
		return _processContext(
56
				new LinkedBlockingQueue<>(),
57
				_getQuery(config.getFindCommunityContexts()));
58
	}
59

    
60
	@Override
61
	@CacheEvict(value = "context-cache", allEntries = true)
62
	public void updateContextParam(final String id, final String name, final String value) {
63
		final Queue<Throwable> errors = Lists.newLinkedList();
64
		_quickSeachProfile(getXQuery(id, name, value), errors);
65

    
66
		if (!errors.isEmpty()) {
67
			errors.forEach(e -> log.error(e));
68
		}
69
	}
70

    
71
	@Override
72
	@CacheEvict(value = "context-cache", allEntries = true)
73
	public void updateContextAttribute(final String id, final String name, final String value) {
74
		final Queue<Throwable> errors = Lists.newLinkedList();
75
		final Escaper esc = XmlEscapers.xmlAttributeEscaper();
76
		_quickSeachProfile(String.format(
77
				"update value collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')" +
78
						"/RESOURCE_PROFILE/BODY/CONFIGURATION/context[./@id = '%s']/@%s with '%s'", id, name, escape(esc, value)),
79
				errors);
80

    
81
		if (!errors.isEmpty()) {
82
			errors.forEach(e -> log.error(e));
83
		}
84
	}
85

    
86
	@Override
87
	@CacheEvict(value = "context-cache", allEntries = true)
88
	public void addConcept(final String id, final String categoryId, final String data) {
89
		final Queue<Throwable> errors = Lists.newLinkedList();
90
		_quickSeachProfile(String.format(
91
				"update insert %s into collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')" +
92
				"/RESOURCE_PROFILE/BODY/CONFIGURATION/context[./@id = '%s']/category[./@id = '%s']", data, id, categoryId), errors);
93

    
94
		if (!errors.isEmpty()) {
95
			errors.forEach(e -> log.error(e));
96
		}
97
	}
98

    
99
	@Override
100
	@CacheEvict(value = "context-cache", allEntries = true)
101
	public void removeConcept(final String id, final String categoryId, final String conceptId) {
102
		final Queue<Throwable> errors = Lists.newLinkedList();
103
		_quickSeachProfile(String.format(
104
				"for $concept in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')" +
105
						"/RESOURCE_PROFILE/BODY/CONFIGURATION/context[./@id = '%s']" +
106
						"/category[./@id = '%s']/concept[./@id = '%s']" +
107
						"return update delete $concept", id, categoryId, conceptId),
108
				errors);
109
		if (!errors.isEmpty()) {
110
			errors.forEach(e -> log.error(e.getMessage()));
111
		}
112
	}
113

    
114
	/// HELPERS
115

    
116
	private String getXQuery(final String id, final String name, final String value) {
117
		final Escaper esc = XmlEscapers.xmlContentEscaper();
118
		if (StringUtils.isNotBlank(value)) {
119
			return String.format(
120
					"update replace collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')" +
121
							"/RESOURCE_PROFILE/BODY/CONFIGURATION/context[./@id = '%s']/param[./@name = '%s'] with <param name='%s'>%s</param>", id, name, name,
122
					escape(esc, value));
123
		} else {
124
			return String.format(
125
					"update replace collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')" +
126
							"/RESOURCE_PROFILE/BODY/CONFIGURATION/context[./@id = '%s']/param[./@name = '%s'] with <param name='%s'/>", id, name, name);
127
		}
128
	}
129

    
130
	public static String escape(final Escaper esc, final String value) {
131
		return StringUtils.isNotBlank(value) ? esc.escape(value) : "";
132
	}
133

    
134
	private Map<String, Context> _processContext(final Queue<Throwable> errors, final String xquery) throws IOException {
135
		try {
136
			return getContextProfiles(errors, xquery).stream()
137
					.filter(StringUtils::isNotBlank)
138
					.map(s -> ContextMappingUtils.parseContext(s, errors))
139
					.collect(Collectors.toMap(
140
							Context::getId,
141
							Function.identity()));
142
		} finally {
143
			if (!errors.isEmpty()) {
144
				log.error(errors);
145
				errors.forEach(e -> e.printStackTrace());
146
			}
147
		}
148
	}
149

    
150
	private List<String> getContextProfiles(final Queue<Throwable> errors, final String xquery) throws IOException {
151
		log.warn("getContextProfiles(): not using cache");
152
		return _quickSeachProfile(xquery, errors);
153
	}
154

    
155
	private String _getQuery(final ClassPathResource resource) throws IOException {
156
		return IOUtils.toString(resource.getInputStream(), Charset.defaultCharset());
157
	}
158

    
159
	private String _isLookUp(final String xquery) throws ISLookUpException {
160
		log.debug(String.format("running xquery:\n%s", xquery));
161
		final String res = isLookUpService.getResourceProfileByQuery(xquery);
162
		//log.debug(String.format("query result: %s", res));
163
		return res;
164
	}
165

    
166
	private List<String> _quickSeachProfile(final String xquery, final Queue<Throwable> errors) {
167
		final List<String> res = Lists.newArrayList();
168
		try {
169
			log.debug(String.format("running xquery:\n%s", xquery));
170
			final List<String> list = isLookUpService.quickSearchProfile(xquery);
171
			if (list != null) {
172
				res.addAll(list);
173
			}
174
			log.debug(String.format("query result size: %s", res.size()));
175
		} catch (Throwable e) {
176
			errors.add(e);
177
			return Lists.newArrayList();
178
		} finally {
179
			return res;
180
		}
181
	}
182

    
183
	@CacheEvict(cacheNames = { "context-cache" }, allEntries = true)
184
	public void dropCache() {
185
		log.info("dropped dsManager IS cache");
186
	}
187

    
188
}
(13-13/13)