Skip to content

Commit

Permalink
UBO-281 Do not rely on certain values of display index of a work. Imp…
Browse files Browse the repository at this point in the history
…roved code readability (#333)
  • Loading branch information
Possommi authored Dec 14, 2023
1 parent b7f25e0 commit d32ab7d
Showing 1 changed file with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package org.mycore.ubo.importer.orcid;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jdom2.Element;
Expand All @@ -21,7 +16,13 @@
import org.orcid.jaxb.model.v3.release.record.summary.WorkSummary;
import org.orcid.jaxb.model.v3.release.record.summary.Works;

public class Orcid2WorksTransformer extends MCRContentTransformer {
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Orcid2WorksTransformer extends MCRContentTransformer implements Comparator<WorkSummary> {

protected Logger LOGGER = LogManager.getLogger(Orcid2WorksTransformer.class);

Expand All @@ -32,17 +33,26 @@ public MCRJDOMContent transform(MCRContent source) throws IOException {
.filter(orcid -> orcid.trim().length() > 0)
.forEach(orcid -> {
try {
Works works = MCRORCIDClientHelper.getClientFactory().createReadClient().fetch(orcid,
MCRORCIDSectionImpl.WORKS, Works.class);
Works works = MCRORCIDClientHelper
.getClientFactory()
.createReadClient()
.fetch(orcid, MCRORCIDSectionImpl.WORKS, Works.class);

for (WorkGroup wg : works.getWorkGroup()) {
WorkSummary ws = wg.getWorkSummary().stream()
.filter(workSummary -> workSummary.getDisplayIndex().equals("1")).findFirst().get();
works.getWorkGroup()
.stream()
.map(WorkGroup::getWorkSummary)
.filter(wg -> wg.size() > 0)
.forEach(workSummaries -> {
workSummaries.sort(this);
WorkSummary workSummary = workSummaries.get(0);

Work work = MCRORCIDClientHelper.getClientFactory().createReadClient().fetch(orcid,
MCRORCIDSectionImpl.WORK, Work.class, ws.getPutCode());
workList.add(work);
}
Work work = MCRORCIDClientHelper
.getClientFactory()
.createReadClient()
.fetch(orcid, MCRORCIDSectionImpl.WORK, Work.class, workSummary.getPutCode());

workList.add(work);
});
} catch (MCRORCIDRequestException e) {
LOGGER.warn("Could not get works for ORCID {}. {}", orcid, e.getMessage());
}
Expand All @@ -53,4 +63,16 @@ public MCRJDOMContent transform(MCRContent source) throws IOException {

return new MCRJDOMContent(modsCollection);
}

@Override
public int compare(WorkSummary ws1, WorkSummary ws2) {
int v = 0;
try {
v = Integer.parseInt(ws1.getDisplayIndex()) - Integer.parseInt(
ws2.getDisplayIndex());
} catch (Exception e) {
LOGGER.error("Could not compare WorkSummaries", e);
}
return v;
}
}

0 comments on commit d32ab7d

Please sign in to comment.