1
|
package eu.dnetlib.data.collector.plugins.oai;
|
2
|
|
3
|
import java.util.Iterator;
|
4
|
import java.util.List;
|
5
|
|
6
|
import org.springframework.beans.factory.annotation.Required;
|
7
|
|
8
|
import com.google.common.base.Function;
|
9
|
import com.google.common.base.Splitter;
|
10
|
import com.google.common.collect.Iterables;
|
11
|
import com.google.common.collect.Iterators;
|
12
|
import com.google.common.collect.Lists;
|
13
|
|
14
|
import eu.dnetlib.data.collector.plugin.AbstractCollectorPlugin;
|
15
|
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
|
16
|
import eu.dnetlib.data.collector.rmi.InterfaceDescriptor;
|
17
|
|
18
|
public class OaiCollectorPlugin extends AbstractCollectorPlugin {
|
19
|
|
20
|
private static final String FORMAT_PARAM = "format";
|
21
|
private static final String OAI_SET_PARAM = "set";
|
22
|
|
23
|
private OaiIteratorFactory oaiIteratorFactory;
|
24
|
|
25
|
@Override
|
26
|
public Iterable<String> collect(final InterfaceDescriptor interfaceDescriptor, final String fromDate, final String untilDate)
|
27
|
throws CollectorServiceException {
|
28
|
final String baseUrl = interfaceDescriptor.getBaseUrl();
|
29
|
final String mdFormat = interfaceDescriptor.getParams().get(FORMAT_PARAM);
|
30
|
final String setParam = interfaceDescriptor.getParams().get(OAI_SET_PARAM);
|
31
|
final List<String> sets = Lists.newArrayList();
|
32
|
if (setParam != null) {
|
33
|
sets.addAll(Lists.newArrayList(Splitter.on(",").omitEmptyStrings().trimResults().split(setParam)));
|
34
|
}
|
35
|
if (sets.isEmpty()) {
|
36
|
// If no set is defined, ALL the sets must be harvested
|
37
|
sets.add("");
|
38
|
}
|
39
|
|
40
|
if (baseUrl == null || baseUrl.isEmpty()) { throw new CollectorServiceException("Param 'baseurl' is null or empty"); }
|
41
|
|
42
|
if (mdFormat == null || mdFormat.isEmpty()) { throw new CollectorServiceException("Param 'mdFormat' is null or empty"); }
|
43
|
|
44
|
if (fromDate != null && !fromDate.matches("\\d{4}-\\d{2}-\\d{2}")) { throw new CollectorServiceException("Invalid date (YYYY-MM-DD): " + fromDate); }
|
45
|
|
46
|
if (untilDate != null && !untilDate.matches("\\d{4}-\\d{2}-\\d{2}")) { throw new CollectorServiceException("Invalid date (YYYY-MM-DD): " + untilDate); }
|
47
|
|
48
|
return new Iterable<String>() {
|
49
|
|
50
|
@SuppressWarnings("unchecked")
|
51
|
@Override
|
52
|
public Iterator<String> iterator() {
|
53
|
final Iterable<Iterator<String>> iter = Iterables.transform(sets, new Function<String, Iterator<String>>() {
|
54
|
|
55
|
@Override
|
56
|
public Iterator<String> apply(final String set) {
|
57
|
return oaiIteratorFactory.newIterator(baseUrl, mdFormat, set, fromDate, untilDate);
|
58
|
}
|
59
|
});
|
60
|
return Iterators.concat(Iterables.toArray(iter, Iterator.class));
|
61
|
}
|
62
|
};
|
63
|
}
|
64
|
|
65
|
public OaiIteratorFactory getOaiIteratorFactory() {
|
66
|
return oaiIteratorFactory;
|
67
|
}
|
68
|
|
69
|
@Required
|
70
|
public void setOaiIteratorFactory(final OaiIteratorFactory oaiIteratorFactory) {
|
71
|
this.oaiIteratorFactory = oaiIteratorFactory;
|
72
|
}
|
73
|
|
74
|
}
|