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