added a default limit in suggestion pages

This commit is contained in:
Michele Artini 2024-05-03 12:23:43 +02:00
parent db18cb601c
commit 19bc482244
4 changed files with 204 additions and 234 deletions

View File

@ -86,20 +86,16 @@ public class OrganizationController extends AbstractDnetController {
@PostMapping("/save")
public List<String> save(@RequestBody final OrganizationView org, final Authentication authentication) {
if (StringUtils.isBlank(org.getName())) {
throw new RuntimeException("Missing field: name");
} else if (StringUtils.isBlank(org.getCountry())) {
throw new RuntimeException("Missing field: country");
} else if (StringUtils.isBlank(org.getType())) {
throw new RuntimeException("Missing field: type");
} else if (UserInfo.isSuperAdmin(authentication)
if (StringUtils.isBlank(org.getName())) { throw new RuntimeException("Missing field: name"); }
if (StringUtils.isBlank(org.getCountry())) { throw new RuntimeException("Missing field: country"); }
if (StringUtils.isBlank(org.getType())) { throw new RuntimeException("Missing field: type"); }
if (UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
final String orgId =
databaseUtils.insertOrUpdateOrganization(org, UserInfo.getEmail(authentication), UserInfo.isSimpleUser(authentication));
return Arrays.asList(orgId);
} else {
throw new RuntimeException("User not authorized");
}
throw new RuntimeException("User not authorized");
}
@GetMapping("/info")
@ -131,27 +127,24 @@ public class OrganizationController extends AbstractDnetController {
if (UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
return org;
} else {
throw new RuntimeException("User not authorized");
}
throw new RuntimeException("User not authorized");
}
@GetMapping("/conflicts")
public List<OrganizationConflict> conflicts(@RequestParam final String id, final Authentication authentication) {
if (UserInfo.isSuperAdmin(authentication) || userCountryRepository.verifyAuthorizationForId(id, UserInfo.getEmail(authentication))) {
return databaseUtils.listConflictsForId(id);
} else {
throw new RuntimeException("User not authorized");
}
throw new RuntimeException("User not authorized");
}
@GetMapping("/duplicates")
public List<OpenaireDuplicateView> duplicates(@RequestParam final String id, final Authentication authentication) {
if (UserInfo.isSuperAdmin(authentication) || userCountryRepository.verifyAuthorizationForId(id, UserInfo.getEmail(authentication))) {
return listDuplicates(id);
} else {
throw new RuntimeException("User not authorized");
}
throw new RuntimeException("User not authorized");
}
private List<OpenaireDuplicateView> listDuplicates(final String id) {
@ -159,45 +152,47 @@ public class OrganizationController extends AbstractDnetController {
}
@GetMapping("/conflicts/byCountry/{country}")
public Collection<Set<OrganizationConflict>> findConflictsByCountry(@PathVariable final String country, final Authentication authentication) {
public Collection<Set<OrganizationConflict>> findConflictsByCountry(@PathVariable final String country,
@RequestParam(required = false, defaultValue = "1000") final int limit,
final Authentication authentication) {
if (UserInfo.isSuperAdmin(authentication)) {
return groupConflicts(conflictGroupViewRepository.findByCountry1OrCountry2(country, country).stream());
} else if (UserInfo.isSimpleUser(authentication) || UserInfo.isNationalAdmin(authentication)) {
return groupConflicts(conflictGroupViewRepository.findByCountry1OrCountry2(country, country, PageRequest.of(0, limit)).stream());
}
if (UserInfo.isSimpleUser(authentication) || UserInfo.isNationalAdmin(authentication)) {
final Stream<ConflictGroupView> list = userCountryRepository.getCountriesForUser(UserInfo.getEmail(authentication))
.stream()
.filter(country::equalsIgnoreCase)
.map(c -> conflictGroupViewRepository.findByCountry1OrCountry2(c, c).stream())
.map(c -> conflictGroupViewRepository.findByCountry1OrCountry2(c, c, PageRequest.of(0, limit)).stream())
.findFirst()
.orElse(Stream.empty());
return groupConflicts(list);
} else {
throw new RuntimeException("User not authorized");
}
throw new RuntimeException("User not authorized");
}
@GetMapping("/duplicates/byCountry/{country}")
public Iterable<DuplicateGroupView> findDuplicatesByCountry(@PathVariable final String country, final Authentication authentication) {
public Iterable<DuplicateGroupView> findDuplicatesByCountry(@PathVariable final String country,
@RequestParam(required = false, defaultValue = "1000") final int limit,
final Authentication authentication) {
if (UserInfo.isSuperAdmin(authentication)) {
return duplicateGroupViewRepository.findByCountry(country);
} else if (UserInfo.isSimpleUser(authentication) || UserInfo.isNationalAdmin(authentication)) {
if (UserInfo.isSuperAdmin(authentication)) { return duplicateGroupViewRepository.findByCountry(country, PageRequest.of(0, limit)); }
if (UserInfo.isSimpleUser(authentication) || UserInfo.isNationalAdmin(authentication)) {
return userCountryRepository.getCountriesForUser(UserInfo.getEmail(authentication))
.stream()
.filter(country::equalsIgnoreCase)
.map(duplicateGroupViewRepository::findByCountry)
.map(c -> duplicateGroupViewRepository.findByCountry(c, PageRequest.of(0, limit)))
.findFirst()
.orElse(new ArrayList<DuplicateGroupView>());
} else {
throw new RuntimeException("User not authorized");
}
throw new RuntimeException("User not authorized");
}
@GetMapping(value = "/duplicates/byCountry/{country}/csv", produces = "text/csv")
public void findDuplicatesByCountryCSV(@PathVariable final String country, final HttpServletResponse res, final Authentication authentication)
throws IOException {
final Iterable<DuplicateGroupView> list = findDuplicatesByCountry(country, authentication);
final Iterable<DuplicateGroupView> list = findDuplicatesByCountry(country, Integer.MAX_VALUE, authentication);
CSVConverter.writeCSV(res.getOutputStream(), list, DuplicateGroupView.class, "id", "name", "city", "country", "numberOfDuplicates");
}
@ -227,9 +222,8 @@ public class OrganizationController extends AbstractDnetController {
if (b) {
databaseUtils.saveDuplicates(simrels, UserInfo.getEmail(authentication));
return listDuplicates(simrels.get(0).getLocalId());
} else {
throw new RuntimeException("User not authorized");
}
throw new RuntimeException("User not authorized");
}
@GetMapping("/search/{page}/{size}")
@ -239,11 +233,11 @@ public class OrganizationController extends AbstractDnetController {
@RequestParam(required = false, defaultValue = "") final String status,
final Authentication authentication) {
if (status.equals(SPECIAL_STATUS_FOR_CANDIDATE_DUP)) {
if (SPECIAL_STATUS_FOR_CANDIDATE_DUP.equals(status)) {
return UserInfo.isSuperAdmin(authentication)
? organizationSimpleViewRepository.searchCandidateDuplicates(q, PageRequest.of(page, size))
: organizationSimpleViewRepository.searchCandidateDuplicatesForUser(q, UserInfo.getEmail(authentication), PageRequest.of(page, size));
} else {
}
final List<String> statuses;
if (StringUtils.isNotBlank(status)) {
statuses = Arrays.asList(status.split(","));
@ -257,7 +251,6 @@ public class OrganizationController extends AbstractDnetController {
? organizationSimpleViewRepository.search(q, statuses, PageRequest.of(page, size))
: organizationSimpleViewRepository.searchForUser(q, UserInfo.getEmail(authentication), statuses, PageRequest.of(page, size));
}
}
@GetMapping("/byCountry/{status}/{code}/{page}/{size}")
public Page<OrganizationSimpleView> findByCountry(@PathVariable final String status,
@ -265,32 +258,23 @@ public class OrganizationController extends AbstractDnetController {
@PathVariable final int page,
@PathVariable final int size,
final Authentication authentication) {
if (UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForCountry(code, UserInfo.getEmail(authentication))) {
if (status.equalsIgnoreCase("all")) {
return organizationSimpleViewRepository.findByCountryOrderByName(code, PageRequest.of(page, size));
} else {
return organizationSimpleViewRepository.findByCountryAndStatusOrderByName(code, status, PageRequest.of(page, size));
}
} else {
if (!UserInfo.isSuperAdmin(authentication) && !userCountryRepository.verifyAuthorizationForCountry(code, UserInfo.getEmail(authentication))) {
throw new RuntimeException("User not authorized");
}
if ("all".equalsIgnoreCase(status)) { return organizationSimpleViewRepository.findByCountryOrderByName(code, PageRequest.of(page, size)); }
return organizationSimpleViewRepository.findByCountryAndStatusOrderByName(code, status, PageRequest.of(page, size));
}
@GetMapping("/byCountry/{status}/{code}")
public Iterable<OrganizationSimpleView> findOrgsByStatusAndCountry(@PathVariable final String status,
public List<OrganizationSimpleView> findOrgsByStatusAndCountry(@PathVariable final String status,
@PathVariable final String code,
@RequestParam(required = false, defaultValue = "1000") final int limit,
final Authentication authentication) {
if (UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForCountry(code, UserInfo.getEmail(authentication))) {
if (status.equalsIgnoreCase("all")) {
return organizationSimpleViewRepository.findByCountryOrderByName(code);
} else {
return organizationSimpleViewRepository.findByCountryAndStatusOrderByName(code, status);
}
} else {
if (!UserInfo.isSuperAdmin(authentication) && !userCountryRepository.verifyAuthorizationForCountry(code, UserInfo.getEmail(authentication))) {
throw new RuntimeException("User not authorized");
}
if ("all".equalsIgnoreCase(status)) { return organizationSimpleViewRepository.findByCountryOrderByName(code, PageRequest.of(0, limit)).getContent(); }
return organizationSimpleViewRepository.findByCountryAndStatusOrderByName(code, status, PageRequest.of(0, limit)).getContent();
}
@GetMapping(value = "/byCountry/{status}/{code}/csv", produces = "text/csv")
@ -298,7 +282,7 @@ public class OrganizationController extends AbstractDnetController {
@PathVariable final String code,
final HttpServletResponse res,
final Authentication authentication) throws IOException {
final Iterable<OrganizationSimpleView> list = findOrgsByStatusAndCountry(status, code, authentication);
final Iterable<OrganizationSimpleView> list = findOrgsByStatusAndCountry(status, code, Integer.MAX_VALUE, authentication);
CSVConverter.writeCSV(res
.getOutputStream(), list, OrganizationSimpleView.class, "id", "name", "type", "city", "country", "acronyms", "urls", "status", "nSimilarDups", "nSuggestedDups", "nDifferentDups");
}
@ -311,19 +295,14 @@ public class OrganizationController extends AbstractDnetController {
final Authentication authentication) {
if (UserInfo.isSuperAdmin(authentication)) {
if (status.equalsIgnoreCase("all")) {
return organizationSimpleViewRepository.findByTypeOrderByName(type, PageRequest.of(page, size));
} else {
if ("all".equalsIgnoreCase(status)) { return organizationSimpleViewRepository.findByTypeOrderByName(type, PageRequest.of(page, size)); }
return organizationSimpleViewRepository.findByTypeAndStatusOrderByName(type, status, PageRequest.of(page, size));
}
} else {
if (status.equalsIgnoreCase("all")) {
if ("all".equalsIgnoreCase(status)) {
return organizationSimpleViewRepository.findByTypeForUser(type, UserInfo.getEmail(authentication), PageRequest.of(page, size));
} else {
}
return organizationSimpleViewRepository
.findByTypeAndStatusForUser(type, status, UserInfo.getEmail(authentication), PageRequest.of(page, size));
}
}
}
@ -347,9 +326,8 @@ public class OrganizationController extends AbstractDnetController {
|| userCountryRepository.verifyAuthorizationForId(ids.get(0), UserInfo.getEmail(authentication))) {
final String newOrgId = databaseUtils.fixConflictSimilars(ids, UserInfo.getEmail(authentication));
return Arrays.asList(newOrgId);
} else {
return new ArrayList<>();
}
return new ArrayList<>();
}
@PostMapping("/conflicts/fix/different")
@ -358,9 +336,8 @@ public class OrganizationController extends AbstractDnetController {
|| userCountryRepository.verifyAuthorizationForId(ids.get(0), UserInfo.getEmail(authentication))) {
databaseUtils.fixConflictDifferents(ids, UserInfo.getEmail(authentication));
return ids;
} else {
return new ArrayList<>();
}
return new ArrayList<>();
}
@GetMapping("/note")
@ -370,9 +347,8 @@ public class OrganizationController extends AbstractDnetController {
if (UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
return noteRepository.findById(id).orElse(new Note(id, "", null, null));
} else {
throw new RuntimeException("User not authorized");
}
throw new RuntimeException("User not authorized");
}
@PostMapping("/note")
@ -381,21 +357,18 @@ public class OrganizationController extends AbstractDnetController {
final OrganizationView org = organizationViewRepository.findById(orgId).get();
if (UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
if (!UserInfo.isSuperAdmin(authentication)
&& !userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
throw new RuntimeException("User not authorized");
}
if (StringUtils.isNotBlank(note.getNote())) {
note.setModifiedBy(UserInfo.getEmail(authentication));
note.setModificationDate(OffsetDateTime.now());
return noteRepository.save(note);
} else {
}
noteRepository.deleteById(orgId);
return new Note(orgId, "", null, null);
}
} else {
throw new RuntimeException("User not authorized");
}
}
@GetMapping("/journal")
public List<JournalEntry> journalEntriesById(@RequestParam final String id, final Authentication authentication) {
@ -404,9 +377,8 @@ public class OrganizationController extends AbstractDnetController {
if (UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
return journalEntryRepository.findByOrgIdOrderByDateDesc(id);
} else {
throw new RuntimeException("User not authorized");
}
throw new RuntimeException("User not authorized");
}
}

View File

@ -2,6 +2,7 @@ package eu.dnetlib.organizations.repository.readonly;
import java.util.List;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import eu.dnetlib.organizations.model.view.ConflictGroupView;
@ -10,6 +11,6 @@ import eu.dnetlib.organizations.model.view.ConflictGroupViewPK;
@Repository
public interface ConflictGroupViewRepository extends ReadOnlyRepository<ConflictGroupView, ConflictGroupViewPK> {
List<ConflictGroupView> findByCountry1OrCountry2(String country1, String country2);
List<ConflictGroupView> findByCountry1OrCountry2(String country1, String country2, Pageable page);
}

View File

@ -2,6 +2,7 @@ package eu.dnetlib.organizations.repository.readonly;
import java.util.List;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import eu.dnetlib.organizations.model.view.DuplicateGroupView;
@ -9,5 +10,5 @@ import eu.dnetlib.organizations.model.view.DuplicateGroupView;
@Repository
public interface DuplicateGroupViewRepository extends ReadOnlyRepository<DuplicateGroupView, String> {
List<DuplicateGroupView> findByCountry(String country);
List<DuplicateGroupView> findByCountry(String country, Pageable page);
}

View File

@ -65,10 +65,6 @@ public interface OrganizationSimpleViewRepository extends ReadOnlyRepository<Org
Page<OrganizationSimpleView> findByCountryOrderByName(String country, Pageable pageable);
Iterable<OrganizationSimpleView> findByCountryOrderByName(String code);
Iterable<OrganizationSimpleView> findByCountryAndStatusOrderByName(String code, String status);
Page<OrganizationSimpleView> findByCountryAndStatusOrderByName(String code, String status, Pageable pageable);
Page<OrganizationSimpleView> findByTypeOrderByName(String type, Pageable pageable);