Project

General

Profile

1
package eu.dnetlib.oai.conf;
2

    
3
import java.util.ArrayList;
4
import java.util.HashSet;
5
import java.util.List;
6
import java.util.Set;
7
import java.util.stream.Collectors;
8

    
9
import org.springframework.beans.factory.annotation.Autowired;
10

    
11
import com.google.common.collect.ArrayListMultimap;
12
import com.google.common.collect.Multimap;
13

    
14
import eu.dnetlib.clients.enabling.ISLookUpClient;
15
import eu.dnetlib.oai.PublisherField;
16
import eu.dnetlib.oai.info.SetInfo;
17
import eu.dnetlib.rmi.common.UnimplementedException;
18
import eu.dnetlib.rmi.enabling.ISLookUpException;
19
import eu.dnetlib.rmi.provision.MDFInfo;
20
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException;
21

    
22
/**
23
 * Instances of this class read the configuration of the OAI publisher from the IS, by performing xquery on the Exist database.
24
 *
25
 * @author alessia
26
 */
27
public class OAIConfigurationExistReader implements OAIConfigurationReader {
28

    
29
	@Autowired
30
	private ISLookUpClient lookupClient;
31

    
32
	private String xpathToIdScheme = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value = 'OAIPublisherConfigurationDSResourceType']//CONFIGURATION/IDSCHEME/text()";
33
	private String xpathToIdNamespace =
34
			"//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value = 'OAIPublisherConfigurationDSResourceType']//CONFIGURATION/IDNAMESPACE/text()";
35

    
36
	@Override
37
	public List<SetInfo> getSets() {
38

    
39
		final String query =
40
				"for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION/OAISETS/OAISET "
41
						+ "return concat($x/spec, ':-:', $x/name, ':-:', $x//description , ':-:', $x//query, ':-:', $x/@enabled/string())";
42
		try {
43
			return this.lookupClient.searchAndMapToClassByConstructor(query, SetInfo.class, ":-:");
44
		} catch (final ISLookUpException e) {
45
			throw new RuntimeException(e);
46
		}
47

    
48
	}
49

    
50
	@Override
51
	public List<SetInfo> getSets(final boolean onlyEnabled) {
52
		if (onlyEnabled) {
53
			final String query =
54
					"for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION/OAISETS/OAISET "
55
							+ "where $x/@enabled/string() = 'true' "
56
							+ "return concat($x/spec, ':-:', $x/name, ':-:', $x//description , ':-:', $x//query, ':-:', $x/@enabled/string())";
57
			try {
58
				return this.lookupClient.searchAndMapToClassByConstructor(query, SetInfo.class, ":-:");
59
			} catch (final ISLookUpException e) {
60
				throw new RuntimeException(e);
61
			}
62
		} else {
63
			return this.getSets();
64
		}
65
	}
66

    
67
	@Override
68
	public List<String> getSetSpecs() {
69
		final String query =
70
				"for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION/OAISETS/OAISET "
71
						+ "return $x/spec/string() ";
72
		try {
73
			return this.lookupClient.search(query);
74
		} catch (final ISLookUpException e) {
75
			throw new OaiPublisherRuntimeException(e);
76
		}
77

    
78
	}
79

    
80
	@Override
81
	public List<String> getSetSpecs(final boolean onlyEnabled) {
82
		final String query =
83
				"for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION/OAISETS/OAISET "
84
						+ "where $x/@enabled/string() = '" + onlyEnabled + "' " + "return $x/spec/string() ";
85
		try {
86
			return this.lookupClient.search(query);
87
		} catch (final ISLookUpException e) {
88
			throw new OaiPublisherRuntimeException(e);
89
		}
90

    
91
	}
92

    
93
	@Override
94
	public SetInfo getSetInfo(final String setSpec) {
95
		final String query =
96
				"for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION/OAISETS/OAISET "
97
						+ "where $x/spec = '"
98
						+ setSpec
99
						+ "' "
100
						+ "return concat($x/spec, ':-:', $x/name, ':-:', $x//description , ':-:', $x//query, ':-:', $x/@enabled/string())";
101

    
102
		try {
103
			final List<SetInfo> info = this.lookupClient.searchAndMapToClassByConstructor(query, SetInfo.class, ":-:");
104
			if (info != null && info.size() > 0) { return info.get(0); }
105
			return null;
106
		} catch (final Throwable e) {
107
			throw new OaiPublisherRuntimeException(e);
108
		}
109
	}
110

    
111
	@Override
