dhp-graph-dump/dump/src/main/java/eu/dnetlib/dhp/skgif/ResultMapper.java

288 lines
9.0 KiB
Java

package eu.dnetlib.dhp.skgif;
import static eu.dnetlib.dhp.oa.graph.dump.ResultMapper.*;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.management.RuntimeErrorException;
import eu.dnetlib.dhp.oa.graph.dump.csv.AuthorResult;
import eu.dnetlib.dhp.oa.model.ResultPid;
import eu.dnetlib.dhp.schema.common.ModelConstants;
import eu.dnetlib.dhp.schema.oaf.*;
import eu.dnetlib.dhp.skgif.exception.NoAllowedTypeException;
import eu.dnetlib.dhp.skgif.exception.NoTitleFoundException;
import eu.dnetlib.dhp.skgif.model.*;
import eu.dnetlib.dhp.skgif.model.AccessRight;
import eu.dnetlib.dhp.utils.DHPUtils;
import scala.Tuple2;
import scala.reflect.internal.Trees;
/**
* @author miriam.baglioni
* @Date 01/09/23
*/
public class ResultMapper implements Serializable {
public static <E extends Result> ResearchProduct map(
E input)
throws Exception {
ResearchProduct out = new ResearchProduct();
Optional<Qualifier> ort = Optional.ofNullable(input.getResulttype());
if (ort.isPresent()) {
try {
out.setLocalIdentifier(input.getId());
mapPid(out, input);
mapTitle(out, input);
mapAbstract(out, input);
mapType(out, input);
mapTopic(out, input);
mapContribution(out, input);
if (!Optional.ofNullable(out.getTitles()).isPresent() ||
!Optional.ofNullable(out.getContributions()).isPresent())
return null;
// TODO map the manifestation directly from the instances
// it is not completed
mapManifestation(out, input);
// TODO extend the mapping to consider relations between these entities and the results
// private List<String> relevantOrganizations;
// private List<String> funding;
// private List<Relations> relatedProducts;
} catch (ClassCastException cce) {
return null;
}
}
return null;
}
private static <E extends Result> void mapManifestation(ResearchProduct out, E input) {
out
.setManifestations(
input
.getInstance()
.stream()
.parallel()
.map(i -> {
try {
return getManifestation(i);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toList()));
}
private static Manifestation getManifestation(Instance i) throws MalformedURLException {
Manifestation manifestation = new Manifestation();
manifestation.setProductLocalType(i.getInstancetype().getClassname());
manifestation.setProductLocalTypeSchema(i.getInstancetype().getSchemename());
Dates dates = new Dates();
dates.setType("publishing");
dates.setValue(i.getDateofacceptance().getValue());
manifestation.setDates(Arrays.asList(dates));
switch (i.getRefereed().getClassid()) {
case "0000":
manifestation.setPeerReview(PeerReview.UNAVAILABLE.label);
break;
case "0001":
manifestation.setPeerReview(PeerReview.PEER_REVIEWED.label);
break;
case "0002":
manifestation.setPeerReview(PeerReview.NON_PEER_REVIEWED.label);
break;
}
manifestation.setMetadataCuration(MetadataCuration.UNAVAILABLE.label);
// TODO filter out the URL that refer to pids. If nothing remains, decide what to do
manifestation.setUrl(new URL(i.getUrl().get(0)));
if (Optional.ofNullable(i.getPid()).isPresent()) {
manifestation.setPid(i.getPid().get(0).getValue());
}
switch (i.getAccessright().getClassid()) {
case "OPEN":
case "OPEN DATA":
case "OPEN SOURCE":
manifestation.setAccessRight(AccessRight.OPEN.label);
break;
case "CLOSED":
manifestation.setAccessRight(AccessRight.CLOSED.label);
break;
case "RESTRICTED":
manifestation.setAccessRight(AccessRight.RESTRICTED.label);
break;
case "EMBARGO":
case "12MONTHS":
case "6MONTHS":
manifestation.setAccessRight(AccessRight.EMBARGO.label);
break;
default:
manifestation.setAccessRight(AccessRight.UNAVAILABLE.label);
}
if (Optional.ofNullable(i.getLicense()).isPresent())
manifestation.setLicence(i.getLicense().getValue());
// TODO to fill the biblio in case it is a journal, we need to join with the datasource and verify the type
Biblio biblio = null;
manifestation.setHostingDatasource(i.getHostedby().getKey());
// TODO verify if the result is published in ojournal or conferences. In that case the venue is the identifier
// of the journal/conference. In case it is not, the venue is the datasource
if (biblio == null) {
manifestation.setVenue(i.getHostedby().getKey());
} else {
manifestation.setVenue("insert the id of the venue");
}
return manifestation;
}
private static Tuple2<String, Boolean> getOrcid(List<StructuredProperty> pid) {
if (!Optional.ofNullable(pid).isPresent())
return null;
if (pid.size() == 0)
return null;
for (StructuredProperty p : pid) {
if (p.getQualifier().getClassid().equals(ModelConstants.ORCID)) {
return new Tuple2<>(p.getValue(), Boolean.TRUE);
}
}
for (StructuredProperty p : pid) {
if (p.getQualifier().getClassid().equals(ModelConstants.ORCID_PENDING)) {
return new Tuple2<>(p.getValue(), Boolean.FALSE);
}
}
return null;
}
private static <E extends Result> void mapContribution(ResearchProduct out, E input) {
if (Optional.ofNullable(input.getAuthor()).isPresent()) {
int count = 0;
for (Author a : input.getAuthor()) {
count += 1;
Contribution contribution = new Contribution();
if (Optional.ofNullable(a.getPid()).isPresent()) {
Tuple2<String, Boolean> orcid = getOrcid(a.getPid());
if (orcid != null) {
contribution.setPerson("person______::"+DHPUtils.md5(orcid._1() + orcid._2()));
} else {
if (Optional.ofNullable(a.getRank()).isPresent()) {
contribution.setPerson("person______::"+DHPUtils.md5(input.getId() + a.getRank()));
} else {
contribution.setPerson("tmp_person__::"+DHPUtils.md5(input.getId() + count));
}
}
}
}
}
// "contributions": [
// {
// "person": "person_123",
// "declared_affiliations": ["org_1", "org_3"],
// "rank": 1,
// "roles": ["writing-original-draft", "conceptualization"]
// }
// ]
}
private static <E extends Result> void mapTopic(ResearchProduct out, E input) {
if (Optional.ofNullable(input.getSubject()).isPresent()) {
out.setTopics(input.getSubject().stream().parallel().map(s -> {
Topic topic = new Topic();
topic.setTopic(getIdentifier(s));
Provenance provenance = new Provenance();
provenance.setTrust(Double.valueOf(s.getDataInfo().getTrust()));
provenance.setType(s.getDataInfo().getInferenceprovenance());
topic.setProvenance(provenance);
return topic;
}).collect(Collectors.toList()));
}
}
private static String getIdentifier(StructuredProperty s) {
return DHPUtils.md5(s.getQualifier().getClassid() + s.getValue());
}
private static <E extends Result> void mapType(ResearchProduct out, E input) throws NoAllowedTypeException {
switch (input.getResulttype().getClassid()) {
case "publication":
out.setProductType(ResearchTypes.LITERATURE.label);
break;
case "dataset":
out.setProductType(ResearchTypes.RESEARCH_DATA.label);
break;
case "software":
out.setProductType(ResearchTypes.RESEARCH_SOFTWARE.label);
break;
case "other":
out.setProductType(ResearchTypes.OTHER.label);
break;
default:
throw new ClassCastException("Result type not present or not allowed");
}
}
private static void mapPid(ResearchProduct out, Result input) {
Optional
.ofNullable(input.getPid())
.ifPresent(
value -> out
.setIdentifiers(
value
.stream()
.map(
p -> {
Identifier identifier = new Identifier();
identifier.setValue(p.getValue());
identifier.setScheme(p.getQualifier().getSchemeid());
return identifier;
})
.collect(Collectors.toList())));
}
private static void mapTitle(ResearchProduct out, Result input) throws NoTitleFoundException {
Optional<List<StructuredProperty>> otitle = Optional.ofNullable(input.getTitle());
if (otitle.isPresent()) {
List<StructuredProperty> iTitle = otitle
.get()
.stream()
.filter(t -> t.getQualifier().getClassid().equalsIgnoreCase("main title"))
.collect(Collectors.toList());
if (!iTitle.isEmpty()) {
out.setTitles(Arrays.asList(iTitle.get(0).getValue()));
return;
}
iTitle = otitle
.get()
.stream()
.filter(t -> t.getQualifier().getClassid().equalsIgnoreCase("subtitle"))
.collect(Collectors.toList());
if (!iTitle.isEmpty()) {
out.setTitles(Arrays.asList(iTitle.get(0).getValue()));
}
}
}
private static void mapAbstract(ResearchProduct out, Result input) {
final List<String> descriptionList = new ArrayList<>();
Optional
.ofNullable(input.getDescription())
.ifPresent(value -> value.forEach(d -> descriptionList.add(d.getValue())));
out.setAbstracts(descriptionList);
}
}