2019-10-24 16:00:28 +02:00
|
|
|
package eu.dnetlib.dhp.graph;
|
|
|
|
|
|
|
|
import eu.dnetlib.data.proto.KindProtos;
|
|
|
|
import eu.dnetlib.data.proto.OafProtos;
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.*;
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
2019-10-24 17:02:35 +02:00
|
|
|
import java.util.stream.Collectors;
|
2019-10-24 16:00:28 +02:00
|
|
|
|
2019-10-24 17:04:13 +02:00
|
|
|
import static eu.dnetlib.dhp.graph.ProtoUtils.mapDataInfo;
|
2019-10-24 16:56:13 +02:00
|
|
|
import static eu.dnetlib.dhp.graph.ProtoUtils.mapKV;
|
2019-10-24 16:00:28 +02:00
|
|
|
|
|
|
|
public class ProtoConverter implements Serializable {
|
|
|
|
|
|
|
|
public static Oaf convert(String s) {
|
|
|
|
try {
|
2019-10-24 16:56:13 +02:00
|
|
|
OafProtos.Oaf oaf = ProtoUtils.parse(s);
|
2019-10-24 16:00:28 +02:00
|
|
|
|
2019-10-24 16:56:13 +02:00
|
|
|
if (oaf.getKind() == KindProtos.Kind.entity)
|
|
|
|
return convertEntity(oaf);
|
2019-10-24 16:00:28 +02:00
|
|
|
else {
|
2019-10-24 16:56:13 +02:00
|
|
|
return convertRelation(oaf);
|
2019-10-24 16:00:28 +02:00
|
|
|
}
|
|
|
|
} catch (Throwable e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 16:56:13 +02:00
|
|
|
private static Relation convertRelation(OafProtos.Oaf oaf) {
|
|
|
|
final OafProtos.OafRel r = oaf.getRel();
|
2019-10-24 17:04:13 +02:00
|
|
|
final Relation rel = new Relation();
|
|
|
|
rel.setDataInfo(mapDataInfo(oaf.getDataInfo()));
|
|
|
|
rel.setLastupdatetimestamp(oaf.getLastupdatetimestamp());
|
2019-10-24 16:56:13 +02:00
|
|
|
return rel
|
|
|
|
.setSource(r.getSource())
|
|
|
|
.setTarget(r.getTarget())
|
|
|
|
.setRelType(r.getRelType().toString())
|
|
|
|
.setSubRelType(r.getSubRelType().toString())
|
|
|
|
.setRelClass(r.getRelClass())
|
2019-10-24 17:04:13 +02:00
|
|
|
.setCollectedFrom(r.getCollectedfromCount() > 0 ?
|
|
|
|
r.getCollectedfromList().stream()
|
|
|
|
.map(kv -> mapKV(kv))
|
|
|
|
.collect(Collectors.toList()) : null);
|
2019-10-24 16:00:28 +02:00
|
|
|
}
|
|
|
|
|
2019-10-24 16:56:13 +02:00
|
|
|
private static OafEntity convertEntity(OafProtos.Oaf oaf) {
|
2019-10-24 16:00:28 +02:00
|
|
|
|
|
|
|
switch (oaf.getEntity().getType()) {
|
|
|
|
case result:
|
|
|
|
return convertResult(oaf);
|
|
|
|
case project:
|
|
|
|
return convertProject(oaf);
|
|
|
|
case datasource:
|
|
|
|
return convertDataSource(oaf);
|
|
|
|
case organization:
|
|
|
|
return convertOrganization(oaf);
|
|
|
|
default:
|
|
|
|
throw new RuntimeException("received unknown type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 16:56:13 +02:00
|
|
|
private static Organization convertOrganization(OafProtos.Oaf oaf) {
|
2019-10-24 16:00:28 +02:00
|
|
|
return new Organization();
|
|
|
|
}
|
|
|
|
|
2019-10-24 17:05:42 +02:00
|
|
|
|
2019-10-24 16:56:13 +02:00
|
|
|
private static Datasource convertDataSource(OafProtos.Oaf oaf) {
|
2019-10-24 17:02:35 +02:00
|
|
|
final Datasource result = new Datasource();
|
|
|
|
|
2019-10-24 17:26:55 +02:00
|
|
|
|
|
|
|
//Set Oaf Fields
|
|
|
|
result.setDataInfo(ProtoUtils.mapDataInfo(oaf.getDataInfo()));
|
|
|
|
|
2019-10-24 17:02:35 +02:00
|
|
|
result.setLastupdatetimestamp(oaf.getLastupdatetimestamp());
|
|
|
|
|
|
|
|
//setting Entity fields
|
2019-10-24 17:26:55 +02:00
|
|
|
final OafProtos.OafEntity entity = oaf.getEntity();
|
|
|
|
|
|
|
|
result.setId(entity.getId());
|
|
|
|
|
|
|
|
result.setOriginalId(entity.getOriginalIdList());
|
|
|
|
|
|
|
|
result.setCollectedfrom(entity.getCollectedfromList()
|
|
|
|
.stream()
|
|
|
|
.map(ProtoUtils::mapKV)
|
|
|
|
.collect(Collectors.toList()));
|
2019-10-24 17:02:35 +02:00
|
|
|
|
2019-10-24 17:26:55 +02:00
|
|
|
result.setPid(entity.getPidList()
|
2019-10-24 17:02:35 +02:00
|
|
|
.stream()
|
2019-10-24 17:26:55 +02:00
|
|
|
.map(ProtoUtils::mapStructuredProperty)
|
2019-10-24 17:02:35 +02:00
|
|
|
.collect(Collectors.toList()));
|
|
|
|
|
2019-10-24 17:26:55 +02:00
|
|
|
result.setDateofcollection(entity.getDateofcollection());
|
|
|
|
|
|
|
|
result.setDateoftransformation(entity.getDateoftransformation());
|
2019-10-24 17:02:35 +02:00
|
|
|
|
2019-10-24 17:26:55 +02:00
|
|
|
result.setExtraInfo(entity.getExtraInfoList()
|
|
|
|
.stream()
|
|
|
|
.map(ProtoUtils::mapExtraInfo)
|
|
|
|
.collect(Collectors.toList()));
|
2019-10-24 17:02:35 +02:00
|
|
|
|
|
|
|
return result;
|
2019-10-24 16:00:28 +02:00
|
|
|
}
|
|
|
|
|
2019-10-24 16:56:13 +02:00
|
|
|
private static Project convertProject(OafProtos.Oaf oaf) {
|
2019-10-24 16:00:28 +02:00
|
|
|
return new Project();
|
|
|
|
}
|
|
|
|
|
2019-10-24 16:56:13 +02:00
|
|
|
private static Result convertResult(OafProtos.Oaf oaf) {
|
2019-10-24 16:00:28 +02:00
|
|
|
switch (oaf.getEntity().getResult().getMetadata().getResulttype().getClassid()) {
|
|
|
|
case "dataset":
|
|
|
|
return createDataset(oaf);
|
|
|
|
case "publication":
|
|
|
|
return createPublication(oaf);
|
|
|
|
case "software":
|
|
|
|
return createSoftware(oaf);
|
|
|
|
case "orp":
|
|
|
|
return createORP(oaf);
|
|
|
|
default:
|
|
|
|
throw new RuntimeException("received unknown type :"+oaf.getEntity().getResult().getMetadata().getResulttype().getClassid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 16:56:13 +02:00
|
|
|
private static Software createSoftware(OafProtos.Oaf oaf) {
|
2019-10-24 16:00:28 +02:00
|
|
|
return new Software();
|
|
|
|
}
|
|
|
|
|
2019-10-24 16:56:13 +02:00
|
|
|
private static OtherResearchProducts createORP(OafProtos.Oaf oaf) {
|
2019-10-24 16:00:28 +02:00
|
|
|
return new OtherResearchProducts();
|
|
|
|
}
|
|
|
|
|
2019-10-24 16:56:13 +02:00
|
|
|
private static Publication createPublication(OafProtos.Oaf oaf) {
|
2019-10-24 16:00:28 +02:00
|
|
|
return new Publication();
|
|
|
|
}
|
|
|
|
|
2019-10-24 16:56:13 +02:00
|
|
|
private static Dataset createDataset(OafProtos.Oaf oaf) {
|
2019-10-24 16:00:28 +02:00
|
|
|
return new Dataset();
|
|
|
|
}
|
|
|
|
}
|