new funder apis

This commit is contained in:
Michele Artini 2024-01-29 15:01:03 +01:00
parent b030f1a747
commit e5901a0181
11 changed files with 259 additions and 207 deletions

View File

@ -0,0 +1,22 @@
package eu.dnetlib.openaire.common;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;
@NoRepositoryBean
public interface ReadOnlyRepository<T, ID> extends Repository<T, ID> {
Optional<T> findById(ID id);
boolean existsById(ID id);
Page<T> findAll(Pageable pageable);
Iterable<T> findAll();
long count();
}

View File

@ -1,91 +0,0 @@
package eu.dnetlib.openaire.funders;
import java.io.StringReader;
import java.util.stream.Collectors;
import eu.dnetlib.openaire.exporter.model.funders.ExtendedFunderDetails;
import eu.dnetlib.openaire.exporter.model.funders.FunderDetails;
import eu.dnetlib.openaire.exporter.model.funders.FundingStream;
import eu.dnetlib.openaire.funders.domain.db.FunderDbEntry;
import eu.dnetlib.openaire.funders.domain.db.FundingPathDbEntry;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import static org.apache.commons.lang3.StringUtils.*;
public class ConversionUtils {
public static final String SEPARATOR = "::";
public static FunderDetails asFunderDetails(final FunderDbEntry fdb) {
final FunderDetails f = new FunderDetails();
f.setId(fdb.getId());
f.setName(fdb.getName());
f.setShortname(fdb.getShortname());
f.setJurisdiction(fdb.getJurisdiction());
f.setRegistrationDate(fdb.getRegistrationdate());
f.setLastUpdateDate(fdb.getLastupdatedate());
return f;
}
public static ExtendedFunderDetails asExtendedFunderDetails(final FunderDbEntry fdb) {
final ExtendedFunderDetails f = new ExtendedFunderDetails(asFunderDetails(fdb));
if (fdb.getFundingpaths() != null) {
f.setFundingStreams(
fdb.getFundingpaths().stream()
.map(ConversionUtils::asFundingStream)
.collect(Collectors.toList()));
}
return f;
}
private static FundingStream asFundingStream(final FundingPathDbEntry pathDbEntry) {
final FundingStream f = new FundingStream();
try {
final Document xml = new SAXReader().read(new StringReader(pathDbEntry.getPath()));
for(int i=2;i>=0;i--) {
if (hasFundingLevel(i, xml)) {
f.setId(getId(i, xml));
f.setName(getName(i, xml));
break;
}
}
if (isBlank(f.getId()) && isNoneBlank(xml.valueOf("//funder/id/text()"))) {
f.setId(xml.valueOf("//funder/shortname/text()"));
f.setName(xml.valueOf("//funder/name/text()"));
}
if (isBlank(f.getId())) {
throw new IllegalStateException("invalid funding path:\n" + xml.asXML());
}
return f;
} catch (DocumentException e) {
throw new IllegalStateException("unable to parse funding path:\n" + pathDbEntry.getPath());
}
}
private static String getName(int level, final Document d) {
return d.valueOf(String.format("//funding_level_%s/description/text()", level));
}
private static String getId(int level, final Document d) {
return substringAfter(
d.valueOf(
String.format("//funding_level_%s/id/text()", level)
), SEPARATOR);
}
private static boolean hasFundingLevel(int level, Document d) {
return isNotBlank(getId(level, d));
}
}

View File

