Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
searchContainer doesn't change after removing an object from User List L6.2
I'm creating a hook for Site Members Directory. My requirement is to filter the search container based on a String value. I was able to remove the objects that I don't want and return new User List with a new size. But the problem is that the results show just the first page of results. For example, let's say I run a search and return 20 objects and after the filter, I return 15 objects. In my view page, it will only display the 1st page and no pagination. I have set delta to 5 so it should show 3 pages but it shows only one.
<% userSearchContainer.setDelta(5);
if (searchTerms.isAdvancedSearch()) { total = UserLocalServiceUtil.searchCount(company.getCompanyId(), searchTerms.getFirstName(), searchTerms.getMiddleName(), searchTerms.getLastName(), searchTerms.getScreenName(), searchTerms.getEmailAddress(), searchTerms.getStatus(), userParams, searchTerms.isAndOperator());
userSearchContainer.setTotal(total);
results = UserLocalServiceUtil.search(company.getCompanyId(), searchTerms.getFirstName(), searchTerms.getMiddleName(), searchTerms.getLastName(), searchTerms.getScreenName(), searchTerms.getEmailAddress(), searchTerms.getStatus(), userParams, searchTerms.isAndOperator(), userSearchContainer.getStart(), userSearchContainer.getEnd(), userSearchContainer.getOrderByComparator());
} else if (Validator.isNotNull(searchTerms.getKeywords())){ total = UserLocalServiceUtil.searchCount(company.getCompanyId(), searchTerms.getKeywords(), searchTerms.getStatus(), userParams);
userSearchContainer.setTotal(total); results = UserLocalServiceUtil.search(company.getCompanyId(), searchTerms.getKeywords(), searchTerms.getStatus(), userParams, userSearchContainer.getStart(), userSearchContainer.getEnd(), userSearchContainer.getOrderByComparator()); } //filter search results if(results != null){ Iterator<User> iterator = results.iterator(); while(iterator.hasNext()){ User res = iterator.next(); %> String TestValue = "test123"; <% if(!res.getScreenName().equals(TestValue)){ iterator.remove(); } } }
System.out.println("New List Total " + results.size());
//set new list size userSearchContainer.setTotal(results.size());
//set new list results userSearchContainer.setResults(results);
%>
That's because you set the total to the current page's results. So it thinks there's no more results and doesn't render pagination.
This probably isn't the best way to filter because some pages may have differing amount of results depending if there's a user to filter or not. I think you could do something like this to add to the query:
userParams.put("test", new CustomSQLParam("WHERE User_.screeName != 'sadf'", StringPool.BLANK));
Thanks for your reply, Amos. I've tried your solution and I can filter based on some value, but still doesn't get me what I want. The way the new search directory work is it runs the search first on the Liferay database and after that, I make another connection to an external database that uses screenName as the primary key for users. After that I do a comparison between Liferay database and another external database and bring the matching rows and display them on the search container.
If I do something like this it works just fine on development env which has just a few users 100 maybe, but when I deploy the hook to pre-prod which has over 100,000 users the connection times out or it takes longer to respond if there are just few return values.
results = UserLocalServiceUtil.search(company.getCompanyId(), searchTerms.getKeywords(), searchTerms.getStatus(), userParams, <span style="color: rgb(255,0,0);">-1</span>, <span style="color: rgb(255,0,0);">-1</span>, userSearchContainer.getOrderByComparator());
Any help guys.
Thanks, I've used your first approach, but on usersRoles and it seems to work.
if (roleid .equals(9898)) { userParams.put("usersRoles", Long.parseLong(roleid)); }
Powered by Liferay™