Search in sources :

Example 1 with Conversation

use of com.pratilipi.data.type.Conversation in project pratilipi by Pratilipi.

the class ConversationDataUtil method saveMessage.

public static void saveMessage(ContactTeam team, Long userId, String name, String email, String phone, String message, JsonObject data) throws InvalidArgumentException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUser(userId);
    Author author = dataAccessor.getAuthorByUserId(userId);
    Conversation conversation = dataAccessor.getConversation(team, userId);
    if (conversation == null)
        dataAccessor.getConversation(team, email);
    if (conversation != null) {
    // Do Nothing !
    } else if (user != null && (user.getState() == UserState.ACTIVE || user.getState() == UserState.REGISTERED)) {
        // &&
        // conversation
        // ==
        // null
        conversation = dataAccessor.newConversation(team, userId);
        conversation.setCreator(userId);
        conversation.setCreatorName(name);
        conversation.setCreatorEmail(email);
        conversation.setCreatorPhone(phone);
        conversation.setCreationDate(new Date());
        List<ConversationUser> conversationUserList = new ArrayList<>(team.getUserIds().length + 1);
        conversationUserList.add(dataAccessor.newConversationUser(conversation.getId(), userId));
        for (Long recipientUserId : team.getUserIds()) conversationUserList.add(dataAccessor.newConversationUser(conversation.getId(), recipientUserId));
        conversation = dataAccessor.createOrUpdateConversation(conversation, conversationUserList);
    } else if (email != null) {
        // && conversation == null
        conversation = dataAccessor.newConversation(team, email);
        conversation.setCreatorName(name);
        conversation.setCreatorEmail(email);
        conversation.setCreatorPhone(phone);
        conversation.setCreationDate(new Date());
        List<ConversationUser> conversationUserList = new ArrayList<>(team.getUserIds().length + 1);
        for (Long recipientUserId : team.getUserIds()) conversationUserList.add(dataAccessor.newConversationUser(conversation.getId(), recipientUserId));
        conversation = dataAccessor.createOrUpdateConversation(conversation, conversationUserList);
    } else {
        throw new InvalidArgumentException("Valid 'email' is required.");
    }
    conversation.setLastUpdated(new Date());
    ConversationMessage conversationMessage = dataAccessor.newConversationMessage();
    conversationMessage.setConversationId(conversation.getId());
    conversationMessage.setCreatorId(userId);
    conversationMessage.setMessage(message);
    conversationMessage.setData(data);
    conversationMessage.setCreationDate(new Date());
    conversation = dataAccessor.createOrUpdateConversation(conversation, conversationMessage);
    String language = author != null ? author.getLanguage().getNameEn().toLowerCase() : null;
    try {
        ArrayList<String> receiverList = createReceiversId(team.name().toLowerCase(), language);
        createSupportMailTask(receiverList, userId.toString(), name, email, phone, message, data, team.name(), language);
    } catch (UnsupportedEncodingException | UnexpectedServerException e) {
        logger.log(Level.SEVERE, "Exception while creating conversation mail task");
        logger.log(Level.SEVERE, "User ID : " + userId);
        logger.log(Level.SEVERE, "Conversation Id : " + conversation.getId());
        e.printStackTrace();
    }
}
Also used : ConversationUser(com.pratilipi.data.type.ConversationUser) User(com.pratilipi.data.type.User) DataAccessor(com.pratilipi.data.DataAccessor) ConversationMessage(com.pratilipi.data.type.ConversationMessage) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Conversation(com.pratilipi.data.type.Conversation) Date(java.util.Date) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) Author(com.pratilipi.data.type.Author) ArrayList(java.util.ArrayList) List(java.util.List) ConversationUser(com.pratilipi.data.type.ConversationUser)

Aggregations

InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 DataAccessor (com.pratilipi.data.DataAccessor)1 Author (com.pratilipi.data.type.Author)1 Conversation (com.pratilipi.data.type.Conversation)1 ConversationMessage (com.pratilipi.data.type.ConversationMessage)1 ConversationUser (com.pratilipi.data.type.ConversationUser)1 User (com.pratilipi.data.type.User)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1