method for sorting

This commit is contained in:
Michele Artini 2024-11-27 15:46:45 +01:00
parent 05b993236c
commit 02655e504a
2 changed files with 59 additions and 35 deletions

View File

@ -19,6 +19,8 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@ -228,6 +230,8 @@ public class OrganizationController extends AbstractDnetController {
@PathVariable final int size,
@RequestParam final String q,
@RequestParam(required = false, defaultValue = "") final String status,
@RequestParam(required = false, defaultValue = "name") final String orderBy,
@RequestParam(required = false, defaultValue = "false") final boolean reverse,
final Authentication authentication) {
if (SPECIAL_STATUS_FOR_CANDIDATE_DUP.equals(status)) {
@ -244,9 +248,11 @@ public class OrganizationController extends AbstractDnetController {
statuses = Arrays.asList(OrganizationStatus.approved.toString(), OrganizationStatus.suggested.toString());
}
final PageRequest pageRequest = PageRequest.of(page, size, Sort.by(reverse ? Order.desc(orderBy) : Order.asc(orderBy)));
return UserInfo.isSuperAdmin(authentication)
? organizationSimpleViewRepository.search(q, statuses, PageRequest.of(page, size))
: organizationSimpleViewRepository.searchForUser(q, UserInfo.getEmail(authentication), statuses, PageRequest.of(page, size));
? organizationSimpleViewRepository.search(q, statuses, pageRequest)
: organizationSimpleViewRepository.searchForUser(q, UserInfo.getEmail(authentication), statuses, pageRequest);
}
@GetMapping("/byCountry/{status}/{code}/{page}/{size}")
@ -254,12 +260,18 @@ public class OrganizationController extends AbstractDnetController {
@PathVariable final String code,
@PathVariable final int page,
@PathVariable final int size,
@RequestParam(required = false, defaultValue = "name") final String orderBy,
@RequestParam(required = false, defaultValue = "false") final boolean reverse,
final Authentication authentication) {
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));
final PageRequest pageRequest = PageRequest.of(page, size, Sort.by(reverse ? Order.desc(orderBy) : Order.asc(orderBy)));
if ("all".equalsIgnoreCase(status)) { return organizationSimpleViewRepository.findByCountry(code, pageRequest); }
return organizationSimpleViewRepository.findByCountryAndStatus(code, status, pageRequest);
}
@GetMapping("/byCountry/{status}/{code}")
@ -267,20 +279,30 @@ public class OrganizationController extends AbstractDnetController {
@PathVariable final String code,
@RequestParam(required = false, defaultValue = "0") final int page,
@RequestParam(required = false, defaultValue = "${openorgs.findOrgsByStatusAndCountry.limit.default}") final int size,
@RequestParam(required = false, defaultValue = "name") final String orderBy,
@RequestParam(required = false, defaultValue = "false") final boolean reverse,
final Authentication authentication) {
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)).getContent(); }
return organizationSimpleViewRepository.findByCountryAndStatusOrderByName(code, status, PageRequest.of(page, size)).getContent();
final PageRequest pageRequest = PageRequest.of(page, size, Sort.by(reverse ? Order.desc(orderBy) : Order.asc(orderBy)));
if ("all".equalsIgnoreCase(status)) { return organizationSimpleViewRepository.findByCountry(code, pageRequest).getContent(); }
return organizationSimpleViewRepository.findByCountryAndStatus(code, status, pageRequest).getContent();
}
@GetMapping(value = "/byCountry/{status}/{code}/csv", produces = "text/csv")
public void findOrgsByStatusAndCountryCSV(@PathVariable final String status,
@PathVariable final String code,
@RequestParam(required = false, defaultValue = "name") final String orderBy,
@RequestParam(required = false, defaultValue = "false") final boolean reverse,
final HttpServletResponse res,
final Authentication authentication) throws IOException {
final Iterable<OrganizationSimpleView> list = findOrgsByStatusAndCountry(status, code, 0, Integer.MAX_VALUE, authentication);
final Iterable<OrganizationSimpleView> list = findOrgsByStatusAndCountry(status, code, 0, Integer.MAX_VALUE, orderBy, reverse, authentication);
CSVConverter.writeCSV(res
.getOutputStream(), list, OrganizationSimpleView.class, "id", "name", "type", "city", "country", "acronyms", "urls", "status", "nSimilarDups", "nSuggestedDups", "nDifferentDups");
}
@ -290,17 +312,21 @@ public class OrganizationController extends AbstractDnetController {
@PathVariable final String type,
@PathVariable final int page,
@PathVariable final int size,
@RequestParam(required = false, defaultValue = "name") final String orderBy,
@RequestParam(required = false, defaultValue = "false") final boolean reverse,
final Authentication authentication) {
final PageRequest pageRequest = PageRequest.of(page, size, Sort.by(reverse ? Order.desc(orderBy) : Order.asc(orderBy)));
if (UserInfo.isSuperAdmin(authentication)) {
if ("all".equalsIgnoreCase(status)) { return organizationSimpleViewRepository.findByTypeOrderByName(type, PageRequest.of(page, size)); }
return organizationSimpleViewRepository.findByTypeAndStatusOrderByName(type, status, PageRequest.of(page, size));
}
if ("all".equalsIgnoreCase(status)) {
return organizationSimpleViewRepository.findByTypeForUser(type, UserInfo.getEmail(authentication), PageRequest.of(page, size));
if ("all".equalsIgnoreCase(status)) { return organizationSimpleViewRepository.findByType(type, pageRequest); }
return organizationSimpleViewRepository.findByTypeAndStatus(type, status, pageRequest);
}
if ("all".equalsIgnoreCase(status)) { return organizationSimpleViewRepository.findByTypeForUser(type, UserInfo.getEmail(authentication), pageRequest); }
return organizationSimpleViewRepository
.findByTypeAndStatusForUser(type, status, UserInfo.getEmail(authentication), PageRequest.of(page, size));
.findByTypeAndStatusForUser(type, status, UserInfo.getEmail(authentication), pageRequest);
}

