1
|
package eu.dnetlib.data.collector.plugins.datasets;
|
2
|
|
3
|
import java.io.BufferedReader;
|
4
|
import java.io.IOException;
|
5
|
import java.io.InputStreamReader;
|
6
|
import java.util.Iterator;
|
7
|
import java.util.Map;
|
8
|
|
9
|
import org.apache.commons.logging.Log;
|
10
|
import org.apache.commons.logging.LogFactory;
|
11
|
|
12
|
import com.google.common.collect.Maps;
|
13
|
|
14
|
/**
|
15
|
* The Class DatasetsByProjectIterator.
|
16
|
*/
|
17
|
public class DatasetsByProjectIterator implements Iterable<String>, Iterator<String> {
|
18
|
|
19
|
private static final String SPLIT_REGEX = ";";
|
20
|
|
21
|
/** The project id key. */
|
22
|
public static String PROJECT_ID_KEY = "id";
|
23
|
|
24
|
/** The project name key. */
|
25
|
public static String PROJECT_NAME_KEY = "name";
|
26
|
|
27
|
/** The project corda id key. */
|
28
|
public static String PROJECT_CORDA_ID_KEY = "corda_id";
|
29
|
|
30
|
/** The current iterator. */
|
31
|
private Iterator<String> currentIterator;
|
32
|
|
33
|
/** The csv reader. */
|
34
|
private BufferedReader csvReader;
|
35
|
|
36
|
/** The current project. */
|
37
|
private Map<String, String> currentProject;
|
38
|
|
39
|
/** The logger. */
|
40
|
private static final Log log = LogFactory.getLog(DatasetsByProjectIterator.class);
|
41
|
|
42
|
/**
|
43
|
* Instantiates a new datasets by project iterator.
|
44
|
*
|
45
|
* @param csvInputStream
|
46
|
* the csv input stream
|
47
|
* @throws IOException
|
48
|
* Signals that an I/O exception has occurred.
|
49
|
*/
|
50
|
public DatasetsByProjectIterator(final InputStreamReader csvInputStream) throws IOException {
|
51
|
this.csvReader = new BufferedReader(csvInputStream);
|
52
|
this.currentProject = extractNextLine();
|
53
|
}
|
54
|
|
55
|
/*
|
56
|
* (non-Javadoc)
|
57
|
*
|
58
|
* @see java.util.Iterator#hasNext()
|
59
|
*/
|
60
|
@Override
|
61
|
public boolean hasNext() {
|
62
|
// CASE WHEN WE REACH THE LAST ITEM ON CSV
|
63
|
// OR WE HAD SOME PROBLEM ON GET NEXT CSV ITEM
|
64
|
if (this.currentProject == null) { return false; }
|
65
|
// IN THIS CASE WE HAVE ANOTHER DATASETS
|
66
|
// FOR THE CURRENT PROJECT AND RETURN TRUE
|
67
|
if (currentIterator != null && currentIterator.hasNext()) { return true; }
|
68
|
// OTHERWISE WE FINISHED TO ITERATE THE CURRENT
|
69
|
// SETS OF DATASETS FOR A PARTICULAR PROJECT
|
70
|
// SO WE HAVE TO RETRIEVE THE NEXT ITERATOR WITH
|
71
|
// ITEMS
|
72
|
this.currentProject = extractNextLine();
|
73
|
|
74
|
while (this.currentProject != null) {
|
75
|
currentIterator = getNextIterator();
|
76
|
// IF THE NEXT ITERATOR HAS ITEMS RETURN YES
|
77
|
// OTHERWISE THE CICLE CONTINUE
|
78
|
if (currentIterator.hasNext()) { return true; }
|
79
|
this.currentProject = extractNextLine();
|
80
|
}
|
81
|
return false;
|
82
|
|
83
|
}
|
84
|
|
85
|
/*
|
86
|
* (non-Javadoc)
|
87
|
*
|
88
|
* @see java.util.Iterator#next()
|
89
|
*/
|
90
|
@Override
|
91
|
public String next() {
|
92
|
return this.currentIterator.next();
|
93
|
}
|
94
|
|
95
|
/*
|
96
|
* (non-Javadoc)
|
97
|
*
|
98
|
* @see java.util.Iterator#remove()
|
99
|
*/
|
100
|
@Override
|
101
|
public void remove() {}
|
102
|
|
103
|
/*
|
104
|
* (non-Javadoc)
|
105
|
*
|
106
|
* @see java.lang.Iterable#iterator()
|
107
|
*/
|
108
|
@Override
|
109
|
public Iterator<String> iterator() {
|
110
|
if (this.currentProject != null) {
|
111
|
currentIterator = getNextIterator();
|
112
|
return this;
|
113
|
}
|
114
|
return null;
|
115
|
|
116
|
}
|
117
|
|
118
|
private Iterator<String> getNextIterator() {
|
119
|
QueryField q = new QueryField();
|
120
|
RequestField r = new RequestField();
|
121
|
r.setQuery(q);
|
122
|
q.getTerm().put("ft-techkeyword", this.currentProject.get(PROJECT_ID_KEY));
|
123
|
return new DatasetsIterator(r, this.currentProject.get(PROJECT_CORDA_ID_KEY), null).iterator();
|
124
|
}
|
125
|
|
126
|
/**
|
127
|
* Extract next line.
|
128
|
*
|
129
|
* @return the map
|
130
|
* @throws IOException
|
131
|
* Signals that an I/O exception has occurred.
|
132
|
*/
|
133
|
private Map<String, String> extractNextLine() {
|
134
|
String line;
|
135
|
try {
|
136
|
line = this.csvReader.readLine();
|
137
|
} catch (IOException e) {
|
138
|
return null;
|
139
|
}
|
140
|
// WE REACH THE END OF THE CSV
|
141
|
if (line == null) { return null; }
|
142
|
log.debug("splitting line: " + line);
|
143
|
String[] values = line.split(SPLIT_REGEX);
|
144
|
if (values == null || values.length != 4) {
|
145
|
log.error("Error on splitting line, the length must be 4");
|
146
|
return null;
|
147
|
}
|
148
|
int id = Integer.parseInt(values[0]);
|
149
|
String project_name = values[2];
|
150
|
String cordaId = values[3];
|
151
|
Map<String, String> splittedMap = Maps.newHashMap();
|
152
|
splittedMap.put(PROJECT_CORDA_ID_KEY, cordaId);
|
153
|
splittedMap.put(PROJECT_ID_KEY, "project" + id);
|
154
|
splittedMap.put(PROJECT_NAME_KEY, project_name);
|
155
|
log.debug(String.format("found project %s with id Corda: %s and id for API: %s", project_name, cordaId, "project" + id));
|
156
|
return splittedMap;
|
157
|
}
|
158
|
}
|