Search in sources :

Example 11 with Conversation

use of codeu.model.data.Conversation in project CodeU-Spring-2018 by jwang281.

the class ConversationServlet method doPost.

/**
 * This function fires when a user submits the form on the conversations page. It gets the
 * logged-in username from the session and the new conversation title from the submitted form
 * data. It uses this to create a new Conversation object that it adds to the model.
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String username = (String) request.getSession().getAttribute("user");
    if (username == null) {
        // user is not logged in, don't let them create a conversation
        response.sendRedirect("/conversations");
        return;
    }
    User user = userStore.getUser(username);
    if (user == null) {
        // user was not found, don't let them create a conversation
        System.out.println("User not found: " + username);
        response.sendRedirect("/conversations");
        return;
    }
    String conversationTitle = request.getParameter("conversationTitle");
    if (!conversationTitle.matches("[\\w*]*")) {
        request.setAttribute("error", "Please enter only letters and numbers.");
        request.getRequestDispatcher("/WEB-INF/view/conversations.jsp").forward(request, response);
        return;
    }
    if (conversationStore.isTitleTaken(conversationTitle)) {
        // conversation title is already taken, just go into that conversation instead of creating a
        // new one
        response.sendRedirect("/chat/" + conversationTitle);
        return;
    }
    Conversation conversation = new Conversation(UUID.randomUUID(), user.getId(), conversationTitle, Instant.now());
    conversationStore.addConversation(conversation);
    response.sendRedirect("/chat/" + conversationTitle);
}
Also used : User(codeu.model.data.User) Conversation(codeu.model.data.Conversation)

Example 12 with Conversation

use of codeu.model.data.Conversation in project CodeU-Spring-2018 by maksymko.

the class PersistentDataStoreTest method testSaveAndLoadConversations.

@Test
public void testSaveAndLoadConversations() throws PersistentDataStoreException {
    UUID idOne = UUID.randomUUID();
    UUID ownerOne = UUID.randomUUID();
    String titleOne = "Test_Title";
    Instant creationOne = Instant.ofEpochMilli(1000);
    Conversation inputConversationOne = new Conversation(idOne, ownerOne, titleOne, creationOne);
    UUID idTwo = UUID.randomUUID();
    UUID ownerTwo = UUID.randomUUID();
    String titleTwo = "Test_Title_Two";
    Instant creationTwo = Instant.ofEpochMilli(2000);
    Conversation inputConversationTwo = new Conversation(idTwo, ownerTwo, titleTwo, creationTwo);
    // save
    persistentDataStore.writeThrough(inputConversationOne);
    persistentDataStore.writeThrough(inputConversationTwo);
    // load
    List<Conversation> resultConversations = persistentDataStore.loadConversations();
    // confirm that what we saved matches what we loaded
    Conversation resultConversationOne = resultConversations.get(0);
    Assert.assertEquals(idOne, resultConversationOne.getId());
    Assert.assertEquals(ownerOne, resultConversationOne.getOwnerId());
    Assert.assertEquals(titleOne, resultConversationOne.getTitle());
    Assert.assertEquals(creationOne, resultConversationOne.getCreationTime());
    Conversation resultConversationTwo = resultConversations.get(1);
    Assert.assertEquals(idTwo, resultConversationTwo.getId());
    Assert.assertEquals(ownerTwo, resultConversationTwo.getOwnerId());
    Assert.assertEquals(titleTwo, resultConversationTwo.getTitle());
    Assert.assertEquals(creationTwo, resultConversationTwo.getCreationTime());
}
Also used : Instant(java.time.Instant) Conversation(codeu.model.data.Conversation) UUID(java.util.UUID) Test(org.junit.Test)

Example 13 with Conversation

use of codeu.model.data.Conversation in project CodeU-Spring-2018 by maksymko.

the class ConversationStoreTest method testGetConversationWithTitle_notFound.

@Test
public void testGetConversationWithTitle_notFound() {
    Conversation resultConversation = conversationStore.getConversationWithTitle("unfound_title");
    Assert.assertNull(resultConversation);
}
Also used : Conversation(codeu.model.data.Conversation) Test(org.junit.Test)

Example 14 with Conversation

use of codeu.model.data.Conversation in project CodeU-Spring-2018 by maksymko.

the class ConversationStoreTest method testGetConversationWithTitle_found.

@Test
public void testGetConversationWithTitle_found() {
    Conversation resultConversation = conversationStore.getConversationWithTitle(CONVERSATION_ONE.getTitle());
    assertEquals(CONVERSATION_ONE, resultConversation);
}
Also used : Conversation(codeu.model.data.Conversation) Test(org.junit.Test)

Example 15 with Conversation

use of codeu.model.data.Conversation in project CodeU-Spring-2018 by maksymko.

the class PersistentDataStore method loadConversations.

/**
 * Loads all Conversation objects from the Datastore service and returns them in a List.
 *
 * @throws PersistentDataStoreException if an error was detected during the load from the
 *     Datastore service
 */
public List<Conversation> loadConversations() throws PersistentDataStoreException {
    List<Conversation> conversations = new ArrayList<>();
    // Retrieve all conversations from the datastore.
    Query query = new Query("chat-conversations");
    PreparedQuery results = datastore.prepare(query);
    for (Entity entity : results.asIterable()) {
        try {
            UUID uuid = UUID.fromString((String) entity.getProperty("uuid"));
            UUID ownerUuid = UUID.fromString((String) entity.getProperty("owner_uuid"));
            String title = (String) entity.getProperty("title");
            Instant creationTime = Instant.parse((String) entity.getProperty("creation_time"));
            Conversation conversation = new Conversation(uuid, ownerUuid, title, creationTime);
            conversations.add(conversation);
        } catch (Exception e) {
            // database entity definition mismatches, or service mismatches.
            throw new PersistentDataStoreException(e);
        }
    }
    return conversations;
}
Also used : Entity(com.google.appengine.api.datastore.Entity) PersistentDataStoreException(codeu.model.store.persistence.PersistentDataStoreException) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(com.google.appengine.api.datastore.Query) Instant(java.time.Instant) ArrayList(java.util.ArrayList) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Conversation(codeu.model.data.Conversation) UUID(java.util.UUID) PersistentDataStoreException(codeu.model.store.persistence.PersistentDataStoreException)

Aggregations

Conversation (codeu.model.data.Conversation)74 Test (org.junit.Test)46 User (codeu.model.data.User)43 Message (codeu.model.data.Message)29 ArrayList (java.util.ArrayList)16 UUID (java.util.UUID)16 Activity (codeu.model.data.Activity)6 Instant (java.time.Instant)6 HashSet (java.util.HashSet)3 PersistentDataStoreException (codeu.model.store.persistence.PersistentDataStoreException)2 ImageStorage (codeu.utils.ImageStorage)2 Entity (com.google.appengine.api.datastore.Entity)2 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)2 Query (com.google.appengine.api.datastore.Query)2 OutputSettings (org.jsoup.nodes.Document.OutputSettings)2 ConversationFilterer (codeu.utils.ConversationFilterer)1 MessageFilterer (codeu.utils.MessageFilterer)1 DateTimeException (java.time.DateTimeException)1 ZonedDateTime (java.time.ZonedDateTime)1 List (java.util.List)1