api params to filter projects
This commit is contained in:
parent
cda62bd5e4
commit
1a4dcddbe3
|
@ -138,9 +138,13 @@ public class CommunityApiController extends AbstractDnetController {
|
|||
@ApiResponse(responseCode = "404", description = "not found"),
|
||||
@ApiResponse(responseCode = "500", description = "unexpected error")
|
||||
})
|
||||
public Page<CommunityProject> getCommunityProjects(@PathVariable final String id, @PathVariable final Integer page, @PathVariable final Integer size)
|
||||
public Page<CommunityProject> getCommunityProjects(@PathVariable final String id,
|
||||
@PathVariable final Integer page,
|
||||
@PathVariable final Integer size,
|
||||
@RequestParam(required = false) final String funder,
|
||||
@RequestParam(required = false) final String searchFilter)
|
||||
throws CommunityException {
|
||||
return communityService.getCommunityProjects(id, page, size);
|
||||
return communityService.getCommunityProjects(id, funder, searchFilter, page, size);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/community/{id}/projects", produces = {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package eu.dnetlib.openaire.community;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
|
@ -10,6 +11,7 @@ import java.util.function.BiConsumer;
|
|||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
@ -19,6 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import eu.dnetlib.openaire.community.model.DbCommunity;
|
||||
|
@ -129,15 +132,52 @@ public class CommunityService {
|
|||
}
|
||||
|
||||
@Transactional
|
||||
public Page<CommunityProject> getCommunityProjects(final String id, final int page, final int size) throws CommunityException {
|
||||
public Page<CommunityProject> getCommunityProjects(final String id, final String funder, final String filter, final int page, final int size)
|
||||
throws CommunityException {
|
||||
if (StringUtils.isBlank(id)) { throw new CommunityException("Empty ID"); }
|
||||
try {
|
||||
return dbProjectRepository.findByCommunity(id, PageRequest.of(page, size)).map(CommunityMappingUtils::toCommunityProject);
|
||||
if (StringUtils.isAllBlank(filter, funder)) {
|
||||
return dbProjectRepository.findByCommunity(id, PageRequest.of(page, size)).map(CommunityMappingUtils::toCommunityProject);
|
||||
} else {
|
||||
final Specification<DbProject> projSpec = prepareProjectSpec(id, funder, filter);
|
||||
return dbProjectRepository.findAll(projSpec, PageRequest.of(page, size)).map(CommunityMappingUtils::toCommunityProject);
|
||||
}
|
||||
} catch (final Throwable e) {
|
||||
log.error(e);
|
||||
throw new CommunityException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Specification<DbProject> prepareProjectSpec(final String community, final String funder, final String other) {
|
||||
return (project, query, cb) -> {
|
||||
|
||||
final List<Predicate> andConds = new ArrayList<>();
|
||||
andConds.add(cb.equal(project.get("community"), community));
|
||||
|
||||
if (StringUtils.isNotBlank(funder)) {
|
||||
andConds.add(cb.equal(project.get("projectFunder"), funder));
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(other)) {
|
||||
final String s = other.toLowerCase().trim();
|
||||
|
||||
final List<Predicate> orConds = new ArrayList<>();
|
||||
orConds.add(cb.equal(cb.lower(project.get("projectId")), s));
|
||||
orConds.add(cb.equal(cb.lower(project.get("projectCode")), s));
|
||||
orConds.add(cb.equal(cb.lower(project.get("projectAcronym")), s));
|
||||
orConds.add(cb.like(cb.lower(project.get("projectName")), "%" + s + "%"));
|
||||
if (StringUtils.isBlank(funder)) {
|
||||
orConds.add(cb.equal(cb.lower(project.get("projectFunder")), s));
|
||||
}
|
||||
|
||||
andConds.add(cb.or(orConds.toArray(new Predicate[orConds.size()])));
|
||||
}
|
||||
|
||||
return cb.and(andConds.toArray(new Predicate[andConds.size()]));
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CommunityProject addCommunityProject(final String id, final CommunityProject project) throws CommunityException {
|
||||
try {
|
||||
|
|
|
@ -4,12 +4,13 @@ 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.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
import eu.dnetlib.openaire.community.model.DbProject;
|
||||
import eu.dnetlib.openaire.community.model.DbProjectPK;
|
||||
|
||||
@ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true")
|
||||
public interface DbProjectRepository extends JpaRepository<DbProject, DbProjectPK> {
|
||||
public interface DbProjectRepository extends JpaRepository<DbProject, DbProjectPK>, JpaSpecificationExecutor<DbProject> {
|
||||
|
||||
Page<DbProject> findByCommunity(String community, Pageable page);
|
||||
|
||||
|
|
Loading…
Reference in New Issue