1
|
package eu.dnetlib.index.solr.feed;
|
2
|
|
3
|
import java.util.Map;
|
4
|
import java.util.function.Function;
|
5
|
|
6
|
import javax.xml.stream.XMLStreamException;
|
7
|
|
8
|
import org.springframework.beans.factory.annotation.Required;
|
9
|
|
10
|
import eu.dnetlib.clients.index.model.Any.ValueType;
|
11
|
import eu.dnetlib.clients.index.model.document.IndexDocument;
|
12
|
import eu.dnetlib.clients.index.model.document.Status;
|
13
|
import eu.dnetlib.index.feed.DocumentMapperFactory;
|
14
|
import eu.dnetlib.index.solr.model.SolrIndexDocument;
|
15
|
import eu.dnetlib.utils.MetadataReference;
|
16
|
|
17
|
/**
|
18
|
* A factory for creating SolrDocumentMapper objects.
|
19
|
*/
|
20
|
public class SolrDocumentMapperFactory implements DocumentMapperFactory {
|
21
|
|
22
|
protected static final String DNETRESULT = "result";
|
23
|
/**
|
24
|
* document factory used for the feed process.
|
25
|
*/
|
26
|
private InputDocumentFactory documentFactory;
|
27
|
|
28
|
|
29
|
private Function<String, IndexDocument> getRecordMapper(final Map<String, ValueType> schema,
|
30
|
final String dsId,
|
31
|
final String version) {
|
32
|
|
33
|
return doc -> {
|
34
|
SolrIndexDocument indexDocument = new SolrIndexDocument(schema, dsId);
|
35
|
try {
|
36
|
indexDocument.setContent(documentFactory.parseDocument(version, doc, dsId, DNETRESULT));
|
37
|
} catch (XMLStreamException e) {
|
38
|
return indexDocument.setMarked();
|
39
|
}
|
40
|
indexDocument.setStatus(Status.OK);
|
41
|
return indexDocument;
|
42
|
};
|
43
|
}
|
44
|
|
45
|
@Override
|
46
|
public Function<String, IndexDocument> getRecordMapper(final Map<String, ValueType> schema, final MetadataReference mdRef, final String dsId, final String version, final boolean emptyResult) {
|
47
|
if (emptyResult == false) return getRecordMapper(schema, dsId, version);
|
48
|
else {
|
49
|
return doc -> {
|
50
|
SolrIndexDocument indexDocument = new SolrIndexDocument(schema, dsId);
|
51
|
try {
|
52
|
|
53
|
ResultTransformer transformer = new ResultTransformer(ResultTransformer.Mode.empty) {
|
54
|
@Override
|
55
|
public String apply(String input) {
|
56
|
return input;
|
57
|
}
|
58
|
};
|
59
|
indexDocument.setContent(documentFactory.parseDocument(version, doc, dsId, DNETRESULT, transformer));
|
60
|
} catch (XMLStreamException e) {
|
61
|
return indexDocument.setMarked();
|
62
|
}
|
63
|
indexDocument.setStatus(Status.OK);
|
64
|
return indexDocument;
|
65
|
};
|
66
|
}
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* Gets the document factory.
|
71
|
*
|
72
|
* @return the documentFactory
|
73
|
*/
|
74
|
public InputDocumentFactory getDocumentFactory() {
|
75
|
return documentFactory;
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* Sets the document factory.
|
80
|
*
|
81
|
* @param documentFactory the documentFactory to set
|
82
|
*/
|
83
|
@Required
|
84
|
public void setDocumentFactory(final InputDocumentFactory documentFactory) {
|
85
|
this.documentFactory = documentFactory;
|
86
|
}
|
87
|
|
88
|
}
|