Search in sources :

Example 1 with MailingListSubscription

use of com.pratilipi.data.type.MailingListSubscription 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)

Aggregations

InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)1 MailingList (com.pratilipi.common.type.MailingList)1 DataAccessor (com.pratilipi.data.DataAccessor)1 AuditLog (com.pratilipi.data.type.AuditLog)1 MailingListSubscription (com.pratilipi.data.type.MailingListSubscription)1 Date (java.util.Date)1 List (java.util.List)1