Revision 56661
Added by Ioannis Diplas almost 5 years ago
PiWikServiceImpl.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import com.fasterxml.jackson.databind.ObjectMapper; |
4 | 4 |
import eu.dnetlib.domain.data.PiwikInfo; |
5 |
import eu.dnetlib.domain.data.Repository; |
|
6 |
import eu.dnetlib.repo.manager.domain.OrderByField; |
|
7 |
import eu.dnetlib.repo.manager.domain.OrderByType; |
|
5 | 8 |
import eu.dnetlib.repo.manager.shared.RepositoryServiceException; |
6 | 9 |
import org.apache.commons.codec.digest.DigestUtils; |
7 | 10 |
import org.apache.log4j.Logger; |
... | ... | |
12 | 15 |
import org.springframework.http.HttpStatus; |
13 | 16 |
import org.springframework.http.ResponseEntity; |
14 | 17 |
import org.springframework.jdbc.core.JdbcTemplate; |
18 |
import org.springframework.jdbc.core.PreparedStatementSetter; |
|
15 | 19 |
import org.springframework.jdbc.core.RowMapper; |
20 |
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; |
|
16 | 21 |
import org.springframework.security.access.prepost.PreAuthorize; |
22 |
import org.springframework.security.core.context.SecurityContextHolder; |
|
17 | 23 |
import org.springframework.stereotype.Service; |
18 | 24 |
import org.springframework.web.bind.annotation.PathVariable; |
19 | 25 |
import org.springframework.web.bind.annotation.RequestBody; |
... | ... | |
24 | 30 |
import java.io.UnsupportedEncodingException; |
25 | 31 |
import java.net.URL; |
26 | 32 |
import java.net.URLEncoder; |
33 |
import java.sql.PreparedStatement; |
|
34 |
import java.sql.SQLException; |
|
27 | 35 |
import java.sql.Types; |
36 |
import java.util.Arrays; |
|
28 | 37 |
import java.util.List; |
29 | 38 |
import java.util.Map; |
39 |
import java.util.stream.Collectors; |
|
30 | 40 |
|
31 | 41 |
@Service("piwikService") |
32 | 42 |
public class PiWikServiceImpl implements PiWikService { |
... | ... | |
41 | 51 |
|
42 | 52 |
|
43 | 53 |
@Autowired |
54 |
private RepositoryService repositoryService; |
|
55 |
|
|
56 |
@Autowired |
|
44 | 57 |
@Qualifier("emailUtils") |
45 | 58 |
EmailUtils emailUtils; |
46 | 59 |
|
... | ... | |
51 | 64 |
|
52 | 65 |
private final static String INSERT_PIWIK_INFO = "insert into piwik_site (repositoryid, siteid, creationdate, requestorname, requestoremail, validated, repositoryname, country, authenticationtoken) values (?, ?, now(), ?, ?, ?, ?, ?, ?)"; |
53 | 66 |
|
54 |
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";
|
|
67 |
private final static String GET_PIWIK_SITES = "select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site "; |
|
55 | 68 |
|
56 | 69 |
private final static String APPROVE_PIWIK_SITE = "update piwik_site set validated=true, validationdate=now() where repositoryid = ?;"; |
57 | 70 |
|
... | ... | |
63 | 76 |
|
64 | 77 |
|
65 | 78 |
@Override |
66 |
public PiwikInfo getPiwikSiteForRepo(@PathVariable("repositoryId") String repositoryId) {
|
|
79 |
public PiwikInfo getPiwikSiteForRepo(String repositoryId) { |
|
67 | 80 |
try{ |
68 | 81 |
return new JdbcTemplate(dataSource).queryForObject(GET_PIWIK_SITE, new String[]{repositoryId}, new int[]{Types.VARCHAR}, piwikRowMapper); |
69 | 82 |
}catch (EmptyResultDataAccessException e){ |
... | ... | |
73 | 86 |
|
74 | 87 |
@Override |
75 | 88 |
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (hasRole('ROLE_USER') and #piwikInfo.requestorEmail == authentication.userInfo.email)") |
76 |
public PiwikInfo savePiwikInfo(@RequestBody PiwikInfo piwikInfo) {
|
|
89 |
public PiwikInfo savePiwikInfo(PiwikInfo piwikInfo) { |
|
77 | 90 |
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); |
78 | 91 |
jdbcTemplate.update(INSERT_PIWIK_INFO, new Object[]{piwikInfo.getRepositoryId(), piwikInfo.getSiteId(), piwikInfo.getRequestorName(), |
79 | 92 |
piwikInfo.getRequestorEmail(), piwikInfo.isValidated(), piwikInfo.getRepositoryName(), piwikInfo.getCountry(), piwikInfo.getAuthenticationToken()}, |
... | ... | |
82 | 95 |
} |
83 | 96 |
|
84 | 97 |
@Override |
85 |
public List<PiwikInfo> getPiwikSitesForRepos() { |
|
98 |
public List<PiwikInfo> getPiwikSitesForRepos(OrderByField orderByField, OrderByType orderByType, int from, int quantity, String searchField) {
|
|
86 | 99 |
LOGGER.debug("Getting piwik sites for repos! "); |
87 | 100 |
try{ |
88 |
return new JdbcTemplate(dataSource).query(GET_PIWIK_SITES, piwikRowMapper); |
|
101 |
String finalizedQuery = GET_PIWIK_SITES + " where ("+ |
|
102 |
" repositoryid ilike ? " + |
|
103 |
" or siteid ilike ?" + |
|
104 |
" or requestorname ilike ?" + |
|
105 |
" or requestoremail ilike ?" + |
|
106 |
" or comment ilike ?" + |
|
107 |
" or repositoryname ilike ?"+ |
|
108 |
" or country ilike ?" |
|
109 |
+") order by "+orderByField + " " + orderByType + " offset ? limit ?"; |
|
110 |
|
|
111 |
return new JdbcTemplate(dataSource).query(finalizedQuery, preparedStatement -> { |
|
112 |
preparedStatement.setString(1,"%"+searchField+"%"); |
|
113 |
preparedStatement.setString(2,"%"+searchField+"%"); |
|
114 |
preparedStatement.setString(3,"%"+searchField+"%"); |
|
115 |
preparedStatement.setString(4,"%"+searchField+"%"); |
|
116 |
preparedStatement.setString(5,"%"+searchField+"%"); |
|
117 |
preparedStatement.setString(6,"%"+searchField+"%"); |
|
118 |
preparedStatement.setString(7,"%"+searchField+"%"); |
|
119 |
preparedStatement.setInt(8,from); |
|
120 |
preparedStatement.setInt(9,quantity); |
|
121 |
}, piwikRowMapper); |
|
89 | 122 |
}catch (EmptyResultDataAccessException e){ |
90 | 123 |
return null; |
91 | 124 |
} |
... | ... | |
94 | 127 |
|
95 | 128 |
@Override |
96 | 129 |
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN')") |
97 |
public ResponseEntity<Object> approvePiwikSite(@PathVariable("repositoryId") String repositoryId) {
|
|
130 |
public ResponseEntity<Object> approvePiwikSite(String repositoryId) { |
|
98 | 131 |
new JdbcTemplate(dataSource).update(APPROVE_PIWIK_SITE, new Object[] {repositoryId}, new int[] {Types.VARCHAR}); |
99 | 132 |
return new ResponseEntity<>("OK",HttpStatus.OK); |
100 | 133 |
} |
101 | 134 |
|
102 | 135 |
@Override |
103 |
public String getOpenaireId(@PathVariable("repositoryId") String repositoryId) {
|
|
136 |
public String getOpenaireId(String repositoryId) { |
|
104 | 137 |
if (repositoryId != null && repositoryId.contains("::")) |
105 | 138 |
return repositoryId.split("::")[0] + "::" + DigestUtils.md5Hex(repositoryId.split("::")[1]); |
106 | 139 |
return null; |
... | ... | |
108 | 141 |
|
109 | 142 |
@Override |
110 | 143 |
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN')") |
111 |
public ResponseEntity<Object> markPiwikSiteAsValidated(@PathVariable("repositoryId") String repositoryId) throws RepositoryServiceException {
|
|
144 |
public ResponseEntity<Object> markPiwikSiteAsValidated(String repositoryId) throws RepositoryServiceException { |
|
112 | 145 |
try { |
113 | 146 |
approvePiwikSite(repositoryId); |
114 | 147 |
|
... | ... | |
129 | 162 |
|
130 | 163 |
@Override |
131 | 164 |
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (hasRole('ROLE_USER') and #piwikInfo.requestorEmail == authentication.userInfo.email)") |
132 |
public PiwikInfo enableMetricsForRepository(@RequestParam("officialName") String officialName,
|
|
133 |
@RequestParam("repoWebsite") String repoWebsite,
|
|
134 |
@RequestBody PiwikInfo piwikInfo) throws RepositoryServiceException {
|
|
165 |
public PiwikInfo enableMetricsForRepository(String officialName, |
|
166 |
String repoWebsite, |
|
167 |
PiwikInfo piwikInfo) throws RepositoryServiceException { |
|
135 | 168 |
try { |
136 | 169 |
String URL = analyticsURL + "siteName=" + URLEncoder.encode(officialName, "UTF-8") + "&url=" |
137 | 170 |
+ URLEncoder.encode(repoWebsite, "UTF-8"); |
... | ... | |
143 | 176 |
piwikInfo.setSiteId(siteId); |
144 | 177 |
|
145 | 178 |
savePiwikInfo(piwikInfo); |
179 |
|
|
180 |
Repository repository = repositoryService.getRepositoryById(piwikInfo.getRepositoryId()); |
|
181 |
repository.setPiwikInfo(piwikInfo); |
|
182 |
|
|
183 |
repositoryService.updateRepository(repository, SecurityContextHolder.getContext().getAuthentication()); |
|
184 |
|
|
146 | 185 |
emailUtils.sendAdministratorRequestToEnableMetrics(piwikInfo); |
147 | 186 |
emailUtils.sendUserRequestToEnableMetrics(piwikInfo); |
148 | 187 |
} catch (UnsupportedEncodingException uee) { |
Also available in: Unified diff
- Fixing some code-smells
- Implemented search of piwik repos
- Added site_id info on repository