pagination

This commit is contained in:
Michele Artini 2022-12-15 15:13:32 +01:00
parent 2c62f11b32
commit 209ed2166d
4 changed files with 62 additions and 41 deletions

View File

@ -3,6 +3,7 @@ package eu.dnetlib.openaire.dsm;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@ -27,13 +28,16 @@ public class DsmAjaxController extends AbstractDnetController {
}
@GetMapping("/searchByField/{field}/{page}/{size}")
public List<SimpleDsWithApis> searchByField(@PathVariable final String field, @RequestParam final String value) {
return dsmService.searchByField(DsmBrowsableFields.valueOf(field), value);
public Page<SimpleDsWithApis> searchByField(@PathVariable final String field,
@PathVariable final int page,
@PathVariable final int size,
@RequestParam final String value) {
return dsmService.searchByField(DsmBrowsableFields.valueOf(field), value, page, size);
}
@GetMapping("/search")
public List<SimpleDsWithApis> search(@RequestParam final String value) {
return dsmService.search(value);
@GetMapping("/search/{page}/{size}")
public Page<SimpleDsWithApis> search(@RequestParam final String value, @PathVariable final int page, @PathVariable final int size) {
return dsmService.search(value, page, size);
}
}

View File

@ -479,31 +479,31 @@ public class DsmService {
}
}
public List<SimpleDsWithApis> searchByField(final DsmBrowsableFields f, final String value) {
public Page<SimpleDsWithApis> searchByField(final DsmBrowsableFields f, final String value, final int page, final int size) {
switch (f) {
case type:
return simpleDsWithApisRepository.findByType(value);
return simpleDsWithApisRepository.findByType(value, PageRequest.of(page, size));
case collectedfrom:
return simpleDsWithApisRepository.findByCollectedFrom(value);
return simpleDsWithApisRepository.findByCollectedFrom(value, PageRequest.of(page, size));
case compliance:
return simpleDsWithApisRepository.findByCompliance(value);
return simpleDsWithApisRepository.findByCompliance(value, PageRequest.of(page, size));
case country:
return simpleDsWithApisRepository.findByCountry(value);
return simpleDsWithApisRepository.findByCountry(value, PageRequest.of(page, size));
case protocol:
return simpleDsWithApisRepository.findByProtocol(value);
return simpleDsWithApisRepository.findByProtocol(value, PageRequest.of(page, size));
case active:
return simpleDsWithApisRepository.findByActive(Boolean.valueOf(value));
return simpleDsWithApisRepository.findByActive(Boolean.valueOf(value), PageRequest.of(page, size));
case consenttermsofuse:
return simpleDsWithApisRepository.findByConsenttermsofuse(Boolean.valueOf(value));
return simpleDsWithApisRepository.findByConsenttermsofuse(Boolean.valueOf(value), PageRequest.of(page, size));
case fulltextdownload:
return simpleDsWithApisRepository.findByFulltextdownload(Boolean.valueOf(value));
return simpleDsWithApisRepository.findByFulltextdownload(Boolean.valueOf(value), PageRequest.of(page, size));
default:
throw new RuntimeException("not implemeted");
@ -511,8 +511,8 @@ public class DsmService {
}
public List<SimpleDsWithApis> search(final String value) {
return simpleDsWithApisRepository.search(value);
public Page<SimpleDsWithApis> search(final String value, final int page, final int size) {
return simpleDsWithApisRepository.search(value, PageRequest.of(page, size));
}
}

View File

@ -13,17 +13,19 @@
<body ng-app="dsmResultsApp" ng-controller="dsmResultsController">
<nav th:replace="fragments/mainParts.html :: mainMenu('Datasources')"></nav>
<div class="container-fluid">
<div class="row mt-5">
<div class="col">
<p class="text-muted">
<b>Number of results:</b> {{nResults}}<br />
<b>Page:</b> {{currPage}} / {{nPages}}
</p>
<p ng-show="results.length > 0">
<input type="text" class="form-control form-control-sm" ng-model="apiFilter" placeholder="Filter..."/>
<input type="text" class="form-control form-control-sm" ng-model="apiFilter" placeholder="Filter in current page..."/>
</p>
<p>
<span class="text-muted"><b>Number of APIs:</b> {{(results | filter:apiFilter).length}}</span>
</p>
<div class="card mb-4 small" ng-repeat="r in results|filter:apiFilter">
<div class="card-body">
@ -87,22 +89,35 @@
<script>
var app = angular.module('dsmResultsApp', []);
app.controller('dsmResultsController', function($scope, $http) {
$scope.field = getField();
$scope.value = getValue();
$scope.results = [];
$scope.nResults = 0;
$scope.currPage = 0;
$scope.nPages = 1;
$scope.pageSize = 100;
$scope.loadPage = function(page) {
var url = './ajax/dsm/';
if ($scope.field) { url += 'searchByField/' + encodeURIComponent($scope.field); }
else { url += 'search' }
url += '/' + (page - 1) + '/' + $scope.pageSize;
url += '?value=' + encodeURIComponent($scope.value) + '&' + $.now();
$http.get(url).then(function successCallback(res) {
$scope.results = res.data.content;
$scope.nResults = res.data.totalElements;
$scope.currPage = res.data.number + 1;
$scope.nPages = res.data.totalPages;
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
}
var url = './ajax/dsm/';
if ($scope.field) { url += 'searchByField/' + encodeURIComponent($scope.field) + "/0/50"; }
else { url += 'search' }
url += '?value=' + encodeURIComponent($scope.value) + '&' + $.now();
$http.get(url).then(function successCallback(res) {
$scope.results = res.data;
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
$scope.loadPage(1);
});
</script>

View File

@ -3,6 +3,8 @@ package eu.dnetlib.data.openaire.dsm.repository;
import java.util.List;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
@ -17,27 +19,27 @@ import eu.dnetlib.data.openaire.dsm.model.view.SimpleDsWithApis;
public interface SimpleDsWithApisRepository extends ReadOnlyRepository<SimpleDsWithApis, String>, JpaSpecificationExecutor<SimpleDsWithApis> {
@Query(value = "select * from dsm_datasources_view where id = :value or name ilike %:value% or other_name ilike %:value%", nativeQuery = true)
List<SimpleDsWithApis> search(@Param("value") String value);
Page<SimpleDsWithApis> search(@Param("value") String value, Pageable pageable);
List<SimpleDsWithApis> findByType(String type);
Page<SimpleDsWithApis> findByType(String type, Pageable pageable);
List<SimpleDsWithApis> findByCollectedFrom(String collectdFrom);
Page<SimpleDsWithApis> findByCollectedFrom(String collectdFrom, Pageable pageable);
List<SimpleDsWithApis> findByConsenttermsofuse(Boolean consenttermsofuse);
Page<SimpleDsWithApis> findByConsenttermsofuse(Boolean consenttermsofuse, Pageable pageable);
List<SimpleDsWithApis> findByFulltextdownload(Boolean fulltextdownload);
Page<SimpleDsWithApis> findByFulltextdownload(Boolean fulltextdownload, Pageable pageable);
@Query(value = "select * from dsm_datasources_view where ? = ANY(hidden_compliances)", nativeQuery = true)
List<SimpleDsWithApis> findByCompliance(String compliance);
Page<SimpleDsWithApis> findByCompliance(String compliance, Pageable pageable);
@Query(value = "select * from dsm_datasources_view where ? = ANY(hidden_countries)", nativeQuery = true)
List<SimpleDsWithApis> findByCountry(String country);
Page<SimpleDsWithApis> findByCountry(String country, Pageable pageable);
@Query(value = "select * from dsm_datasources_view where ? = ANY(hidden_protocols)", nativeQuery = true)
List<SimpleDsWithApis> findByProtocol(String brotocol);
Page<SimpleDsWithApis> findByProtocol(String brotocol, Pageable pageable);
@Query(value = "select * from dsm_datasources_view where ? = ANY(hidden_actives)", nativeQuery = true)
List<SimpleDsWithApis> findByActive(Boolean active);
Page<SimpleDsWithApis> findByActive(Boolean active, Pageable pageable);
@Query(value = "select type as term, type as name, count(*) as total from dsm_datasources_view group by type order by total desc", nativeQuery = true)
List<BrowseTerm> browseTermsForType();