Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update sort by source functionality to sort also alphabetically #6909

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,8 +36,6 @@ public Comparator<FundingGroup> 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) {
Expand Down Expand Up @@ -107,9 +108,19 @@ public Comparator<FundingGroup> getInstance(String key, boolean sortAsc, String
return g1.getStartDate().compareTo(g2.getStartDate());
};

public Comparator<FundingGroup> SOURCE_COMPARATOR = (g1, g2) -> Boolean.compare(isSelfAsserted(g1), isSelfAsserted(g2));
public List<FundingGroup> sortBySource(List<FundingGroup> fundingGroups, boolean sortAsc, String orcid) {
List<FundingGroup> 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<FundingGroup> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -45,7 +47,11 @@ public Page<WorkGroup> getWorksPage(String orcid, int offset, int pageSize, bool
Page<WorkGroup> worksPage = new Page<WorkGroup>();
if (works != null) {
List<org.orcid.jaxb.model.v3.release.record.summary.WorkGroup> 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());

Expand All @@ -65,7 +71,11 @@ public Page<WorkGroup> getWorksExtendedPage(String orcid, int offset, int pageSi
Page<WorkGroup> worksPage = new Page<WorkGroup>();
if (works != null) {
List<WorkGroupExtended> 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());

Expand Down Expand Up @@ -126,8 +136,6 @@ private List<org.orcid.jaxb.model.v3.release.record.summary.WorkGroup> 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) {
Expand All @@ -143,8 +151,6 @@ private List<WorkGroupExtended> sortExtended(List<WorkGroupExtended> 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) {
Expand Down Expand Up @@ -457,21 +463,37 @@ public int compare(WorkGroupExtended o1, WorkGroupExtended o2) {
}
}

private class SourceComparatorWorkGroupExtended implements Comparator<WorkGroupExtended> {
private String orcid;
public List<org.orcid.jaxb.model.v3.release.record.summary.WorkGroup> sortBySource(List<org.orcid.jaxb.model.v3.release.record.summary.WorkGroup> workGroups, boolean sortAsc, String orcid) {
List<org.orcid.jaxb.model.v3.release.record.summary.WorkGroup> selfAsserted = workGroups.stream()
.filter(work -> SourceUtils.isSelfAsserted(work.getWorkSummary().get(0).getSource(), orcid))
.collect(Collectors.toList());

SourceComparatorWorkGroupExtended(String orcid) {
this.orcid = orcid;
}
List<org.orcid.jaxb.model.v3.release.record.summary.WorkGroup> 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<WorkGroupExtended> sortBySourceExtended(List<WorkGroupExtended> workGroups, boolean sortAsc, String orcid) {
List<WorkGroupExtended> selfAsserted = workGroups.stream()
.filter(work -> SourceUtils.isSelfAsserted(work.getWorkSummary().get(0).getSource(), orcid))
.collect(Collectors.toList());

List<WorkGroupExtended> 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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,8 @@ public void testGetFundingsJsonSortedBySource() {
List<FundingGroup> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down