added pagination to the sushilite call

This commit is contained in:
myrto.koukouli 2018-08-06 08:55:53 +00:00
parent 596c1f84ec
commit c69a7d51a8
2 changed files with 62 additions and 14 deletions

View File

@ -1,6 +1,7 @@
package eu.dnetlib.repo.manager.service.controllers;
import eu.dnetlib.usagestats.sushilite.ReportResponseWrapper;
import eu.dnetlib.usagestats.sushilite.domain.ReportResponse;
import eu.dnetlib.usagestats.sushilite.domain.ReportResponseWrapper;
import io.swagger.annotations.Api;
import org.json.JSONException;
import org.springframework.http.MediaType;
@ -16,7 +17,9 @@ public interface SushiliteApi {
@RequestMapping(value = "/getReportResults", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
ReportResponseWrapper getReportResults(String Report,
ReportResponseWrapper getReportResults(String page,
String pageSize,
String Report,
String Release,
String RequestorID,
String BeginDate,

View File

@ -1,43 +1,57 @@
package eu.dnetlib.repo.manager.service.controllers;
import eu.dnetlib.usagestats.sushilite.ReportResponseWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import eu.dnetlib.usagestats.sushilite.domain.ReportItem;
import eu.dnetlib.usagestats.sushilite.domain.ReportResponse;
import eu.dnetlib.usagestats.sushilite.domain.ReportResponseWrapper;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.ArrayList;
import java.util.List;
@Component
public class SushiliteApiImpl implements SushiliteApi {
@Value("${services.repomanager.usagestats.sushiliteEndoint}")
@Value("${services.repomanager.usagestats.sushiliteEndpoint}")
private String usagestatsSushiliteEndpoint;
@Autowired
private EmailUtils emailUtils;
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
.getLogger(SushiliteApiImpl.class);
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(SushiliteApiImpl.class);
@Override
@PreAuthorize("hasRole('ROLE_USER')")
public ReportResponseWrapper getReportResults(@RequestParam(value = "Report") String Report,
public ReportResponseWrapper getReportResults(@PathVariable("page") String page,
@PathVariable("pageSize") String pageSize,
@RequestParam(value = "Report") String Report,
@RequestParam(value = "Release",defaultValue="4") String Release,
@RequestParam(value = "RequestorID",required=false) String RequestorID,
@RequestParam(value = "BeginDate",required=false) String BeginDate,
@RequestParam(value = "EndDate",required=false) String EndDate,
@RequestParam(value = "RequestorID",required=false,defaultValue="anonymous") String RequestorID,
@RequestParam(value = "BeginDate",required=false,defaultValue="") String BeginDate,
@RequestParam(value = "EndDate",required=false,defaultValue="") String EndDate,
@RequestParam(value = "RepositoryIdentifier") String RepositoryIdentifier,
@RequestParam(value = "ItemIdentifier",required=false) String ItemIdentifier,
@RequestParam(value = "ItemDataType",required=false) String ItemDataType,
@RequestParam(value = "ItemIdentifier",required=false,defaultValue="") String ItemIdentifier,
@RequestParam(value = "ItemDataType",required=false,defaultValue="") String ItemDataType,
@RequestParam(value = "Granularity") String Granularity,
@RequestParam(value = "Pretty",required=false) String Pretty) {
@RequestParam(value = "Pretty",required=false,defaultValue="") String Pretty) {
//build the uri params
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.usagestatsSushiliteEndpoint + "GetReport/")
@ -55,6 +69,7 @@ public class SushiliteApiImpl implements SushiliteApi {
//create new template engine
RestTemplate template = new RestTemplate();
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<ReportResponseWrapper> resp;
try {
//communicate with endpoint
@ -70,6 +85,36 @@ public class SushiliteApiImpl implements SushiliteApi {
throw e;
}
return resp.getBody();
System.out.println("sushi responded status " + resp.getStatusCode());
List<ReportItem> requestedItemList = new ArrayList<>();
if (resp.getBody().getReportResponse().getReportWrapper().getReport().getCustomer().getReportItems() != null) {
try {
int totalItems = resp.getBody().getReportResponse().getReportWrapper().getReport().getCustomer().getReportItems().size();
int offset = Integer.parseInt(page);
int size = Integer.parseInt(pageSize);
if (offset <= (totalItems / size) ) {
int upperIndex = offset+size;
if (upperIndex > totalItems) {
upperIndex = totalItems;
}
requestedItemList = resp.getBody().getReportResponse().getReportWrapper().getReport().getCustomer().getReportItems().subList(offset,upperIndex);
}
} catch (NumberFormatException e) {
LOGGER.debug("Exception on getReportResults - trying to cast strings to integers", e);
//emailUtils.reportException(e);
throw e;
}
}
ReportResponseWrapper newReportResponse = resp.getBody();
newReportResponse.getReportResponse().getReportWrapper().getReport().getCustomer().setReportItems(requestedItemList);
return newReportResponse;
}
}