use of codeu.model.data.Conversation in project CodeU-Spring-2018 by jwang281.
the class PersistentStorageAgentTest method testWriteThroughConversation.
@Test
public void testWriteThroughConversation() {
Conversation conversation = new Conversation(UUID.randomUUID(), UUID.randomUUID(), "test_conversation", Instant.now());
persistentStorageAgent.writeThrough(conversation);
Mockito.verify(mockPersistentDataStore).writeThrough(conversation);
}
use of codeu.model.data.Conversation in project CodeU-Spring-2018 by jwang281.
the class DefaultDataStore method addRandomMessages.
private void addRandomMessages() {
for (int i = 0; i < DEFAULT_MESSAGE_COUNT; i++) {
Conversation conversation = getRandomElement(conversations);
User author = getRandomElement(users);
String content = getRandomMessageContent();
Message message = new Message(UUID.randomUUID(), conversation.getId(), author.getId(), content, Instant.now());
PersistentStorageAgent.getInstance().writeThrough(message);
messages.add(message);
}
}
use of codeu.model.data.Conversation in project CodeU-Spring-2018 by jwang281.
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;
}
use of codeu.model.data.Conversation in project CodeU-Spring-2018 by jwang281.
the class ChatServlet method doPost.
/**
* This function fires when a user submits the form on the chat page. It gets the logged-in
* username from the session, the conversation title from the URL, and the chat message from the
* submitted form data. It creates a new Message from that data, adds it to the model, and then
* redirects back to the chat page.
*/
@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 add a message
response.sendRedirect("/login");
return;
}
User user = userStore.getUser(username);
if (user == null) {
// user was not found, don't let them add a message
response.sendRedirect("/login");
return;
}
String requestUrl = request.getRequestURI();
String conversationTitle = requestUrl.substring("/chat/".length());
Conversation conversation = conversationStore.getConversationWithTitle(conversationTitle);
if (conversation == null) {
// couldn't find conversation, redirect to conversation list
response.sendRedirect("/conversations");
return;
}
String messageContent = request.getParameter("message");
// this removes any HTML from the message content
String cleanedMessageContent = Jsoup.clean(messageContent, Whitelist.none());
Message message = new Message(UUID.randomUUID(), conversation.getId(), user.getId(), cleanedMessageContent, Instant.now());
messageStore.addMessage(message);
// redirect to a GET request
response.sendRedirect("/chat/" + conversationTitle);
}
use of codeu.model.data.Conversation in project CodeU-Spring-2018 by jwang281.
the class ChatServlet method doGet.
/**
* This function fires when a user navigates to the chat page. It gets the conversation title from
* the URL, finds the corresponding Conversation, and fetches the messages in that Conversation.
* It then forwards to chat.jsp for rendering.
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String requestUrl = request.getRequestURI();
String conversationTitle = requestUrl.substring("/chat/".length());
Conversation conversation = conversationStore.getConversationWithTitle(conversationTitle);
if (conversation == null) {
// couldn't find conversation, redirect to conversation list
System.out.println("Conversation was null: " + conversationTitle);
response.sendRedirect("/conversations");
return;
}
UUID conversationId = conversation.getId();
List<Message> messages = messageStore.getMessagesInConversation(conversationId);
request.setAttribute("conversation", conversation);
request.setAttribute("messages", messages);
request.getRequestDispatcher("/WEB-INF/view/chat.jsp").forward(request, response);
}
Aggregations