Project

General

Profile

1
package eu.dnetlib.index.feed;
2

    
3
import java.util.concurrent.Callable;
4

    
5
import eu.dnetlib.clients.index.model.document.IndexDocument;
6
import eu.dnetlib.clients.index.utils.IndexFieldUtility;
7
import eu.dnetlib.index.IndexCollection;
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10

    
11
/**
12
 * The Class DocumentFeeder.
13
 */
14
public class DocumentFeeder implements Callable<FeedResult> {
15

    
16
	/**
17
	 * The Constant log.
18
	 */
19
	private static final Log log = LogFactory.getLog(DocumentFeeder.class);
20

    
21
	/**
22
	 * The index collection.
23
	 */
24
	private IndexCollection indexCollection;
25

    
26
	/**
27
	 * The records.
28
	 */
29
	private Iterable<IndexDocument> records;
30

    
31
	/**
32
	 * Instantiates a new document feeder.
33
	 *
34
	 * @param indexCollection the index collection
35
	 * @param records         the records
36
	 */
37
	public DocumentFeeder(final IndexCollection indexCollection, final Iterable<IndexDocument> records) {
38
		this.indexCollection = indexCollection;
39
		this.records = records;
40
	}
41

    
42
	/**
43
	 * {@inheritDoc}
44
	 *
45
	 * @see Callable#call()
46
	 */
47
	@Override
48
	public FeedResult call() throws Exception {
49
		final FeedResult res = new FeedResult(System.currentTimeMillis());
50
		for (IndexDocument doc : records) {
51
			boolean rsp;
52
			switch (doc.getStatus()) {
53

    
54
			case OK:
55
				rsp = indexCollection.add(doc);
56
				if (rsp) {
57
					res.add();
58
				} else {
59
					res.mark();
60
				}
61
				break;
62

    
63
			case MARKED:
64
				res.mark();
65
				log.debug("skipping record: " + doc.getFieldValue(IndexFieldUtility.INDEX_RECORD_ID));
66
				break;
67

    
68
			case ERROR:
69
				res.skip();
70
				log.info("Error on record: " + doc.getFieldValue(IndexFieldUtility.INDEX_RECORD_ID));
71
				break;
72

    
73
			default:
74
				throw new IllegalStateException("unknow document status");
75
			}
76
		}
77
		return res;
78
	}
79

    
80
}
(1-1/4)