1
|
package eu.dnetlib.oai.utils;
|
2
|
|
3
|
import java.util.List;
|
4
|
import java.util.function.Function;
|
5
|
|
6
|
import com.google.common.collect.Lists;
|
7
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
8
|
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
|
9
|
import eu.dnetlib.oai.conf.OAIConfigurationExistReader;
|
10
|
import eu.dnetlib.rmi.enabling.ISLookUpDocumentNotFoundException;
|
11
|
import eu.dnetlib.rmi.enabling.ISLookUpException;
|
12
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
13
|
import eu.dnetlib.rmi.provision.MDFInfo;
|
14
|
import eu.dnetlib.rmi.provision.OaiPublisherException;
|
15
|
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException;
|
16
|
import org.apache.commons.lang3.StringUtils;
|
17
|
import org.apache.commons.logging.Log;
|
18
|
import org.apache.commons.logging.LogFactory;
|
19
|
import org.dom4j.DocumentException;
|
20
|
import org.springframework.beans.factory.annotation.Autowired;
|
21
|
import org.springframework.beans.factory.annotation.Value;
|
22
|
import org.springframework.cache.annotation.CacheEvict;
|
23
|
import org.springframework.cache.annotation.Cacheable;
|
24
|
|
25
|
/**
|
26
|
* Helper class for common queries to the IS.
|
27
|
*
|
28
|
* @author alessia
|
29
|
*/
|
30
|
public class OAILookUpClient {
|
31
|
|
32
|
private static final Log log = LogFactory.getLog(OAILookUpClient.class); // NOPMD by marko on 11/24/08 5:02 PM
|
33
|
|
34
|
@Value("${services.oai.publisher.db.xquery}")
|
35
|
private String xpathToCurrentDB;
|
36
|
|
37
|
/**
|
38
|
* lookup service.
|
39
|
*/
|
40
|
@Autowired
|
41
|
private UniqueServiceLocator serviceLocator;
|
42
|
|
43
|
@Autowired
|
44
|
private OAIConfigurationExistReader configuration;
|
45
|
|
46
|
public List<MDFInfo> listMetadataFormats(final boolean onlyEnabled) throws ISLookUpException, DocumentException, OaiPublisherException {
|
47
|
return Lists.newArrayList(this.configuration.getMetadataFormatInfo(onlyEnabled));
|
48
|
}
|
49
|
|
50
|
@Cacheable(value = "unaryFunctions", key = "#tdsRuleId")
|
51
|
public Function<String, String> getUnaryFunctionFromTDSRule(final String tdsRuleId) {
|
52
|
log.fatal("Not using cache to get TDSRule " + tdsRuleId);
|
53
|
final String queryCodeRule = "//RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value='" + tdsRuleId
|
54
|
+ "']/BODY/CONFIGURATION/SCRIPT/CODE/*[local-name()='stylesheet']";
|
55
|
String xsltCode = null;
|
56
|
try {
|
57
|
xsltCode = this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(queryCodeRule);
|
58
|
} catch (final Throwable e) {
|
59
|
log.fatal("Unexisting TDSRule profile with identifier " + tdsRuleId);
|
60
|
throw new OaiPublisherRuntimeException(e);
|
61
|
}
|
62
|
if (StringUtils.isBlank(xsltCode)) {
|
63
|
log.warn("Unexpected blank stylesheet in TDSRule profile with id: " + tdsRuleId + ". Returning identity function.");
|
64
|
return it -> it;
|
65
|
} else {
|
66
|
return new ApplyXslt(xsltCode);
|
67
|
}
|
68
|
}
|
69
|
|
70
|
@Cacheable("oaiDB")
|
71
|
public String getCurrentDB() throws ISLookUpDocumentNotFoundException, ISLookUpException {
|
72
|
log.fatal("Not using cache to get the current OAI db");
|
73
|
final String currentDBName = this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(this.xpathToCurrentDB);
|
74
|
log.info("Current OAI DB:" + currentDBName);
|
75
|
return currentDBName;
|
76
|
}
|
77
|
|
78
|
@CacheEvict(value = { "unaryFunctions", "oaiDB" }, allEntries = true, beforeInvocation = true)
|
79
|
public void evictCaches() {
|
80
|
log.info("Evicted caches oaiDB and unaryFunctions");
|
81
|
}
|
82
|
|
83
|
public OAIConfigurationExistReader getConfiguration() {
|
84
|
return this.configuration;
|
85
|
}
|
86
|
|
87
|
public void setConfiguration(final OAIConfigurationExistReader configuration) {
|
88
|
this.configuration = configuration;
|
89
|
}
|
90
|
|
91
|
}
|