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 com.google.common.collect.ArrayListMultimap;
10
import com.google.common.collect.Multimap;
11
import eu.dnetlib.clients.enabling.ISLookUpClient;
12
import eu.dnetlib.oai.PublisherField;
13
import eu.dnetlib.oai.info.SetInfo;
14
import eu.dnetlib.rmi.common.UnimplementedException;
15
import eu.dnetlib.rmi.enabling.ISLookUpException;
16
import eu.dnetlib.rmi.provision.MDFInfo;
17
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException;
18
import eu.dnetlib.utils.MetadataReference;
19
import org.springframework.beans.factory.annotation.Autowired;
20

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

    
28
	@Autowired
29
	private ISLookUpClient lookupClient;
30

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

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

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

    
47
	}
48

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

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

    
77
	}
78

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

    
90
	}
91

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

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

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

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

    
124
		res.forEach(src -> {
125
			final String[] splitted = src.split(":-:");
126
			MetadataReference mdref = new MetadataReference(splitted[0],splitted[1],splitted[2] );
127
			sources.add(mdref);
128
		});
129
		return sources.stream().collect(Collectors.toList());
130
	}
131

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

    
142
		} catch (final Throwable e) {
143
			throw new OaiPublisherRuntimeException(e);
144
		}
145
	}
146

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

    
159
			} catch (final Throwable e) {
160
				throw new OaiPublisherRuntimeException(e);
161
			}
162
		} else {
163
			return getMetadataFormatInfo();
164
		}
165
	}
166

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

    
182
		} catch (final Throwable e) {
183
			throw new OaiPublisherRuntimeException(e);
184
		}
185
	}
186

    
187
	@Override
188
	public List<PublisherField> getFields() {
189
		throw new UnimplementedException();
190
	}
191

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

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

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

    
251
		try {
252
			return this.lookupClient.searchAndMapToClassByConstructor(query, MDFInfo.class, ":-:");
253
		} catch (final ISLookUpException e) {
254
			throw new OaiPublisherRuntimeException(e);
255
		}
256

    
257
	}
258

    
259
	@Override
260
	public String getIdScheme() {
261
		try {
262
			return this.lookupClient.getResourceProfileByQuery(getXpathToIdScheme());
263
		} catch (final ISLookUpException e) {
264
			throw new OaiPublisherRuntimeException(e);
265
		}
266
	}
267

    
268
	@Override
269
	public String getIdNamespace() {
270
		try {
271
			return this.lookupClient.getResourceProfileByQuery(getXpathToIdNamespace());
272
		} catch (final ISLookUpException e) {
273
			throw new OaiPublisherRuntimeException(e);
274
		}
275
	}
276

    
277
	public ISLookUpClient getLookupClient() {
278
		return lookupClient;
279
	}
280

    
281
	public void setLookupClient(final ISLookUpClient lookupClient) {
282
		this.lookupClient = lookupClient;
283
	}
284

    
285
	public String getXpathToIdScheme() {
286
		return this.xpathToIdScheme;
287
	}
288

    
289
	public void setXpathToIdScheme(final String xpathToIdScheme) {
290
		this.xpathToIdScheme = xpathToIdScheme;
291
	}
292

    
293
	public String getXpathToIdNamespace() {
294
		return this.xpathToIdNamespace;
295
	}
296

    
297
	public void setXpathToIdNamespace(final String xpathToIdNamespace) {
298
		this.xpathToIdNamespace = xpathToIdNamespace;
299
	}
300

    
301
}
(2-2/6)