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