Revision 60748
Added by Antonis Koulalis about 3 years ago
PiWikController.java | ||
---|---|---|
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 eu.dnetlib.repo.manager.service.RepositoryService; |
|
10 |
import io.swagger.annotations.Api; |
|
11 |
import io.swagger.annotations.ApiImplicitParam; |
|
12 |
import io.swagger.annotations.ApiImplicitParams; |
|
13 |
import org.apache.log4j.Logger; |
|
14 |
import org.springframework.beans.factory.annotation.Autowired; |
|
15 |
import org.springframework.core.io.FileSystemResource; |
|
16 |
import org.springframework.http.MediaType; |
|
17 |
import org.springframework.http.ResponseEntity; |
|
18 |
import org.springframework.security.access.prepost.PreAuthorize; |
|
19 |
import org.springframework.web.bind.annotation.*; |
|
20 |
|
|
21 |
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 |
import java.util.List; |
|
31 |
|
|
32 |
@RestController |
|
33 |
@RequestMapping(value = "/piwik") |
|
34 |
@Api(description = "Piwik API", tags = {"piwik"}) |
|
35 |
public class PiWikController { |
|
36 |
|
|
37 |
private static final Logger LOGGER = Logger |
|
38 |
.getLogger(PiWikController.class); |
|
39 |
|
|
40 |
@Autowired |
|
41 |
private PiWikServiceImpl piWikService; |
|
42 |
|
|
43 |
@Autowired |
|
44 |
private RepositoryService repositoryService; |
|
45 |
|
|
46 |
// TODO: Antonis K. - replace here the registeredBy |
|
47 |
|
|
48 |
@RequestMapping(value = "/getPiwikSiteForRepo/{repositoryId}" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE) |
|
49 |
@ResponseBody |
|
50 |
@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 |
public PiwikInfo getPiwikSiteForRepo(@PathVariable("repositoryId") String repositoryId) { |
|
52 |
return piWikService.getPiwikSiteForRepo(repositoryId); |
|
53 |
} |
|
54 |
|
|
55 |
@RequestMapping(value = "/savePiwikInfo" , method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE) |
|
56 |
@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 |
public PiwikInfo savePiwikInfo(@RequestBody PiwikInfo piwikInfo) { |
|
58 |
return piWikService.savePiwikInfo(piwikInfo); |
|
59 |
} |
|
60 |
|
|
61 |
@RequestMapping(value = "/getPiwikSitesForRepos" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE) |
|
62 |
@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 |
@ApiImplicitParam(name = "orderField", dataType = "eu.dnetlib.repo.manager.domain.OrderByField", paramType = "query"), |
|
67 |
@ApiImplicitParam(name = "searchField", dataType = "string", paramType = "query"), |
|
68 |
}) |
|
69 |
public Paging<PiwikInfo> getPiwikSitesForRepos( |
|
70 |
@RequestParam(value = "from",required=false,defaultValue = "0") int from, |
|
71 |
@RequestParam(value = "quantity",required=false,defaultValue = "100") int quantity, |
|
72 |
@RequestParam(value = "order",required=false,defaultValue = "DSC") OrderByType orderType, |
|
73 |
@RequestParam(value = "orderField", required = false, defaultValue = "REPOSITORY_NAME") OrderByField orderField, |
|
74 |
@RequestParam(value = "searchField", required = false, defaultValue = "") String searchField |
|
75 |
|
|
76 |
){ |
|
77 |
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 |
results.setTotal(piWikService.getPiwikSitesTotals(searchField)); |
|
82 |
results.setResults(returning); |
|
83 |
return results; |
|
84 |
} |
|
85 |
@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 |
@RequestParam(value = "from",required=false,defaultValue = "0") int from, |
|
96 |
@RequestParam(value = "quantity",required=false,defaultValue = "10000") int quantity, |
|
97 |
@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 |
|
|
104 |
Path p = Files.createTempFile("exportingCsv-", new Date().toString()); |
|
105 |
List<PiwikInfo> returning = piWikService.getPiwikSitesForRepos(orderField,orderType,0,10000,searchField); |
|
106 |
try (PrintWriter writer = new PrintWriter(p.toFile())) { |
|
107 |
|
|
108 |
StringBuilder sb = new StringBuilder(); |
|
109 |
sb.append(" Repository ID , Repository name, Country, Site ID, Authentication token, Creation date, Requestor full name, Requestor email, Validated, Validation date, Comment \n"); |
|
110 |
|
|
111 |
for(PiwikInfo piwikInfo : returning){ |
|
112 |
sb.append( |
|
113 |
(piwikInfo.getRepositoryId() == null ? "," : piwikInfo.getRepositoryId()+ ",")+ |
|
114 |
(piwikInfo.getRepositoryName() == null ? "," : piwikInfo.getRepositoryName()+ ",")+ |
|
115 |
(piwikInfo.getCountry() == null ? "," : piwikInfo.getCountry()+ ",")+ |
|
116 |
(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 |
(piwikInfo.getComment() == null ? "\n" : piwikInfo.getComment()+ "\n") |
|
124 |
|
|
125 |
); |
|
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 |
@RequestMapping(value = "/approvePiwikSite/{repositoryId}" , method = RequestMethod.GET) |
|
157 |
@ResponseBody |
|
158 |
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN')") |
|
159 |
public ResponseEntity<Object> approvePiwikSite(@PathVariable("repositoryId") String repositoryId) { |
|
160 |
return piWikService.approvePiwikSite(repositoryId); |
|
161 |
} |
|
162 |
|
|
163 |
@RequestMapping(value = "/getOpenaireId/{repositoryId}" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE) |
|
164 |
@ResponseBody |
|
165 |
@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 |
} |
|
169 |
|
|
170 |
@RequestMapping(value = "/markPiwikSiteAsValidated/{repositoryId}" , method = RequestMethod.POST, |
|
171 |
consumes = MediaType.APPLICATION_JSON_VALUE) |
|
172 |
@ResponseBody |
|
173 |
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN')") |
|
174 |
public ResponseEntity<Object> markPiwikSiteAsValidated(@PathVariable("repositoryId") String repositoryId) throws RepositoryServiceException { |
|
175 |
return piWikService.markPiwikSiteAsValidated(repositoryId); |
|
176 |
} |
|
177 |
|
|
178 |
@RequestMapping(value = "/enableMetricsForRepository", method = RequestMethod.POST, |
|
179 |
consumes = MediaType.APPLICATION_JSON_VALUE) |
|
180 |
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or (hasRole('ROLE_USER') and #piwikInfo.requestorEmail == authentication.userInfo.email)") |
|
181 |
public PiwikInfo enableMetricsForRepository(@RequestParam("officialName") String officialName, |
|
182 |
@RequestParam("repoWebsite") String repoWebsite, |
|
183 |
@RequestBody PiwikInfo piwikInfo) throws RepositoryServiceException { |
|
184 |
return piWikService.enableMetricsForRepository(officialName, repoWebsite, piwikInfo); |
|
185 |
} |
|
186 |
|
|
187 |
|
|
188 |
} |
Also available in: Unified diff