1
|
package eu.dnetlib.dli.collector.plugin;
|
2
|
|
3
|
import eu.dnetlib.dli.resolver.model.serializer.DLIResolverSerializer;
|
4
|
import eu.dnetlib.rmi.data.CollectorServiceException;
|
5
|
import eu.dnetlib.rmi.data.InterfaceDescriptor;
|
6
|
import eu.dnetlib.rmi.data.plugin.AbstractCollectorPlugin;
|
7
|
import eu.dnetlib.rmi.data.plugin.CollectorPlugin;
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
11
|
|
12
|
import java.nio.file.DirectoryStream;
|
13
|
import java.nio.file.FileSystems;
|
14
|
import java.nio.file.Files;
|
15
|
import java.nio.file.Path;
|
16
|
import java.util.Iterator;
|
17
|
import java.util.Objects;
|
18
|
|
19
|
|
20
|
class ScholixIterator implements Iterator<String> {
|
21
|
private static final Log log = LogFactory.getLog(ScholixIterator.class);
|
22
|
|
23
|
private final Iterator<Path> pathIterator;
|
24
|
private Iterator<String> currentIterator;
|
25
|
private String nextItem;
|
26
|
private final DLIResolverSerializer serializer;
|
27
|
|
28
|
private final static String template ="<record xmlns:oaf=\"http://namespace.dnet.eu/oaf\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">%s</record>";
|
29
|
|
30
|
|
31
|
|
32
|
public ScholixIterator(DirectoryStream<Path> pathIterator,DLIResolverSerializer serializer ) {
|
33
|
this.pathIterator = pathIterator.iterator();
|
34
|
this.serializer = serializer;
|
35
|
nextItem =generateNextItem();
|
36
|
|
37
|
}
|
38
|
|
39
|
|
40
|
private String generateNextItem() {
|
41
|
if (currentIterator== null || !currentIterator.hasNext()) {
|
42
|
getNextIterator();
|
43
|
if (currentIterator!= null && currentIterator.hasNext())
|
44
|
return currentIterator.next();
|
45
|
return null;
|
46
|
} else {
|
47
|
return currentIterator.next();
|
48
|
}
|
49
|
}
|
50
|
|
51
|
|
52
|
private void getNextIterator() {
|
53
|
if (pathIterator.hasNext()) {
|
54
|
try {
|
55
|
final Path nextPath = pathIterator.next();
|
56
|
log.debug("Request Next Iterator NextPath :"+nextPath.getFileName());
|
57
|
currentIterator = Files.lines(nextPath).map(ScholixJSONParser::parse).filter(Objects::nonNull).map(serializer::serializeToXML).map(it->String.format(template,it)).iterator();
|
58
|
} catch (Throwable e) {
|
59
|
throw new RuntimeException("Error on generating next Iterator ",e);
|
60
|
}
|
61
|
}
|
62
|
}
|
63
|
|
64
|
@Override
|
65
|
public boolean hasNext() {
|
66
|
return nextItem!= null;
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
public String next() {
|
71
|
final String tmp = nextItem;
|
72
|
nextItem = generateNextItem();
|
73
|
return tmp;
|
74
|
|
75
|
|
76
|
}
|
77
|
}
|
78
|
|
79
|
public class ScholixFromFSCollectorPlugin extends AbstractCollectorPlugin implements CollectorPlugin {
|
80
|
private static final Log log = LogFactory.getLog(ScholixFromFSCollectorPlugin.class);
|
81
|
|
82
|
@Autowired
|
83
|
private DLIResolverSerializer serializer;
|
84
|
|
85
|
@Override
|
86
|
public Iterable<String> collect(InterfaceDescriptor interfaceDescriptor, String fromDate, String untilDate) throws CollectorServiceException {
|
87
|
try {
|
88
|
final String baseUrl = interfaceDescriptor.getBaseUrl();
|
89
|
final Path dir = FileSystems.getDefault().getPath(baseUrl);
|
90
|
final DirectoryStream<Path> pathIterator = Files.newDirectoryStream(dir, "*json"); ;
|
91
|
return () -> new ScholixIterator(pathIterator, serializer);
|
92
|
} catch (Throwable e) {
|
93
|
throw new CollectorServiceException(e);
|
94
|
}
|
95
|
}
|
96
|
|
97
|
public DLIResolverSerializer getSerializer() {
|
98
|
return serializer;
|
99
|
}
|
100
|
|
101
|
public void setSerializer(DLIResolverSerializer serializer) {
|
102
|
this.serializer = serializer;
|
103
|
}
|
104
|
}
|