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.oai.publisher.OaiPublisherRuntimeException;
15
import eu.dnetlib.rmi.common.UnimplementedException;
16
import eu.dnetlib.rmi.enabling.ISLookUpException;
17
import eu.dnetlib.rmi.provision.MDFInfo;
18
import org.springframework.beans.factory.annotation.Autowired;
19

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

    
27
	@Autowired
28
	private ISLookUpClient lookupClient;
29

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

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

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

    
46
	}
47

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

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

    
74
	}
75

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

    
87
	}
88

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

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

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

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

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

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

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

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

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

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

    
182
		} catch (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 (ISLookUpException e) {
205
			throw new OaiPublisherRuntimeException(e);
206
		}
207
		List<PublisherField> fields = new ArrayList<>();
208
		for (String index : res) {
209
			String[] splitted = index.split(":-:");
210
			String indexName = splitted[0];
211
			String repeatable = splitted[1];
212
			PublisherField field = new PublisherField();
213
			field.setFieldName(indexName);
214
			field.setRepeatable(Boolean.valueOf(repeatable));
215
			Multimap<String, String> sources = ArrayListMultimap.create();
216
			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
		String query = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value = 'OAIPublisherConfigurationDSResourceType']//CONFIGURATION//INDICES/INDEX/@name/string()";
229
		try {
230
			return this.lookupClient.search(query);
231
		} catch (ISLookUpException e) {
232
			throw new OaiPublisherRuntimeException(e);
233
		}
234
	}
235

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

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

    
256
	}
257

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

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

    
276
	public String getXpathToIdScheme() {
277
		return xpathToIdScheme;
278
	}
279

    
280
	public void setXpathToIdScheme(final String xpathToIdScheme) {
281
		this.xpathToIdScheme = xpathToIdScheme;
282
	}
283

    
284
	public String getXpathToIdNamespace() {
285
		return xpathToIdNamespace;
286
	}
287

    
288
	public void setXpathToIdNamespace(final String xpathToIdNamespace) {
289
		this.xpathToIdNamespace = xpathToIdNamespace;
290
	}
291

    
292
}
(2-2/9)