Search in sources :

Example 16 with AuditLog

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

the class UserPratilipiDataUtil method saveUserPratilipiAddToLibrary.

public static UserPratilipiData saveUserPratilipiAddToLibrary(Long userId, Long pratilipiId, String lastOpenedPage, Boolean addedToLibrary) throws InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(userId, pratilipiId);
    if (userPratilipi == null) {
        userPratilipi = dataAccessor.newUserPratilipi();
        userPratilipi.setUserId(userId);
        userPratilipi.setPratilipiId(pratilipiId);
    }
    if (lastOpenedPage == null && addedToLibrary == null)
        return createUserPratilipiData(userPratilipi);
    if (!hasAccessToUpdateUserPratilipiData(userPratilipi, AccessType.USER_PRATILIPI_LIBRARY))
        throw new InsufficientAccessException();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_PRATILIPI_LIBRARY, userPratilipi);
    if (lastOpenedPage != null) {
        userPratilipi.setLastOpenedPage(lastOpenedPage);
        userPratilipi.setLastOpenedDate(new Date());
    }
    if (addedToLibrary != null) {
        userPratilipi.setAddedToLib(addedToLibrary);
        userPratilipi.setAddedToLibDate(new Date());
    }
    userPratilipi = dataAccessor.createOrUpdateUserPratilipi(userPratilipi, auditLog);
    return createUserPratilipiData(userPratilipi);
}
Also used : UserPratilipi(com.pratilipi.data.type.UserPratilipi) DataAccessor(com.pratilipi.data.DataAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 17 with AuditLog

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

the class UserDataUtil method registerUser.

public static UserData registerUser(String firstName, String lastName, String email, String password, UserSignUpSource signUpSource) throws InvalidArgumentException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUserByEmail(email.toLowerCase());
    if (user == null || user.getState() == UserState.DELETED) {
        user = dataAccessor.newUser();
    } else if (user.getState() != UserState.REFERRAL) {
        JsonObject errorMessages = new JsonObject();
        errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REGISTERED_ALREADY);
        throw new InvalidArgumentException(errorMessages);
    }
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_ADD, user);
    user.setPassword(PasswordUtil.getSaltedHash(password));
    user.setEmail(email.toLowerCase());
    user.setState(UserState.REGISTERED);
    user.setSignUpDate(new Date());
    user.setSignUpSource(signUpSource);
    user = dataAccessor.createOrUpdateUser(user, auditLog);
    UserData userData = createUserData(user);
    userData.setFirstName(firstName);
    userData.setLastName(lastName);
    return userData;
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UserData(com.pratilipi.data.client.UserData) DataAccessor(com.pratilipi.data.DataAccessor) JsonObject(com.google.gson.JsonObject) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 18 with AuditLog

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

the class UserDataUtil method verifyUserEmail.

public static void verifyUserEmail(String email, String verificationToken) throws InvalidArgumentException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUserByEmail(email.toLowerCase());
    if (user == null || user.getState() != UserState.REGISTERED)
        return;
    if (!_verifyToken(user, verificationToken))
        throw new InvalidArgumentException(GenericRequest.ERR_VERIFICATION_TOKEN_INVALID_OR_EXPIRED);
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_UPDATE, user);
    user.setState(UserState.ACTIVE);
    // Note: Do not clear VerificationToken
    user = dataAccessor.createOrUpdateUser(user, auditLog);
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) AuditLog(com.pratilipi.data.type.AuditLog)

Example 19 with AuditLog

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

the class PratilipiDocUtil method updateMeta.

public static boolean updateMeta(Long pratilipiId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiId);
    PratilipiMetaDoc pmDoc = docAccessor.getPratilipiMetaDoc(pratilipiId);
    if (pmDoc == null)
        pmDoc = docAccessor.newPratilipiMetaDoc();
    int wordCount = 0;
    int imageCount = 0;
    int chapterCount = 0;
    Map<String, Integer> wordCounts = new HashMap<>();
    for (PratilipiContentDoc.Chapter chapter : pcDoc.getChapterList()) {
        String content = chapter.getTitle() == null ? "" : chapter.getTitle();
        for (PratilipiContentDoc.Page page : chapter.getPageList()) {
            for (PratilipiContentDoc.Pagelet pagelet : page.getPageletList()) {
                if (pagelet.getType() == PratilipiContentDoc.PageletType.IMAGE)
                    imageCount++;
                else
                    content += " " + pagelet.getDataAsString();
            }
        }
        content = content.trim();
        if (content.length() > 1) {
            String[] words = content.replaceAll(nonKeywordsPattern, " ").split("[\\s]+");
            for (String word : words) {
                Integer count = wordCounts.get(word);
                count = count == null ? 1 : count++;
                wordCounts.put(word, count);
            }
            wordCount += words.length;
        }
        chapterCount++;
    }
    boolean isChanged = wordCounts.size() != pmDoc.getWordCounts().size();
    if (!isChanged) {
        for (Entry<String, Integer> entry : pmDoc.getWordCounts().entrySet()) {
            if (!entry.getValue().equals(wordCounts.get(entry.getKey()))) {
                isChanged = true;
                break;
            }
        }
    }
    if (isChanged) {
        // Update & Save Pratilipi Meta Doc
        pmDoc.setWordCounts(wordCounts);
        docAccessor.save(pratilipiId, pmDoc);
        // Update counts in Pratilipi Entity
        AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
        pratilipi.setWordCount(wordCount);
        pratilipi.setImageCount(imageCount);
        pratilipi.setChapterCount(chapterCount);
        pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
    }
    return isChanged;
}
Also used : HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) AuditLog(com.pratilipi.data.type.AuditLog) PratilipiMetaDoc(com.pratilipi.data.type.PratilipiMetaDoc) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc)

Example 20 with AuditLog

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

AuditLog (com.pratilipi.data.type.AuditLog)29 DataAccessor (com.pratilipi.data.DataAccessor)28 Date (java.util.Date)19 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)17 UserPratilipi (com.pratilipi.data.type.UserPratilipi)15 Pratilipi (com.pratilipi.data.type.Pratilipi)10 User (com.pratilipi.data.type.User)8 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)7 DocAccessor (com.pratilipi.data.DocAccessor)5 BlobAccessor (com.pratilipi.data.BlobAccessor)4 JsonObject (com.google.gson.JsonObject)3 UserData (com.pratilipi.data.client.UserData)3 Comment (com.pratilipi.data.type.Comment)3 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)3 Chapter (com.pratilipi.data.type.PratilipiContentDoc.Chapter)3 HashMap (java.util.HashMap)3 Get (com.pratilipi.api.annotation.Get)2 GenericResponse (com.pratilipi.api.shared.GenericResponse)2 AccessToken (com.pratilipi.data.type.AccessToken)2 BlobEntry (com.pratilipi.data.type.BlobEntry)2