1
|
package eu.dnetlib.msro.openaireplus.workflows.nodes.claims;
|
2
|
|
3
|
import java.util.Iterator;
|
4
|
import java.util.NoSuchElementException;
|
5
|
import java.util.Spliterator;
|
6
|
import java.util.Spliterators;
|
7
|
import java.util.stream.Stream;
|
8
|
import java.util.stream.StreamSupport;
|
9
|
import javax.annotation.PostConstruct;
|
10
|
|
11
|
import eu.dnetlib.enabling.database.DataSourceFactory;
|
12
|
import eu.dnetlib.enabling.database.utils.JdbcTemplateFactory;
|
13
|
import org.springframework.beans.factory.annotation.Autowired;
|
14
|
import org.springframework.beans.factory.annotation.Value;
|
15
|
import org.springframework.jdbc.core.JdbcTemplate;
|
16
|
import org.springframework.jdbc.support.rowset.SqlRowSet;
|
17
|
import org.springframework.stereotype.Component;
|
18
|
|
19
|
/**
|
20
|
* Created by Alessia Bardi on 23/06/2017.
|
21
|
*
|
22
|
* @author Alessia Bardi
|
23
|
*/
|
24
|
@Component
|
25
|
public class ClaimDatabaseUtils {
|
26
|
|
27
|
@Autowired
|
28
|
private JdbcTemplateFactory claimsJdbcTemplateFactory;
|
29
|
@Autowired
|
30
|
private DataSourceFactory claimsDataSourceFactory;
|
31
|
|
32
|
private JdbcTemplate jdbcTemplate;
|
33
|
|
34
|
@Value("${dnet.openaire.claims.db.name}")
|
35
|
private String dbName;
|
36
|
|
37
|
@PostConstruct
|
38
|
public void init() {
|
39
|
claimsJdbcTemplateFactory.setDataSourceFactory(claimsDataSourceFactory);
|
40
|
this.jdbcTemplate = claimsJdbcTemplateFactory.createJdbcTemplate(dbName);
|
41
|
}
|
42
|
|
43
|
public int count(final String sqlCountQuery) {
|
44
|
return jdbcTemplate.queryForObject(sqlCountQuery, Integer.class);
|
45
|
}
|
46
|
|
47
|
public Stream<Claim> query(final String sqlQuery) {
|
48
|
final SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sqlQuery);
|
49
|
return StreamSupport.stream(
|
50
|
Spliterators.spliteratorUnknownSize(
|
51
|
new Iterator<Claim>() {
|
52
|
|
53
|
@Override
|
54
|
public boolean hasNext() {
|
55
|
return !rowSet.isLast();
|
56
|
}
|
57
|
|
58
|
@Override
|
59
|
public Claim next() {
|
60
|
if (!rowSet.next()) {
|
61
|
throw new NoSuchElementException();
|
62
|
}
|
63
|
return asClaim(rowSet);
|
64
|
}
|
65
|
},
|
66
|
Spliterator.IMMUTABLE), false);
|
67
|
}
|
68
|
|
69
|
private Claim asClaim(SqlRowSet rs) {
|
70
|
return new Claim()
|
71
|
.setSemantics(rs.getString("semantics"))
|
72
|
.setSource(rs.getString("source_id")).setSourceType(rs.getString("source_type"))
|
73
|
.setTarget(rs.getString("target_id")).setTargetType(rs.getString("target_type"));
|
74
|
}
|
75
|
|
76
|
}
|