Project

General

Profile

1 26600 sandro.lab
package eu.dnetlib.msro.workflows.resultset;
2
3
import java.util.List;
4
5
import org.apache.commons.logging.Log;
6
import org.apache.commons.logging.LogFactory;
7
8
import com.google.common.collect.Lists;
9
import com.googlecode.sarasvati.GraphProcess;
10
11
import eu.dnetlib.enabling.resultset.ResultSet;
12
import eu.dnetlib.enabling.resultset.ResultSetAware;
13
import eu.dnetlib.enabling.resultset.ResultSetListener;
14
import eu.dnetlib.enabling.resultset.rmi.ResultSetException;
15
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
16
17
public class ProcessCountingResultSetListener implements ResultSetListener, ResultSetAware {
18
	private int count;
19
20
	private GraphProcess process;
21
	private ResultSetService inputService;
22
	private String inputRsId;
23
	private ResultSet outputResulset;
24
	private int inputSize;
25
	private boolean inaccurate = false;
26
27
	/**
28
	 * Logger.
29
	 */
30
	private static final Log log = LogFactory.getLog(ProcessCountingResultSetListener.class);
31
32
	public ProcessCountingResultSetListener(final GraphProcess process, final ResultSetService inputService, final String inputRsId) throws ResultSetException {
33
		super();
34
		this.process = process;
35
		this.inputService = inputService;
36
		this.inputRsId = inputRsId;
37
		this.inputSize = inputService.getNumberOfElements(inputRsId);
38
		this.count = 0;
39
	}
40
41
	@Override
42
	public List<String> getResult(int from, int to) {
43
		if (process.isCanceled()) {
44
			this.outputResulset.close();
45
			return Lists.newArrayList();
46
		}
47
		try {
48
			this.count = to;
49
			if (count >= this.inputSize) {
50
				outputResulset.close();
51
			}
52
			return inputService.getResult(inputRsId, from, to, "WAITING");
53
		} catch (ResultSetException e) {
54
			log.error("Error fetching records from resultset: " + inputRsId);
55
			throw new RuntimeException(e);
56
		}
57
	}
58
59
	@Override
60
	public int getSize() {
61
		if (process.isCanceled()) {
62
			this.outputResulset.close();
63
			return count;
64
		}
65
		try {
66
			int size = inputService.getNumberOfElements(inputRsId);
67
			if (this.inputSize != size) {
68
				this.inputSize = size;
69
				this.inaccurate = true;
70
			}
71
			this.inputSize = size;
72
			if (count >= this.inputSize) {
73
				outputResulset.close();
74
				this.inaccurate = false;
75
			}
76
			return this.inputSize;
77
		} catch (ResultSetException e) {
78
			log.error("Error fetching records from resultset: " + inputRsId);
79
			throw new RuntimeException(e);
80
		}
81
82
	}
83
84
	@Override
85
	public void setResultSet(ResultSet outputResulset) {
86
		this.outputResulset = outputResulset;
87
	}
88
89
	public int getCount() {
90
		return count;
91
	}
92
93
	public void setCount(int count) {
94
		this.count = count;
95
	}
96
97
	public boolean isInaccurate() {
98
		return inaccurate;
99
	}
100
101
}