112
	public List<MDFInfo> getSourceMetadataFormats() {
113
		final String query =
114
				"for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION//METADATAFORMAT "
115
						+ "return concat($x//SOURCE_METADATA_FORMAT/@name/string(), ':-:',  $x//SOURCE_METADATA_FORMAT/@layout/string(), ':-:', $x//SOURCE_METADATA_FORMAT/@interpretation/string())";
116

    
117
		List<String> res;
118
		try {
119
			res = this.lookupClient.search(query);
120
		} catch (final ISLookUpException e) {
121
			throw new OaiPublisherRuntimeException(e);
122
		}
123
		final Set<MDFInfo> sources = new HashSet<>();
124

    
125
		res.forEach(src -> {
126
			final String[] splitted = src.split(":-:");
127
			final MDFInfo mdfInfo = new MDFInfo();
128
			mdfInfo.setSourceFormatName(splitted[0]);
129
			mdfInfo.setSourceFormatLayout(splitted[1]);
130
			mdfInfo.setSourceFormatInterpretation(splitted[2]);
131
			sources.add(mdfInfo);
132
		});
133
		return sources.stream().collect(Collectors.toList());
134
	}
135

    
136
	@Override
137
	public List<MDFInfo> getMetadataFormatInfo() {
138
		final String query =
139
				"for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION//METADATAFORMAT "
140
						+ "return concat($x/@metadataPrefix/string(), ':-:', $x//SCHEMA , ':-:', $x//NAMESPACE,  "
141
						+ "':-:', $x//SOURCE_METADATA_FORMAT/@name/string(), ':-:',  $x//SOURCE_METADATA_FORMAT/@layout/string(), ':-:', $x//SOURCE_METADATA_FORMAT/@interpretation/string(), "
142
						+ "':-:', $x//BASE_QUERY, ':-:',  $x//TRANSFORMATION_RULE, ':-:', $x/@exportable/string() )";
143
		try {
144
			return this.lookupClient.searchAndMapToClassByConstructor(query, MDFInfo.class, ":-:");
145

    
146
		} catch (final Throwable e) {
147
			throw new OaiPublisherRuntimeException(e);
148
		}
149
	}
150

    
151
	@Override
152
	public List<MDFInfo> getMetadataFormatInfo(final boolean onlyEnabled) {
153
		if (onlyEnabled) {
154
			final String query =
155
					"for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION//METADATAFORMAT "
156
							+ "where $x/@exportable/string() = 'true' "
157
							+ "return concat($x/@metadataPrefix/string(), ':-:', $x//SCHEMA , ':-:', $x//NAMESPACE,  "
158
							+ "':-:', $x//SOURCE_METADATA_FORMAT/@name/string(), ':-:',  $x//SOURCE_METADATA_FORMAT/@layout/string(), ':-:', $x//SOURCE_METADATA_FORMAT/@interpretation/string(), "
159
							+ "':-:', $x//BASE_QUERY, ':-:',  $x//TRANSFORMATION_RULE, ':-:', $x/@exportable/string() )";
160
			try {
161
				return this.lookupClient.searchAndMapToClassByConstructor(query, MDFInfo.class, ":-:");
162

    
163
			} catch (final Throwable e) {
164
				throw new OaiPublisherRuntimeException(e);
165
			}
166
		} else {
167
			return getMetadataFormatInfo();
168
		}
169
	}
170

    
171
	@Override
172
	public MDFInfo getMetadataFormatInfo(final String mdPrefix) {
173
		final String query =
174
				"for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION//METADATAFORMAT "
175
						+ "where $x/@metadataPrefix/string()='"
176
						+ mdPrefix
177
						+ "' "
178
						+ "return concat($x/@metadataPrefix/string(), ':-:', $x//SCHEMA , ':-:', $x//NAMESPACE,  "
179
						+ "':-:', $x//SOURCE_METADATA_FORMAT/@name/string(), ':-:',  $x//SOURCE_METADATA_FORMAT/@layout/string(), ':-:', $x//SOURCE_METADATA_FORMAT/@interpretation/string(), "
180
						+ "':-:', $x//BASE_QUERY,':-:', $x//TRANSFORMATION_RULE, ':-:', $x/@exportable/string() )";
181
		try {
182
			final List<MDFInfo> mdfInfos = this.lookupClient.searchAndMapToClassByConstructor(query, MDFInfo.class, ":-:");
183
			if (mdfInfos != null && mdfInfos.size() > 0) { return mdfInfos.get(0); }
184
			return null;
185

    
186
		} catch (final Throwable e) {
187
			throw new OaiPublisherRuntimeException(e);
188
		}
189
	}
190

    
191
	@Override
