1
|
package eu.dnetlib.repo.manager.service.controllers;
|
2
|
|
3
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
4
|
import eu.dnetlib.domain.data.PiwikInfo;
|
5
|
import eu.dnetlib.domain.data.Repository;
|
6
|
import eu.dnetlib.repo.manager.shared.RepositoryServiceException;
|
7
|
import org.apache.commons.codec.digest.DigestUtils;
|
8
|
import org.springframework.beans.factory.annotation.Autowired;
|
9
|
import org.springframework.beans.factory.annotation.Qualifier;
|
10
|
import org.springframework.beans.factory.annotation.Value;
|
11
|
import org.springframework.dao.EmptyResultDataAccessException;
|
12
|
import org.springframework.jdbc.core.JdbcTemplate;
|
13
|
import org.springframework.jdbc.core.RowMapper;
|
14
|
import org.springframework.security.access.prepost.PreAuthorize;
|
15
|
import org.springframework.stereotype.Component;
|
16
|
import org.springframework.web.bind.annotation.PathVariable;
|
17
|
import org.springframework.web.bind.annotation.RequestBody;
|
18
|
import org.springframework.web.bind.annotation.RequestParam;
|
19
|
|
20
|
import javax.sql.DataSource;
|
21
|
import java.io.IOException;
|
22
|
import java.io.UnsupportedEncodingException;
|
23
|
import java.net.URL;
|
24
|
import java.net.URLEncoder;
|
25
|
import java.sql.Types;
|
26
|
import java.util.List;
|
27
|
import java.util.Map;
|
28
|
|
29
|
@Component
|
30
|
public class PiWikApiImpl implements PiWikApi{
|
31
|
|
32
|
@Autowired
|
33
|
@Qualifier("repomanager.dataSource")
|
34
|
private DataSource dataSource;
|
35
|
|
36
|
|
37
|
@Value("${services.repomanager.analyticsURL}")
|
38
|
private String analyticsURL;
|
39
|
|
40
|
|
41
|
@Autowired
|
42
|
@Qualifier("emailUtils")
|
43
|
EmailUtils emailUtils;
|
44
|
|
45
|
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
|
46
|
.getLogger(PiWikApiImpl.class);
|
47
|
|
48
|
private final static String GET_PIWIK_SITE = "select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site where repositoryid = ?;";
|
49
|
|
50
|
private final static String INSERT_PIWIK_INFO = "insert into piwik_site (repositoryid, siteid, creationdate, requestorname, requestoremail, validated, repositoryname, country, authenticationtoken) values (?, ?, now(), ?, ?, ?, ?, ?, ?)";
|
51
|
|
52
|
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";
|
53
|
|
54
|
private final static String APPROVE_PIWIK_SITE = "update piwik_site set validated=true, validationdate=now() where repositoryid = ?;";
|
55
|
|
56
|
|
57
|
|
58
|
private RowMapper<PiwikInfo> piwikRowMapper = (rs, i) -> new PiwikInfo(rs.getString("repositoryid"), getOpenaireId(rs.getString("repositoryid")), rs.getString("repositoryname"), rs.getString("country"),
|
59
|
rs.getString("siteid"), rs.getString("authenticationtoken"), rs.getTimestamp("creationdate"), rs.getString("requestorname"), rs.getString("requestoremail"),
|
60
|
rs.getBoolean("validated"), rs.getTimestamp("validationdate"), rs.getString("comment"));
|
61
|
|
62
|
|
63
|
@Override
|
64
|
public PiwikInfo getPiwikSiteForRepo(@PathVariable("repositoryId") String repositoryId) {
|
65
|
try{
|
66
|
return new JdbcTemplate(dataSource).queryForObject(GET_PIWIK_SITE, new String[]{repositoryId}, new int[]{Types.VARCHAR}, piwikRowMapper);
|
67
|
}catch (EmptyResultDataAccessException e){
|
68
|
return null;
|
69
|
}
|
70
|
}
|
71
|
|
72
|
@Override
|
73
|
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
74
|
public PiwikInfo savePiwikInfo(@RequestBody PiwikInfo piwikInfo) {
|
75
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
76
|
jdbcTemplate.update(INSERT_PIWIK_INFO, new Object[]{piwikInfo.getRepositoryId(), piwikInfo.getSiteId(), piwikInfo.getRequestorName(),
|
77
|
piwikInfo.getRequestorEmail(), piwikInfo.isValidated(), piwikInfo.getRepositoryName(), piwikInfo.getCountry(), piwikInfo.getAuthenticationToken()},
|
78
|
new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.BOOLEAN, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR});
|
79
|
return piwikInfo;
|
80
|
}
|
81
|
|
82
|
@Override
|
83
|
public List<PiwikInfo> getPiwikSitesForRepos() {
|
84
|
LOGGER.debug("Getting piwik sites for repos! ");
|
85
|
try{
|
86
|
return new JdbcTemplate(dataSource).query(GET_PIWIK_SITES, piwikRowMapper);
|
87
|
}catch (EmptyResultDataAccessException e){
|
88
|
return null;
|
89
|
}
|
90
|
|
91
|
}
|
92
|
|
93
|
@Override
|
94
|
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
95
|
public void approvePiwikSite(@PathVariable("repositoryId") String repositoryId) {
|
96
|
new JdbcTemplate(dataSource).update(APPROVE_PIWIK_SITE, new Object[] {repositoryId}, new int[] {Types.VARCHAR});
|
97
|
}
|
98
|
|
99
|
@Override
|
100
|
public String getOpenaireId(@PathVariable("repositoryId") String repositoryId) {
|
101
|
if (repositoryId != null && repositoryId.contains("::"))
|
102
|
return repositoryId.split("::")[0] + "::" + DigestUtils.md5Hex(repositoryId.split("::")[1]);
|
103
|
return null;
|
104
|
}
|
105
|
|
106
|
@Override
|
107
|
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
108
|
public void markPiwikSiteAsValidated(@PathVariable("repositoryId") String repositoryId) throws RepositoryServiceException {
|
109
|
try {
|
110
|
approvePiwikSite(repositoryId);
|
111
|
|
112
|
PiwikInfo piwikInfo = getPiwikSiteForRepo(repositoryId);
|
113
|
emailUtils.sendAdministratorMetricsEnabled(piwikInfo);
|
114
|
emailUtils.sendUserMetricsEnabled(piwikInfo);
|
115
|
|
116
|
} catch (EmptyResultDataAccessException e) {
|
117
|
LOGGER.error("Error while approving piwik site: ", e);
|
118
|
emailUtils.reportException(e);
|
119
|
throw new RepositoryServiceException("General error", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
|
120
|
} catch (Exception e) {
|
121
|
LOGGER.error("Error while sending email to administrator or user about the enabling of metrics", e);
|
122
|
emailUtils.reportException(e);
|
123
|
}
|
124
|
}
|
125
|
|
126
|
@Override
|
127
|
@PreAuthorize("hasRole('ROLE_USER')")
|
128
|
public void enableMetricsForRepository(@RequestParam("officialName") String officialName,
|
129
|
@RequestParam("repoWebsite") String repoWebsite,
|
130
|
@RequestBody PiwikInfo piwikInfo) throws RepositoryServiceException {
|
131
|
|
132
|
try {
|
133
|
|
134
|
String URL = analyticsURL + "siteName=" + URLEncoder.encode(officialName, "UTF-8") + "&url="
|
135
|
+ URLEncoder.encode(repoWebsite, "UTF-8");
|
136
|
Map map = new ObjectMapper().readValue(new URL(URL), Map.class);
|
137
|
String siteId = null;
|
138
|
if(map.get("value")!=null) {
|
139
|
siteId = map.get("value").toString();
|
140
|
}
|
141
|
piwikInfo.setSiteId(siteId);
|
142
|
|
143
|
savePiwikInfo(piwikInfo);
|
144
|
emailUtils.sendAdministratorRequestToEnableMetrics(piwikInfo);
|
145
|
emailUtils.sendUserRequestToEnableMetrics(piwikInfo);
|
146
|
} catch (UnsupportedEncodingException uee) {
|
147
|
LOGGER.error("Error while creating piwikScript URL", uee);
|
148
|
emailUtils.reportException(uee);
|
149
|
throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
|
150
|
} catch (IOException ioe) {
|
151
|
LOGGER.error("Error while creating piwik site", ioe);
|
152
|
emailUtils.reportException(ioe);
|
153
|
throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
|
154
|
} catch (Exception e) {
|
155
|
LOGGER.error("Error while sending email to administrator or user about the request to enable metrics", e);
|
156
|
emailUtils.reportException(e);
|
157
|
}
|
158
|
}
|
159
|
|
160
|
|
161
|
}
|