dnet-applications/apps/scholexplorer-api/src/main/java/eu/dnetlib/scholix/api/controller/ScholixControllerV2.java

78 lines
4.4 KiB
Java

package eu.dnetlib.scholix.api.controller;
import eu.dnetlib.common.controller.AbstractDnetController;
import eu.dnetlib.dhp.schema.sx.api.model.v2.PageResultType;
import eu.dnetlib.dhp.schema.sx.api.model.v2.ScholixType;
import eu.dnetlib.dhp.schema.sx.scholix.Scholix;
import eu.dnetlib.scholix.api.ScholixException;
import eu.dnetlib.scholix.api.index.ScholixIndexManager;
import io.micrometer.core.annotation.Timed;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/v2")
@Api(tags = {
"Links : Operation related to the Scholix Links"
})
public class ScholixControllerV2 extends AbstractDnetController {
@Autowired
private ScholixIndexManager manager;
@Timed(value = "scholix.v2.links", description = "Time taken to return links on Version 2.0 of Scholix")
@ApiOperation("Get Scholix Links")
@GetMapping("/Links")
public PageResultType links(
@Parameter(in = ParameterIn.QUERY,
description = "Filter Scholix relationships collected from a LinkProvider") String linkProvider,
@Parameter(in = ParameterIn.QUERY,
description = "Filter Scholix relationships having a target pid") String targetPid,
@Parameter(in = ParameterIn.QUERY,
description = "Filter Scholix relationships having a target pid type") String targetPidType,
@Parameter(in = ParameterIn.QUERY,
description = "Filter Scholix relationships having a target published in a Publisher named targetPublisher") String targetPublisher,
@Parameter(in = ParameterIn.QUERY,
description = "Filter Scholix relationships having a target type (literature, dataset, unknown)") String targetType,
@Parameter(in = ParameterIn.QUERY,
description = "Filter Scholix relationships having a source pid") String sourcePid,
@Parameter(in = ParameterIn.QUERY,
description = "Filter Scholix relationships having a source pid type") String sourcePidType,
@Parameter(in = ParameterIn.QUERY,
description = "Filter Scholix relationships having a source published in a Publisher named sourcePublisher") String sourcePublisher,
@Parameter(in = ParameterIn.QUERY,
description = "Filter Scholix relationships having a source type (literature, dataset, unknown)") String sourceType,
@Parameter(in = ParameterIn.QUERY,
description = "Filter scholix Links having collected after this date") String harvestedAfter,
@Parameter(in = ParameterIn.QUERY,
description = "select page of result") Integer page) throws Exception {
if (StringUtils.isEmpty(sourcePid) && StringUtils.isEmpty(targetPid) && StringUtils.isEmpty(sourcePublisher)&& StringUtils.isEmpty(targetPublisher)&& StringUtils.isEmpty(linkProvider))
throw new ScholixException("The method requires one of the following parameters: sourcePid, targetPid, sourcePublisher, targetPublisher, linkProvider");
try {
final int currentPage = page != null ? page : 0;
Pair<Long, List<Scholix>> scholixResult = manager.linksFromPid( linkProvider, targetPid, targetPidType, targetPublisher, targetType, sourcePid, sourcePidType, sourcePublisher, sourceType, harvestedAfter, currentPage);
final PageResultType pageResult = new PageResultType();
pageResult.setTotalPages(scholixResult.getLeft().intValue() / 10);
pageResult.setTotalLinks(scholixResult.getLeft().intValue());
pageResult.setResult(scholixResult.getRight().stream().map(ScholixType::fromScholix).collect(Collectors.toList()));
return pageResult;
} catch (Throwable e) {
throw new ScholixException("Error on requesting url ", e);
}
}
}