1
|
package eu.dnetlib.repo.manager.service.controllers;
|
2
|
|
3
|
import eu.dnetlib.repo.manager.shared.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.sql.DataSource;
|
13
|
import java.security.NoSuchAlgorithmException;
|
14
|
import java.sql.ResultSet;
|
15
|
import java.sql.SQLException;
|
16
|
import java.sql.Types;
|
17
|
import java.util.List;
|
18
|
|
19
|
@Component
|
20
|
public class PiWiApiImpl implements PiWiApi{
|
21
|
|
22
|
@Autowired
|
23
|
@Qualifier("repomanager.dataSource")
|
24
|
private DataSource dataSource;
|
25
|
|
26
|
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
|
27
|
.getLogger(BrokerApiImpl.class);
|
28
|
|
29
|
private final static String GET_PIWIK_SITE = "select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, approved, approvaldate, comment, repositoryname, country from piwik_site where repositoryid = ?;";
|
30
|
|
31
|
private final static String INSERT_PIWIK_INFO = "insert into piwik_site (repositoryid, siteid, creationdate, requestorname, requestoremail, approved, repositoryname, country, authenticationtoken) values (?, ?, now(), ?, ?, ?, ?, ?, ?)";
|
32
|
|
33
|
private final static String GET_PIWIK_SITES = "select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, approved, approvaldate, comment, repositoryname, country from piwik_site order by repositoryname";
|
34
|
|
35
|
private final static String APPROVE_PIWIK_SITE = "update piwik_site set approved=true, approvaldate=now() where repositoryid = ?;";
|
36
|
|
37
|
private RowMapper<PiwikInfo> piwikRowMapper = new RowMapper<PiwikInfo>() {
|
38
|
@Override
|
39
|
public PiwikInfo mapRow(ResultSet rs, int i) throws SQLException {
|
40
|
return 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("approved"), rs.getTimestamp("approvaldate"), rs.getString("comment"));
|
43
|
}
|
44
|
};
|
45
|
|
46
|
|
47
|
@Override
|
48
|
public PiwikInfo getPiwikSiteForRepo(@PathVariable("repositoryId") String repositoryId) {
|
49
|
try {
|
50
|
LOGGER.debug("URL -> " + dataSource.getConnection());
|
51
|
} catch (SQLException e) {
|
52
|
LOGGER.debug("error on connection",e);
|
53
|
}
|
54
|
return new JdbcTemplate(dataSource).queryForObject(GET_PIWIK_SITE, new String[]{repositoryId}, new int[]{Types.VARCHAR}, piwikRowMapper);
|
55
|
}
|
56
|
|
57
|
@Override
|
58
|
public PiwikInfo savePiwikInfo(PiwikInfo piwikInfo) {
|
59
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
60
|
jdbcTemplate.update(INSERT_PIWIK_INFO, new Object[]{piwikInfo.getRepositoryId(), piwikInfo.getSiteId(), piwikInfo.getRequestorName(),
|
61
|
piwikInfo.getRequestorEmail(), piwikInfo.isApproved(), piwikInfo.getRepositoryName(), piwikInfo.getCountry(), piwikInfo.getAuthenticationToken()},
|
62
|
new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.BOOLEAN, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR});
|
63
|
return piwikInfo;
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
public List<PiwikInfo> getPiwikSitesForRepos() {
|
68
|
return new JdbcTemplate(dataSource).query(GET_PIWIK_SITES, piwikRowMapper);
|
69
|
}
|
70
|
|
71
|
@Override
|
72
|
public void approvePiwikSite(@PathVariable("repositoryId") String repositoryId) {
|
73
|
new JdbcTemplate(dataSource).update(APPROVE_PIWIK_SITE, new Object[] {repositoryId}, new int[] {Types.VARCHAR});
|
74
|
}
|
75
|
|
76
|
@Override
|
77
|
public String getOpenaireId(@PathVariable("repositoryId") String repositoryid) {
|
78
|
try {
|
79
|
if (repositoryid != null && repositoryid.contains("::"))
|
80
|
return repositoryid.split("::")[0] + "::" + MD5.encrypt2Hex(repositoryid.split("::")[1]);
|
81
|
else
|
82
|
return null;
|
83
|
} catch (NoSuchAlgorithmException e) {
|
84
|
e.printStackTrace();
|
85
|
}
|
86
|
return null;
|
87
|
}
|
88
|
|
89
|
|
90
|
}
|