@ -9,8 +9,7 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import eu.dnetlib.openaire.exporter.exceptions.FundersApiException;
import eu.dnetlib.openaire.exporter.model.funders.ExtendedFunderDetails;
import eu.dnetlib.openaire.exporter.model.funders.FunderDetails;
import eu.dnetlib.openaire.funders.domain.db.FunderDbEntry;
@Component
@ConditionalOnProperty(value = "openaire.exporter.enable.funders", havingValue = "true")
@ -19,25 +18,23 @@ public class FunderDao {
@Autowired
private FunderRepository funderRepository;
public ExtendedFunderDetails getExtendedFunderDetails(final String funderId) throws FundersApiException {
return ConversionUtils
.asExtendedFunderDetails(funderRepository.findById(funderId).orElseThrow(() -> new FundersApiException("Funder not found. ID: " + funderId)));
public FunderDbEntry findFunder(final String funderId) throws FundersApiException {
return funderRepository.findById(funderId).orElseThrow(() -> new FundersApiException("Funder not found. ID: " + funderId));
}
public List<FunderDetails> listFunderDetails(final int page, final int size) throws FundersApiException {
public List<FunderDbEntry> listFunders(final int page, final int size) throws FundersApiException {
return funderRepository.findAll(PageRequest.of(page, size))
.getContent()
.stream()
.map(ConversionUtils::asFunderDetails)
.collect(Collectors.toList());
.getContent()
.stream()
.collect(Collectors.toList());
}
public List<String> listFunderIds(final int page, final int size) throws FundersApiException {
return funderRepository.findAll(PageRequest.of(page, size))
.getContent()
.stream()
.map(f -> f.getId())
.collect(Collectors.toList());
.getContent()
.stream()
.map(FunderDbEntry::getId)
.collect(Collectors.toList());
}
}

View File

@ -1,10 +1,11 @@
package eu.dnetlib.openaire.funders;
import eu.dnetlib.openaire.funders.domain.db.FunderDbEntry;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import eu.dnetlib.openaire.common.ReadOnlyRepository;
import eu.dnetlib.openaire.funders.domain.db.FunderDbEntry;
@Repository
public interface FunderRepository extends JpaRepository<FunderDbEntry, String> {
public interface FunderRepository extends ReadOnlyRepository<FunderDbEntry, String> {
}

View File

@ -2,8 +2,6 @@ package eu.dnetlib.openaire.funders;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.CrossOrigin;
@ -14,8 +12,7 @@ import org.springframework.web.bind.annotation.RestController;
import eu.dnetlib.openaire.common.AbstractExporterController;
import eu.dnetlib.openaire.exporter.exceptions.FundersApiException;
import eu.dnetlib.openaire.exporter.model.funders.ExtendedFunderDetails;
import eu.dnetlib.openaire.exporter.model.funders.FunderDetails;
import eu.dnetlib.openaire.funders.domain.db.FunderDbEntry;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
@ -23,57 +20,53 @@ import io.swagger.v3.oas.annotations.tags.Tag;
@RestController
@CrossOrigin(origins = {
"*"
"*"
})
@ConditionalOnProperty(value = "openaire.exporter.enable.funders", havingValue = "true")
@Tag(name = "OpenAIRE funders API", description = "the OpenAIRE funders API")
public class FundersApiController extends AbstractExporterController {
private static final Log log = LogFactory.getLog(FundersApiController.class);
@Autowired
private FunderDao fDao;
@RequestMapping(value = "/funders", produces = {
"application/json"
"application/json"
}, method = RequestMethod.GET)
@Operation(summary = "get basic information about funders", description = "basic information about funders: id, name, shortname, last update date, registration date")
@Operation(summary = "get basic information about funders", description = "basic information about funders: id, name, shortname, registration date")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "500", description = "unexpected error")
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public List<FunderDetails> getFunders(
@PathVariable final int page,
@PathVariable final int size) throws FundersApiException {
public List<FunderDbEntry> getFunders(
@PathVariable final int page,
@PathVariable final int size) throws FundersApiException {
return fDao.listFunderDetails(page, size);
return fDao.listFunders(page, size);
}
@RequestMapping(value = "/funder/{id}", produces = {
"application/json"
"application/json"
}, method = RequestMethod.GET)
@Operation(summary = "get the funder details", description = "complete funder information")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "500", description = "unexpected error")
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public ExtendedFunderDetails getFunderDetails(
@PathVariable final String id) throws FundersApiException {
return fDao.getExtendedFunderDetails(id);
public FunderDbEntry getFunderDetails(@PathVariable final String id) throws FundersApiException {
return fDao.findFunder(id);
}
@RequestMapping(value = "/funder/ids", produces = {
"application/json"
"application/json"
}, method = RequestMethod.GET)
@Operation(summary = "get the list of funder ids", description = "get the list of funder ids")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "500", description = "unexpected error")
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public List<String> getFunderIds(
@PathVariable final int page,
@PathVariable final int size) throws FundersApiException {
public List<String> listFunderIds(
@PathVariable final int page,
@PathVariable final int size) throws FundersApiException {
return fDao.listFunderIds(page, size);
}

View File

@ -1,38 +1,43 @@
package eu.dnetlib.openaire.funders.domain.db;
import java.sql.Date;
import java.util.Set;
import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import eu.dnetlib.openaire.dsm.domain.db.IdentityDbEntry;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "funders")
public class FunderDbEntry {
public class FunderDbEntry implements Serializable {
private static final long serialVersionUID = 1290088460508203016L;
@Id
@Column(name = "id")
private String id;
private String name;
private String shortname;
private String jurisdiction;
private String websiteurl;
private String policy;
private Date registrationdate;
private Date lastupdatedate;
@OneToMany(
cascade = { CascadeType.PERSIST, CascadeType.MERGE },
fetch = FetchType.LAZY )
@JoinTable(
name = "funder_identity",
joinColumns = @JoinColumn(name = "funder"),
inverseJoinColumns = @JoinColumn(name = "pid"))
private Set<IdentityDbEntry> pids;
@Column(name = "legalshortname")
private String legalShortName;
@OneToMany(mappedBy = "funderid", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<FundingPathDbEntry> fundingpaths;
@Column(name = "legalname")
private String legalName;
public FunderDbEntry() {}
@Column(name = "websiteurl")
private String websiteUrl;
@Column(name = "logourl")
private String logoUrl;
@Column(name = "country")
private String country;
@Column(name = "registrationdate")
private LocalDateTime registrationDate;
@Column(name = "registered")
private boolean registered;
public String getId() {
return id;
@ -42,75 +47,60 @@ public class FunderDbEntry {
this.id = id;
}
public String getName() {
return name;
public String getLegalShortName() {
return legalShortName;
}
public void setName(final String name) {
this.name = name;
public void setLegalShortName(final String legalShortName) {
this.legalShortName = legalShortName;
}
public String getShortname() {
return shortname;
public String getLegalName() {
return legalName;
}
public void setShortname(final String shortname) {
this.shortname = shortname;
public void setLegalName(final String legalName) {
this.legalName = legalName;
}
public String getJurisdiction() {
return jurisdiction;
public String getWebsiteUrl() {
return websiteUrl;
}
public void setJurisdiction(final String jurisdiction) {
this.jurisdiction = jurisdiction;
public void setWebsiteUrl(final String websiteUrl) {
this.websiteUrl = websiteUrl;
}
public String getWebsiteurl() {
return websiteurl;
public String getLogoUrl() {
return logoUrl;
}
public void setWebsiteurl(final String websiteurl) {
this.websiteurl = websiteurl;
public void setLogoUrl(final String logoUrl) {
this.logoUrl = logoUrl;
}
public String getPolicy() {
return policy;
public String getCountry() {
return country;
}
public void setPolicy(final String policy) {
this.policy = policy;
public void setCountry(final String country) {
this.country = country;
}
public Date getRegistrationdate() {
return registrationdate;
public LocalDateTime getRegistrationDate() {
return registrationDate;
}
public void setRegistrationdate(final Date registrationdate) {
this.registrationdate = registrationdate;
public void setRegistrationDate(final LocalDateTime registrationDate) {
this.registrationDate = registrationDate;
}
public Date getLastupdatedate() {
return lastupdatedate;
public boolean isRegistered() {
return registered;
}
public void setLastupdatedate(final Date lastupdatedate) {
this.lastupdatedate = lastupdatedate;
public void setRegistered(final boolean registered) {
this.registered = registered;
}
public Set<IdentityDbEntry> getPids() {
return pids;
}
public void setPids(final Set<IdentityDbEntry> pids) {
this.pids = pids;
}
public Set<FundingPathDbEntry> getFundingpaths() {
return fundingpaths;
}
public void setFundingpaths(final Set<FundingPathDbEntry> fundingpaths) {
this.fundingpaths = fundingpaths;
}
}

View File

@ -0,0 +1,118 @@
package eu.dnetlib.openaire.funders.domain.db;
import java.sql.Date;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import eu.dnetlib.openaire.dsm.domain.db.IdentityDbEntry;
// @Entity
// @Table(name = "funders")
@Deprecated
public class OldFunderDbEntry {
@Id
private String id;
private String name;
private String shortname;
private String jurisdiction;
private String websiteurl;
private String policy;
private Date registrationdate;
private Date lastupdatedate;
@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
@JoinTable(name = "funder_identity", joinColumns = @JoinColumn(name = "funder"), inverseJoinColumns = @JoinColumn(name = "pid"))
private Set<IdentityDbEntry> pids;
@OneToMany(mappedBy = "funderid", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<OldFundingPathDbEntry> fundingpaths;
public OldFunderDbEntry() {}
public String getId() {
return id;
}
public void setId(final String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public String getShortname() {
return shortname;
}
public void setShortname(final String shortname) {
this.shortname = shortname;
}
public String getJurisdiction() {
return jurisdiction;
}
public void setJurisdiction(final String jurisdiction) {
this.jurisdiction = jurisdiction;
}
public String getWebsiteurl() {
return websiteurl;
}
public void setWebsiteurl(final String websiteurl) {
this.websiteurl = websiteurl;
}
public String getPolicy() {
return policy;
}
public void setPolicy(final String policy) {
this.policy = policy;
}
public Date getRegistrationdate() {
return registrationdate;
}
public void setRegistrationdate(final Date registrationdate) {
this.registrationdate = registrationdate;
}
public Date getLastupdatedate() {
return lastupdatedate;
}
public void setLastupdatedate(final Date lastupdatedate) {
this.lastupdatedate = lastupdatedate;
}
public Set<IdentityDbEntry> getPids() {
return pids;
}
public void setPids(final Set<IdentityDbEntry> pids) {
this.pids = pids;
}
public Set<OldFundingPathDbEntry> getFundingpaths() {
return fundingpaths;
}
public void setFundingpaths(final Set<OldFundingPathDbEntry> fundingpaths) {
this.fundingpaths = fundingpaths;
}
}

View File

@ -1,12 +1,15 @@
package eu.dnetlib.openaire.funders.domain.db;
import javax.persistence.*;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Transient;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
@Table(name = "fundingpaths")
public class FundingPathDbEntry {
// @Entity
// @Table(name = "fundingpaths")
@Deprecated
public class OldFundingPathDbEntry {
@JsonIgnore
@Transient
@ -28,7 +31,7 @@ public class FundingPathDbEntry {
private String funderid;
public FundingPathDbEntry() {}
public OldFundingPathDbEntry() {}
public String getDnetresourceidentifier() {
return dnetresourceidentifier;

View File

@ -0,0 +1,17 @@
ALTER TABLE dsm_organizations ADD COLUMN registered_funder boolean;
CREATE VIEW funders_view AS SELECT
o.id AS id,
o.legalshortname AS legalshortname,
o.legalname AS legalname,
o.websiteurl AS websiteurl,
o.logourl AS logourl,
o.country AS country,
o.dateofcollection AS registrationdate,
o.registered_funder AS registered
FROM
dsm_organizations o
JOIN dsm_service_organization so ON (o.id = so.organization)
JOIN dsm_services s ON (so.service = s.id)
JOIN projects p ON (p.collectedfrom = s.id)
GROUP BY o.id;

View File

@ -4,6 +4,7 @@ import java.util.List;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
@Deprecated
@JsonAutoDetect
public class ExtendedFunderDetails extends FunderDetails {

View File

@ -1,5 +1,6 @@
package eu.dnetlib.openaire.exporter.model.funders;
@Deprecated
public class FundingStream {
private String id;