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

182 lines
5.3 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("product___::" + DHPUtils.md5(input.getId()));
mapPid(out, input);
mapTitle(out, input);
mapAbstract(out, input);
mapType(out, input);
mapTopic(out, input);
mapContribution(out, input);
//The manifestation will be included extending the result as well as the relations to funder, organization and other results
} catch (ClassCastException cce) {
return null;
}
}
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 = Utils.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));
}
}
}
}
}
}
private static <E extends Result> void mapTopic(ResearchProduct out, E input) {
if (Optional.ofNullable(input.getSubject()).isPresent()) {
out
.setTopics(
input
.getSubject()
.stream()
.filter(s -> !s.getQualifier().getClassid().equalsIgnoreCase("keyword"))
.map(s -> {
ResultTopic topic = new ResultTopic();
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 "topic_______::" + 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);
}
}