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