Project

General

Profile

1 49868 panagiotis
package eu.dnetlib.repo.manager.service.controllers;
2
3 49878 antonis.le
import eu.dnetlib.domain.data.PiwikInfo;
4 49868 panagiotis
import eu.dnetlib.utils.md5.MD5;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.beans.factory.annotation.Qualifier;
7 50219 panagiotis
import org.springframework.dao.EmptyResultDataAccessException;
8 49868 panagiotis
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 49898 panagiotis
import javax.annotation.PostConstruct;
14 49868 panagiotis
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 49898 panagiotis
public class PiWikApiImpl implements PiWikApi{
23 49868 panagiotis
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 49898 panagiotis
            .getLogger(PiWikApiImpl.class);
30 49868 panagiotis
31 49898 panagiotis
    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 49868 panagiotis
33 49898 panagiotis
    private final static String INSERT_PIWIK_INFO = "insert into piwik_site (repositoryid, siteid, creationdate, requestorname, requestoremail, validated, repositoryname, country, authenticationtoken) values (?, ?, now(), ?, ?, ?, ?, ?, ?)";
34 49868 panagiotis
35 49898 panagiotis
    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 49868 panagiotis
37 49931 panagiotis
    private final static String APPROVE_PIWIK_SITE = "update piwik_site set validated=true, validationdate=now() where repositoryid = ?;";
38 49868 panagiotis
39
40 49898 panagiotis
    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 49868 panagiotis
44 49898 panagiotis
45 49868 panagiotis
    @Override
46
    public PiwikInfo getPiwikSiteForRepo(@PathVariable("repositoryId") String repositoryId) {
47 50219 panagiotis
        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 49868 panagiotis
    }
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 49878 antonis.le
                        piwikInfo.getRequestorEmail(), piwikInfo.isValidated(), piwikInfo.getRepositoryName(), piwikInfo.getCountry(), piwikInfo.getAuthenticationToken()},
59 49868 panagiotis
                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 49898 panagiotis
        LOGGER.debug("Getting piwik sites for repos! ");
66 50219 panagiotis
        try{
67
            return new JdbcTemplate(dataSource).query(GET_PIWIK_SITES, piwikRowMapper);
68
        }catch (EmptyResultDataAccessException e){
69
            return null;
70
        }
71
72 49868 panagiotis
    }
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
}