Revision 49879
Added by Antonis Lempesis over 6 years ago
PiwikDAO.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.repo.manager.server.services; |
2 | 2 |
|
3 | 3 |
import eu.dnetlib.domain.data.PiwikInfo; |
4 |
import org.apache.commons.codec.digest.DigestUtils; |
|
4 |
import eu.dnetlib.repo.manager.service.controllers.PiWiApi; |
|
5 |
import eu.dnetlib.utils.md5.MD5; |
|
5 | 6 |
import org.springframework.beans.factory.annotation.Autowired; |
6 | 7 |
import org.springframework.beans.factory.annotation.Qualifier; |
7 | 8 |
import org.springframework.jdbc.core.JdbcTemplate; |
... | ... | |
9 | 10 |
import org.springframework.stereotype.Component; |
10 | 11 |
|
11 | 12 |
import javax.sql.DataSource; |
13 |
import java.security.NoSuchAlgorithmException; |
|
12 | 14 |
import java.sql.ResultSet; |
13 | 15 |
import java.sql.SQLException; |
14 | 16 |
import java.sql.Types; |
... | ... | |
20 | 22 |
@Component(value = "piwikDAO") |
21 | 23 |
public class PiwikDAO { |
22 | 24 |
|
25 |
|
|
23 | 26 |
@Autowired |
24 |
@Qualifier("repomanager.dataSource") |
|
25 |
private DataSource dataSource; |
|
27 |
private PiWiApi piWiApi; |
|
26 | 28 |
|
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 | 29 |
public PiwikInfo getPiwikSiteForRepo(String repositoryId) { |
45 |
return new JdbcTemplate(dataSource).queryForObject(GET_PIWIK_SITE, new String[]{repositoryId}, new int[]{Types.VARCHAR}, piwikRowMapper);
|
|
30 |
return piWiApi.getPiwikSiteForRepo(repositoryId);
|
|
46 | 31 |
} |
47 | 32 |
|
48 | 33 |
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; |
|
34 |
return piWiApi.savePiwikInfo(piwikInfo); |
|
54 | 35 |
} |
55 | 36 |
|
56 | 37 |
public List<PiwikInfo> getPiwikSitesForRepos() { |
57 |
return new JdbcTemplate(dataSource).query(GET_PIWIK_SITES, piwikRowMapper);
|
|
38 |
return piWiApi.getPiwikSitesForRepos();
|
|
58 | 39 |
} |
59 | 40 |
|
60 | 41 |
public void markPiwikSiteAsValidated(String repositoryId) { |
61 |
new JdbcTemplate(dataSource).update(VALIDATE_PIWIK_SITE, new Object[] {repositoryId}, new int[] {Types.VARCHAR});
|
|
42 |
piWiApi.approvePiwikSite(repositoryId);
|
|
62 | 43 |
} |
63 | 44 |
|
64 | 45 |
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; |
|
46 |
return piWiApi.getOpenaireId(repositoryid); |
|
71 | 47 |
} |
72 | 48 |
} |
Also available in: Unified diff
merged newAPI branch