Now exporting to csv

This commit is contained in:
Ioannis Diplas 2019-07-23 09:39:08 +00:00
parent 16b7099e75
commit be2b3f15fb
1 changed files with 85 additions and 1 deletions

View File

@ -9,12 +9,24 @@ import eu.dnetlib.repo.manager.shared.RepositoryServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@RestController
@ -22,6 +34,9 @@ import java.util.List;
@Api(description = "Piwik API", tags = {"piwik"})
public class PiWikController {
private static final Logger LOGGER = Logger
.getLogger(PiWikController.class);
@Autowired
private PiWikServiceImpl piWikService;
@ -50,7 +65,7 @@ public class PiWikController {
public Paging<PiwikInfo> getPiwikSitesForRepos(
@RequestParam(value = "from",required=false,defaultValue = "0") int from,
@RequestParam(value = "quantity",required=false,defaultValue = "100") int quantity,
@RequestParam(value = "order",required=false,defaultValue = "ASC") OrderByType orderType,
@RequestParam(value = "order",required=false,defaultValue = "DSC") OrderByType orderType,
@RequestParam(value = "orderField", required = false, defaultValue = "REPOSITORY_NAME") OrderByField orderField,
@RequestParam(value = "searchField", required = false, defaultValue = "") String searchField
@ -63,6 +78,75 @@ public class PiWikController {
results.setResults(returning);
return results;
}
@ApiImplicitParams({
@ApiImplicitParam(name = "from", dataType = "number", paramType = "query"),
@ApiImplicitParam(name = "quantity", dataType = "number", paramType = "query"),
@ApiImplicitParam(name = "order", dataType = "eu.dnetlib.repo.manager.domain.OrderByType", paramType = "query"),
@ApiImplicitParam(name = "searchField", dataType = "eu.dnetlib.repo.manager.domain.OrderByField", paramType = "query"),
@ApiImplicitParam(name = "orderField", dataType = "string", paramType = "query"),
})
@RequestMapping(value = "/getPiwikSitesForRepos/csv" , method = RequestMethod.GET,produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
public FileSystemResource getPiwikSitesForReposToCsv(
@RequestParam(value = "from",required=false,defaultValue = "0") int from,
@RequestParam(value = "quantity",required=false,defaultValue = "100") int quantity,
@RequestParam(value = "order",required=false,defaultValue = "DSC") OrderByType orderType,
@RequestParam(value = "orderField", required = false, defaultValue = "REPOSITORY_NAME") OrderByField orderField,
@RequestParam(value = "searchField", required = false, defaultValue = "") String searchField,
HttpServletResponse response,
HttpServletRequest request
) throws IOException {
Path p = Files.createTempFile("exportingCsv-", new Date().toString());
List<PiwikInfo> returning = piWikService.getPiwikSitesForRepos(orderField,orderType,from,quantity,searchField);
try (PrintWriter writer = new PrintWriter(p.toFile())) {
StringBuilder sb = new StringBuilder();
sb.append(" repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country \n");
for(PiwikInfo piwikInfo : returning){
sb.append(
(piwikInfo.getRepositoryId() == null ? "," : piwikInfo.getRepositoryId()+ ",")+
(piwikInfo.getSiteId() == null ? "," : piwikInfo.getSiteId()+ ",") +
(piwikInfo.getAuthenticationToken() == null ? "," : piwikInfo.getAuthenticationToken()+ ",")+
(piwikInfo.getCreationDate() == null ? "," : piwikInfo.getCreationDate().toString()+ ",") +
(piwikInfo.getRequestorName() == null ? "," : piwikInfo.getRequestorName()+ ",") +
(piwikInfo.getRequestorEmail() == null ? "," : piwikInfo.getRequestorEmail()+ ",")+
piwikInfo.isValidated() + "," +
(piwikInfo.getValidationDate() == null ? "," : piwikInfo.getValidationDate().toString()+ ",") +
(piwikInfo.getComment() == null ? "," : piwikInfo.getComment()+ ",") +
(piwikInfo.getRepositoryName() == null ? "," : piwikInfo.getRepositoryName()+ ",")+
(piwikInfo.getCountry() == null ? "\n" : piwikInfo.getCountry()+ "\n")
);
}
writer.write(sb.toString());
} catch (FileNotFoundException e) {
LOGGER.error(e.getMessage());
}
String mimeType = request.getServletContext().getMimeType(p.toFile().getAbsolutePath());
if (mimeType == null) {
mimeType = "application/octet-stream";
}
response.setContentType(mimeType);
response.setContentLength((int) p.toFile().length());
String headerKey = "Content-Disposition";
SimpleDateFormat sdfDate = new SimpleDateFormat("ddMMyyyy");//dd/MM/yyyy
Date now = new Date();
String strDate = sdfDate.format(now);
String headerValue = String.format("attachment; filename=\"csv-%s.csv\"",
strDate);
response.setHeader(headerKey, headerValue);
return new FileSystemResource(p.toFile());
}
@RequestMapping(value = "/approvePiwikSite/{repositoryId}" , method = RequestMethod.GET)
@ResponseBody