Search in sources :

Example 1 with ConversationFilterer

use of codeu.utils.ConversationFilterer 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);
}
Also used : ConversationFilterer(codeu.utils.ConversationFilterer) User(codeu.model.data.User) Message(codeu.model.data.Message) ArrayList(java.util.ArrayList) Conversation(codeu.model.data.Conversation) DateTimeException(java.time.DateTimeException) MessageFilterer(codeu.utils.MessageFilterer) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Conversation (codeu.model.data.Conversation)1 Message (codeu.model.data.Message)1 User (codeu.model.data.User)1 ConversationFilterer (codeu.utils.ConversationFilterer)1 MessageFilterer (codeu.utils.MessageFilterer)1 DateTimeException (java.time.DateTimeException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1