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

feature: Add sorting by source functionality to funding and works #6895

Merged
merged 2 commits into from
Oct 2, 2023
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 @@ -3,6 +3,7 @@
import java.util.Collections;
import java.util.Comparator;

import org.orcid.core.utils.v3.SourceUtils;
import org.orcid.pojo.ajaxForm.FundingForm;
import org.orcid.pojo.grouping.FundingGroup;

Expand All @@ -13,17 +14,24 @@ public class FundingComparators {
private static final String DATE_SORT_KEY = "date";

private static final String TYPE_SORT_KEY = "type";

public static Comparator<FundingGroup> getInstance(String key, boolean sortAsc) {

private static final String SOURCE_SORT_KEY = "source";

private static String orcid = null;

public static Comparator<FundingGroup> getInstance(String key, boolean sortAsc, String orcid) {
Comparator<FundingGroup> comparator = null;
if (DATE_SORT_KEY.equals(key)) {
comparator = FundingComparators.DATE_COMPARATOR;
} else if (TITLE_SORT_KEY.equals(key)) {
comparator = FundingComparators.TITLE_COMPARATOR;
} else if (TYPE_SORT_KEY.equals(key)) {
comparator = FundingComparators.TYPE_COMPARATOR;
} else if (SOURCE_SORT_KEY.equals(key)) {
FundingComparators.orcid = orcid;
comparator = FundingComparators.SOURCE_COMPARATOR;
}

if (sortAsc) {
return comparator;
} else {
Expand Down Expand Up @@ -94,4 +102,9 @@ public static Comparator<FundingGroup> getInstance(String key, boolean sortAsc)
return g1.getStartDate().compareTo(g2.getStartDate());
};

public static Comparator<FundingGroup> SOURCE_COMPARATOR = (g1, g2) -> Boolean.compare(isSelfAsserted(g1), isSelfAsserted(g2));

private static boolean isSelfAsserted(FundingGroup fundingGroup) {
return SourceUtils.isSelfAsserted(fundingGroup.getSource(), FundingComparators.orcid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ FundingForm getFunding() {
fundingGroups.add(fundingGroup);
}

fundingGroups.sort(FundingComparators.getInstance(sort, sortAsc));
fundingGroups.sort(FundingComparators.getInstance(sort, sortAsc, getEffectiveUserOrcid()));
return fundingGroups;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ private boolean isRecordReadyForIndexing(ProfileEntity profile) {
fundingGroups.add(fundingGroup);
}

fundingGroups.sort(FundingComparators.getInstance(sort, sortAsc));
fundingGroups.sort(FundingComparators.getInstance(sort, sortAsc, orcid));
return fundingGroups;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.orcid.core.manager.v3.WorksCacheManager;
import org.orcid.core.manager.v3.WorksExtendedCacheManager;
import org.orcid.core.manager.v3.read_only.WorkManagerReadOnly;
import org.orcid.core.utils.v3.SourceUtils;
import org.orcid.jaxb.model.v3.release.common.PublicationDate;
import org.orcid.jaxb.model.v3.release.common.Visibility;
import org.orcid.jaxb.model.v3.release.record.summary.WorkSummary;
Expand All @@ -27,7 +28,9 @@ public class WorksPaginator {
static final String DATE_SORT_KEY = "date";

static final String TYPE_SORT_KEY = "type";


static final String SOURCE_SORT_KEY = "source";

@Resource(name = "workManagerReadOnlyV3")
private WorkManagerReadOnly workManagerReadOnly;

Expand All @@ -42,7 +45,7 @@ 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);
filteredGroups = sort(filteredGroups, sort, sortAsc, orcid);

worksPage.setTotalGroups(filteredGroups.size());

Expand All @@ -62,7 +65,7 @@ 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);
filteredGroups = sortExtended(filteredGroups, sort, sortAsc, orcid);

worksPage.setTotalGroups(filteredGroups.size());

Expand All @@ -79,7 +82,7 @@ public Page<WorkGroup> getWorksExtendedPage(String orcid, int offset, int pageSi

public Page<WorkGroup> refreshWorks(String orcid, int limit, String sort, boolean sortAsc) {
Works works = worksCacheManager.getGroupedWorks(orcid);
List<org.orcid.jaxb.model.v3.release.record.summary.WorkGroup> sortedGroups = sort(works.getWorkGroup(), sort, sortAsc);
List<org.orcid.jaxb.model.v3.release.record.summary.WorkGroup> sortedGroups = sort(works.getWorkGroup(), sort, sortAsc, orcid);

Page<WorkGroup> worksPage = new Page<WorkGroup>();
worksPage.setTotalGroups(sortedGroups.size());
Expand All @@ -100,7 +103,7 @@ public Page<WorkGroup> getAllWorks(String orcid, boolean justPublic, String sort
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);
filteredGroups = sort(filteredGroups, sort, sortAsc, orcid);

worksPage.setTotalGroups(filteredGroups.size());

Expand All @@ -116,13 +119,15 @@ public Page<WorkGroup> getAllWorks(String orcid, boolean justPublic, String sort
return worksPage;
}

private List<org.orcid.jaxb.model.v3.release.record.summary.WorkGroup> sort(List<org.orcid.jaxb.model.v3.release.record.summary.WorkGroup> list, String sort, boolean sortAsc) {
private List<org.orcid.jaxb.model.v3.release.record.summary.WorkGroup> sort(List<org.orcid.jaxb.model.v3.release.record.summary.WorkGroup> list, String sort, boolean sortAsc, String orcid) {
if (TITLE_SORT_KEY.equals(sort)) {
Collections.sort(list, new TitleComparator());
} else if (DATE_SORT_KEY.equals(sort)) {
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 @@ -131,13 +136,15 @@ private List<org.orcid.jaxb.model.v3.release.record.summary.WorkGroup> sort(List
return list;
}

private List<WorkGroupExtended> sortExtended(List<WorkGroupExtended> list, String sort, boolean sortAsc) {
private List<WorkGroupExtended> sortExtended(List<WorkGroupExtended> list, String sort, boolean sortAsc, String orcid) {
if (TITLE_SORT_KEY.equals(sort)) {
Collections.sort(list, new TitleComparatorWorkGroupExtended());
} else if (DATE_SORT_KEY.equals(sort)) {
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 @@ -312,6 +319,24 @@ public int compare(org.orcid.jaxb.model.v3.release.record.summary.WorkGroup o1,
}
}

private class SourceComparator implements Comparator<org.orcid.jaxb.model.v3.release.record.summary.WorkGroup> {

private String orcid;

SourceComparator(String orcid) {
this.orcid = orcid;
}

@Override
public int compare(org.orcid.jaxb.model.v3.release.record.summary.WorkGroup o1, org.orcid.jaxb.model.v3.release.record.summary.WorkGroup o2) {
return Boolean.compare(isSelfAsserted(o1.getWorkSummary().get(0)), isSelfAsserted(o2.getWorkSummary().get(0)));
}

private boolean isSelfAsserted(WorkSummary workSummary) {
return SourceUtils.isSelfAsserted(workSummary.getSource(), orcid);
}
}

private class DateComparatorWorkGroupExtended implements Comparator<WorkGroupExtended> {

@Override
Expand Down Expand Up @@ -432,4 +457,21 @@ public int compare(WorkGroupExtended o1, WorkGroupExtended o2) {
}
}

private class SourceComparatorWorkGroupExtended implements Comparator<WorkGroupExtended> {
private String orcid;

SourceComparatorWorkGroupExtended(String orcid) {
this.orcid = orcid;
}

@Override
public int compare(WorkGroupExtended o1, WorkGroupExtended o2) {
return Boolean.compare(isSelfAsserted(o1.getWorkSummary().get(0)), isSelfAsserted(o2.getWorkSummary().get(0)));
}

private boolean isSelfAsserted(WorkSummaryExtended workSummary) {
return SourceUtils.isSelfAsserted(workSummary.getSource(), orcid);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,19 @@ public void testAddFundingWithInvalidDates() throws Exception {
assertEquals(fundingController.getMessage("fundings.endDate.after"), funding.getErrors().get(0));
}

@Test
public void testGetFundingsJsonSortedBySource() {
HttpSession session = mock(HttpSession.class);
when(servletRequest.getSession()).thenReturn(session);
when(localeManager.getLocale()).thenReturn(new Locale("us", "EN"));

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

private FundingForm getFundingForm() {
FundingForm funding = fundingController.getFunding();
funding.setFundingType(Text.valueOf("award"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.stream.Collectors;

import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -74,7 +74,7 @@ public void setUp() {
public void testGetWorksPage() {
int pageSize = 100;

Mockito.when(worksCacheManager.getGroupedWorks(Mockito.anyString())).thenReturn(get1000PublicWorkGroups());
Mockito.when(worksCacheManager.getGroupedWorks(Mockito.anyString())).thenReturn(getPublicWorkGroups(1000));
Page<org.orcid.pojo.grouping.WorkGroup> page = worksPaginator.getWorksPage("orcid", 0, pageSize, false, WorksPaginator.DATE_SORT_KEY, true);
assertEquals(pageSize, page.getGroups().size());
org.orcid.pojo.grouping.WorkGroup workGroupPage1 = page.getGroups().get(0);
Expand Down Expand Up @@ -167,28 +167,22 @@ public void testGetPublicWorksExtendedPage() {
}

@Test
public void testTitleSortCaseInsensitive() {
int pageSize = 100;

Works works = get1000PublicWorkGroups();
for (WorkGroup workGroup : works.getWorkGroup()) {
if (new Random().nextBoolean()) {
for (WorkSummary summary : workGroup.getWorkSummary()) {
summary.getTitle().setTitle(new Title(summary.getTitle().getTitle().getContent().toUpperCase()));
}
public void testSourceSort() {
int pageSize = 50;

Works works = getPublicWorkGroups(50);
List<WorkGroup> workGroups = works.getWorkGroup().stream().limit(2).collect(Collectors.toList());
for (WorkGroup workGroup : workGroups) {
for (WorkSummary summary : workGroup.getWorkSummary()) {
summary.setSource(new Source("orcid"));
}
}

Mockito.when(worksCacheManager.getGroupedWorks(Mockito.anyString())).thenReturn(works);
Page<org.orcid.pojo.grouping.WorkGroup> page = worksPaginator.getWorksPage("orcid", 0, pageSize, false, WorksPaginator.TITLE_SORT_KEY, true);
Page<org.orcid.pojo.grouping.WorkGroup> page = worksPaginator.getWorksPage("orcid", 0, pageSize, false, WorksPaginator.SOURCE_SORT_KEY, false);

org.orcid.pojo.grouping.WorkGroup previous = page.getGroups().remove(0);
while (!page.getGroups().isEmpty()) {
org.orcid.pojo.grouping.WorkGroup next = page.getGroups().remove(0);
String previousTitle = previous.getWorks().get(0).getTitle().getValue();
String nextTitle = next.getWorks().get(0).getTitle().getValue();
assertTrue(previousTitle.toLowerCase().compareTo(nextTitle.toLowerCase()) <= 0);
previous = next;
}
assertEquals("APP-5555-5555-5555-5555", page.getGroups().get(0).getWorks().get(0).getSource());
assertEquals("orcid", page.getGroups().get(49).getWorks().get(0).getSource());
}

@Test
Expand All @@ -208,10 +202,29 @@ public void testReverseSecondaryTitleSortForNullDates() {
previous = next;
}
}


@Test
public void testTitleSortCase() {
int pageSize = 50;

Works works = getPublicWorkGroups(50);
List<WorkGroup> workGroups = works.getWorkGroup().stream().limit(2).collect(Collectors.toList());
for (WorkGroup workGroup : workGroups) {
for (WorkSummary summary : workGroup.getWorkSummary()) {
summary.setSource(new Source("orcid"));
}
}

Mockito.when(worksCacheManager.getGroupedWorks(Mockito.anyString())).thenReturn(works);
Page<org.orcid.pojo.grouping.WorkGroup> page = worksPaginator.getWorksPage("orcid", 0, pageSize, false, WorksPaginator.SOURCE_SORT_KEY, false);

assertEquals("APP-5555-5555-5555-5555", page.getGroups().get(0).getWorks().get(0).getSource());
assertEquals("orcid", page.getGroups().get(49).getWorks().get(0).getSource());
}

@Test
public void testGetAllWorks() {
Works works = get1000PublicWorkGroups();
Works works = getPublicWorkGroups(1000);
Mockito.when(worksCacheManager.getGroupedWorks(Mockito.anyString())).thenReturn(works);
Page<org.orcid.pojo.grouping.WorkGroup> page = worksPaginator.getAllWorks("orcid", false, WorksPaginator.TITLE_SORT_KEY, true);
assertEquals(1000, page.getTotalGroups());
Expand Down Expand Up @@ -257,7 +270,7 @@ public void testGetWorkWithNulltitle() {
}

private Works getWorkGroupsWithNullDates() {
Works works = get1000PublicWorkGroups();
Works works = getPublicWorkGroups(1000);
for (WorkGroup workGroup : works.getWorkGroup()) {
for (WorkSummary workSummary : workGroup.getWorkSummary()) {
workSummary.setPublicationDate(null);
Expand All @@ -266,12 +279,12 @@ private Works getWorkGroupsWithNullDates() {
return works;
}

private Works get1000PublicWorkGroups() {
private Works getPublicWorkGroups(int numberOfWorks) {
Works works = new Works();
works.setLastModifiedDate(new LastModifiedDate(DateUtils.convertToXMLGregorianCalendar(System.currentTimeMillis())));
works.setPath("some path");

for (int i = 0; i < 1000; i++) {
for (int i = 0; i < numberOfWorks; i++) {
works.getWorkGroup().add(getPublicWorkGroup(i));
}
return works;
Expand Down