Revision 56699
Added by Ioannis Diplas over 4 years ago
PiWikController.java | ||
---|---|---|
9 | 9 |
import io.swagger.annotations.Api; |
10 | 10 |
import io.swagger.annotations.ApiImplicitParam; |
11 | 11 |
import io.swagger.annotations.ApiImplicitParams; |
12 |
import org.apache.log4j.Logger; |
|
12 | 13 |
import org.springframework.beans.factory.annotation.Autowired; |
14 |
import org.springframework.core.io.FileSystemResource; |
|
13 | 15 |
import org.springframework.http.MediaType; |
14 | 16 |
import org.springframework.http.ResponseEntity; |
15 | 17 |
import org.springframework.security.access.prepost.PreAuthorize; |
16 | 18 |
import org.springframework.web.bind.annotation.*; |
17 | 19 |
|
20 |
import javax.servlet.http.HttpServletRequest; |
|
21 |
import javax.servlet.http.HttpServletResponse; |
|
22 |
import java.io.File; |
|
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; |
|
18 | 30 |
import java.util.List; |
19 | 31 |
|
20 | 32 |
@RestController |
... | ... | |
22 | 34 |
@Api(description = "Piwik API", tags = {"piwik"}) |
23 | 35 |
public class PiWikController { |
24 | 36 |
|
37 |
private static final Logger LOGGER = Logger |
|
38 |
.getLogger(PiWikController.class); |
|
39 |
|
|
25 | 40 |
@Autowired |
26 | 41 |
private PiWikServiceImpl piWikService; |
27 | 42 |
|
... | ... | |
50 | 65 |
public Paging<PiwikInfo> getPiwikSitesForRepos( |
51 | 66 |
@RequestParam(value = "from",required=false,defaultValue = "0") int from, |
52 | 67 |
@RequestParam(value = "quantity",required=false,defaultValue = "100") int quantity, |
53 |
@RequestParam(value = "order",required=false,defaultValue = "ASC") OrderByType orderType,
|
|
68 |
@RequestParam(value = "order",required=false,defaultValue = "DSC") OrderByType orderType,
|
|
54 | 69 |
@RequestParam(value = "orderField", required = false, defaultValue = "REPOSITORY_NAME") OrderByField orderField, |
55 | 70 |
@RequestParam(value = "searchField", required = false, defaultValue = "") String searchField |
56 | 71 |
|
... | ... | |
63 | 78 |
results.setResults(returning); |
64 | 79 |
return results; |
65 | 80 |
} |
81 |
@ApiImplicitParams({ |
|
82 |
@ApiImplicitParam(name = "from", dataType = "number", paramType = "query"), |
|
83 |
@ApiImplicitParam(name = "quantity", dataType = "number", paramType = "query"), |
|
84 |
@ApiImplicitParam(name = "order", dataType = "eu.dnetlib.repo.manager.domain.OrderByType", paramType = "query"), |
|
85 |
@ApiImplicitParam(name = "searchField", dataType = "eu.dnetlib.repo.manager.domain.OrderByField", paramType = "query"), |
|
86 |
@ApiImplicitParam(name = "orderField", dataType = "string", paramType = "query"), |
|
87 |
}) |
|
88 |
@RequestMapping(value = "/getPiwikSitesForRepos/csv" , method = RequestMethod.GET,produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) |
|
89 |
@ResponseBody |
|
90 |
public FileSystemResource getPiwikSitesForReposToCsv( |
|
91 |
@RequestParam(value = "from",required=false,defaultValue = "0") int from, |
|
92 |
@RequestParam(value = "quantity",required=false,defaultValue = "100") int quantity, |
|
93 |
@RequestParam(value = "order",required=false,defaultValue = "DSC") OrderByType orderType, |
|
94 |
@RequestParam(value = "orderField", required = false, defaultValue = "REPOSITORY_NAME") OrderByField orderField, |
|
95 |
@RequestParam(value = "searchField", required = false, defaultValue = "") String searchField, |
|
96 |
HttpServletResponse response, |
|
97 |
HttpServletRequest request |
|
98 |
) throws IOException { |
|
66 | 99 |
|
100 |
Path p = Files.createTempFile("exportingCsv-", new Date().toString()); |
|
101 |
List<PiwikInfo> returning = piWikService.getPiwikSitesForRepos(orderField,orderType,from,quantity,searchField); |
|
102 |
try (PrintWriter writer = new PrintWriter(p.toFile())) { |
|
103 |
|
|
104 |
StringBuilder sb = new StringBuilder(); |
|
105 |
sb.append(" repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country \n"); |
|
106 |
|
|
107 |
for(PiwikInfo piwikInfo : returning){ |
|
108 |
sb.append( |
|
109 |
(piwikInfo.getRepositoryId() == null ? "," : piwikInfo.getRepositoryId()+ ",")+ |
|
110 |
(piwikInfo.getSiteId() == null ? "," : piwikInfo.getSiteId()+ ",") + |
|
111 |
(piwikInfo.getAuthenticationToken() == null ? "," : piwikInfo.getAuthenticationToken()+ ",")+ |
|
112 |
(piwikInfo.getCreationDate() == null ? "," : piwikInfo.getCreationDate().toString()+ ",") + |
|
113 |
(piwikInfo.getRequestorName() == null ? "," : piwikInfo.getRequestorName()+ ",") + |
|
114 |
(piwikInfo.getRequestorEmail() == null ? "," : piwikInfo.getRequestorEmail()+ ",")+ |
|
115 |
piwikInfo.isValidated() + "," + |
|
116 |
(piwikInfo.getValidationDate() == null ? "," : piwikInfo.getValidationDate().toString()+ ",") + |
|
117 |
(piwikInfo.getComment() == null ? "," : piwikInfo.getComment()+ ",") + |
|
118 |
(piwikInfo.getRepositoryName() == null ? "," : piwikInfo.getRepositoryName()+ ",")+ |
|
119 |
(piwikInfo.getCountry() == null ? "\n" : piwikInfo.getCountry()+ "\n") |
|
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 |
|
|
67 | 151 |
@RequestMapping(value = "/approvePiwikSite/{repositoryId}" , method = RequestMethod.GET) |
68 | 152 |
@ResponseBody |
69 | 153 |
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN')") |
Also available in: Unified diff
Now exporting to csv