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