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

    
61
	public int register(final String record) throws ParthenosPublisherException{
62
		try {
63
			if (StringUtils.isBlank(record)) {
64
				log.warn("Got empty record");
65
				return 0;
66
			}
67
			String objIdentifier = extractFromRecord(record, xpathSelectorObjIdentifier);
68
			if (StringUtils.isBlank(objIdentifier)) {
69
				log.warn("Got record with no objIdentifier -- skipping");
70
				return 0;
71
			}
72
			//TODO: get the json strings to register
73
			Iterable<String> resources = resourceGenerator.getGCubeER(record);
74
			for(String res : resources){
75
				//resourceRegistryPublisher.
76
			}
77
			return 0;
78
		}catch(Throwable e){
79
			log.error(e.getMessage());
80
			throw new ParthenosPublisherException(e);
81
		}
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

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

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

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

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

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

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

    
131
	public GCubeResourceGenerator getResourceGenerator() {
132
		return resourceGenerator;
133
	}
134

    
135
	public void setResourceGenerator(final GCubeResourceGenerator resourceGenerator) {
136
		this.resourceGenerator = resourceGenerator;
137
	}
138

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

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

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

    
151
	public void setResourceRegistryClient(final ResourceRegistryClient resourceRegistryClient) {
152
		this.resourceRegistryClient = resourceRegistryClient;
153
	}
154
}
(3-3/5)