1
|
package eu.dnetlib.repo.manager.server.services;
|
2
|
|
3
|
import eu.dnetlib.domain.data.PiwikInfo;
|
4
|
import org.apache.commons.codec.digest.DigestUtils;
|
5
|
import org.springframework.beans.factory.annotation.Autowired;
|
6
|
import org.springframework.beans.factory.annotation.Qualifier;
|
7
|
import org.springframework.jdbc.core.JdbcTemplate;
|
8
|
import org.springframework.jdbc.core.RowMapper;
|
9
|
import org.springframework.stereotype.Component;
|
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 stefania on 12/19/16.
|
19
|
*/
|
20
|
@Component(value = "piwikDAO")
|
21
|
public class PiwikDAO {
|
22
|
|
23
|
@Autowired
|
24
|
@Qualifier("repomanager.dataSource")
|
25
|
private DataSource dataSource;
|
26
|
|
27
|
private final static String GET_PIWIK_SITE = "select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site where repositoryid = ?;";
|
28
|
|
29
|
private final static String INSERT_PIWIK_INFO = "insert into piwik_site (repositoryid, siteid, creationdate, requestorname, requestoremail, validated, repositoryname, country, authenticationtoken) values (?, ?, now(), ?, ?, ?, ?, ?, ?)";
|
30
|
|
31
|
private final static String GET_PIWIK_SITES = "select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site order by repositoryname";
|
32
|
|
33
|
private final static String VALIDATE_PIWIK_SITE = "update piwik_site set validated=true, validationdate=now() where repositoryid = ?;";
|
34
|
|
35
|
private RowMapper<PiwikInfo> piwikRowMapper = new RowMapper<PiwikInfo>() {
|
36
|
@Override
|
37
|
public PiwikInfo mapRow(ResultSet rs, int i) throws SQLException {
|
38
|
return new PiwikInfo(rs.getString("repositoryid"), getOpenaireId(rs.getString("repositoryid")), rs.getString("repositoryname"), rs.getString("country"),
|
39
|
rs.getString("siteid"), rs.getString("authenticationtoken"), rs.getTimestamp("creationdate"), rs.getString("requestorname"), rs.getString("requestoremail"),
|
40
|
rs.getBoolean("validated"), rs.getTimestamp("validationdate"), rs.getString("comment"));
|
41
|
}
|
42
|
};
|
43
|
|
44
|
public PiwikInfo getPiwikSiteForRepo(String repositoryId) {
|
45
|
return new JdbcTemplate(dataSource).queryForObject(GET_PIWIK_SITE, new String[]{repositoryId}, new int[]{Types.VARCHAR}, piwikRowMapper);
|
46
|
}
|
47
|
|
48
|
public PiwikInfo savePiwikInfo(PiwikInfo piwikInfo) {
|
49
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
50
|
jdbcTemplate.update(INSERT_PIWIK_INFO, new Object[]{piwikInfo.getRepositoryId(), piwikInfo.getSiteId(), piwikInfo.getRequestorName(),
|
51
|
piwikInfo.getRequestorEmail(), piwikInfo.isValidated(), piwikInfo.getRepositoryName(), piwikInfo.getCountry(), piwikInfo.getAuthenticationToken()},
|
52
|
new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.BOOLEAN, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR});
|
53
|
return piwikInfo;
|
54
|
}
|
55
|
|
56
|
public List<PiwikInfo> getPiwikSitesForRepos() {
|
57
|
return new JdbcTemplate(dataSource).query(GET_PIWIK_SITES, piwikRowMapper);
|
58
|
}
|
59
|
|
60
|
public void markPiwikSiteAsValidated(String repositoryId) {
|
61
|
new JdbcTemplate(dataSource).update(VALIDATE_PIWIK_SITE, new Object[] {repositoryId}, new int[] {Types.VARCHAR});
|
62
|
}
|
63
|
|
64
|
private String getOpenaireId(String repositoryid) {
|
65
|
|
66
|
if (repositoryid != null && repositoryid.contains("::")) {
|
67
|
return repositoryid.split("::")[0] + "::" + DigestUtils.md5Hex(repositoryid.split("::")[1]);
|
68
|
}
|
69
|
|
70
|
return null;
|
71
|
}
|
72
|
}
|