Adding totals to final array

This commit is contained in:
Ioannis Diplas 2019-07-23 08:07:39 +00:00
parent aaf2677c11
commit 16b7099e75
4 changed files with 100 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package eu.dnetlib.repo.manager.controllers;
import eu.dnetlib.domain.data.PiwikInfo;
import eu.dnetlib.repo.manager.domain.OrderByField;
import eu.dnetlib.repo.manager.domain.OrderByType;
import eu.dnetlib.repo.manager.domain.Paging;
import eu.dnetlib.repo.manager.service.PiWikServiceImpl;
import eu.dnetlib.repo.manager.shared.RepositoryServiceException;
import io.swagger.annotations.Api;
@ -46,7 +47,7 @@ public class PiWikController {
@ApiImplicitParam(name = "searchField", dataType = "eu.dnetlib.repo.manager.domain.OrderByField", paramType = "query"),
@ApiImplicitParam(name = "orderField", dataType = "string", paramType = "query"),
})
public List<PiwikInfo> getPiwikSitesForRepos(
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,
@ -54,7 +55,13 @@ public class PiWikController {
@RequestParam(value = "searchField", required = false, defaultValue = "") String searchField
){
return piWikService.getPiwikSitesForRepos(orderField,orderType,from,quantity,searchField);
Paging<PiwikInfo> results = new Paging<>();
List<PiwikInfo> returning = piWikService.getPiwikSitesForRepos(orderField,orderType,from,quantity,searchField);
results.setFrom(from);
results.setTo(from + returning.size());
results.setTotal(piWikService.getPiwikSitesTotals());
results.setResults(returning);
return results;
}
@RequestMapping(value = "/approvePiwikSite/{repositoryId}" , method = RequestMethod.GET)

View File

@ -0,0 +1,77 @@
package eu.dnetlib.repo.manager.domain;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
public class Paging<T> {
private int total;
private int from;
private int to;
private List<T> results;
public Paging(int total, int from, int to, List<T> results) {
this.total = total;
this.from = from;
this.to = to;
this.results = results;
}
public Paging(@NotNull Paging<T> page) {
this.total = page.getTotal();
this.from = page.getFrom();
this.to = page.getTo();
this.results = page.getResults();
}
public <K> Paging(@NotNull Paging<K> page, List<T> results) {
this.total = page.getTotal();
this.from = page.getFrom();
this.to = page.getTo();
this.results = results;
}
public Paging() {
this.total = 0;
this.from = 0;
this.to = 0;
this.results = new ArrayList<>();
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getFrom() {
return from;
}
public void setFrom(int from) {
this.from = from;
}
public int getTo() {
return to;
}
public void setTo(int to) {
this.to = to;
}
public List<T> getResults() {
return results;
}
public void setResults(List<T> results) {
this.results = results;
}
}

View File

@ -18,6 +18,8 @@ public interface PiWikService {
List<PiwikInfo> getPiwikSitesForRepos(OrderByField orderByField, OrderByType orderByType, int from, int quantity, String searchField);
int getPiwikSitesTotals();
ResponseEntity<Object> approvePiwikSite(String repositoryId);
String getOpenaireId(String repositoryid);

View File

@ -64,7 +64,9 @@ public class PiWikServiceImpl implements PiWikService {
private final static String INSERT_PIWIK_INFO = "insert into piwik_site (repositoryid, siteid, creationdate, requestorname, requestoremail, validated, repositoryname, country, authenticationtoken) values (?, ?, now(), ?, ?, ?, ?, ?, ?)";
private final static String GET_PIWIK_SITES = "select count(*) OVER() as totals, repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site ";
private final static String GET_PIWIK_SITES = "select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site ";
private final static String GET_PIWIK_SITES_TOTAL = "select count(*) as totals from piwik_site ";
private final static String APPROVE_PIWIK_SITE = "update piwik_site set validated=true, validationdate=now() where repositoryid = ?;";
@ -123,6 +125,15 @@ public class PiWikServiceImpl implements PiWikService {
}
@Override
public int getPiwikSitesTotals(){
try{
return new JdbcTemplate(dataSource).queryForObject(GET_PIWIK_SITES_TOTAL,Integer.class);
}catch (EmptyResultDataAccessException e){
return 0;
}
}
@Override
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN')")
public ResponseEntity<Object> approvePiwikSite(String repositoryId) {