1
|
package eu.dnetlib.repo.manager.service.controllers;
|
2
|
|
3
|
import eu.dnetlib.domain.data.PiwikInfo;
|
4
|
import eu.dnetlib.utils.md5.MD5;
|
5
|
import org.springframework.beans.factory.annotation.Autowired;
|
6
|
import org.springframework.beans.factory.annotation.Qualifier;
|
7
|
import org.springframework.dao.EmptyResultDataAccessException;
|
8
|
import org.springframework.jdbc.core.JdbcTemplate;
|
9
|
import org.springframework.jdbc.core.RowMapper;
|
10
|
import org.springframework.stereotype.Component;
|
11
|
import org.springframework.web.bind.annotation.PathVariable;
|
12
|
|
13
|
import javax.annotation.PostConstruct;
|
14
|
import javax.sql.DataSource;
|
15
|
import java.security.NoSuchAlgorithmException;
|
16
|
import java.sql.ResultSet;
|
17
|
import java.sql.SQLException;
|
18
|
import java.sql.Types;
|
19
|
import java.util.List;
|
20
|
|
21
|
@Component
|
22
|
public class PiWikApiImpl implements PiWikApi{
|
23
|
|
24
|
@Autowired
|
25
|
@Qualifier("repomanager.dataSource")
|
26
|
private DataSource dataSource;
|
27
|
|
28
|
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
|
29
|
.getLogger(PiWikApiImpl.class);
|
30
|
|
31
|
private final static String GET_PIWIK_SITE = "select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site where repositoryid = ?;";
|
32
|
|
33
|
private final static String INSERT_PIWIK_INFO = "insert into piwik_site (repositoryid, siteid, creationdate, requestorname, requestoremail, validated, repositoryname, country, authenticationtoken) values (?, ?, now(), ?, ?, ?, ?, ?, ?)";
|
34
|
|
35
|
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";
|
36
|
|
37
|
private final static String APPROVE_PIWIK_SITE = "update piwik_site set validated=true, validationdate=now() where repositoryid = ?;";
|
38
|
|
39
|
|
40
|
private RowMapper<PiwikInfo> piwikRowMapper = (rs, i) -> new PiwikInfo(rs.getString("repositoryid"), getOpenaireId(rs.getString("repositoryid")), rs.getString("repositoryname"), rs.getString("country"),
|
41
|
rs.getString("siteid"), rs.getString("authenticationtoken"), rs.getTimestamp("creationdate"), rs.getString("requestorname"), rs.getString("requestoremail"),
|
42
|
rs.getBoolean("validated"), rs.getTimestamp("validationdate"), rs.getString("comment"));
|
43
|
|
44
|
|
45
|
@Override
|
46
|
public PiwikInfo getPiwikSiteForRepo(@PathVariable("repositoryId") String repositoryId) {
|
47
|
try{
|
48
|
return new JdbcTemplate(dataSource).queryForObject(GET_PIWIK_SITE, new String[]{repositoryId}, new int[]{Types.VARCHAR}, piwikRowMapper);
|
49
|
}catch (EmptyResultDataAccessException e){
|
50
|
return null;
|
51
|
}
|
52
|
}
|
53
|
|
54
|
@Override
|
55
|
public PiwikInfo savePiwikInfo(PiwikInfo piwikInfo) {
|
56
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
57
|
jdbcTemplate.update(INSERT_PIWIK_INFO, new Object[]{piwikInfo.getRepositoryId(), piwikInfo.getSiteId(), piwikInfo.getRequestorName(),
|
58
|
piwikInfo.getRequestorEmail(), piwikInfo.isValidated(), piwikInfo.getRepositoryName(), piwikInfo.getCountry(), piwikInfo.getAuthenticationToken()},
|
59
|
new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.BOOLEAN, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR});
|
60
|
return piwikInfo;
|
61
|
}
|
62
|
|
63
|
@Override
|
64
|
public List<PiwikInfo> getPiwikSitesForRepos() {
|
65
|
LOGGER.debug("Getting piwik sites for repos! ");
|
66
|
try{
|
67
|
return new JdbcTemplate(dataSource).query(GET_PIWIK_SITES, piwikRowMapper);
|
68
|
}catch (EmptyResultDataAccessException e){
|
69
|
return null;
|
70
|
}
|
71
|
|
72
|
}
|
73
|
|
74
|
@Override
|
75
|
public void approvePiwikSite(@PathVariable("repositoryId") String repositoryId) {
|
76
|
new JdbcTemplate(dataSource).update(APPROVE_PIWIK_SITE, new Object[] {repositoryId}, new int[] {Types.VARCHAR});
|
77
|
}
|
78
|
|
79
|
@Override
|
80
|
public String getOpenaireId(@PathVariable("repositoryId") String repositoryid) {
|
81
|
try {
|
82
|
if (repositoryid != null && repositoryid.contains("::"))
|
83
|
return repositoryid.split("::")[0] + "::" + MD5.encrypt2Hex(repositoryid.split("::")[1]);
|
84
|
else
|
85
|
return null;
|
86
|
} catch (NoSuchAlgorithmException e) {
|
87
|
e.printStackTrace();
|
88
|
}
|
89
|
return null;
|
90
|
}
|
91
|
|
92
|
|
93
|
}
|