Project

General

Profile

1
package eu.dnetlib.oai.utils;
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 com.google.common.collect.Lists;
17

    
18
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
19
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
20
import eu.dnetlib.oai.conf.OAIConfigurationExistReader;
21
import eu.dnetlib.rmi.enabling.ISLookUpDocumentNotFoundException;
22
import eu.dnetlib.rmi.enabling.ISLookUpException;
23
import eu.dnetlib.rmi.enabling.ISLookUpService;
24
import eu.dnetlib.rmi.provision.MDFInfo;
25
import eu.dnetlib.rmi.provision.OaiPublisherException;
26
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException;
27

    
28
/**
29
 * Helper class for common queries to the IS.
30
 *
31
 * @author alessia
32
 */
33
public class OAILookUpClient {
34

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

    
37
	@Value("${services.oai.publisher.db.xquery}")
38
	private String xpathToCurrentDB;
39

    
40
	/**
41
	 * lookup service.
42
	 */
43
	@Resource
44
	private UniqueServiceLocator serviceLocator;
45

    
46
	@Resource
47
	private OAIConfigurationExistReader configuration;
48

    
49
	public List<MDFInfo> listMetadataFormats(final boolean onlyEnabled) throws ISLookUpException, DocumentException, OaiPublisherException {
50
		return Lists.newArrayList(this.configuration.getMetadataFormatInfo(onlyEnabled));
51
	}
52

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

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

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

    
86
	public OAIConfigurationExistReader getConfiguration() {
87
		return this.configuration;
88
	}
89

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

    
94
}
    (1-1/1)