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 String registryBaseURL,
47
			final SaxonHelper saxonHelper,
48
			final String defaultBaseURI,
49
			final GCubeResourceRegistrator gCubeResourceRegistrator)
50
			throws ParthenosPublisherException {
51
		this.registryBaseURL = registryBaseURL;
52
		this.saxonHelper = saxonHelper;
53
		this.defaultBaseURI = defaultBaseURI;
54
		this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
55
		this.resourceRegistryClient = ResourceRegistryClientFactory.create();
56
		this.gCubeResourceRegistrator = gCubeResourceRegistrator;
57
		try {
58
			prepareXpathSelectors();
59
		} catch (SaxonApiException e) {
60
			throw new ParthenosPublisherException(e);
61
		}
62
	}
63

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

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

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

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

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

    
110
	public String getRegistryBaseURL() {
111
		return registryBaseURL;
112
	}
113

    
114
	public void setRegistryBaseURL(final String registryBaseURL) {
115
		this.registryBaseURL = registryBaseURL;
116
	}
117

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

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

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

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

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

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

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

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

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