Project

General

Profile

1
package eu.dnetlib.oai.conf;
2

    
3
import java.util.List;
4
import java.util.function.Function;
5

    
6
import javax.annotation.Resource;
7

    
8
import org.apache.commons.lang3.StringUtils;
9
import org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11
import org.dom4j.DocumentException;
12
import org.springframework.beans.factory.annotation.Value;
13
import org.springframework.cache.annotation.CacheEvict;
14
import org.springframework.cache.annotation.Cacheable;
15

    
16
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
17
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
18
import eu.dnetlib.rmi.enabling.ISLookUpDocumentNotFoundException;
19
import eu.dnetlib.rmi.enabling.ISLookUpException;
20
import eu.dnetlib.rmi.enabling.ISLookUpService;
21
import eu.dnetlib.rmi.provision.MDFInfo;
22
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException;
23

    
24
/**
25
 * Helper class for common queries to the IS.
26
 *
27
 * @author alessia
28
 */
29
public class OAIISLookUpClient {
30

    
31
	private static final Log log = LogFactory.getLog(OAIISLookUpClient.class); // NOPMD by marko on 11/24/08 5:02 PM
32

    
33
	@Value("${services.oai.publisher.db.xquery}")
34
	private String xpathToCurrentDB;
35

    
36
	/**
37
	 * lookup service.
38
	 */
39
	@Resource
40
	private UniqueServiceLocator serviceLocator;
41

    
42
	@Resource
43
	private OAIConfigurationExistReader configuration;
44

    
45
	public List<MDFInfo> listMetadataFormats(final boolean onlyEnabled) throws ISLookUpException, DocumentException {
46
		return this.configuration.getMetadataFormatInfo(onlyEnabled);
47
	}
48

    
49
	@Cacheable(value = "unaryFunctions", key = "#tdsRuleId")
50
	public Function<String, String> getUnaryFunctionFromTDSRule(final String tdsRuleId) {
51
		log.fatal("Not using cache to get TDSRule " + tdsRuleId);
52
		final String queryCodeRule = "//RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value='" + tdsRuleId
53
				+ "']/BODY/CONFIGURATION/SCRIPT/CODE/*[local-name()='stylesheet']";
54
		String xsltCode = null;
55
		try {
56
			xsltCode = this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(queryCodeRule);
57
		} catch (final ISLookUpDocumentNotFoundException e) {
58
			log.fatal("Unexisting TDSRule profile with identifier " + tdsRuleId);
59
			throw new OaiPublisherRuntimeException(e);
60
		} catch (final ISLookUpException e) {
61
			throw new OaiPublisherRuntimeException(e);
62
		}
63
		if (StringUtils.isBlank(xsltCode)) {
64
			log.warn("Unexpected blank stylesheet in TDSRule profile with id: " + tdsRuleId + ". Returning identity function.");
65
			return it -> it;
66
		} else {
67
			return new ApplyXslt(xsltCode);
68
		}
69
	}
70

    
71
	@Cacheable("oaiDB")
72
	public String getCurrentDB() throws ISLookUpDocumentNotFoundException, ISLookUpException {
73
		log.fatal("Not using cache to get the current OAI db");
74
		final String currentDBName = this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(this.xpathToCurrentDB);
75
		log.info("Current OAI DB:" + currentDBName);
76
		return currentDBName;
77
	}
78

    
79
	@CacheEvict(value = { "unaryFunctions", "oaiDB" }, allEntries = true, beforeInvocation = true)
80
	public void evictCaches() {
81
		log.info("Evicted caches oaiDB and unaryFunctions");
82
	}
83

    
84
	public OAIConfigurationExistReader getConfiguration() {
85
		return this.configuration;
86
	}
87

    
88
	public void setConfiguration(final OAIConfigurationExistReader configuration) {
89
		this.configuration = configuration;
90
	}
91

    
92
}
(7-7/9)