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") @PostMapping("/save")
public List<String> save(@RequestBody final OrganizationView org, final Authentication authentication) { public List<String> save(@RequestBody final OrganizationView org, final Authentication authentication) {
if (StringUtils.isBlank(org.getName())) { if (StringUtils.isBlank(org.getName())) { throw new RuntimeException("Missing field: name"); }
throw new RuntimeException("Missing field: name"); if (StringUtils.isBlank(org.getCountry())) { throw new RuntimeException("Missing field: country"); }
} else if (StringUtils.isBlank(org.getCountry())) { if (StringUtils.isBlank(org.getType())) { throw new RuntimeException("Missing field: type"); }
throw new RuntimeException("Missing field: country"); if (UserInfo.isSuperAdmin(authentication)
} else if (StringUtils.isBlank(org.getType())) { || userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
throw new RuntimeException("Missing field: type"); final String orgId =
} else if (UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
final String orgId =
databaseUtils.insertOrUpdateOrganization(org, UserInfo.getEmail(authentication), UserInfo.isSimpleUser(authentication)); databaseUtils.insertOrUpdateOrganization(org, UserInfo.getEmail(authentication), UserInfo.isSimpleUser(authentication));
return Arrays.asList(orgId); return Arrays.asList(orgId);
} else { }
throw new RuntimeException("User not authorized"); throw new RuntimeException("User not authorized");
}
} }
@GetMapping("/info") @GetMapping("/info")
@ -115,11 +111,11 @@ public class OrganizationController extends AbstractDnetController {
suggestionInfoViewByCountryRepository.findAll().forEach(info::add); suggestionInfoViewByCountryRepository.findAll().forEach(info::add);
} else if (UserInfo.isSimpleUser(authentication) || UserInfo.isNationalAdmin(authentication)) { } else if (UserInfo.isSimpleUser(authentication) || UserInfo.isNationalAdmin(authentication)) {
userCountryRepository.getCountriesForUser(UserInfo.getEmail(authentication)) userCountryRepository.getCountriesForUser(UserInfo.getEmail(authentication))
.stream() .stream()
.map(suggestionInfoViewByCountryRepository::findById) .map(suggestionInfoViewByCountryRepository::findById)
.filter(Optional::isPresent) .filter(Optional::isPresent)
.map(Optional::get) .map(Optional::get)
.forEach(info::add); .forEach(info::add);
} }
return info; return info;
} }
@ -129,29 +125,26 @@ public class OrganizationController extends AbstractDnetController {
final OrganizationView org = organizationViewRepository.findById(id).get(); final OrganizationView org = organizationViewRepository.findById(id).get();
if (UserInfo.isSuperAdmin(authentication) if (UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) { || userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
return org; return org;
} else {
throw new RuntimeException("User not authorized");
} }
throw new RuntimeException("User not authorized");
} }
@GetMapping("/conflicts") @GetMapping("/conflicts")
public List<OrganizationConflict> conflicts(@RequestParam final String id, final Authentication authentication) { public List<OrganizationConflict> conflicts(@RequestParam final String id, final Authentication authentication) {
if (UserInfo.isSuperAdmin(authentication) || userCountryRepository.verifyAuthorizationForId(id, UserInfo.getEmail(authentication))) { if (UserInfo.isSuperAdmin(authentication) || userCountryRepository.verifyAuthorizationForId(id, UserInfo.getEmail(authentication))) {
return databaseUtils.listConflictsForId(id); return databaseUtils.listConflictsForId(id);
} else {
throw new RuntimeException("User not authorized");
} }
throw new RuntimeException("User not authorized");
} }
@GetMapping("/duplicates") @GetMapping("/duplicates")
public List<OpenaireDuplicateView> duplicates(@RequestParam final String id, final Authentication authentication) { public List<OpenaireDuplicateView> duplicates(@RequestParam final String id, final Authentication authentication) {
if (UserInfo.isSuperAdmin(authentication) || userCountryRepository.verifyAuthorizationForId(id, UserInfo.getEmail(authentication))) { if (UserInfo.isSuperAdmin(authentication) || userCountryRepository.verifyAuthorizationForId(id, UserInfo.getEmail(authentication))) {
return listDuplicates(id); return listDuplicates(id);
} else {
throw new RuntimeException("User not authorized");
} }
throw new RuntimeException("User not authorized");
} }
private List<OpenaireDuplicateView> listDuplicates(final String id) { private List<OpenaireDuplicateView> listDuplicates(final String id) {
@ -159,45 +152,47 @@ public class OrganizationController extends AbstractDnetController {
} }
@GetMapping("/conflicts/byCountry/{country}") @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)) { if (UserInfo.isSuperAdmin(authentication)) {
return groupConflicts(conflictGroupViewRepository.findByCountry1OrCountry2(country, country).stream()); return groupConflicts(conflictGroupViewRepository.findByCountry1OrCountry2(country, country, PageRequest.of(0, limit)).stream());
} else 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())
.findFirst()
.orElse(Stream.empty());
return groupConflicts(list);
} else {
throw new RuntimeException("User not authorized");
} }
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, PageRequest.of(0, limit)).stream())
.findFirst()
.orElse(Stream.empty());
return groupConflicts(list);
}
throw new RuntimeException("User not authorized");
} }
@GetMapping("/duplicates/byCountry/{country}") @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)) { if (UserInfo.isSuperAdmin(authentication)) { return duplicateGroupViewRepository.findByCountry(country, PageRequest.of(0, limit)); }
return duplicateGroupViewRepository.findByCountry(country); if (UserInfo.isSimpleUser(authentication) || UserInfo.isNationalAdmin(authentication)) {
} else if (UserInfo.isSimpleUser(authentication) || UserInfo.isNationalAdmin(authentication)) {
return userCountryRepository.getCountriesForUser(UserInfo.getEmail(authentication)) return userCountryRepository.getCountriesForUser(UserInfo.getEmail(authentication))
.stream() .stream()
.filter(country::equalsIgnoreCase) .filter(country::equalsIgnoreCase)
.map(duplicateGroupViewRepository::findByCountry) .map(c -> duplicateGroupViewRepository.findByCountry(c, PageRequest.of(0, limit)))
.findFirst() .findFirst()
.orElse(new ArrayList<DuplicateGroupView>()); .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") @GetMapping(value = "/duplicates/byCountry/{country}/csv", produces = "text/csv")
public void findDuplicatesByCountryCSV(@PathVariable final String country, final HttpServletResponse res, final Authentication authentication) public void findDuplicatesByCountryCSV(@PathVariable final String country, final HttpServletResponse res, final Authentication authentication)
throws IOException { 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"); CSVConverter.writeCSV(res.getOutputStream(), list, DuplicateGroupView.class, "id", "name", "city", "country", "numberOfDuplicates");
} }
@ -219,148 +214,130 @@ public class OrganizationController extends AbstractDnetController {
if (simrels.isEmpty()) { return new ArrayList<>(); } if (simrels.isEmpty()) { return new ArrayList<>(); }
final boolean b = UserInfo.isSuperAdmin(authentication) final boolean b = UserInfo.isSuperAdmin(authentication)
|| simrels.stream() || simrels.stream()
.map(OpenaireDuplicate::getLocalId) .map(OpenaireDuplicate::getLocalId)
.distinct() .distinct()
.allMatch(id -> userCountryRepository.verifyAuthorizationForId(id, UserInfo.getEmail(authentication))); .allMatch(id -> userCountryRepository.verifyAuthorizationForId(id, UserInfo.getEmail(authentication)));
if (b) { if (b) {
databaseUtils.saveDuplicates(simrels, UserInfo.getEmail(authentication)); databaseUtils.saveDuplicates(simrels, UserInfo.getEmail(authentication));
return listDuplicates(simrels.get(0).getLocalId()); return listDuplicates(simrels.get(0).getLocalId());
} else {
throw new RuntimeException("User not authorized");
} }
throw new RuntimeException("User not authorized");
} }
@GetMapping("/search/{page}/{size}") @GetMapping("/search/{page}/{size}")
public Page<OrganizationSimpleView> search(@PathVariable final int page, public Page<OrganizationSimpleView> search(@PathVariable final int page,
@PathVariable final int size, @PathVariable final int size,
@RequestParam final String q, @RequestParam final String q,
@RequestParam(required = false, defaultValue = "") final String status, @RequestParam(required = false, defaultValue = "") final String status,
final Authentication authentication) { final Authentication authentication) {
if (status.equals(SPECIAL_STATUS_FOR_CANDIDATE_DUP)) { if (SPECIAL_STATUS_FOR_CANDIDATE_DUP.equals(status)) {
return UserInfo.isSuperAdmin(authentication) return UserInfo.isSuperAdmin(authentication)
? organizationSimpleViewRepository.searchCandidateDuplicates(q, PageRequest.of(page, size)) ? organizationSimpleViewRepository.searchCandidateDuplicates(q, PageRequest.of(page, size))
: organizationSimpleViewRepository.searchCandidateDuplicatesForUser(q, UserInfo.getEmail(authentication), PageRequest.of(page, size)); : organizationSimpleViewRepository.searchCandidateDuplicatesForUser(q, UserInfo.getEmail(authentication), PageRequest.of(page, size));
}
final List<String> statuses;
if (StringUtils.isNotBlank(status)) {
statuses = Arrays.asList(status.split(","));
} else if (UserInfo.isSimpleUser(authentication)) {
statuses = Arrays.asList(OrganizationStatus.approved.toString());
} else { } else {
final List<String> statuses; statuses = Arrays.asList(OrganizationStatus.approved.toString(), OrganizationStatus.suggested.toString());
if (StringUtils.isNotBlank(status)) { }
statuses = Arrays.asList(status.split(","));
} else if (UserInfo.isSimpleUser(authentication)) {
statuses = Arrays.asList(OrganizationStatus.approved.toString());
} else {
statuses = Arrays.asList(OrganizationStatus.approved.toString(), OrganizationStatus.suggested.toString());
}
return UserInfo.isSuperAdmin(authentication) return UserInfo.isSuperAdmin(authentication)
? organizationSimpleViewRepository.search(q, statuses, PageRequest.of(page, size)) ? organizationSimpleViewRepository.search(q, statuses, PageRequest.of(page, size))
: organizationSimpleViewRepository.searchForUser(q, UserInfo.getEmail(authentication), statuses, PageRequest.of(page, size)); : organizationSimpleViewRepository.searchForUser(q, UserInfo.getEmail(authentication), statuses, PageRequest.of(page, size));
}
} }
@GetMapping("/byCountry/{status}/{code}/{page}/{size}") @GetMapping("/byCountry/{status}/{code}/{page}/{size}")
public Page<OrganizationSimpleView> findByCountry(@PathVariable final String status, public Page<OrganizationSimpleView> findByCountry(@PathVariable final String status,
@PathVariable final String code, @PathVariable final String code,
@PathVariable final int page, @PathVariable final int page,
@PathVariable final int size, @PathVariable final int size,
final Authentication authentication) { final Authentication authentication) {
if (UserInfo.isSuperAdmin(authentication) if (!UserInfo.isSuperAdmin(authentication) && !userCountryRepository.verifyAuthorizationForCountry(code, UserInfo.getEmail(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 {
throw new RuntimeException("User not authorized"); 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}") @GetMapping("/byCountry/{status}/{code}")
public Iterable<OrganizationSimpleView> findOrgsByStatusAndCountry(@PathVariable final String status, public List<OrganizationSimpleView> findOrgsByStatusAndCountry(@PathVariable final String status,
@PathVariable final String code, @PathVariable final String code,
final Authentication authentication) { @RequestParam(required = false, defaultValue = "1000") final int limit,
if (UserInfo.isSuperAdmin(authentication) final Authentication authentication) {
|| userCountryRepository.verifyAuthorizationForCountry(code, UserInfo.getEmail(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 {
throw new RuntimeException("User not authorized"); 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") @GetMapping(value = "/byCountry/{status}/{code}/csv", produces = "text/csv")
public void findOrgsByStatusAndCountryCSV(@PathVariable final String status, public void findOrgsByStatusAndCountryCSV(@PathVariable final String status,
@PathVariable final String code, @PathVariable final String code,
final HttpServletResponse res, final HttpServletResponse res,
final Authentication authentication) throws IOException { 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 CSVConverter.writeCSV(res
.getOutputStream(), list, OrganizationSimpleView.class, "id", "name", "type", "city", "country", "acronyms", "urls", "status", "nSimilarDups", "nSuggestedDups", "nDifferentDups"); .getOutputStream(), list, OrganizationSimpleView.class, "id", "name", "type", "city", "country", "acronyms", "urls", "status", "nSimilarDups", "nSuggestedDups", "nDifferentDups");
} }
@GetMapping("/byType/{status}/{type}/{page}/{size}") @GetMapping("/byType/{status}/{type}/{page}/{size}")
public Page<OrganizationSimpleView> findByType(@PathVariable final String status, public Page<OrganizationSimpleView> findByType(@PathVariable final String status,
@PathVariable final String type, @PathVariable final String type,
@PathVariable final int page, @PathVariable final int page,
@PathVariable final int size, @PathVariable final int size,
final Authentication authentication) { final Authentication authentication) {
if (UserInfo.isSuperAdmin(authentication)) { if (UserInfo.isSuperAdmin(authentication)) {
if (status.equalsIgnoreCase("all")) { if ("all".equalsIgnoreCase(status)) { return organizationSimpleViewRepository.findByTypeOrderByName(type, PageRequest.of(page, size)); }
return organizationSimpleViewRepository.findByTypeOrderByName(type, PageRequest.of(page, size)); return organizationSimpleViewRepository.findByTypeAndStatusOrderByName(type, status, PageRequest.of(page, size));
} else {
return organizationSimpleViewRepository.findByTypeAndStatusOrderByName(type, status, PageRequest.of(page, size));
}
} else {
if (status.equalsIgnoreCase("all")) {
return organizationSimpleViewRepository.findByTypeForUser(type, UserInfo.getEmail(authentication), PageRequest.of(page, size));
} else {
return organizationSimpleViewRepository
.findByTypeAndStatusForUser(type, status, UserInfo.getEmail(authentication), PageRequest.of(page, size));
}
} }
if ("all".equalsIgnoreCase(status)) {
return organizationSimpleViewRepository.findByTypeForUser(type, UserInfo.getEmail(authentication), PageRequest.of(page, size));
}
return organizationSimpleViewRepository
.findByTypeAndStatusForUser(type, status, UserInfo.getEmail(authentication), PageRequest.of(page, size));
} }
@GetMapping("/browse/countries") @GetMapping("/browse/countries")
public List<BrowseEntry> browseCountries(final Authentication authentication) { public List<BrowseEntry> browseCountries(final Authentication authentication) {
return UserInfo.isSuperAdmin(authentication) return UserInfo.isSuperAdmin(authentication)
? databaseUtils.browseCountries() ? databaseUtils.browseCountries()
: databaseUtils.browseCountriesForUser(UserInfo.getEmail(authentication)); : databaseUtils.browseCountriesForUser(UserInfo.getEmail(authentication));
} }
@GetMapping("/browse/types") @GetMapping("/browse/types")
public List<BrowseEntry> browseOrganizationTypes(final Authentication authentication) { public List<BrowseEntry> browseOrganizationTypes(final Authentication authentication) {
return UserInfo.isSuperAdmin(authentication) return UserInfo.isSuperAdmin(authentication)
? databaseUtils.browseTypes() ? databaseUtils.browseTypes()
: databaseUtils.browseTypesForUser(UserInfo.getEmail(authentication)); : databaseUtils.browseTypesForUser(UserInfo.getEmail(authentication));
} }
@PostMapping("/conflicts/fix/similar") @PostMapping("/conflicts/fix/similar")
public List<String> fixConflictSim(final Authentication authentication, @RequestBody final List<String> ids) { public List<String> fixConflictSim(final Authentication authentication, @RequestBody final List<String> ids) {
if (ids.size() > 1 && UserInfo.isSuperAdmin(authentication) if (ids.size() > 1 && UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForId(ids.get(0), UserInfo.getEmail(authentication))) { || userCountryRepository.verifyAuthorizationForId(ids.get(0), UserInfo.getEmail(authentication))) {
final String newOrgId = databaseUtils.fixConflictSimilars(ids, UserInfo.getEmail(authentication)); final String newOrgId = databaseUtils.fixConflictSimilars(ids, UserInfo.getEmail(authentication));
return Arrays.asList(newOrgId); return Arrays.asList(newOrgId);
} else {
return new ArrayList<>();
} }
return new ArrayList<>();
} }
@PostMapping("/conflicts/fix/different") @PostMapping("/conflicts/fix/different")
public List<String> fixConflictDiff(final Authentication authentication, @RequestBody final List<String> ids) { public List<String> fixConflictDiff(final Authentication authentication, @RequestBody final List<String> ids) {
if (ids.size() > 1 && UserInfo.isSuperAdmin(authentication) if (ids.size() > 1 && UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForId(ids.get(0), UserInfo.getEmail(authentication))) { || userCountryRepository.verifyAuthorizationForId(ids.get(0), UserInfo.getEmail(authentication))) {
databaseUtils.fixConflictDifferents(ids, UserInfo.getEmail(authentication)); databaseUtils.fixConflictDifferents(ids, UserInfo.getEmail(authentication));
return ids; return ids;
} else {
return new ArrayList<>();
} }
return new ArrayList<>();
} }
@GetMapping("/note") @GetMapping("/note")
@ -368,11 +345,10 @@ public class OrganizationController extends AbstractDnetController {
final OrganizationView org = organizationViewRepository.findById(id).get(); final OrganizationView org = organizationViewRepository.findById(id).get();
if (UserInfo.isSuperAdmin(authentication) if (UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) { || userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
return noteRepository.findById(id).orElse(new Note(id, "", null, null)); 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") @PostMapping("/note")
@ -381,20 +357,17 @@ public class OrganizationController extends AbstractDnetController {
final OrganizationView org = organizationViewRepository.findById(orgId).get(); final OrganizationView org = organizationViewRepository.findById(orgId).get();
if (UserInfo.isSuperAdmin(authentication) if (!UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) { && !userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
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"); throw new RuntimeException("User not authorized");
} }
if (StringUtils.isNotBlank(note.getNote())) {
note.setModifiedBy(UserInfo.getEmail(authentication));
note.setModificationDate(OffsetDateTime.now());
return noteRepository.save(note);
}
noteRepository.deleteById(orgId);
return new Note(orgId, "", null, null);
} }
@GetMapping("/journal") @GetMapping("/journal")
@ -402,11 +375,10 @@ public class OrganizationController extends AbstractDnetController {
final OrganizationView org = organizationViewRepository.findById(id).get(); final OrganizationView org = organizationViewRepository.findById(id).get();
if (UserInfo.isSuperAdmin(authentication) if (UserInfo.isSuperAdmin(authentication)
|| userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) { || userCountryRepository.verifyAuthorizationForCountry(org.getCountry(), UserInfo.getEmail(authentication))) {
return journalEntryRepository.findByOrgIdOrderByDateDesc(id); 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 java.util.List;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import eu.dnetlib.organizations.model.view.ConflictGroupView; import eu.dnetlib.organizations.model.view.ConflictGroupView;
@ -10,6 +11,6 @@ import eu.dnetlib.organizations.model.view.ConflictGroupViewPK;
@Repository @Repository
public interface ConflictGroupViewRepository extends ReadOnlyRepository<ConflictGroupView, ConflictGroupViewPK> { 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 java.util.List;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import eu.dnetlib.organizations.model.view.DuplicateGroupView; import eu.dnetlib.organizations.model.view.DuplicateGroupView;
@ -9,5 +10,5 @@ import eu.dnetlib.organizations.model.view.DuplicateGroupView;
@Repository @Repository
public interface DuplicateGroupViewRepository extends ReadOnlyRepository<DuplicateGroupView, String> { public interface DuplicateGroupViewRepository extends ReadOnlyRepository<DuplicateGroupView, String> {
List<DuplicateGroupView> findByCountry(String country); List<DuplicateGroupView> findByCountry(String country, Pageable page);
} }

View File

@ -15,60 +15,56 @@ public interface OrganizationSimpleViewRepository extends ReadOnlyRepository<Org
// SEARCH // SEARCH
@Query(value = "SELECT \n" @Query(value = "SELECT \n"
+ " org.id,\n" + " org.id,\n"
+ " org.name,\n" + " org.name,\n"
+ " org.type,\n" + " org.type,\n"
+ " org.city,\n" + " org.city,\n"
+ " org.country,\n" + " org.country,\n"
+ " org.status,\n" + " org.status,\n"
+ " array_remove(array_agg(DISTINCT a.acronym), NULL) AS acronyms,\n" + " array_remove(array_agg(DISTINCT a.acronym), NULL) AS acronyms,\n"
+ " array_remove(array_agg(DISTINCT u.url), NULL) AS urls,\n" + " array_remove(array_agg(DISTINCT u.url), NULL) AS urls,\n"
+ " count(DISTINCT d1.oa_original_id) FILTER (WHERE d1.reltype = 'is_similar') AS n_similar_dups,\n" + " count(DISTINCT d1.oa_original_id) FILTER (WHERE d1.reltype = 'is_similar') AS n_similar_dups,\n"
+ " count(DISTINCT d1.oa_original_id) FILTER (WHERE d1.reltype = 'suggested') AS n_suggested_dups,\n" + " count(DISTINCT d1.oa_original_id) FILTER (WHERE d1.reltype = 'suggested') AS n_suggested_dups,\n"
+ " count(DISTINCT d1.oa_original_id) FILTER (WHERE d1.reltype = 'is_different') AS n_different_dups\n" + " count(DISTINCT d1.oa_original_id) FILTER (WHERE d1.reltype = 'is_different') AS n_different_dups\n"
+ "FROM org_index_search idx " + "FROM org_index_search idx "
+ " JOIN organizations org ON (idx.id = org.id) \n" + " JOIN organizations org ON (idx.id = org.id) \n"
+ " LEFT OUTER JOIN acronyms a ON org.id = a.id\n" + " LEFT OUTER JOIN acronyms a ON org.id = a.id\n"
+ " LEFT OUTER JOIN urls u ON org.id = u.id\n" + " LEFT OUTER JOIN urls u ON org.id = u.id\n"
+ " LEFT OUTER JOIN oa_duplicates d1 ON org.id = d1.local_id\n" + " LEFT OUTER JOIN oa_duplicates d1 ON org.id = d1.local_id\n"
+ "WHERE org.status in :statuses AND (org.name ilike '%'||:text||'%' OR idx.txt @@ plainto_tsquery(:text))\n" + "WHERE org.status in :statuses AND (org.name ilike '%'||:text||'%' OR idx.txt @@ plainto_tsquery(:text))\n"
+ "GROUP BY org.id, org.name, org.type, org.city, org.country, org.status\n" + "GROUP BY org.id, org.name, org.type, org.city, org.country, org.status\n"
+ "ORDER BY org.name", nativeQuery = true) + "ORDER BY org.name", nativeQuery = true)
Page<OrganizationSimpleView> search(@Param("text") String text, @Param("statuses") List<String> statuses, Pageable pageable); Page<OrganizationSimpleView> search(@Param("text") String text, @Param("statuses") List<String> statuses, Pageable pageable);
// SEARCH FOR USER // SEARCH FOR USER
@Query(value = "SELECT\n" @Query(value = "SELECT\n"
+ " org.id,\n" + " org.id,\n"
+ " org.name,\n" + " org.name,\n"
+ " org.type,\n" + " org.type,\n"
+ " org.city,\n" + " org.city,\n"
+ " org.country,\n" + " org.country,\n"
+ " org.status,\n" + " org.status,\n"
+ " array_remove(array_agg(DISTINCT a.acronym), NULL) AS acronyms,\n" + " array_remove(array_agg(DISTINCT a.acronym), NULL) AS acronyms,\n"
+ " array_remove(array_agg(DISTINCT u.url), NULL) AS urls,\n" + " array_remove(array_agg(DISTINCT u.url), NULL) AS urls,\n"
+ " count(DISTINCT d1.oa_original_id) FILTER (WHERE d1.reltype = 'is_similar' ) AS n_similar_dups,\n" + " count(DISTINCT d1.oa_original_id) FILTER (WHERE d1.reltype = 'is_similar' ) AS n_similar_dups,\n"
+ " count(DISTINCT d1.oa_original_id) FILTER (WHERE d1.reltype = 'suggested' ) AS n_suggested_dups,\n" + " count(DISTINCT d1.oa_original_id) FILTER (WHERE d1.reltype = 'suggested' ) AS n_suggested_dups,\n"
+ " count(DISTINCT d1.oa_original_id) FILTER (WHERE d1.reltype = 'is_different') AS n_different_dups\n" + " count(DISTINCT d1.oa_original_id) FILTER (WHERE d1.reltype = 'is_different') AS n_different_dups\n"
+ "FROM org_index_search idx\n" + "FROM org_index_search idx\n"
+ " JOIN organizations org ON (idx.id = org.id)\n" + " JOIN organizations org ON (idx.id = org.id)\n"
+ " LEFT OUTER JOIN acronyms a ON (org.id = a.id)\n" + " LEFT OUTER JOIN acronyms a ON (org.id = a.id)\n"
+ " LEFT OUTER JOIN urls u ON (org.id = u.id)\n" + " LEFT OUTER JOIN urls u ON (org.id = u.id)\n"
+ " LEFT OUTER JOIN oa_duplicates d1 ON (org.id = d1.local_id)\n" + " LEFT OUTER JOIN oa_duplicates d1 ON (org.id = d1.local_id)\n"
+ " LEFT OUTER JOIN user_countries uc ON (uc.country = org.country) \n" + " LEFT OUTER JOIN user_countries uc ON (uc.country = org.country) \n"
+ "WHERE uc.email = :email AND org.status IN :statuses AND (org.name ilike '%'||:text||'%' OR idx.txt @@ plainto_tsquery(:text))\n" + "WHERE uc.email = :email AND org.status IN :statuses AND (org.name ilike '%'||:text||'%' OR idx.txt @@ plainto_tsquery(:text))\n"
+ "GROUP BY org.id, org.name, org.type, org.city, org.country, org.status\n" + "GROUP BY org.id, org.name, org.type, org.city, org.country, org.status\n"
+ "ORDER BY org.name", nativeQuery = true) + "ORDER BY org.name", nativeQuery = true)
Page<OrganizationSimpleView> searchForUser(@Param("text") String text, Page<OrganizationSimpleView> searchForUser(@Param("text") String text,
@Param("email") String email, @Param("email") String email,
@Param("statuses") List<String> statuses, @Param("statuses") List<String> statuses,
Pageable pageable); Pageable pageable);
Page<OrganizationSimpleView> findByCountryOrderByName(String country, Pageable pageable); 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> findByCountryAndStatusOrderByName(String code, String status, Pageable pageable);
Page<OrganizationSimpleView> findByTypeOrderByName(String type, Pageable pageable); Page<OrganizationSimpleView> findByTypeOrderByName(String type, Pageable pageable);
@ -83,51 +79,51 @@ public interface OrganizationSimpleViewRepository extends ReadOnlyRepository<Org
// SEARCH FOR VALID DUPLICATE CANDIDATES // SEARCH FOR VALID DUPLICATE CANDIDATES
@Query(value = "SELECT\n" @Query(value = "SELECT\n"
+ " org.id,\n" + " org.id,\n"
+ " org.name,\n" + " org.name,\n"
+ " org.type,\n" + " org.type,\n"
+ " org.city,\n" + " org.city,\n"
+ " org.country,\n" + " org.country,\n"
+ " org.status,\n" + " org.status,\n"
+ " array_remove(array_agg(DISTINCT a.acronym), NULL) AS acronyms,\n" + " array_remove(array_agg(DISTINCT a.acronym), NULL) AS acronyms,\n"
+ " array_remove(array_agg(DISTINCT u.url), NULL) AS urls,\n" + " array_remove(array_agg(DISTINCT u.url), NULL) AS urls,\n"
+ " NULL AS n_similar_dups,\n" + " NULL AS n_similar_dups,\n"
+ " NULL AS n_suggested_dups,\n" + " NULL AS n_suggested_dups,\n"
+ " NULL AS n_different_dups\n" + " NULL AS n_different_dups\n"
+ "FROM org_index_search idx\n" + "FROM org_index_search idx\n"
+ " JOIN organizations org ON (idx.id = org.id) \n" + " JOIN organizations org ON (idx.id = org.id) \n"
+ " LEFT OUTER JOIN acronyms a ON (org.id = a.id)\n" + " LEFT OUTER JOIN acronyms a ON (org.id = a.id)\n"
+ " LEFT OUTER JOIN urls u ON (org.id = u.id)\n" + " LEFT OUTER JOIN urls u ON (org.id = u.id)\n"
+ " LEFT OUTER JOIN oa_duplicates d2 ON (org.id = d2.oa_original_id)\n" + " LEFT OUTER JOIN oa_duplicates d2 ON (org.id = d2.oa_original_id)\n"
+ "WHERE org.status = 'raw' AND (org.name ilike '%'||:text||'%' OR idx.txt @@ plainto_tsquery(:text))\n" + "WHERE org.status = 'raw' AND (org.name ilike '%'||:text||'%' OR idx.txt @@ plainto_tsquery(:text))\n"
+ "GROUP BY org.id, org.name, org.type, org.city, org.country, org.status\n" + "GROUP BY org.id, org.name, org.type, org.city, org.country, org.status\n"
+ "HAVING not('is_similar' = ANY(array_agg(d2.reltype)))\n" + "HAVING not('is_similar' = ANY(array_agg(d2.reltype)))\n"
+ "ORDER BY org.name", nativeQuery = true) + "ORDER BY org.name", nativeQuery = true)
Page<OrganizationSimpleView> searchCandidateDuplicates(@Param("text") String text, Pageable pageable); Page<OrganizationSimpleView> searchCandidateDuplicates(@Param("text") String text, Pageable pageable);
// SEARCH FOR VALID DUPLICATE CANDIDATES FOR USER // SEARCH FOR VALID DUPLICATE CANDIDATES FOR USER
@Query(value = "SELECT\n" @Query(value = "SELECT\n"
+ " org.id,\n" + " org.id,\n"
+ " org.name,\n" + " org.name,\n"
+ " org.type,\n" + " org.type,\n"
+ " org.city,\n" + " org.city,\n"
+ " org.country,\n" + " org.country,\n"
+ " org.status,\n" + " org.status,\n"
+ " array_remove(array_agg(DISTINCT a.acronym), NULL) AS acronyms,\n" + " array_remove(array_agg(DISTINCT a.acronym), NULL) AS acronyms,\n"
+ " array_remove(array_agg(DISTINCT u.url), NULL) AS urls,\n" + " array_remove(array_agg(DISTINCT u.url), NULL) AS urls,\n"
+ " NULL AS n_similar_dups,\n" + " NULL AS n_similar_dups,\n"
+ " NULL AS n_suggested_dups,\n" + " NULL AS n_suggested_dups,\n"
+ " NULL AS n_different_dups\n" + " NULL AS n_different_dups\n"
+ "FROM org_index_search idx\n" + "FROM org_index_search idx\n"
+ " JOIN organizations org ON (idx.id = org.id) \n" + " JOIN organizations org ON (idx.id = org.id) \n"
+ " LEFT OUTER JOIN acronyms a ON (org.id = a.id)\n" + " LEFT OUTER JOIN acronyms a ON (org.id = a.id)\n"
+ " LEFT OUTER JOIN urls u ON (org.id = u.id)\n" + " LEFT OUTER JOIN urls u ON (org.id = u.id)\n"
+ " LEFT OUTER JOIN oa_duplicates d2 ON (org.id = d2.oa_original_id)\n" + " LEFT OUTER JOIN oa_duplicates d2 ON (org.id = d2.oa_original_id)\n"
+ " LEFT OUTER JOIN user_countries uc ON (uc.country = org.country)\n" + " LEFT OUTER JOIN user_countries uc ON (uc.country = org.country)\n"
+ "WHERE org.status = 'raw' AND uc.email = :email AND (org.name ilike '%'||:text||'%' OR idx.txt @@ plainto_tsquery(:text))\n" + "WHERE org.status = 'raw' AND uc.email = :email AND (org.name ilike '%'||:text||'%' OR idx.txt @@ plainto_tsquery(:text))\n"
+ "GROUP BY org.id, org.name, org.type, org.city, org.country, org.status\n" + "GROUP BY org.id, org.name, org.type, org.city, org.country, org.status\n"
+ "HAVING not('is_similar' = ANY(array_agg(d2.reltype)))\n" + "HAVING not('is_similar' = ANY(array_agg(d2.reltype)))\n"
+ "ORDER BY org.name", nativeQuery = true) + "ORDER BY org.name", nativeQuery = true)
Page<OrganizationSimpleView> searchCandidateDuplicatesForUser(@Param("text") String text, @Param("email") String email, Pageable pageable); Page<OrganizationSimpleView> searchCandidateDuplicatesForUser(@Param("text") String text, @Param("email") String email, Pageable pageable);
} }