From 2a49c72387d880b145ba33fe9b37e1bc41daee55 Mon Sep 17 00:00:00 2001 From: PradnyaC11 Date: Thu, 10 Apr 2025 11:59:02 -0700 Subject: [PATCH 1/3] [CITE-236] Added sort functions for others parameters --- .../core/service/impl/CitationManager.java | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/core/service/impl/CitationManager.java b/citesphere/src/main/java/edu/asu/diging/citesphere/core/service/impl/CitationManager.java index e2aff4cb4..bbfb6c457 100644 --- a/citesphere/src/main/java/edu/asu/diging/citesphere/core/service/impl/CitationManager.java +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/core/service/impl/CitationManager.java @@ -2,6 +2,7 @@ import java.time.OffsetDateTime; import java.util.ArrayList; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -15,8 +16,6 @@ import javax.transaction.Transactional; import org.bson.types.ObjectId; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; @@ -47,6 +46,7 @@ import edu.asu.diging.citesphere.model.bib.ICitation; import edu.asu.diging.citesphere.model.bib.ICitationCollection; import edu.asu.diging.citesphere.model.bib.ICitationGroup; +import edu.asu.diging.citesphere.model.bib.IPerson; import edu.asu.diging.citesphere.model.bib.IReference; import edu.asu.diging.citesphere.model.bib.ItemType; import edu.asu.diging.citesphere.model.bib.impl.BibField; @@ -93,11 +93,36 @@ public class CitationManager implements ICitationManager { @PostConstruct public void init() { sortFunctions = new HashMap<>(); - sortFunctions.put("title", ((o1, o2) -> { - String o1Title = o1 != null && o1.getTitle() != null ? o1.getTitle() : ""; - String o2Title = o2 != null && o2.getTitle() != null ? o2.getTitle() : ""; - return o1Title.toLowerCase().compareTo(o2Title.toLowerCase()); - })); + + sortFunctions.put("title", (o1, o2) -> { + String t1 = Optional.ofNullable(o1).map(ICitation::getTitle).orElse("").toLowerCase(); + String t2 = Optional.ofNullable(o2).map(ICitation::getTitle).orElse("").toLowerCase(); + return t1.compareTo(t2); + }); + // Author (first author's last name) + sortFunctions.put("author", (o1, o2) -> { + String a1 = o1 != null && o1.getAuthors() != null + ? o1.getAuthors().stream().findFirst().map(IPerson::getLastName).orElse("") + : ""; + String a2 = o2 != null && o2.getAuthors() != null + ? o2.getAuthors().stream().findFirst().map(IPerson::getLastName).orElse("") + : ""; + return a1.compareToIgnoreCase(a2); + }); + + // Date (dateFreetext) + sortFunctions.put("date", (o1, o2) -> { + Integer d1 = Integer.parseInt(Optional.ofNullable(o1).map(ICitation::getDateFreetext).orElse(null)); + Integer d2 = Integer.parseInt(Optional.ofNullable(o2).map(ICitation::getDateFreetext).orElse(null)); + return d1.compareTo(d2); + }); + + // URL + sortFunctions.put("url", (o1, o2) -> { + String u1 = Optional.ofNullable(o1).map(ICitation::getUrl).orElse("").toLowerCase(); + String u2 = Optional.ofNullable(o2).map(ICitation::getUrl).orElse("").toLowerCase(); + return u1.compareTo(u2); + }); } @Override @@ -443,6 +468,11 @@ public CitationResults getGroupItems(IUser user, String groupId, String collecti total = citations.size(); } } + BiFunction sorter = sortFunctions.get(sortBy); + if (sorter != null) { + Comparator comparator = sorter::apply; + citations.sort(comparator); + } results.setCitations(citations != null ? citations : new ArrayList<>()); results.setTotalResults(total); return results; From 2825ca0a271434ed4254faab9ee2f4c096a6c064 Mon Sep 17 00:00:00 2001 From: PradnyaC11 Date: Fri, 11 Apr 2025 16:38:45 -0700 Subject: [PATCH 2/3] [CITE-236] Updated sort function and resolved the error --- .../api/v1/user/ItemsApiController.java | 2 +- .../core/service/impl/CitationManager.java | 12 +++++++ .../web/user/CollectionItemsController.java | 2 +- .../web/user/FetchItemsController.java | 2 +- .../web/user/GroupItemsController.java | 6 ++-- .../web/user/MoveItemsController.java | 5 +-- .../WEB-INF/views/auth/group/items.html | 33 ++++++++++++++++++- 7 files changed, 53 insertions(+), 9 deletions(-) diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/api/v1/user/ItemsApiController.java b/citesphere/src/main/java/edu/asu/diging/citesphere/api/v1/user/ItemsApiController.java index b9cd34be3..e83a3f4ac 100644 --- a/citesphere/src/main/java/edu/asu/diging/citesphere/api/v1/user/ItemsApiController.java +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/api/v1/user/ItemsApiController.java @@ -65,7 +65,7 @@ public ResponseEntity getCollectionsByGroupId(@RequestHeader HttpHeaders @PathVariable("zoteroGroupId") String groupId, @PathVariable(value = "collectionId", required = false) String collectionId, @RequestParam(defaultValue = "1", required = false, value = "page") String page, - @RequestParam(defaultValue = "title", required = false, value = "sort") String sort, + @RequestParam(defaultValue = "title", required = false, value = "sortBy") String sort, @RequestParam(required = false, value = "columns") String[] columns, Principal principal) throws GroupDoesNotExistException { Integer pageInt = 1; diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/core/service/impl/CitationManager.java b/citesphere/src/main/java/edu/asu/diging/citesphere/core/service/impl/CitationManager.java index bbfb6c457..1907b181a 100644 --- a/citesphere/src/main/java/edu/asu/diging/citesphere/core/service/impl/CitationManager.java +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/core/service/impl/CitationManager.java @@ -99,6 +99,18 @@ public void init() { String t2 = Optional.ofNullable(o2).map(ICitation::getTitle).orElse("").toLowerCase(); return t1.compareTo(t2); }); + + sortFunctions.put("type", (o1, o2) -> { + String s1 = Optional.ofNullable(o1) + .map(ICitation::getItemType) + .map(Enum::name) + .orElse(""); + String s2 = Optional.ofNullable(o2) + .map(ICitation::getItemType) + .map(Enum::name) + .orElse(""); + return s1.compareToIgnoreCase(s2); + }); // Author (first author's last name) sortFunctions.put("author", (o1, o2) -> { String a1 = o1 != null && o1.getAuthors() != null diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/CollectionItemsController.java b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/CollectionItemsController.java index 0bd328a3a..75026ae15 100644 --- a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/CollectionItemsController.java +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/CollectionItemsController.java @@ -33,7 +33,7 @@ public class CollectionItemsController { public String show(Authentication authentication, Model model, @PathVariable("zoteroGroupId") String groupId, @RequestParam(defaultValue = "1", required = false, value = "page") String page, - @RequestParam(defaultValue = "title", required = false, value = "sort") String sort) + @RequestParam(defaultValue = "title", required = false, value = "sortBy") String sort) throws GroupDoesNotExistException, ZoteroHttpStatusException { Integer pageInt = 1; try { diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/FetchItemsController.java b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/FetchItemsController.java index 3eca9681d..53a1e451f 100644 --- a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/FetchItemsController.java +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/FetchItemsController.java @@ -58,7 +58,7 @@ public class FetchItemsController { public @ResponseBody String show(Authentication authentication, @PathVariable("zoteroGroupId") String groupId, @PathVariable(value = "collectionId", required = false) String collectionId, @RequestParam(defaultValue = "1", required = false, value = "page") String page, - @RequestParam(defaultValue = "title", required = false, value = "sort") String sort, + @RequestParam(defaultValue = "title", required = false, value = "sortBy") String sort, @RequestParam(required = false, value = "columns") String[] columns) { Integer pageInt = 1; try { diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/GroupItemsController.java b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/GroupItemsController.java index b4c7de9ef..ead3f7299 100644 --- a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/GroupItemsController.java +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/GroupItemsController.java @@ -58,7 +58,7 @@ public class GroupItemsController { public String show(Authentication authentication, Model model, @PathVariable("zoteroGroupId") String groupId, @PathVariable(value="collectionId", required=false) String collectionId, @RequestParam(defaultValue = "1", required = false, value = "page") String page, - @RequestParam(defaultValue = "title", required = false, value = "sort") String sort, + @RequestParam(defaultValue = "title", required = false, value = "sortBy") String sortBy, @RequestParam(required = false, value = "columns") String[] columns, @RequestParam(required = false, defaultValue = "", value = "conceptIds") String[] conceptIds) { Integer pageInt = 1; @@ -70,7 +70,7 @@ public String show(Authentication authentication, Model model, @PathVariable("zo IUser user = (IUser) authentication.getPrincipal(); CitationResults results; try { - results = citationManager.getGroupItems(user, groupId, collectionId, pageInt, sort, Arrays.asList(conceptIds)); + results = citationManager.getGroupItems(user, groupId, collectionId, pageInt, sortBy, Arrays.asList(conceptIds)); } catch(ZoteroHttpStatusException e) { logger.error("Exception occured", e); return "error/500"; @@ -86,7 +86,7 @@ public String show(Authentication authentication, Model model, @PathVariable("zo model.addAttribute("zoteroGroupId", groupId); model.addAttribute("group", groupManager.getGroup(user, groupId)); model.addAttribute("collectionId", collectionId); - model.addAttribute("sort", sort); + model.addAttribute("sortBy", sortBy); model.addAttribute("results", results); // more than 200 really don't make sense here, this needs to be changed try { diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/MoveItemsController.java b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/MoveItemsController.java index 0858c497f..7b91ecf45 100644 --- a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/MoveItemsController.java +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/MoveItemsController.java @@ -98,10 +98,11 @@ public class MoveItemsController { }) public @ResponseBody Sync startSync(Authentication authentication, @PathVariable("zoteroGroupId") String zoteroGroupId, @PathVariable("targetCollectionId") String collectionId, - @RequestParam(defaultValue = "1", required = false, value = "page") String page) { + @RequestParam(defaultValue = "1", required = false, value = "page") String page, + @RequestParam(defaultValue = "title", required = false, value = "sortBy") String sortBy) { try { citationManager.getGroupItems((IUser) authentication.getPrincipal(), zoteroGroupId, collectionId, - new Integer(page), null, null); + new Integer(page), sortBy, null); Sync sync = new Sync(); sync.setStatus("sync-started"); return sync; diff --git a/citesphere/src/main/webapp/WEB-INF/views/auth/group/items.html b/citesphere/src/main/webapp/WEB-INF/views/auth/group/items.html index 3965aeff8..18b09577e 100644 --- a/citesphere/src/main/webapp/WEB-INF/views/auth/group/items.html +++ b/citesphere/src/main/webapp/WEB-INF/views/auth/group/items.html @@ -550,6 +550,23 @@

$("#addionalColumns").replaceWith(columnsList); } +function onSortChange(sortBy) { + const sortLabelMap = { + title: "Title", + type: "Type", + author: "Author", + date: "Date", + url: "URL" + }; + + const label = sortLabelMap[sortBy] || sortBy; + document.getElementById("sortLabel").textContent = "Sort by: " + label; + + const params = new URLSearchParams(window.location.search); + params.set("sortBy", sortBy); + window.location.search = params.toString(); +} + @@ -648,7 +665,7 @@

-