use of codeu.utils.MessageFilterer in project codeu-2018-team12 by codeu-2018-team12.
the class SearchServlet method doGet.
/**
* This function fires when a user requests the /search URL. It finds the set of users that match
* the given string and forwards that information to search.jsp
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
User loggedInUser = userStore.getUser((String) request.getSession().getAttribute("user"));
String search = null;
if (request.getParameter("searchuser") != null) {
search = request.getParameter("searchuser");
List<User> result = userStore.searchUsersSorted(search);
request.setAttribute("users", result);
} else if (request.getParameter("searchconvo") != null) {
search = request.getParameter("searchconvo");
List<Conversation> conversations = loggedInUser == null ? ConversationStore.sort(conversationStore.getAllPublicConversations()) : ConversationStore.sort(conversationStore.getAllPermittedConversations(loggedInUser.getId()));
try {
ConversationFilterer filterer = new ConversationFilterer(conversations, userStore.getUsers());
List<Conversation> result = filterer.filterConversations(search);
request.setAttribute("conversations", result);
} catch (DateTimeException dte) {
System.out.println(dte);
request.setAttribute("conversations", new ArrayList<Conversation>());
} catch (UnsupportedOperationException uoe) {
System.out.println(uoe);
request.setAttribute("conversations", new ArrayList<Conversation>());
}
} else if (request.getParameter("searchmessage") != null) {
search = request.getParameter("searchmessage");
String convoTitle = request.getParameter("searchbutton");
Conversation convo = conversationStore.getConversationWithTitle(convoTitle);
List<Message> messages = convo == null ? new ArrayList<Message>() : MessageStore.sort(messageStore.getMessagesInConversation(convo.getId()));
try {
MessageFilterer filterer = new MessageFilterer(messages, userStore.getUsers());
List<Message> result = filterer.filterMessages(search);
request.setAttribute("messages", result);
} catch (DateTimeException dte) {
System.out.println(dte);
request.setAttribute("messages", new ArrayList<Message>());
} catch (UnsupportedOperationException uoe) {
System.out.println(uoe);
request.setAttribute("messages", new ArrayList<Message>());
}
}
request.getRequestDispatcher("/WEB-INF/view/search.jsp").forward(request, response);
}
Aggregations