Search in sources :

Example 26 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class BlogPostDataUtil method _validateBlogPostDataForSave.

private static void _validateBlogPostDataForSave(BlogPostData blogPostData) throws InvalidArgumentException {
    boolean isNew = blogPostData.getId() == null;
    JsonObject errorMessages = new JsonObject();
    // New blogPost must have blog id.
    if (isNew && (!blogPostData.hasBlogId() || blogPostData.getBlogId() == null))
        errorMessages.addProperty("blogId", GenericRequest.ERR_BLOG_ID_REQUIRED);
    else // Blog id can not be un-set or set to null.
    if (!isNew && blogPostData.hasBlogId() && blogPostData.getBlogId() == null)
        errorMessages.addProperty("blogId", GenericRequest.ERR_BLOG_ID_REQUIRED);
    // New blogPost must have language.
    if (isNew && (!blogPostData.hasLanguage() || blogPostData.getLanguage() == null))
        errorMessages.addProperty("language", GenericRequest.ERR_LANGUAGE_REQUIRED);
    else // Language can not be un-set or set to null.
    if (!isNew && blogPostData.hasLanguage() && blogPostData.getLanguage() == null)
        errorMessages.addProperty("language", GenericRequest.ERR_LANGUAGE_REQUIRED);
    // New blogPost must have state.
    if (isNew && (!blogPostData.hasState() || blogPostData.getState() == null))
        errorMessages.addProperty("state", GenericRequest.ERR_BLOG_POST_STATE_REQUIRED);
    if (errorMessages.entrySet().size() > 0)
        throw new InvalidArgumentException(errorMessages);
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonObject(com.google.gson.JsonObject)

Example 27 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class EventDataUtil method _validateEventDataForSave.

private static void _validateEventDataForSave(EventData eventData) throws InvalidArgumentException {
    boolean isNew = eventData.getId() == null;
    JsonObject errorMessages = new JsonObject();
    // New event must have language.
    if (isNew && (!eventData.hasLanguage() || eventData.getLanguage() == null))
        errorMessages.addProperty("langauge", GenericRequest.ERR_LANGUAGE_REQUIRED);
    else // Language can not be un-set or set to null.
    if (!isNew && eventData.hasLanguage() && eventData.getLanguage() == null)
        errorMessages.addProperty("langauge", GenericRequest.ERR_LANGUAGE_REQUIRED);
    if (errorMessages.entrySet().size() > 0)
        throw new InvalidArgumentException(errorMessages);
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonObject(com.google.gson.JsonObject)

Example 28 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException 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)

Example 29 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class AuthorDataUtil method saveAuthorImage.

public static String saveAuthorImage(Long authorId, BlobEntry blobEntry) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    if (blobEntry.getData() == null || blobEntry.getData().length == 0)
        throw new InvalidArgumentException("Image data is missing.");
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Author author = dataAccessor.getAuthor(authorId);
    if (!hasAccessToUpdateAuthorData(author, null))
        throw new InsufficientAccessException();
    String profileImageName = new Date().getTime() + "";
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    blobEntry.setName("author/" + authorId + "/images/profile/" + profileImageName);
    blobAccessor.createOrUpdateBlob(blobEntry);
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.AUTHOR_UPDATE, author);
    author.setProfileImage(profileImageName);
    author.setLastUpdated(new Date());
    author = dataAccessor.createOrUpdateAuthor(author, auditLog);
    return createAuthorProfileImageUrl(author);
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) BlobAccessor(com.pratilipi.data.BlobAccessor) Author(com.pratilipi.data.type.Author) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Date(java.util.Date) AuditLog(com.pratilipi.data.type.AuditLog)

Example 30 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class AuthorDataUtil method _validateAuthorDataForSave.

private static void _validateAuthorDataForSave(AuthorData authorData) throws InvalidArgumentException {
    boolean isNew = authorData.getId() == null;
    JsonObject errorMessages = new JsonObject();
    // Language is mandatory.
    if (isNew && (!authorData.hasLanguage() || authorData.getLanguage() == null))
        errorMessages.addProperty("langauge", GenericRequest.ERR_LANGUAGE_REQUIRED);
    // Language can not be un-set or set to null.
    if (!isNew && authorData.hasLanguage() && authorData.getLanguage() == null)
        errorMessages.addProperty("langauge", GenericRequest.ERR_LANGUAGE_REQUIRED);
    // State must be ACTIVE for new profile.
    if (isNew && (!authorData.hasState() || authorData.getState() != AuthorState.ACTIVE))
        errorMessages.addProperty("state", GenericRequest.ERR_AUTHOR_STATE_INVALID);
    // State can not be un-set or set to null.
    if (!isNew && authorData.hasState() && authorData.getState() == null)
        errorMessages.addProperty("state", GenericRequest.ERR_AUTHOR_STATE_REQUIRED);
    if (errorMessages.entrySet().size() > 0)
        throw new InvalidArgumentException(errorMessages);
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonObject(com.google.gson.JsonObject)

Aggregations

InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)37 JsonObject (com.google.gson.JsonObject)21 DataAccessor (com.pratilipi.data.DataAccessor)19 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)11 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)9 User (com.pratilipi.data.type.User)9 Gson (com.google.gson.Gson)7 GenericResponse (com.pratilipi.api.shared.GenericResponse)7 AuditLog (com.pratilipi.data.type.AuditLog)7 Author (com.pratilipi.data.type.Author)6 JsonElement (com.google.gson.JsonElement)5 Post (com.pratilipi.api.annotation.Post)5 UserData (com.pratilipi.data.client.UserData)5 Page (com.pratilipi.data.type.Page)5 Pratilipi (com.pratilipi.data.type.Pratilipi)5 Date (java.util.Date)5 HashMap (java.util.HashMap)5 Language (com.pratilipi.common.type.Language)3 UserPratilipi (com.pratilipi.data.type.UserPratilipi)3 PrintWriter (java.io.PrintWriter)3