Converting the Hits into the UserList
By default liferay indexes the content and So when ever we are searching the any content in the liferay, It reterives from the index.
Suppose we think we need search the Users which we need to implement programmatically. Liferay has one utility function which search the user and returns in the form of a Hits.
Hits hits = UserLocalServiceUtil.search(-,-,-,-,-,-,-,-,-,-,-,);
So how can convert the hits into UserList. Here the code.
public List<User> getUserList(Hits hits) throws PortalException, SystemException {
List<Document> documents = hits.toList(); // Converting the Hits into the Documents
List<User> userList = new ArrayList<User>();
for (Document document : documents) {
long userId = GetterUtil.getLong(document.get(Field.USER_ID)); // Getting the Individual User by userId.
try {
User user = UserLocalServiceUtil.getUser(userId);
userList.add(user); // Adding the User to the userList.
}
catch (NoSuchUserException nsue) { //If user does not exist delete the user index.
Indexer indexer = IndexerRegistryUtil.getIndexer(User.class);
long companyId = GetterUtil.getLong(document.get(Field.COMPANY_ID));
indexer.delete(companyId, document.getUID());
}
}
return userList;
}
Here the It returns the Hits in the form of a Users.
Comments
Post a Comment