1
|
package eu.dnetlib.repo.manager.controllers;
|
2
|
|
3
|
import eu.dnetlib.domain.data.PiwikInfo;
|
4
|
import eu.dnetlib.repo.manager.domain.OrderByField;
|
5
|
import eu.dnetlib.repo.manager.domain.OrderByType;
|
6
|
import eu.dnetlib.repo.manager.domain.Paging;
|
7
|
import eu.dnetlib.repo.manager.domain.RepositoryServiceException;
|
8
|
import eu.dnetlib.repo.manager.service.PiWikServiceImpl;
|
9
|
import io.swagger.annotations.Api;
|
10
|
import io.swagger.annotations.ApiImplicitParam;
|
11
|
import io.swagger.annotations.ApiImplicitParams;
|
12
|
import org.apache.log4j.Logger;
|
13
|
import org.springframework.beans.factory.annotation.Autowired;
|
14
|
import org.springframework.core.io.FileSystemResource;
|
15
|
import org.springframework.http.MediaType;
|
16
|
import org.springframework.http.ResponseEntity;
|
17
|
import org.springframework.security.access.prepost.PreAuthorize;
|
18
|
import org.springframework.web.bind.annotation.*;
|
19
|
|
20
|
import javax.servlet.http.HttpServletRequest;
|
21
|
import javax.servlet.http.HttpServletResponse;
|
22
|
import java.io.FileNotFoundException;
|
23
|
import java.io.IOException;
|
24
|
import java.io.PrintWriter;
|
25
|
import java.nio.file.Files;
|
26
|
import java.nio.file.Path;
|
27
|
import java.text.SimpleDateFormat;
|
28
|
import java.util.Date;
|
29
|
import java.util.List;
|
30
|
|
31
|
@RestController
|
32
|
@RequestMapping(value = "/piwik")
|
33
|
@Api(description = "Piwik API", tags = {"piwik"})
|
34
|
public class PiWikController {
|
35
|
|
36
|
private static final Logger LOGGER = Logger
|
37
|
.getLogger(PiWikController.class);
|
38
|
|
39
|
@Autowired
|
40
|
private PiWikServiceImpl piWikService;
|
41
|
|
42
|
|
43
|
@RequestMapping(value = "/getPiwikSiteForRepo/{repositoryId}" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
|
44
|
@ResponseBody
|
45
|
@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN') or @authorizationService.isMemberOf(#repositoryId) or (@repositoryService.getRepositoryById(#repositoryId).registeredBy=='null' and hasRole('ROLE_USER'))")
|
46
|
public PiwikInfo getPiwikSiteForRepo(@PathVariable("repositoryId") String repositoryId) {
|
47
|
return piWikService.getPiwikSiteForRepo(repositoryId);
|
48
|
}
|
49
|
|
50
|
@RequestMapping(value = "/savePiwikInfo" , method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
|
51
|
@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN') or @authorizationService.isMemberOf(#piwikInfo.repositoryId) or (@repositoryService.getRepositoryById(#piwikInfo.repositoryId).registeredBy=='null' and hasRole('ROLE_USER'))")
|
52
|
public PiwikInfo savePiwikInfo(@RequestBody PiwikInfo piwikInfo) {
|
53
|
return piWikService.savePiwikInfo(piwikInfo);
|
54
|
}
|
55
|
|
56
|
@RequestMapping(value = "/getPiwikSitesForRepos" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
|
57
|
@ApiImplicitParams({
|
58
|
@ApiImplicitParam(name = "from", dataType = "number", paramType = "query"),
|
59
|
@ApiImplicitParam(name = "quantity", dataType = "number", paramType = "query"),
|
60
|
@ApiImplicitParam(name = "order", dataType = "eu.dnetlib.repo.manager.domain.OrderByType", paramType = "query"),
|
61
|
@ApiImplicitParam(name = "orderField", dataType = "eu.dnetlib.repo.manager.domain.OrderByField", paramType = "query"),
|
62
|
@ApiImplicitParam(name = "searchField", dataType = "string", paramType = "query"),
|
63
|
})
|
64
|
public Paging<PiwikInfo> getPiwikSitesForRepos(
|
65
|
@RequestParam(value = "from",required=false,defaultValue = "0") int from,
|
66
|
@RequestParam(value = "quantity",required=false,defaultValue = "100") int quantity,
|
67
|
@RequestParam(value = "order",required=false,defaultValue = "DSC") OrderByType orderType,
|
68
|
@RequestParam(value = "orderField", required = false, defaultValue = "REPOSITORY_NAME") OrderByField orderField,
|
69
|
@RequestParam(value = "searchField", required = false, defaultValue = "") String searchField
|
70
|
|
71
|
){
|
72
|
Paging<PiwikInfo> results = new Paging<>();
|
73
|
List<PiwikInfo> returning = piWikService.getPiwikSitesForRepos(orderField,orderType,from,quantity,searchField);
|
74
|
results.setFrom(from);
|
75
|
results.setTo(from + returning.size());
|
76
|
results.setTotal(piWikService.getPiwikSitesTotals(searchField));
|
77
|
results.setResults(returning);
|
78
|
return results;
|
79
|
}
|
80
|
@ApiImplicitParams({
|
81
|
@ApiImplicitParam(name = "from", dataType = "number", paramType = "query"),
|
82
|
@ApiImplicitParam(name = "quantity", dataType = "number", paramType = "query"),
|
83
|
@ApiImplicitParam(name = "order", dataType = "eu.dnetlib.repo.manager.domain.OrderByType", paramType = "query"),
|
84
|
@ApiImplicitParam(name = "searchField", dataType = "eu.dnetlib.repo.manager.domain.OrderByField", paramType = "query"),
|
85
|
@ApiImplicitParam(name = "orderField", dataType = "string", paramType = "query"),
|
86
|
})
|
87
|
@RequestMapping(value = "/getPiwikSitesForRepos/csv" , method = RequestMethod.GET,produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
|
88
|
@ResponseBody
|
89
|
public FileSystemResource getPiwikSitesForReposToCsv(
|
90
|
@RequestParam(value = "from",required=false,defaultValue = "0") int from,
|
91
|
@RequestParam(value = "quantity",required=false,defaultValue = "10000") int quantity,
|
92
|
@RequestParam(value = "order",required=false,defaultValue = "DSC") OrderByType orderType,
|
93
|
@RequestParam(value = "orderField", required = false, defaultValue = "REPOSITORY_NAME") OrderByField orderField,
|
94
|
@RequestParam(value = "searchField", required = false, defaultValue = "") String searchField,
|
95
|
HttpServletResponse response,
|
96
|
HttpServletRequest request
|
97
|
) throws IOException {
|
98
|
|
99
|
Path p = Files.createTempFile("exportingCsv-", new Date().toString());
|
100
|
List<PiwikInfo> returning = piWikService.getPiwikSitesForRepos(orderField,orderType,0,10000,searchField);
|
101
|
try (PrintWriter writer = new PrintWriter(p.toFile())) {
|
102
|
|
103
|
StringBuilder sb = new StringBuilder();
|
104
|
sb.append(" Repository ID , Repository name, Country, Site ID, Authentication token, Creation date, Requestor full name, Requestor email, Validated, Validation date, Comment \n");
|
105
|
|
106
|
for(PiwikInfo piwikInfo : returning){
|
107
|
sb.append(
|
108
|
(piwikInfo.getRepositoryId() == null ? "," : piwikInfo.getRepositoryId()+ ",")+
|
109
|
(piwikInfo.getRepositoryName() == null ? "," : piwikInfo.getRepositoryName()+ ",")+
|
110
|
(piwikInfo.getCountry() == null ? "," : piwikInfo.getCountry()+ ",")+
|
111
|
(piwikInfo.getSiteId() == null ? "," : piwikInfo.getSiteId()+ ",") +
|
112
|
(piwikInfo.getAuthenticationToken() == null ? "," : piwikInfo.getAuthenticationToken()+ ",")+
|
113
|
(piwikInfo.getCreationDate() == null ? "," : piwikInfo.getCreationDate().toString()+ ",") +
|
114
|
(piwikInfo.getRequestorName() == null ? "," : piwikInfo.getRequestorName()+ ",") +
|
115
|
(piwikInfo.getRequestorEmail() == null ? "," : piwikInfo.getRequestorEmail()+ ",")+
|
116
|
piwikInfo.isValidated() + "," +
|
117
|
(piwikInfo.getValidationDate() == null ? "," : piwikInfo.getValidationDate().toString()+ ",") +
|
118
|
(piwikInfo.getComment() == null ? "\n" : piwikInfo.getComment()+ "\n")
|
119
|
|
120
|
);
|
121
|
}
|
122
|
writer.write(sb.toString());
|
123
|
|
124
|
} catch (FileNotFoundException e) {
|
125
|
LOGGER.error(e.getMessage());
|
126
|
}
|
127
|
|
128
|
|
129
|
String mimeType = request.getServletContext().getMimeType(p.toFile().getAbsolutePath());
|
130
|
if (mimeType == null) {
|
131
|
mimeType = "application/octet-stream";
|
132
|
}
|
133
|
response.setContentType(mimeType);
|
134
|
response.setContentLength((int) p.toFile().length());
|
135
|
|
136
|
|
137
|
String headerKey = "Content-Disposition";
|
138
|
SimpleDateFormat sdfDate = new SimpleDateFormat("ddMMyyyy");//dd/MM/yyyy
|
139
|
Date now = new Date();
|
140
|
String strDate = sdfDate.format(now);
|
141
|
String headerValue = String.format("attachment; filename=\"csv-%s.csv\"",
|
142
|
strDate);
|
143
|
response.setHeader(headerKey, headerValue);
|
144
|
|
145
|
|
146
|
return new FileSystemResource(p.toFile());
|
147
|
|
148
|
}
|
149
|
|
150
|
|
151
|
@RequestMapping(value = "/approvePiwikSite/{repositoryId}" , method = RequestMethod.GET)
|
152
|
@ResponseBody
|
153
|
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN')")
|
154
|
public ResponseEntity<Object> approvePiwikSite(@PathVariable("repositoryId") String repositoryId) {
|
155
|
return piWikService.approvePiwikSite(repositoryId);
|
156
|
}
|
157
|
|
158
|
@RequestMapping(value = "/getOpenaireId/{repositoryId}" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
|
159
|
@ResponseBody
|
160
|
@PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN') or @authorizationService.isMemberOf(#repositoryId) or (@repositoryService.getRepositoryById(#repositoryId).registeredBy=='null' and hasRole('ROLE_USER'))")
|
161
|
public String getOpenaireId(@PathVariable("repositoryId") String repositoryId){
|
162
|
return piWikService.getOpenaireId(repositoryId);
|
163
|
}
|
164
|
|
165
|
@RequestMapping(value = "/markPiwikSiteAsValidated/{repositoryId}" , method = RequestMethod.POST,
|
166
|
consumes = MediaType.APPLICATION_JSON_VALUE)
|
167
|
@ResponseBody
|
168
|
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN')")
|
169
|
public ResponseEntity<Object> markPiwikSiteAsValidated(@PathVariable("repositoryId") String repositoryId) throws RepositoryServiceException {
|
170
|
return piWikService.markPiwikSiteAsValidated(repositoryId);
|
171
|
}
|
172
|
|
173
|
@RequestMapping(value = "/enableMetricsForRepository", method = RequestMethod.POST,
|
174
|
consumes = MediaType.APPLICATION_JSON_VALUE)
|
175
|
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (hasRole('ROLE_USER') and #piwikInfo.requestorEmail == authentication.userInfo.email)")
|
176
|
public PiwikInfo enableMetricsForRepository(@RequestParam("officialName") String officialName,
|
177
|
@RequestParam("repoWebsite") String repoWebsite,
|
178
|
@RequestBody PiwikInfo piwikInfo) throws RepositoryServiceException {
|
179
|
return piWikService.enableMetricsForRepository(officialName, repoWebsite, piwikInfo);
|
180
|
}
|
181
|
|
182
|
|
183
|
}
|