Search in sources :

Example 31 with InvalidArgumentException

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

the class InitDataUtil method getInitBanner.

public static BlobEntry getInitBanner(Language language, String name, Integer width) throws InvalidArgumentException, UnexpectedServerException {
    BlobEntry blobEntry = DataAccessorFactory.getBlobAccessor().getBlob("init/banners/" + language.getCode() + "/" + name);
    if (blobEntry == null) {
        JsonObject errorMessages = new JsonObject();
        errorMessages.addProperty("name", "Invalid banner name.");
        throw new InvalidArgumentException(errorMessages);
    }
    if (width != null)
        blobEntry.setData(ImageUtil.resize(blobEntry.getData(), width));
    return blobEntry;
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) BlobEntry(com.pratilipi.data.type.BlobEntry) JsonObject(com.google.gson.JsonObject)

Example 32 with InvalidArgumentException

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

the class MailingListSubscriptionDataUtil method subscribe.

public static void subscribe(MailingList mailingList, Long userId, String email, String phone, Language language, String comment) throws InvalidArgumentException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    MailingListSubscription mailingListSubscription = null;
    if (email != null) {
        List<MailingListSubscription> mailingListSubscriptionList = dataAccessor.getMailingListSubscriptionList(mailingList, email, null);
        for (MailingListSubscription subs : mailingListSubscriptionList) {
            if (language != null && subs.getLanguage() != null && language != subs.getLanguage())
                continue;
            if (phone != null && subs.getPhone() != null && !phone.equals(subs.getPhone()))
                continue;
            if (comment != null && subs.getComment() != null && !comment.equals(subs.getComment()))
                continue;
            mailingListSubscription = subs;
            break;
        }
    } else if (phone != null) {
        List<MailingListSubscription> mailingListSubscriptionList = dataAccessor.getMailingListSubscriptionList(mailingList, null, phone);
        for (MailingListSubscription subs : mailingListSubscriptionList) {
            if (language != null && subs.getLanguage() != null && language != subs.getLanguage())
                continue;
            if (comment != null && subs.getComment() != null && !comment.equals(subs.getComment()))
                continue;
            mailingListSubscription = subs;
            break;
        }
    } else {
        throw new InvalidArgumentException("Either email or phone must be provided.");
    }
    if (mailingListSubscription == null)
        mailingListSubscription = dataAccessor.newMailingListSubscription();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.MAILING_LIST_SUBSCRIPTION_ADD, mailingListSubscription);
    boolean bool = true;
    if (// New entry
    mailingListSubscription.getMailingList() == null)
        mailingListSubscription.setMailingList(mailingList);
    if (userId != null && mailingListSubscription.getUserId() == null)
        mailingListSubscription.setUserId(userId);
    if (email != null && mailingListSubscription.getEmail() == null) {
        mailingListSubscription.setEmail(email);
        bool = false;
    }
    if (phone != null && mailingListSubscription.getPhone() == null) {
        mailingListSubscription.setPhone(phone);
        bool = false;
    }
    if (language != null && mailingListSubscription.getLanguage() == null) {
        mailingListSubscription.setLanguage(language);
        bool = false;
    }
    if (comment != null && mailingListSubscription.getComment() == null) {
        mailingListSubscription.setComment(comment);
        bool = false;
    }
    if (// New entry
    mailingListSubscription.getSubscriptionDate() == null)
        mailingListSubscription.setSubscriptionDate(new Date());
    if (bool)
        throw new InvalidArgumentException(GenericRequest.ERR_MAILING_LIST_SUBSCRIBED_ALREDY);
    dataAccessor.createOrUpdateMailingListSubscription(mailingListSubscription, auditLog);
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) MailingListSubscription(com.pratilipi.data.type.MailingListSubscription) DataAccessor(com.pratilipi.data.DataAccessor) List(java.util.List) MailingList(com.pratilipi.common.type.MailingList) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 33 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 34 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 35 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)

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