1
|
package eu.dnetlib.goldoa.service.utils;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.ManagerException;
|
4
|
import eu.dnetlib.goldoa.domain.Publication;
|
5
|
import eu.dnetlib.goldoa.domain.Request;
|
6
|
import eu.dnetlib.goldoa.domain.stats.Tuple;
|
7
|
import eu.dnetlib.goldoa.service.PublicationManager;
|
8
|
import org.springframework.jdbc.core.JdbcTemplate;
|
9
|
import org.springframework.jdbc.core.RowMapper;
|
10
|
|
11
|
import javax.sql.DataSource;
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
import java.sql.Types;
|
15
|
import java.util.List;
|
16
|
|
17
|
/*
|
18
|
* Created by antleb on 3/7/16.
|
19
|
*/
|
20
|
|
21
|
public class LicenseUpdateTask implements Runnable {
|
22
|
|
23
|
private static final String GET_MISSING_LICENSES = "select pub.id, pub.doi from publication pub join request r on r.publication=pub.id where doi is not null and license is null and r.status=" + Request.RequestStatus.ACCOUNTING_PAID.getCode();
|
24
|
private static final String UPDATE_LICENSE = "update publication set license=? where id=?";
|
25
|
|
26
|
private DataSource dataSource;
|
27
|
private PublicationManager publicationManager;
|
28
|
|
29
|
public LicenseUpdateTask(DataSource dataSource, PublicationManager publicationManager) {
|
30
|
this.dataSource = dataSource;
|
31
|
this.publicationManager = publicationManager;
|
32
|
}
|
33
|
|
34
|
@Override
|
35
|
public void run() {
|
36
|
System.out.println("Updating paid request licenses!");
|
37
|
|
38
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
39
|
|
40
|
List<Tuple<String, String>> pubs = jdbcTemplate.query(GET_MISSING_LICENSES, new RowMapper<Tuple<String, String>>() {
|
41
|
@Override
|
42
|
public Tuple<String, String> mapRow(ResultSet rs, int rowNum) throws SQLException {
|
43
|
return new Tuple<String, String>(rs.getString("id"), rs.getString("doi"));
|
44
|
}
|
45
|
});
|
46
|
|
47
|
for (Tuple<String, String> pub:pubs) {
|
48
|
try {
|
49
|
Publication publication = publicationManager.resolveDOI(pub.getSecond());
|
50
|
|
51
|
if (publication.getLicense() != null) {
|
52
|
jdbcTemplate.update(UPDATE_LICENSE, new String[] {publication.getLicense(), pub.getFirst()}, new int[] {Types.VARCHAR, Types.VARCHAR});
|
53
|
}
|
54
|
} catch (ManagerException e) {
|
55
|
System.out.print("Error updating license " + e.getErrorCause());
|
56
|
} catch (Exception e) {
|
57
|
System.out.print("Error updating license " + e.getMessage());
|
58
|
}
|
59
|
}
|
60
|
}
|
61
|
}
|