1 |
54525
|
panagiotis
|
package eu.dnetlib.repo.manager.service;
|
2 |
49868
|
panagiotis
|
|
3 |
51525
|
panagiotis
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
4 |
49878
|
antonis.le
|
import eu.dnetlib.domain.data.PiwikInfo;
|
5 |
56661
|
ioannis.di
|
import eu.dnetlib.domain.data.Repository;
|
6 |
|
|
import eu.dnetlib.repo.manager.domain.OrderByField;
|
7 |
|
|
import eu.dnetlib.repo.manager.domain.OrderByType;
|
8 |
57741
|
ioannis.di
|
import eu.dnetlib.repo.manager.domain.RepositoryServiceException;
|
9 |
50945
|
panagiotis
|
import org.apache.commons.codec.digest.DigestUtils;
|
10 |
52781
|
panagiotis
|
import org.apache.log4j.Logger;
|
11 |
49868
|
panagiotis
|
import org.springframework.beans.factory.annotation.Autowired;
|
12 |
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
13 |
51525
|
panagiotis
|
import org.springframework.beans.factory.annotation.Value;
|
14 |
50219
|
panagiotis
|
import org.springframework.dao.EmptyResultDataAccessException;
|
15 |
52781
|
panagiotis
|
import org.springframework.http.HttpStatus;
|
16 |
|
|
import org.springframework.http.ResponseEntity;
|
17 |
49868
|
panagiotis
|
import org.springframework.jdbc.core.JdbcTemplate;
|
18 |
|
|
import org.springframework.jdbc.core.RowMapper;
|
19 |
51656
|
panagiotis
|
import org.springframework.security.access.prepost.PreAuthorize;
|
20 |
56661
|
ioannis.di
|
import org.springframework.security.core.context.SecurityContextHolder;
|
21 |
54525
|
panagiotis
|
import org.springframework.stereotype.Service;
|
22 |
49868
|
panagiotis
|
|
23 |
|
|
import javax.sql.DataSource;
|
24 |
51525
|
panagiotis
|
import java.io.IOException;
|
25 |
|
|
import java.io.UnsupportedEncodingException;
|
26 |
|
|
import java.net.URL;
|
27 |
|
|
import java.net.URLEncoder;
|
28 |
49868
|
panagiotis
|
import java.sql.Types;
|
29 |
|
|
import java.util.List;
|
30 |
51525
|
panagiotis
|
import java.util.Map;
|
31 |
49868
|
panagiotis
|
|
32 |
54525
|
panagiotis
|
@Service("piwikService")
|
33 |
54690
|
panagiotis
|
public class PiWikServiceImpl implements PiWikService {
|
34 |
49868
|
panagiotis
|
|
35 |
|
|
@Autowired
|
36 |
|
|
private DataSource dataSource;
|
37 |
|
|
|
38 |
51525
|
panagiotis
|
|
39 |
|
|
@Value("${services.repomanager.analyticsURL}")
|
40 |
|
|
private String analyticsURL;
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
@Autowired
|
44 |
56661
|
ioannis.di
|
private RepositoryService repositoryService;
|
45 |
|
|
|
46 |
|
|
@Autowired
|
47 |
51525
|
panagiotis
|
@Qualifier("emailUtils")
|
48 |
56681
|
ioannis.di
|
private EmailUtils emailUtils;
|
49 |
51525
|
panagiotis
|
|
50 |
52781
|
panagiotis
|
private static final Logger LOGGER = Logger
|
51 |
54690
|
panagiotis
|
.getLogger(PiWikServiceImpl.class);
|
52 |
49868
|
panagiotis
|
|
53 |
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 = ?;";
|
54 |
49868
|
panagiotis
|
|
55 |
49898
|
panagiotis
|
private final static String INSERT_PIWIK_INFO = "insert into piwik_site (repositoryid, siteid, creationdate, requestorname, requestoremail, validated, repositoryname, country, authenticationtoken) values (?, ?, now(), ?, ?, ?, ?, ?, ?)";
|
56 |
49868
|
panagiotis
|
|
57 |
56683
|
ioannis.di
|
private final static String GET_PIWIK_SITES = "select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site ";
|
58 |
49868
|
panagiotis
|
|
59 |
56683
|
ioannis.di
|
private final static String GET_PIWIK_SITES_TOTAL = "select count(*) as totals from piwik_site ";
|
60 |
|
|
|
61 |
49931
|
panagiotis
|
private final static String APPROVE_PIWIK_SITE = "update piwik_site set validated=true, validationdate=now() where repositoryid = ?;";
|
62 |
49868
|
panagiotis
|
|
63 |
49898
|
panagiotis
|
private RowMapper<PiwikInfo> piwikRowMapper = (rs, i) -> new PiwikInfo(rs.getString("repositoryid"), getOpenaireId(rs.getString("repositoryid")), rs.getString("repositoryname"), rs.getString("country"),
|
64 |
|
|
rs.getString("siteid"), rs.getString("authenticationtoken"), rs.getTimestamp("creationdate"), rs.getString("requestorname"), rs.getString("requestoremail"),
|
65 |
|
|
rs.getBoolean("validated"), rs.getTimestamp("validationdate"), rs.getString("comment"));
|
66 |
49868
|
panagiotis
|
|
67 |
49898
|
panagiotis
|
|
68 |
49868
|
panagiotis
|
@Override
|
69 |
56661
|
ioannis.di
|
public PiwikInfo getPiwikSiteForRepo(String repositoryId) {
|
70 |
50219
|
panagiotis
|
try{
|
71 |
|
|
return new JdbcTemplate(dataSource).queryForObject(GET_PIWIK_SITE, new String[]{repositoryId}, new int[]{Types.VARCHAR}, piwikRowMapper);
|
72 |
|
|
}catch (EmptyResultDataAccessException e){
|
73 |
|
|
return null;
|
74 |
|
|
}
|
75 |
49868
|
panagiotis
|
}
|
76 |
|
|
|
77 |
|
|
@Override
|
78 |
61441
|
antonis.le
|
@PreAuthorize("hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or (hasAuthority('REGISTERED_USER') and #piwikInfo.requestorEmail == authentication.userInfo.email)")
|
79 |
56661
|
ioannis.di
|
public PiwikInfo savePiwikInfo(PiwikInfo piwikInfo) {
|
80 |
49868
|
panagiotis
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
81 |
|
|
jdbcTemplate.update(INSERT_PIWIK_INFO, new Object[]{piwikInfo.getRepositoryId(), piwikInfo.getSiteId(), piwikInfo.getRequestorName(),
|
82 |
49878
|
antonis.le
|
piwikInfo.getRequestorEmail(), piwikInfo.isValidated(), piwikInfo.getRepositoryName(), piwikInfo.getCountry(), piwikInfo.getAuthenticationToken()},
|
83 |
49868
|
panagiotis
|
new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.BOOLEAN, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR});
|
84 |
|
|
return piwikInfo;
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
@Override
|
88 |
56661
|
ioannis.di
|
public List<PiwikInfo> getPiwikSitesForRepos(OrderByField orderByField, OrderByType orderByType, int from, int quantity, String searchField) {
|
89 |
50219
|
panagiotis
|
try{
|
90 |
56661
|
ioannis.di
|
String finalizedQuery = GET_PIWIK_SITES + " where ("+
|
91 |
|
|
" repositoryid ilike ? " +
|
92 |
|
|
" or siteid ilike ?" +
|
93 |
|
|
" or requestorname ilike ?" +
|
94 |
|
|
" or requestoremail ilike ?" +
|
95 |
|
|
" or comment ilike ?" +
|
96 |
|
|
" or repositoryname ilike ?"+
|
97 |
|
|
" or country ilike ?"
|
98 |
|
|
+") order by "+orderByField + " " + orderByType + " offset ? limit ?";
|
99 |
|
|
|
100 |
|
|
return new JdbcTemplate(dataSource).query(finalizedQuery, preparedStatement -> {
|
101 |
|
|
preparedStatement.setString(1,"%"+searchField+"%");
|
102 |
|
|
preparedStatement.setString(2,"%"+searchField+"%");
|
103 |
|
|
preparedStatement.setString(3,"%"+searchField+"%");
|
104 |
|
|
preparedStatement.setString(4,"%"+searchField+"%");
|
105 |
|
|
preparedStatement.setString(5,"%"+searchField+"%");
|
106 |
|
|
preparedStatement.setString(6,"%"+searchField+"%");
|
107 |
|
|
preparedStatement.setString(7,"%"+searchField+"%");
|
108 |
|
|
preparedStatement.setInt(8,from);
|
109 |
|
|
preparedStatement.setInt(9,quantity);
|
110 |
|
|
}, piwikRowMapper);
|
111 |
50219
|
panagiotis
|
}catch (EmptyResultDataAccessException e){
|
112 |
|
|
return null;
|
113 |
|
|
}
|
114 |
|
|
|
115 |
49868
|
panagiotis
|
}
|
116 |
|
|
|
117 |
|
|
@Override
|
118 |
56828
|
ioannis.di
|
public int getPiwikSitesTotals(String searchField){
|
119 |
56683
|
ioannis.di
|
try{
|
120 |
56828
|
ioannis.di
|
String finalizedQuery = GET_PIWIK_SITES_TOTAL + " where ("+
|
121 |
|
|
" repositoryid ilike ? " +
|
122 |
|
|
" or siteid ilike ?" +
|
123 |
|
|
" or requestorname ilike ?" +
|
124 |
|
|
" or requestoremail ilike ?" +
|
125 |
|
|
" or comment ilike ?" +
|
126 |
|
|
" or repositoryname ilike ?"+
|
127 |
|
|
" or country ilike ?)";
|
128 |
|
|
|
129 |
|
|
return new JdbcTemplate(dataSource).query(finalizedQuery, preparedStatement -> {
|
130 |
|
|
preparedStatement.setString(1,"%"+searchField+"%");
|
131 |
|
|
preparedStatement.setString(2,"%"+searchField+"%");
|
132 |
|
|
preparedStatement.setString(3,"%"+searchField+"%");
|
133 |
|
|
preparedStatement.setString(4,"%"+searchField+"%");
|
134 |
|
|
preparedStatement.setString(5,"%"+searchField+"%");
|
135 |
|
|
preparedStatement.setString(6,"%"+searchField+"%");
|
136 |
|
|
preparedStatement.setString(7,"%"+searchField+"%");
|
137 |
|
|
},rowMapper -> {
|
138 |
|
|
rowMapper.next();
|
139 |
|
|
return rowMapper.getInt("totals");
|
140 |
|
|
});
|
141 |
56683
|
ioannis.di
|
}catch (EmptyResultDataAccessException e){
|
142 |
|
|
return 0;
|
143 |
|
|
}
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
@Override
|
147 |
61441
|
antonis.le
|
@PreAuthorize("hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR')")
|
148 |
56661
|
ioannis.di
|
public ResponseEntity<Object> approvePiwikSite(String repositoryId) {
|
149 |
49868
|
panagiotis
|
new JdbcTemplate(dataSource).update(APPROVE_PIWIK_SITE, new Object[] {repositoryId}, new int[] {Types.VARCHAR});
|
150 |
52781
|
panagiotis
|
return new ResponseEntity<>("OK",HttpStatus.OK);
|
151 |
49868
|
panagiotis
|
}
|
152 |
|
|
|
153 |
|
|
@Override
|
154 |
56661
|
ioannis.di
|
public String getOpenaireId(String repositoryId) {
|
155 |
50945
|
panagiotis
|
if (repositoryId != null && repositoryId.contains("::"))
|
156 |
|
|
return repositoryId.split("::")[0] + "::" + DigestUtils.md5Hex(repositoryId.split("::")[1]);
|
157 |
49868
|
panagiotis
|
return null;
|
158 |
|
|
}
|
159 |
|
|
|
160 |
51525
|
panagiotis
|
@Override
|
161 |
61441
|
antonis.le
|
@PreAuthorize("hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR')")
|
162 |
56661
|
ioannis.di
|
public ResponseEntity<Object> markPiwikSiteAsValidated(String repositoryId) throws RepositoryServiceException {
|
163 |
51525
|
panagiotis
|
try {
|
164 |
|
|
approvePiwikSite(repositoryId);
|
165 |
49868
|
panagiotis
|
|
166 |
51525
|
panagiotis
|
PiwikInfo piwikInfo = getPiwikSiteForRepo(repositoryId);
|
167 |
|
|
emailUtils.sendAdministratorMetricsEnabled(piwikInfo);
|
168 |
|
|
emailUtils.sendUserMetricsEnabled(piwikInfo);
|
169 |
|
|
|
170 |
|
|
} catch (EmptyResultDataAccessException e) {
|
171 |
|
|
LOGGER.error("Error while approving piwik site: ", e);
|
172 |
|
|
emailUtils.reportException(e);
|
173 |
|
|
throw new RepositoryServiceException("General error", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
|
174 |
|
|
} catch (Exception e) {
|
175 |
|
|
LOGGER.error("Error while sending email to administrator or user about the enabling of metrics", e);
|
176 |
|
|
emailUtils.reportException(e);
|
177 |
|
|
}
|
178 |
52781
|
panagiotis
|
return new ResponseEntity<>("OK",HttpStatus.OK);
|
179 |
51525
|
panagiotis
|
}
|
180 |
|
|
|
181 |
|
|
@Override
|
182 |
61441
|
antonis.le
|
@PreAuthorize("hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or (hasAuthority('REGISTERED_USER') and #piwikInfo.requestorEmail == authentication.userInfo.email)")
|
183 |
56661
|
ioannis.di
|
public PiwikInfo enableMetricsForRepository(String officialName,
|
184 |
|
|
String repoWebsite,
|
185 |
|
|
PiwikInfo piwikInfo) throws RepositoryServiceException {
|
186 |
51525
|
panagiotis
|
try {
|
187 |
51656
|
panagiotis
|
String URL = analyticsURL + "siteName=" + URLEncoder.encode(officialName, "UTF-8") + "&url="
|
188 |
|
|
+ URLEncoder.encode(repoWebsite, "UTF-8");
|
189 |
|
|
Map map = new ObjectMapper().readValue(new URL(URL), Map.class);
|
190 |
|
|
String siteId = null;
|
191 |
|
|
if(map.get("value")!=null) {
|
192 |
|
|
siteId = map.get("value").toString();
|
193 |
|
|
}
|
194 |
|
|
piwikInfo.setSiteId(siteId);
|
195 |
|
|
|
196 |
51525
|
panagiotis
|
savePiwikInfo(piwikInfo);
|
197 |
56661
|
ioannis.di
|
|
198 |
|
|
Repository repository = repositoryService.getRepositoryById(piwikInfo.getRepositoryId());
|
199 |
|
|
repository.setPiwikInfo(piwikInfo);
|
200 |
|
|
|
201 |
|
|
repositoryService.updateRepository(repository, SecurityContextHolder.getContext().getAuthentication());
|
202 |
|
|
|
203 |
51525
|
panagiotis
|
emailUtils.sendAdministratorRequestToEnableMetrics(piwikInfo);
|
204 |
|
|
emailUtils.sendUserRequestToEnableMetrics(piwikInfo);
|
205 |
|
|
} catch (UnsupportedEncodingException uee) {
|
206 |
|
|
LOGGER.error("Error while creating piwikScript URL", uee);
|
207 |
|
|
emailUtils.reportException(uee);
|
208 |
|
|
throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
|
209 |
|
|
} catch (IOException ioe) {
|
210 |
|
|
LOGGER.error("Error while creating piwik site", ioe);
|
211 |
|
|
emailUtils.reportException(ioe);
|
212 |
|
|
throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
|
213 |
|
|
} catch (Exception e) {
|
214 |
|
|
LOGGER.error("Error while sending email to administrator or user about the request to enable metrics", e);
|
215 |
|
|
emailUtils.reportException(e);
|
216 |
|
|
}
|
217 |
52781
|
panagiotis
|
return piwikInfo;
|
218 |
51525
|
panagiotis
|
}
|
219 |
|
|
|
220 |
|
|
|
221 |
49868
|
panagiotis
|
}
|