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