192
	public List<PublisherField> getFields() {
193
		throw new UnimplementedException();
194
	}
195

    
196
	@Override
197
	public List<PublisherField> getFields(final String format, final String interpretation, final String layout) {
198
		final String query =
199
				"for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION//INDICES/INDEX "
200
						+ "return concat($x/@name, ':-:', $x/@repeatable, ':-:', string-join($x/SOURCE[@name='"
201
						+ format
202
						+ "' and @interpretation='"
203
						+ interpretation
204
						+ "' and @layout='" + layout + "']/@path, ':-:'))";
205
		List<String> res;
206
		try {
207
			res = this.lookupClient.search(query);
208
		} catch (final ISLookUpException e) {
209
			throw new OaiPublisherRuntimeException(e);
210
		}
211
		final List<PublisherField> fields = new ArrayList<>();
212
		for (final String index : res) {
213
			final String[] splitted = index.split(":-:");
214
			final String indexName = splitted[0];
215
			final String repeatable = splitted[1];
216
			final PublisherField field = new PublisherField();
217
			field.setFieldName(indexName);
218
			field.setRepeatable(Boolean.valueOf(repeatable));
219
			final Multimap<String, String> sources = ArrayListMultimap.create();
220
			final String mdFormat = format + "-" + layout + "-" + interpretation;
221
			for (int i = 2; i < splitted.length; i++) {
222
				sources.put(mdFormat, splitted[i]);
223
			}
224
			field.setSources(sources);
225
			fields.add(field);
226
		}
227
		return fields;
228
	}
229

    
230
	@Override
231
	public List<String> getFieldNames() {
232
		final String query =
233
				"//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value = 'OAIPublisherConfigurationDSResourceType']//CONFIGURATION//INDICES/INDEX/@name/string()";
234
		try {
235
			return this.lookupClient.search(query);
236
		} catch (final ISLookUpException e) {
237
			throw new OaiPublisherRuntimeException(e);
238
		}
239
	}
240

    
241
	@Override
242
	public List<MDFInfo> getFormatsServedBy(final String sourceFormatName, final String sourceFormatLayout, final String sourceFormatInterpretation) {
243
		final String query =
244
				"for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION//METADATAFORMAT[.//SOURCE_METADATA_FORMAT/@name = '"
245
						+ sourceFormatName
246
						+ "' and .//SOURCE_METADATA_FORMAT/@layout = '"
247
						+ sourceFormatLayout
248
						+ "' and .//SOURCE_METADATA_FORMAT/@interpretation = '"
249
						+ sourceFormatInterpretation
250
						+ "'] "
251
						+ "return concat($x/@metadataPrefix/string(), ':-:', $x//SCHEMA , ':-:', $x//NAMESPACE,  "
252
						+ "':-:', $x//SOURCE_METADATA_FORMAT/@name/string(), ':-:',  $x//SOURCE_METADATA_FORMAT/@layout/string(), ':-:', $x//SOURCE_METADATA_FORMAT/@interpretation/string(), "
253
						+ "':-:', $x//BASE_QUERY, ':-:',  $x//TRANSFORMATION_RULE, ':-:', $x/@exportable/string() )";
254

    
255
		try {
256
			return this.lookupClient.searchAndMapToClassByConstructor(query, MDFInfo.class, ":-:");
257
		} catch (final ISLookUpException e) {
258
			throw new OaiPublisherRuntimeException(e);
259
		}
260

    
261
	}
262

    
263
	@Override
264
	public String getIdScheme() {
265
		try {
266
			return this.lookupClient.getResourceProfileByQuery(getXpathToIdScheme());
267
		} catch (final ISLookUpException e) {
268
			throw new OaiPublisherRuntimeException(e);
269
		}
270
	}
271

    
272
	@Override
273
	public String getIdNamespace() {
274
		try {
275
			return this.lookupClient.getResourceProfileByQuery(getXpathToIdNamespace());
276
		} catch (final ISLookUpException e) {
277
			throw new OaiPublisherRuntimeException(e);
278
		}
279
	}
280

    
281
	public String getXpathToIdScheme() {
282
		return this.xpathToIdScheme;
283
	}
284

    
285
	public void setXpathToIdScheme(final String xpathToIdScheme) {
286
		this.xpathToIdScheme = xpathToIdScheme;
287
	}
288

    
289
	public String getXpathToIdNamespace() {
290
		return this.xpathToIdNamespace;
291
	}
292

    
293
	public void setXpathToIdNamespace(final String xpathToIdNamespace) {
294
		this.xpathToIdNamespace = xpathToIdNamespace;
295
	}
296

    
297
}
(2-2/9)