View File

@ -15,12 +15,12 @@ public interface OrganizationSimpleViewRepository extends ReadOnlyRepository<Org
// SEARCH
@Query(value = "SELECT \n"
+ " org.id,\n"
+ " org.name,\n"
+ " org.type,\n"
+ " org.city,\n"
+ " org.country,\n"
+ " org.status,\n"
+ " org.id AS id,\n"
+ " org.name AS name,\n"
+ " org.type AS type,\n"
+ " org.city AS city,\n"
+ " org.country AS country,\n"
+ " org.status AS status,\n"
+ " array_remove(array_agg(DISTINCT a.acronym), NULL) AS acronyms,\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"
@ -32,18 +32,17 @@ public interface OrganizationSimpleViewRepository extends ReadOnlyRepository<Org
+ " LEFT OUTER JOIN urls u ON org.id = u.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"
+ "GROUP BY org.id, org.name, org.type, org.city, org.country, org.status\n"
+ "ORDER BY org.name", nativeQuery = true)
+ "GROUP BY org.id, org.name, org.type, org.city, org.country, org.status", nativeQuery = true)
Page<OrganizationSimpleView> search(@Param("text") String text, @Param("statuses") List<String> statuses, Pageable pageable);
// SEARCH FOR USER
@Query(value = "SELECT\n"
+ " org.id,\n"
+ " org.name,\n"
+ " org.type,\n"
+ " org.city,\n"
+ " org.country,\n"
+ " org.status,\n"
+ " org.id AS id,\n"
+ " org.name AS name,\n"
+ " org.type AS type,\n"
+ " org.city AS city,\n"
+ " org.country AS country,\n"
+ " org.status AS status,\n"
+ " array_remove(array_agg(DISTINCT a.acronym), NULL) AS acronyms,\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"
@ -56,25 +55,24 @@ public interface OrganizationSimpleViewRepository extends ReadOnlyRepository<Org
+ " 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"
+ "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"
+ "ORDER BY org.name", nativeQuery = true)
+ "GROUP BY org.id, org.name, org.type, org.city, org.country, org.status", nativeQuery = true)
Page<OrganizationSimpleView> searchForUser(@Param("text") String text,
@Param("email") String email,
@Param("statuses") List<String> statuses,
Pageable pageable);
Page<OrganizationSimpleView> findByCountryOrderByName(String country, Pageable pageable);
Page<OrganizationSimpleView> findByCountry(String country, Pageable pageable);
Page<OrganizationSimpleView> findByCountryAndStatusOrderByName(String code, String status, Pageable pageable);
Page<OrganizationSimpleView> findByCountryAndStatus(String code, String status, Pageable pageable);
Page<OrganizationSimpleView> findByTypeOrderByName(String type, Pageable pageable);
Page<OrganizationSimpleView> findByType(String type, Pageable pageable);
Page<OrganizationSimpleView> findByTypeAndStatusOrderByName(String type, String status, Pageable pageable);
Page<OrganizationSimpleView> findByTypeAndStatus(String type, String status, Pageable pageable);
@Query(value = "select o.* from organizations_simple_view o left outer join user_countries uc on (uc.country = o.country) where uc.email = ?2 and o.type = ?1 order by o.name", nativeQuery = true)
@Query(value = "select o.* from organizations_simple_view o left outer join user_countries uc on (uc.country = o.country) where uc.email = ?2 and o.type = ?1", nativeQuery = true)
Page<OrganizationSimpleView> findByTypeForUser(String type, String name, Pageable pageable);
@Query(value = "select o.* from organizations_simple_view o left outer join user_countries uc on (uc.country = o.country) where o.type = ?1 and o.status = ?2 and uc.email = ?3 order by o.name", nativeQuery = true)
@Query(value = "select o.* from organizations_simple_view o left outer join user_countries uc on (uc.country = o.country) where o.type = ?1 and o.status = ?2 and uc.email = ?3", nativeQuery = true)
Page<OrganizationSimpleView> findByTypeAndStatusForUser(String type, String status, String name, Pageable pageable);
// SEARCH FOR VALID DUPLICATE CANDIDATES