Skip to content

Commit

Permalink
Merge pull request #1810 from TexasDigitalLibrary/sprint5-hotfix-save…
Browse files Browse the repository at this point in the history
…_filter_still_being_used

Hotfix: Making changes after selecting a saved filter alters the saved filter.
  • Loading branch information
kaladay authored Jun 22, 2023
2 parents 13f674d + 1cff26b commit fd5f00b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ public ApiResponse setActiveFilter(@WeaverUser User user, @RequestBody NamedSear
return new ApiResponse(ERROR, "Failed to find filter with ID " + namedSearchFilterGroup.getId() + ".");
}

user.setActiveFilter(desiredFilter.get());

user = userRepo.update(user);
user = userRepo.setActiveFilter(user, desiredFilter.get());

simpMessagingTemplate.convertAndSend("/channel/active-filters/user/" + user.getId(), new ApiResponse(SUCCESS, FilterAction.SET, user.getActiveFilter()));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.tdl.vireo.model.repo.custom;

import org.tdl.vireo.model.NamedSearchFilterGroup;
import org.tdl.vireo.model.Role;
import org.tdl.vireo.model.User;

Expand All @@ -11,4 +12,6 @@ public interface UserRepoCustom {

public User clearActiveFilter(User user);

public User setActiveFilter(User user, NamedSearchFilterGroup filter);

}
40 changes: 40 additions & 0 deletions src/main/java/org/tdl/vireo/model/repo/impl/UserRepoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.tdl.vireo.model.NamedSearchFilter;
import org.tdl.vireo.model.NamedSearchFilterGroup;
import org.tdl.vireo.model.Role;
import org.tdl.vireo.model.SubmissionListColumn;
import org.tdl.vireo.model.User;
import org.tdl.vireo.model.repo.NamedSearchFilterGroupRepo;
import org.tdl.vireo.model.repo.NamedSearchFilterRepo;
Expand Down Expand Up @@ -100,6 +101,45 @@ public User clearActiveFilter(User user) {
return user;
}

/**
* Set the active filter group.
*
* This copies the active filter if it is a saved filter to isolate it from the saved filter.
*
* @param user The user to update the active filter group of.
* @param filter The active filter group to set.
*
* @return The updated user model.
*/
public User setActiveFilter(User user, NamedSearchFilterGroup filter) {

if (filter.getName() == null) {
user.setActiveFilter(filter);
return userRepo.update(user);
}

NamedSearchFilterGroup persistedFilterGroup = namedSearchFilterGroupRepo.getOrCreatePersistedActiveFilterForUser(user);

persistedFilterGroup.getNamedSearchFilters().clear();
persistedFilterGroup.getSavedColumns().clear();
persistedFilterGroup.setColumnsFlag(persistedFilterGroup.getColumnsFlag());

for (NamedSearchFilter namedSearchFilter : filter.getNamedSearchFilters()) {
persistedFilterGroup.addFilterCriterion(namedSearchFilterRepo.clone(namedSearchFilter));
};

if (filter.getColumnsFlag()) {
for (SubmissionListColumn column : filter.getSavedColumns()) {
persistedFilterGroup.addSavedColumn(column);
}
}

persistedFilterGroup = namedSearchFilterGroupRepo.save(persistedFilterGroup);

user.setActiveFilter(persistedFilterGroup);
return userRepo.update(user);
}

@Override
public User update(User user) {
initializeActiveFilter(user);
Expand Down

0 comments on commit fd5f00b

Please sign in to comment.