diff --git a/orcid-core/src/main/java/org/orcid/core/utils/v3/SourceUtils.java b/orcid-core/src/main/java/org/orcid/core/utils/v3/SourceUtils.java index 04f10cac2bc..35c8dc4dc1d 100644 --- a/orcid-core/src/main/java/org/orcid/core/utils/v3/SourceUtils.java +++ b/orcid-core/src/main/java/org/orcid/core/utils/v3/SourceUtils.java @@ -316,11 +316,7 @@ public static boolean isSelfAsserted(Source source, String orcid) { assertionOriginOrcid = source.getAssertionOriginOrcid().getPath(); } // If the affiliation source is the user himself or any member with OBO, then, it is considered self asserted - if(orcid.equals(sourceId) || orcid.equals(assertionOriginOrcid)) { - return false; - } else { - return true; - } + return orcid.equals(sourceId) || orcid.equals(assertionOriginOrcid); } public static boolean isSelfAsserted(AffiliationForm af, String orcid) { diff --git a/orcid-core/src/main/java/org/orcid/core/utils/v3/activities/FundingComparators.java b/orcid-core/src/main/java/org/orcid/core/utils/v3/activities/FundingComparators.java index 3f24dce975d..857461b6e12 100644 --- a/orcid-core/src/main/java/org/orcid/core/utils/v3/activities/FundingComparators.java +++ b/orcid-core/src/main/java/org/orcid/core/utils/v3/activities/FundingComparators.java @@ -2,6 +2,9 @@ import java.util.Collections; import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.orcid.core.utils.v3.SourceUtils; import org.orcid.pojo.ajaxForm.FundingForm; @@ -33,8 +36,6 @@ public Comparator getInstance(String key, boolean sortAsc, String comparator = new FundingComparators().TITLE_COMPARATOR; } else if (TYPE_SORT_KEY.equals(key)) { comparator = new FundingComparators().TYPE_COMPARATOR; - } else if (SOURCE_SORT_KEY.equals(key)) { - comparator = new FundingComparators(orcid).SOURCE_COMPARATOR; } if (sortAsc) { @@ -107,9 +108,19 @@ public Comparator getInstance(String key, boolean sortAsc, String return g1.getStartDate().compareTo(g2.getStartDate()); }; - public Comparator SOURCE_COMPARATOR = (g1, g2) -> Boolean.compare(isSelfAsserted(g1), isSelfAsserted(g2)); + public List sortBySource(List fundingGroups, boolean sortAsc, String orcid) { + List selfAsserted = fundingGroups.stream() + .filter(fundingGroup -> SourceUtils.isSelfAsserted(fundingGroup.getDefaultFunding(), orcid)) + .collect(Collectors.toList()); - private boolean isSelfAsserted(FundingGroup fundingGroup) { - return SourceUtils.isSelfAsserted(fundingGroup.getDefaultFunding(), orcid); + List validated = fundingGroups.stream() + .filter(fundingGroup -> !SourceUtils.isSelfAsserted(fundingGroup.getDefaultFunding(), orcid)) + .collect(Collectors.toList()); + + selfAsserted.sort(new FundingComparators().TITLE_COMPARATOR); + validated.sort(new FundingComparators().TITLE_COMPARATOR); + + return (sortAsc ? Stream.concat(selfAsserted.stream(), validated.stream()) : Stream.concat(validated.stream(), selfAsserted.stream())) + .collect(Collectors.toList()); } } diff --git a/orcid-core/src/main/java/org/orcid/pojo/summary/ExternalIdentifiersSummary.java b/orcid-core/src/main/java/org/orcid/pojo/summary/ExternalIdentifiersSummary.java index d4593ba47a7..61fab224d86 100644 --- a/orcid-core/src/main/java/org/orcid/pojo/summary/ExternalIdentifiersSummary.java +++ b/orcid-core/src/main/java/org/orcid/pojo/summary/ExternalIdentifiersSummary.java @@ -86,7 +86,7 @@ public static ExternalIdentifiersSummary valueOf(PersonExternalIdentifier person } if (personExternalIdentifier.getSource() != null) { - form.setValidated(SourceUtils.isSelfAsserted(personExternalIdentifier.getSource(), orcid)); + form.setValidated(!SourceUtils.isSelfAsserted(personExternalIdentifier.getSource(), orcid)); } } return form; diff --git a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/FundingsController.java b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/FundingsController.java index 711f65e75eb..4646516bc42 100644 --- a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/FundingsController.java +++ b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/FundingsController.java @@ -6,11 +6,14 @@ import java.text.NumberFormat; import java.text.ParsePosition; import java.util.ArrayList; +import java.util.Collections; import java.util.Currency; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; @@ -24,7 +27,9 @@ import org.orcid.core.manager.v3.ProfileFundingManager; import org.orcid.core.security.visibility.OrcidVisibilityDefaults; import org.orcid.core.utils.v3.ContributorUtils; +import org.orcid.core.utils.v3.SourceUtils; import org.orcid.core.utils.v3.activities.FundingComparators; +import org.orcid.frontend.web.pagination.WorksPaginator; import org.orcid.frontend.web.util.LanguagesMap; import org.orcid.jaxb.model.common.FundingType; import org.orcid.jaxb.model.common.Relationship; @@ -196,7 +201,12 @@ FundingForm getFunding() { fundingGroups.add(fundingGroup); } - fundingGroups.sort(new FundingComparators().getInstance(sort, sortAsc, getEffectiveUserOrcid())); + if ("source".equals(sort)) { + fundingGroups = new FundingComparators().sortBySource(fundingGroups, sortAsc, getEffectiveUserOrcid()); + } else { + fundingGroups.sort(new FundingComparators().getInstance(sort, sortAsc, getEffectiveUserOrcid())); + } + return fundingGroups; } diff --git a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/PublicProfileController.java b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/PublicProfileController.java index 53144913727..0fb40021d1f 100644 --- a/orcid-web/src/main/java/org/orcid/frontend/web/controllers/PublicProfileController.java +++ b/orcid-web/src/main/java/org/orcid/frontend/web/controllers/PublicProfileController.java @@ -84,11 +84,14 @@ import java.math.BigDecimal; import java.text.NumberFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; import java.util.stream.IntStream; +import java.util.stream.Stream; @Controller public class PublicProfileController extends BaseWorkspaceController { @@ -428,7 +431,11 @@ private boolean isRecordReadyForIndexing(ProfileEntity profile) { fundingGroups.add(fundingGroup); } - fundingGroups.sort(new FundingComparators().getInstance(sort, sortAsc, orcid)); + if ("source".equals(sort)) { + fundingGroups = new FundingComparators().sortBySource(fundingGroups, sortAsc, orcid); + } else { + fundingGroups.sort(new FundingComparators().getInstance(sort, sortAsc, orcid)); + } return fundingGroups; } diff --git a/orcid-web/src/main/java/org/orcid/frontend/web/pagination/WorksPaginator.java b/orcid-web/src/main/java/org/orcid/frontend/web/pagination/WorksPaginator.java index ccb7796241f..58931afd1a3 100644 --- a/orcid-web/src/main/java/org/orcid/frontend/web/pagination/WorksPaginator.java +++ b/orcid-web/src/main/java/org/orcid/frontend/web/pagination/WorksPaginator.java @@ -5,6 +5,8 @@ import java.util.Comparator; import java.util.Iterator; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.annotation.Resource; @@ -45,7 +47,11 @@ public Page getWorksPage(String orcid, int offset, int pageSize, bool Page worksPage = new Page(); if (works != null) { List filteredGroups = filter(works, justPublic); - filteredGroups = sort(filteredGroups, sort, sortAsc, orcid); + if ("source".equals(sort)) { + filteredGroups = sortBySource(filteredGroups, sortAsc, orcid); + } else { + filteredGroups = sort(filteredGroups, sort, sortAsc, orcid); + } worksPage.setTotalGroups(filteredGroups.size()); @@ -65,7 +71,11 @@ public Page getWorksExtendedPage(String orcid, int offset, int pageSi Page worksPage = new Page(); if (works != null) { List filteredGroups = filterWorksExtended(works, justPublic); - filteredGroups = sortExtended(filteredGroups, sort, sortAsc, orcid); + if ("source".equals(sort)) { + filteredGroups = sortBySourceExtended(filteredGroups, sortAsc, orcid); + } else { + filteredGroups = sortExtended(filteredGroups, sort, sortAsc, orcid); + } worksPage.setTotalGroups(filteredGroups.size()); @@ -126,8 +136,6 @@ private List sort(List Collections.sort(list, new DateComparator()); } else if (TYPE_SORT_KEY.equals(sort)) { Collections.sort(list, new TypeComparator()); - } else if (SOURCE_SORT_KEY.equals(sort)) { - Collections.sort(list, new SourceComparator(orcid)); } if (!sortAsc) { @@ -143,8 +151,6 @@ private List sortExtended(List list, Strin Collections.sort(list, new DateComparatorWorkGroupExtended()); } else if (TYPE_SORT_KEY.equals(sort)) { Collections.sort(list, new TypeComparatorWorkGroupExtended()); - } else if (SOURCE_SORT_KEY.equals(sort)) { - Collections.sort(list, new SourceComparatorWorkGroupExtended(orcid)); } if (!sortAsc) { @@ -457,21 +463,37 @@ public int compare(WorkGroupExtended o1, WorkGroupExtended o2) { } } - private class SourceComparatorWorkGroupExtended implements Comparator { - private String orcid; + public List sortBySource(List workGroups, boolean sortAsc, String orcid) { + List selfAsserted = workGroups.stream() + .filter(work -> SourceUtils.isSelfAsserted(work.getWorkSummary().get(0).getSource(), orcid)) + .collect(Collectors.toList()); - SourceComparatorWorkGroupExtended(String orcid) { - this.orcid = orcid; - } + List validated = workGroups.stream() + .filter(work -> !SourceUtils.isSelfAsserted(work.getWorkSummary().get(0).getSource(), orcid)) + .collect(Collectors.toList()); - @Override - public int compare(WorkGroupExtended o1, WorkGroupExtended o2) { - return Boolean.compare(isSelfAsserted(o1.getWorkSummary().get(0)), isSelfAsserted(o2.getWorkSummary().get(0))); - } + selfAsserted.sort(new TitleComparator()); + validated.sort(new TitleComparator()); - private boolean isSelfAsserted(WorkSummaryExtended workSummary) { - return SourceUtils.isSelfAsserted(workSummary.getSource(), orcid); - } + return sortAsc ? Stream.concat(selfAsserted.stream(), validated.stream()) + .collect(Collectors.toList()) : Stream.concat(validated.stream(), selfAsserted.stream()) + .collect(Collectors.toList()); + } + + public List sortBySourceExtended(List workGroups, boolean sortAsc, String orcid) { + List selfAsserted = workGroups.stream() + .filter(work -> SourceUtils.isSelfAsserted(work.getWorkSummary().get(0).getSource(), orcid)) + .collect(Collectors.toList()); + + List validated = workGroups.stream() + .filter(work -> !SourceUtils.isSelfAsserted(work.getWorkSummary().get(0).getSource(), orcid)) + .collect(Collectors.toList()); + + selfAsserted.sort(new TitleComparatorWorkGroupExtended()); + validated.sort(new TitleComparatorWorkGroupExtended()); + + return (sortAsc ? Stream.concat(selfAsserted.stream(), validated.stream()) : Stream.concat(validated.stream(), selfAsserted.stream())) + .collect(Collectors.toList()); } } diff --git a/orcid-web/src/test/java/org/orcid/frontend/web/controllers/FundingsControllerTest.java b/orcid-web/src/test/java/org/orcid/frontend/web/controllers/FundingsControllerTest.java index 711dd988f02..da8c571d045 100644 --- a/orcid-web/src/test/java/org/orcid/frontend/web/controllers/FundingsControllerTest.java +++ b/orcid-web/src/test/java/org/orcid/frontend/web/controllers/FundingsControllerTest.java @@ -646,8 +646,8 @@ public void testGetFundingsJsonSortedBySource() { List fundings = fundingController.getFundingsJson("source", true); assertNotNull(fundings); assertEquals(3, fundings.size()); - assertEquals("4444-4444-4444-4441", fundings.get(0).getFundings().get(0).getSource()); - assertEquals("4444-4444-4444-4443", fundings.get(2).getFundings().get(0).getSource()); + assertEquals("4444-4444-4444-4443", fundings.get(0).getFundings().get(0).getSource()); + assertEquals("4444-4444-4444-4441", fundings.get(2).getFundings().get(0).getSource()); } private FundingForm getFundingForm() { diff --git a/orcid-web/src/test/java/org/orcid/frontend/web/controllers/PublicRecordControllerTest.java b/orcid-web/src/test/java/org/orcid/frontend/web/controllers/PublicRecordControllerTest.java index ea2ec422e91..f910d1a146a 100644 --- a/orcid-web/src/test/java/org/orcid/frontend/web/controllers/PublicRecordControllerTest.java +++ b/orcid-web/src/test/java/org/orcid/frontend/web/controllers/PublicRecordControllerTest.java @@ -155,8 +155,8 @@ public void testGetRecordSummary() { // Check external identifiers assertNotNull(record.getExternalIdentifiers()); assertEquals(3, record.getExternalIdentifiers().size()); - - // Added by member + + // User OBO assertEquals(String.valueOf(19), record.getExternalIdentifiers().get(0).getId()); assertEquals("self_public_user_obo_type", record.getExternalIdentifiers().get(0).getCommonName()); assertEquals("self_public_user_obo_ref", record.getExternalIdentifiers().get(0).getReference()); @@ -168,7 +168,7 @@ public void testGetRecordSummary() { assertEquals("self_public_ref", record.getExternalIdentifiers().get(1).getReference()); assertEquals("http://ext-id/self/public", record.getExternalIdentifiers().get(1).getUrl()); assertFalse(record.getExternalIdentifiers().get(1).isValidated()); - // User OBO + // Added by member assertEquals(String.valueOf(13), record.getExternalIdentifiers().get(2).getId()); assertEquals("public_type", record.getExternalIdentifiers().get(2).getCommonName()); assertEquals("public_ref", record.getExternalIdentifiers().get(2).getReference());