master #59

Closed
claudio.atzori wants to merge 3221 commits from master into stable_ids
2 changed files with 126 additions and 50 deletions
Showing only changes of commit d40e20f437 - Show all commits

View File

@ -2,7 +2,10 @@
package eu.dnetlib.dhp.broker.oa.util; package eu.dnetlib.dhp.broker.oa.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -45,13 +48,10 @@ public class ConversionUtils {
private static final Logger log = LoggerFactory.getLogger(ConversionUtils.class); private static final Logger log = LoggerFactory.getLogger(ConversionUtils.class);
private ConversionUtils() { private ConversionUtils() {}
}
public static List<OaBrokerInstance> oafInstanceToBrokerInstances(final Instance i) { public static List<OaBrokerInstance> oafInstanceToBrokerInstances(final Instance i) {
if (i == null) { if (i == null) { return new ArrayList<>(); }
return new ArrayList<>();
}
return mappedList(i.getUrl(), url -> { return mappedList(i.getUrl(), url -> {
final OaBrokerInstance res = new OaBrokerInstance(); final OaBrokerInstance res = new OaBrokerInstance();
@ -72,30 +72,26 @@ public class ConversionUtils {
} }
public static OaBrokerRelatedDataset oafDatasetToBrokerDataset(final Dataset d) { public static OaBrokerRelatedDataset oafDatasetToBrokerDataset(final Dataset d) {
if (d == null) { if (d == null) { return null; }
return null;
}
final OaBrokerRelatedDataset res = new OaBrokerRelatedDataset(); final OaBrokerRelatedDataset res = new OaBrokerRelatedDataset();
res.setOpenaireId(cleanOpenaireId(d.getId())); res.setOpenaireId(cleanOpenaireId(d.getId()));
res.setOriginalId(first(d.getOriginalId())); res.setOriginalId(first(d.getOriginalId()));
res.setTitle(structPropValue(d.getTitle())); res.setTitle(structPropValue(d.getTitle()));
res.setPids(mappedList(d.getPid(), ConversionUtils::oafPidToBrokerPid)); res.setPids(allResultPids(d));
res.setInstances(flatMappedList(d.getInstance(), ConversionUtils::oafInstanceToBrokerInstances)); res.setInstances(flatMappedList(d.getInstance(), ConversionUtils::oafInstanceToBrokerInstances));
res.setCollectedFrom(mappedFirst(d.getCollectedfrom(), KeyValue::getValue)); res.setCollectedFrom(mappedFirst(d.getCollectedfrom(), KeyValue::getValue));
return res; return res;
} }
public static OaBrokerRelatedPublication oafPublicationToBrokerPublication(final Publication p) { public static OaBrokerRelatedPublication oafPublicationToBrokerPublication(final Publication p) {
if (p == null) { if (p == null) { return null; }
return null;
}
final OaBrokerRelatedPublication res = new OaBrokerRelatedPublication(); final OaBrokerRelatedPublication res = new OaBrokerRelatedPublication();
res.setOpenaireId(cleanOpenaireId(p.getId())); res.setOpenaireId(cleanOpenaireId(p.getId()));
res.setOriginalId(first(p.getOriginalId())); res.setOriginalId(first(p.getOriginalId()));
res.setTitle(structPropValue(p.getTitle())); res.setTitle(structPropValue(p.getTitle()));
res.setPids(mappedList(p.getPid(), ConversionUtils::oafPidToBrokerPid)); res.setPids(allResultPids(p));
res.setInstances(flatMappedList(p.getInstance(), ConversionUtils::oafInstanceToBrokerInstances)); res.setInstances(flatMappedList(p.getInstance(), ConversionUtils::oafInstanceToBrokerInstances));
res.setCollectedFrom(mappedFirst(p.getCollectedfrom(), KeyValue::getValue)); res.setCollectedFrom(mappedFirst(p.getCollectedfrom(), KeyValue::getValue));
@ -103,9 +99,7 @@ public class ConversionUtils {
} }
public static OaBrokerMainEntity oafResultToBrokerResult(final Result result) { public static OaBrokerMainEntity oafResultToBrokerResult(final Result result) {
if (result == null) { if (result == null) { return null; }
return null;
}
final OaBrokerMainEntity res = new OaBrokerMainEntity(); final OaBrokerMainEntity res = new OaBrokerMainEntity();
@ -122,9 +116,8 @@ public class ConversionUtils {
res.setEmbargoenddate(fieldValue(result.getEmbargoenddate())); res.setEmbargoenddate(fieldValue(result.getEmbargoenddate()));
res.setContributor(fieldList(result.getContributor())); res.setContributor(fieldList(result.getContributor()));
res res
.setJournal( .setJournal(result instanceof Publication ? oafJournalToBrokerJournal(((Publication) result).getJournal()) : null);
result instanceof Publication ? oafJournalToBrokerJournal(((Publication) result).getJournal()) : null); res.setPids(allResultPids(result));
res.setPids(mappedList(result.getPid(), ConversionUtils::oafPidToBrokerPid));
res.setInstances(flatMappedList(result.getInstance(), ConversionUtils::oafInstanceToBrokerInstances)); res.setInstances(flatMappedList(result.getInstance(), ConversionUtils::oafInstanceToBrokerInstances));
res res
.setExternalReferences(mappedList(result.getExternalReference(), ConversionUtils::oafExtRefToBrokerExtRef)); .setExternalReferences(mappedList(result.getExternalReference(), ConversionUtils::oafExtRefToBrokerExtRef));
@ -132,14 +125,23 @@ public class ConversionUtils {
return res; return res;
} }
protected static List<OaBrokerTypedValue> allResultPids(final Result result) {
final Map<String, StructuredProperty> map = new HashMap<>();
result.getPid().forEach(sp -> map.put(sp.getValue(), sp));
result.getInstance().forEach(i -> {
i.getPid().forEach(sp -> map.put(sp.getValue(), sp));
i.getAlternateIdentifier().forEach(sp -> map.put(sp.getValue(), sp));
});
final List<OaBrokerTypedValue> pids = mappedList(map.values(), ConversionUtils::oafPidToBrokerPid);
return pids;
}
public static String cleanOpenaireId(final String id) { public static String cleanOpenaireId(final String id) {
return id.contains("|") ? StringUtils.substringAfter(id, "|") : id; return id.contains("|") ? StringUtils.substringAfter(id, "|") : id;
} }
private static OaBrokerAuthor oafAuthorToBrokerAuthor(final Author author) { private static OaBrokerAuthor oafAuthorToBrokerAuthor(final Author author) {
if (author == null) { if (author == null) { return null; }
return null;
}
final String pids = author.getPid() != null ? author final String pids = author.getPid() != null ? author
.getPid() .getPid()
@ -163,9 +165,7 @@ public class ConversionUtils {
} }
private static OaBrokerJournal oafJournalToBrokerJournal(final Journal journal) { private static OaBrokerJournal oafJournalToBrokerJournal(final Journal journal) {
if (journal == null) { if (journal == null) { return null; }
return null;
}
final OaBrokerJournal res = new OaBrokerJournal(); final OaBrokerJournal res = new OaBrokerJournal();
res.setName(journal.getName()); res.setName(journal.getName());
@ -177,9 +177,7 @@ public class ConversionUtils {
} }
private static OaBrokerExternalReference oafExtRefToBrokerExtRef(final ExternalReference ref) { private static OaBrokerExternalReference oafExtRefToBrokerExtRef(final ExternalReference ref) {
if (ref == null) { if (ref == null) { return null; }
return null;
}
final OaBrokerExternalReference res = new OaBrokerExternalReference(); final OaBrokerExternalReference res = new OaBrokerExternalReference();
res.setRefidentifier(ref.getRefidentifier()); res.setRefidentifier(ref.getRefidentifier());
@ -190,9 +188,7 @@ public class ConversionUtils {
} }
public static OaBrokerProject oafProjectToBrokerProject(final Project p) { public static OaBrokerProject oafProjectToBrokerProject(final Project p) {
if (p == null) { if (p == null) { return null; }
return null;
}
final OaBrokerProject res = new OaBrokerProject(); final OaBrokerProject res = new OaBrokerProject();
res.setOpenaireId(cleanOpenaireId(p.getId())); res.setOpenaireId(cleanOpenaireId(p.getId()));
@ -216,9 +212,7 @@ public class ConversionUtils {
} }
public static OaBrokerRelatedSoftware oafSoftwareToBrokerSoftware(final Software sw) { public static OaBrokerRelatedSoftware oafSoftwareToBrokerSoftware(final Software sw) {
if (sw == null) { if (sw == null) { return null; }
return null;
}
final OaBrokerRelatedSoftware res = new OaBrokerRelatedSoftware(); final OaBrokerRelatedSoftware res = new OaBrokerRelatedSoftware();
res.setOpenaireId(cleanOpenaireId(sw.getId())); res.setOpenaireId(cleanOpenaireId(sw.getId()));
@ -231,9 +225,7 @@ public class ConversionUtils {
} }
public static OaBrokerRelatedDatasource oafDatasourceToBrokerDatasource(final Datasource ds) { public static OaBrokerRelatedDatasource oafDatasourceToBrokerDatasource(final Datasource ds) {
if (ds == null) { if (ds == null) { return null; }
return null;
}
final OaBrokerRelatedDatasource res = new OaBrokerRelatedDatasource(); final OaBrokerRelatedDatasource res = new OaBrokerRelatedDatasource();
res.setName(StringUtils.defaultIfBlank(fieldValue(ds.getOfficialname()), fieldValue(ds.getEnglishname()))); res.setName(StringUtils.defaultIfBlank(fieldValue(ds.getOfficialname()), fieldValue(ds.getEnglishname())));
@ -293,9 +285,7 @@ public class ConversionUtils {
} }
private static List<OaBrokerTypedValue> structPropTypedList(final List<StructuredProperty> list) { private static List<OaBrokerTypedValue> structPropTypedList(final List<StructuredProperty> list) {
if (list == null) { if (list == null) { return new ArrayList<>(); }
return new ArrayList<>();
}
return list return list
.stream() .stream()
@ -304,10 +294,8 @@ public class ConversionUtils {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
private static <F, T> List<T> mappedList(final List<F> list, final Function<F, T> func) { private static <F, T> List<T> mappedList(final Collection<F> list, final Function<F, T> func) {
if (list == null) { if (list == null) { return new ArrayList<>(); }
return new ArrayList<>();
}
return list return list
.stream() .stream()
@ -318,9 +306,7 @@ public class ConversionUtils {
} }
private static <F, T> List<T> flatMappedList(final List<F> list, final Function<F, List<T>> func) { private static <F, T> List<T> flatMappedList(final List<F> list, final Function<F, List<T>> func) {
if (list == null) { if (list == null) { return new ArrayList<>(); }
return new ArrayList<>();
}
return list return list
.stream() .stream()
@ -332,9 +318,7 @@ public class ConversionUtils {
} }
private static <F, T> T mappedFirst(final List<F> list, final Function<F, T> func) { private static <F, T> T mappedFirst(final List<F> list, final Function<F, T> func) {
if (list == null) { if (list == null) { return null; }
return null;
}
return list return list
.stream() .stream()

View File

@ -0,0 +1,92 @@
package eu.dnetlib.dhp.broker.oa.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import eu.dnetlib.broker.objects.OaBrokerTypedValue;
import eu.dnetlib.dhp.schema.oaf.Instance;
import eu.dnetlib.dhp.schema.oaf.Qualifier;
import eu.dnetlib.dhp.schema.oaf.Result;
import eu.dnetlib.dhp.schema.oaf.StructuredProperty;
class ConversionUtilsTest {
@BeforeEach
void setUp() throws Exception {}
@Test
void testAllResultPids() {
final Qualifier qf = new Qualifier();
qf.setClassid("test");
qf.setClassname("test");
qf.setSchemeid("test");
qf.setSchemename("test");
final StructuredProperty sp1 = new StructuredProperty();
sp1.setValue("1");
sp1.setQualifier(qf);
final StructuredProperty sp2 = new StructuredProperty();
sp2.setValue("2");
sp2.setQualifier(qf);
final StructuredProperty sp3 = new StructuredProperty();
sp3.setValue("3");
sp3.setQualifier(qf);
final StructuredProperty sp4a = new StructuredProperty();
sp4a.setValue("4");
sp4a.setQualifier(qf);
final StructuredProperty sp4b = new StructuredProperty();
sp4b.setValue("4");
sp4b.setQualifier(qf);
final StructuredProperty sp5 = new StructuredProperty();
sp5.setValue("5");
sp5.setQualifier(qf);
final StructuredProperty sp6a = new StructuredProperty();
sp6a.setValue("6");
sp6a.setQualifier(qf);
final StructuredProperty sp6b = new StructuredProperty();
sp6b.setValue("6");
sp6b.setQualifier(qf);
final Result oaf = new Result();
oaf.setPid(new ArrayList<>());
oaf.getPid().add(sp1);
oaf.getPid().add(sp2);
oaf.getPid().add(sp4a);
final Instance instance1 = new Instance();
instance1.setPid(new ArrayList<>());
instance1.setAlternateIdentifier(new ArrayList<>());
instance1.getPid().add(sp3);
instance1.getPid().add(sp4b);
instance1.getAlternateIdentifier().add(sp5);
instance1.getAlternateIdentifier().add(sp6a);
final Instance instance2 = new Instance();
instance2.setPid(new ArrayList<>());
instance2.setAlternateIdentifier(new ArrayList<>());
instance2.getPid().add(sp6b);
oaf.setInstance(new ArrayList<>());
oaf.getInstance().add(instance1);
oaf.getInstance().add(instance2);
final List<OaBrokerTypedValue> list = ConversionUtils.allResultPids(oaf);
// list.forEach(x -> System.out.println(x.getValue()));
assertEquals(6, list.size());
}
}