Project

General

Profile

1
package eu.dnetlib.parthenos.registry;
2

    
3
import java.util.Map;
4

    
5
import com.google.common.collect.Maps;
6
import eu.dnetlib.parthenos.publisher.ParthenosPublisherException;
7
import eu.dnetlib.parthenos.publisher.SaxonHelper;
8
import net.sf.saxon.s9api.SaxonApiException;
9
import net.sf.saxon.s9api.Serializer;
10
import net.sf.saxon.s9api.XPathSelector;
11
import org.apache.commons.lang3.StringUtils;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
15
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
16
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher;
17
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory;
18

    
19
/**
20
 * Created by Alessia Bardi on 26/09/2017.
21
 *
22
 * @author Alessia Bardi
23
 */
24
public class RegistryClient {
25

    
26
	private static final Log log = LogFactory.getLog(RegistryClient.class);
27

    
28
	private static final String OAI_NAMESPACE_URI = "http://www.openarchives.org/OAI/2.0/";
29
	private static final String DRI_NAMESPACE_URI = "http://www.driver-repository.eu/namespace/dri";
30
	private static final String RDF_NAMESPACE_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
31

    
32
	private GCubeResourceRegistrator gCubeResourceRegistrator;
33
	private SaxonHelper saxonHelper;
34
	private XPathSelector xpathSelectorObjIdentifier;
35
	//	private XPathSelector xpathSelectorCollectionDate;
36
	//	private XPathSelector xpathSelectorTransformationDate;
37
	//	private XPathSelector xpathSelectorDatasourceName;
38
	//	private XPathSelector xpathSelectorDatasourceApi;
39
	private XPathSelector xpathSelectorRDF;
40

    
41
	private ResourceRegistryPublisher resourceRegistryPublisher;
42
	private ResourceRegistryClient resourceRegistryClient;
43
	//private String registryBaseURL;
44
	private String defaultBaseURI;
45

    
46
	protected RegistryClient(final SaxonHelper saxonHelper,
47
			final String defaultBaseURI,
48
			final GCubeResourceRegistrator gCubeResourceRegistrator)
49
			throws ParthenosPublisherException {
50
		//this.registryBaseURL = registryBaseURL;
51
		this.saxonHelper = saxonHelper;
52
		this.defaultBaseURI = defaultBaseURI;
53
		this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
54
		this.resourceRegistryClient = ResourceRegistryClientFactory.create();
55
		this.gCubeResourceRegistrator = gCubeResourceRegistrator;
56
		try {
57
			prepareXpathSelectors();
58
		} catch (SaxonApiException e) {
59
			throw new ParthenosPublisherException(e);
60
		}
61
	}
62

    
63
	public int unregister(final String datasourceInterface) {
64
		return gCubeResourceRegistrator.unregister(datasourceInterface);
65
	}
66

    
67
	public void register(final String record) throws ParthenosPublisherException {
68
		try {
69
			if (StringUtils.isBlank(record)) {
70
				log.warn("Got empty record");
71
			}
72
			String objIdentifier = extractFromRecord(record, xpathSelectorObjIdentifier);
73
			if (StringUtils.isBlank(objIdentifier)) {
74
				log.warn("Got record with no objIdentifier -- skipping");
75
			}
76
			String recordRDF = extractFromRecord(record, xpathSelectorRDF);
77
			gCubeResourceRegistrator.register(recordRDF, objIdentifier);
78
		} catch (Throwable e) {
79
			log.error(e.getMessage());
80
			throw new ParthenosPublisherException(e);
81
		}
82
	}
83

    
84
	private void prepareXpathSelectors() throws SaxonApiException {
85
		Map<String, String> namespaces = Maps.newHashMap();
86
		namespaces.put("oai", OAI_NAMESPACE_URI);
87
		namespaces.put("dri", DRI_NAMESPACE_URI);
88
		namespaces.put("rdf", RDF_NAMESPACE_URI);
89
		xpathSelectorObjIdentifier = this.saxonHelper.help().prepareXPathSelector("//oai:header/dri:objIdentifier/text()", namespaces);
90
		//xpathSelectorCollectionDate = this.saxonHelper.help().prepareXPathSelector("//oai:header/dri:dateOfCollection/text()", namespaces);
91
		//xpathSelectorTransformationDate = this.saxonHelper.help().prepareXPathSelector("//oai:header/dri:dateOfTransformation/text()", namespaces);
92
		//xpathSelectorDatasourceName = this.saxonHelper.help().prepareXPathSelector("//oai:header/dri:datasourcename/text()", namespaces);
93
		//xpathSelectorDatasourceApi = this.saxonHelper.help().prepareXPathSelector("//oai:header/dri:datasourceapi/text()", namespaces);
94
		xpathSelectorRDF = this.saxonHelper.help().prepareXPathSelector("//oai:metadata/rdf:RDF", namespaces);
95
	}
96

    
97
	private String extractFromRecord(final String record, final XPathSelector xPathSelector) {
98
		try {
99
			return this.saxonHelper.help().setSerializerProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes").evaluateSingleAsString(record, xPathSelector);
100
		} catch (SaxonApiException e) {
101
			throw new RuntimeException("Cannot extract content ", e);
102
		}
103
	}
104

    
105
	public void setSaxonHelper(final SaxonHelper saxonHelper) {
106
		this.saxonHelper = saxonHelper;
107
	}
108

    
109
//	public String getRegistryBaseURL() {
110
//		return registryBaseURL;
111
//	}
112
//
113
//	public void setRegistryBaseURL(final String registryBaseURL) {
114
//		this.registryBaseURL = registryBaseURL;
115
//	}
116

    
117
	public void setDefaultBaseURI(final String defaultBaseURI) {
118
		this.defaultBaseURI = defaultBaseURI;
119
	}
120

    
121
	public SaxonHelper getSaxonHelper() {
122
		return saxonHelper;
123
	}
124

    
125
	public String getDefaultBaseURI() {
126
		return defaultBaseURI;
127
	}
128

    
129
	public GCubeResourceRegistrator getgCubeResourceRegistrator() {
130
		return gCubeResourceRegistrator;
131
	}
132

    
133
	public void setgCubeResourceRegistrator(final GCubeResourceRegistrator gCubeResourceRegistrator) {
134
		this.gCubeResourceRegistrator = gCubeResourceRegistrator;
135
	}
136

    
137
	public ResourceRegistryPublisher getResourceRegistryPublisher() {
138
		return resourceRegistryPublisher;
139
	}
140

    
141
	public void setResourceRegistryPublisher(final ResourceRegistryPublisher resourceRegistryPublisher) {
142
		this.resourceRegistryPublisher = resourceRegistryPublisher;
143
	}
144

    
145
	public ResourceRegistryClient getResourceRegistryClient() {
146
		return resourceRegistryClient;
147
	}
148

    
149
	public void setResourceRegistryClient(final ResourceRegistryClient resourceRegistryClient) {
150
		this.resourceRegistryClient = resourceRegistryClient;
151
	}
152
}
(4-4/6)