Search in sources :

Example 1 with AuditLog

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

the class CommentDataUtil method saveCommentData.

public static CommentData saveCommentData(CommentData commentData) throws InvalidArgumentException, InsufficientAccessException {
    _validateCommentDataForSave(commentData);
    boolean isNew = commentData.getId() == null;
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Comment comment = isNew ? dataAccessor.newComment() : dataAccessor.getComment(commentData.getId());
    if (isNew && !hasAccessToAddCommentData(commentData))
        throw new InsufficientAccessException();
    if (!isNew && !hasAccessToUpdateCommentData(comment))
        throw new InsufficientAccessException();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), isNew ? AccessType.COMMENT_ADD : AccessType.COMMENT_UPDATE, comment);
    if (isNew) {
        comment.setUserId(commentData.getUserId());
        comment.setParentType(commentData.getParentType());
        comment.setParentId(commentData.getParentId());
        if (commentData.getParentType() == CommentParentType.REVIEW) {
            UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(commentData.getParentId());
            comment.setReferenceType(ReferenceType.PRATILIPI);
            comment.setReferenceId(userPratilipi.getPratilipiId());
        }
        comment.setCreationDate(new Date());
    } else {
        comment.setLastUpdated(new Date());
    }
    if (commentData.hasContent())
        comment.setContent(commentData.getContent());
    if (commentData.hasState())
        comment.setState(commentData.getState());
    comment = dataAccessor.createOrUpdateComment(comment, auditLog);
    User user = dataAccessor.getUser(commentData.getUserId());
    UserData userData = UserDataUtil.createUserData(user);
    commentData = createCommentData(comment);
    commentData.setUser(userData);
    return commentData;
}
Also used : Comment(com.pratilipi.data.type.Comment) UserPratilipi(com.pratilipi.data.type.UserPratilipi) User(com.pratilipi.data.type.User) UserData(com.pratilipi.data.client.UserData) DataAccessor(com.pratilipi.data.DataAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 2 with AuditLog

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

the class UserDataUtil method loginUser.

public static UserData loginUser(String email, String password) throws InvalidArgumentException, InsufficientAccessException {
    // Do nothing if a user is already logged in
    if (!AccessTokenFilter.getAccessToken().getUserId().equals(0L))
        return getCurrentUser();
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUserByEmail(email.toLowerCase());
    if (user == null || user.getState() == UserState.REFERRAL || user.getState() == UserState.DELETED) {
        JsonObject errorMessages = new JsonObject();
        errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_NOT_REGISTERED);
        throw new InvalidArgumentException(errorMessages);
    } else if (user.getState() == UserState.BLOCKED) {
        throw new InsufficientAccessException(GenericRequest.ERR_ACCOUNT_BLOCKED);
    }
    if (user.getPassword() == null && user.getFacebookId() != null)
        throw new InvalidArgumentException(GenericRequest.ERR_EMAIL_REGISTERED_WITH_FACEBOOK);
    if (user.getPassword() == null && user.getGoogleId() != null)
        throw new InvalidArgumentException(GenericRequest.ERR_EMAIL_REGISTERED_WITH_GOOGLE);
    if (PasswordUtil.check(password, user.getPassword())) {
        _loginUser(AccessTokenFilter.getAccessToken(), user);
        return createUserData(user);
    }
    if (_verifyToken(user, password)) {
        AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_ADD, user);
        user.setVerificationToken(null);
        dataAccessor.createOrUpdateUser(user, auditLog);
        _loginUser(AccessTokenFilter.getAccessToken(), user);
        return createUserData(user);
    }
    throw new InvalidArgumentException(GenericRequest.ERR_INVALID_CREDENTIALS);
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) JsonObject(com.google.gson.JsonObject) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog)

Example 3 with AuditLog

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

the class VoteDataUtil method saveVoteData.

public static VoteData saveVoteData(Long userId, VoteParentType parentType, String parentId, VoteType type) throws InsufficientAccessException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Vote vote = dataAccessor.getVote(userId, parentType, parentId);
    boolean isNew = vote == null;
    if (isNew)
        vote = dataAccessor.newVote();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.VOTE, vote);
    if (isNew) {
        vote.setUserId(userId);
        vote.setParentType(parentType);
        vote.setParentId(parentId);
        if (parentType == VoteParentType.REVIEW) {
            UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(parentId);
            vote.setReferenceType(ReferenceType.PRATILIPI);
            vote.setReferenceId(userPratilipi.getPratilipiId());
        } else if (parentType == VoteParentType.COMMENT) {
            Comment comment = dataAccessor.getComment(Long.parseLong(parentId));
            UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(comment.getParentId());
            vote.setReferenceType(ReferenceType.PRATILIPI);
            vote.setReferenceId(userPratilipi.getPratilipiId());
        }
        vote.setCreationDate(new Date());
    } else {
        vote.setLastUpdated(new Date());
    }
    vote.setType(type);
    if (!hasAccessToAddOrUpdateData(vote))
        throw new InsufficientAccessException();
    vote = dataAccessor.createOrUpdateVote(vote, auditLog);
    return createVoteData(vote);
}
Also used : UserPratilipi(com.pratilipi.data.type.UserPratilipi) Comment(com.pratilipi.data.type.Comment) Vote(com.pratilipi.data.type.Vote) DataAccessor(com.pratilipi.data.DataAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 4 with AuditLog

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

the class UserAuthorDataUtil method saveUserAuthorFollow.

public static UserAuthorData saveUserAuthorFollow(Long userId, Long authorId, UserFollowState followState) throws InvalidArgumentException, InsufficientAccessException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    UserAuthor userAuthor = dataAccessor.getUserAuthor(userId, authorId);
    if (userAuthor == null) {
        userAuthor = dataAccessor.newUserAuthor();
        userAuthor.setUserId(userId);
        userAuthor.setAuthorId(authorId);
    }
    if (!hasAccessToUpdateUserAuthorData(userAuthor, AccessType.USER_AUTHOR_FOLLOWING))
        throw new InsufficientAccessException();
    AccessToken accessToken = AccessTokenFilter.getAccessToken();
    AuditLog auditLog = dataAccessor.newAuditLog(accessToken, AccessType.USER_AUTHOR_FOLLOWING, userAuthor);
    userAuthor.setFollowState(followState);
    userAuthor.setFollowDate(new Date());
    userAuthor = dataAccessor.createOrUpdateUserAuthor(userAuthor, auditLog);
    return createUserAuthorData(userAuthor);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) AccessToken(com.pratilipi.data.type.AccessToken) UserAuthor(com.pratilipi.data.type.UserAuthor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 5 with AuditLog

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

the class UserDataUtil method updateUserAuthorStats.

public static void updateUserAuthorStats(Long userId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    User user = dataAccessor.getUser(userId);
    UserFollowsDoc followsDoc = docAccessor.getUserFollowsDoc(userId);
    if (user.getFollowCount().equals(followsDoc.getFollows(UserFollowState.FOLLOWING).size()))
        return;
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_UPDATE, user);
    user.setFollowCount((long) followsDoc.getFollows(UserFollowState.FOLLOWING).size());
    user = dataAccessor.createOrUpdateUser(user, auditLog);
}
Also used : User(com.pratilipi.data.type.User) DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) UserFollowsDoc(com.pratilipi.data.type.UserFollowsDoc) AuditLog(com.pratilipi.data.type.AuditLog)

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