Project

General

Profile

1
package eu.dnetlib.data.information.oai.publisher.conf;
2

    
3
import java.util.List;
4
import javax.annotation.Resource;
5

    
6
import com.google.common.collect.Lists;
7
import eu.dnetlib.data.information.oai.publisher.OaiPublisherException;
8
import eu.dnetlib.data.information.oai.publisher.OaiPublisherRuntimeException;
9
import eu.dnetlib.data.information.oai.publisher.info.MDFInfo;
10
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
11
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
12
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
13
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
14
import eu.dnetlib.miscutils.functional.IdentityFunction;
15
import eu.dnetlib.miscutils.functional.UnaryFunction;
16
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
17
import org.apache.commons.lang3.StringUtils;
18
import org.apache.commons.logging.Log;
19
import org.apache.commons.logging.LogFactory;
20
import org.dom4j.DocumentException;
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
 */
31
public class ISLookUpClient {
32

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

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

    
38
	/**
39
	 * lookup service.
40
	 */
41
	@Resource
42
	private UniqueServiceLocator serviceLocator;
43

    
44
	@Resource
45
	private OAIConfigurationExistReader configuration;
46

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

    
51
	@Cacheable(value = "unaryFunctions", key = "#tdsRuleId")
52
	public UnaryFunction<String, String> getUnaryFunctionFromTDSRule(final String tdsRuleId) {
53
		log.fatal("Not using cache to get TDSRule " + tdsRuleId);
54
		final String queryCodeRule = "//RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value='" + tdsRuleId
55
				+ "']/BODY/CONFIGURATION/SCRIPT/CODE/*[local-name()='stylesheet']";
56
		String xsltCode = null;
57
		try {
58
			xsltCode = this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(queryCodeRule);
59
		} catch (ISLookUpDocumentNotFoundException e) {
60
			log.fatal("Unexisting TDSRule profile with identifier " + tdsRuleId);
61
			throw new OaiPublisherRuntimeException(e);
62
		} catch (ISLookUpException e) {
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 new IdentityFunction<String>();
68
		} else return new ApplyXslt(xsltCode);
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
		String currentDBName = this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(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 configuration;
86
	}
87

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

    
92
}
(1-1/7)