Search in sources :

Example 1 with UserData

use of com.pratilipi.data.client.UserData 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 UserData

use of com.pratilipi.data.client.UserData in project pratilipi by Pratilipi.

the class EmailDataUtil method _createDataModelForUserPratilipiEmail.

private static Object[] _createDataModelForUserPratilipiEmail(String userPratilipiId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    UserPratilipiData userPratilipi = UserPratilipiDataUtil.createUserPratilipiData(dataAccessor.getUserPratilipi(userPratilipiId));
    PratilipiData pratilipi = PratilipiDataUtil.createPratilipiData(dataAccessor.getPratilipi(userPratilipi.getPratilipiId()));
    UserData reviewer = UserDataUtil.createUserData(dataAccessor.getUser(userPratilipi.getUserId()));
    Map<String, Object> dataModel = new HashMap<>();
    dataModel.put("pratilipi_title", pratilipi.getTitle() != null ? pratilipi.getTitle() : pratilipi.getTitleEn());
    dataModel.put("pratilipi_page_url", _getDomainName(pratilipi.getLanguage()) + pratilipi.getPageUrl());
    dataModel.put("reviewer_name", reviewer.getAuthor().getName() != null ? reviewer.getAuthor().getName() : reviewer.getAuthor().getNameEn());
    dataModel.put("reviewer_page_url", _getDomainName(pratilipi.getLanguage()) + reviewer.getProfilePageUrl());
    dataModel.put("reviewer_image_url", reviewer.getAuthor().getProfileImageUrl(100));
    dataModel.put("review_creation_date", _getDateFormat(userPratilipi.getReviewDate()));
    if (userPratilipi.getRating() != null)
        dataModel.put("rating", userPratilipi.getRating());
    if (userPratilipi.getReview() != null)
        dataModel.put("review_review", HtmlUtil.truncateText(userPratilipi.getReview(), 250));
    if (userPratilipi.getCommentCount() != null)
        dataModel.put("review_comment_count", userPratilipi.getCommentCount().toString());
    return new Object[] { dataModel, pratilipi.getLanguage() };
}
Also used : UserData(com.pratilipi.data.client.UserData) HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) UserPratilipiData(com.pratilipi.data.client.UserPratilipiData) PratilipiData(com.pratilipi.data.client.PratilipiData) UserPratilipiData(com.pratilipi.data.client.UserPratilipiData)

Example 3 with UserData

use of com.pratilipi.data.client.UserData in project pratilipi by Pratilipi.

the class UserPratilipiDataUtil method createUserPratilipiData.

public static UserPratilipiData createUserPratilipiData(UserPratilipi userPratilipi) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    User user = dataAccessor.getUser(userPratilipi.getUserId());
    UserData userData = UserDataUtil.createUserData(user);
    PratilipiReviewsDoc reviewsDoc = docAccessor.getPratilipiReviewsDoc(userPratilipi.getPratilipiId());
    UserPratilipiData userPratilipiData = new UserPratilipiData();
    userPratilipiData.setId(userPratilipi.getId());
    userPratilipiData.setUserId(userPratilipi.getUserId());
    userPratilipiData.setPratilipiId(userPratilipi.getPratilipiId());
    userPratilipiData.setUser(userData);
    userPratilipiData.setUserName(userData.getDisplayName());
    userPratilipiData.setUserImageUrl(userData.getProfileImageUrl());
    userPratilipiData.setUserProfilePageUrl(userData.getProfilePageUrl());
    userPratilipiData.setRating(userPratilipi.getRating());
    userPratilipiData.setReview(processReview(userPratilipi.getReviewTitle(), userPratilipi.getReview()));
    userPratilipiData.setReviewState(userPratilipi.getReviewState());
    userPratilipiData.setReviewDate(userPratilipi.getReviewDate());
    for (UserPratilipiDoc review : reviewsDoc.getReviews()) {
        if (review.getId().equals(userPratilipi.getId())) {
            userPratilipiData.setCommentCount(review.getCommentCount());
            userPratilipiData.setLikeCount(review.getLikeCount());
            userPratilipiData.setLiked(review.getLikedByUserIds().contains(AccessTokenFilter.getAccessToken().getUserId()));
            break;
        }
    }
    userPratilipiData.setAddedToLib(userPratilipi.isAddedToLib());
    userPratilipiData.setAccessToReview(hasAccessToUpdateUserPratilipiData(userPratilipi, AccessType.USER_PRATILIPI_REVIEW));
    return userPratilipiData;
}
Also used : User(com.pratilipi.data.type.User) UserData(com.pratilipi.data.client.UserData) DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) UserPratilipiDoc(com.pratilipi.data.type.UserPratilipiDoc) UserPratilipiData(com.pratilipi.data.client.UserPratilipiData) PratilipiReviewsDoc(com.pratilipi.data.type.PratilipiReviewsDoc)

Example 4 with UserData

use of com.pratilipi.data.client.UserData in project pratilipi by Pratilipi.

the class UserPasswordUpdateApi method post.

@Post
public UserV1Api.Response post(PostRequest request) throws InvalidArgumentException, InsufficientAccessException {
    UserData userData;
    if (request.getEmail() != null && request.getVerificationToken() != null) {
        // Verifying email
        UserDataUtil.verifyUserEmail(request.getEmail(), request.getVerificationToken());
        // Updating password
        UserDataUtil.updateUserPassword(request.getEmail(), request.getVerificationToken(), request.getNewPassword());
        // Logging-in the user
        userData = UserDataUtil.loginUser(request.getEmail(), request.getVerificationToken());
    } else if (request.getPassword() != null) {
        // Updating password
        UserDataUtil.updateUserPassword(request.getPassword(), request.getNewPassword());
        userData = UserDataUtil.getCurrentUser();
    } else {
        throw new InvalidArgumentException(GenericRequest.ERR_INSUFFICIENT_ARGS);
    }
    return new UserV1Api.Response(userData, UserPasswordUpdateApi.class);
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UserData(com.pratilipi.data.client.UserData) Post(com.pratilipi.api.annotation.Post)

Example 5 with UserData

use of com.pratilipi.data.client.UserData in project pratilipi by Pratilipi.

the class UserDataUtil method _loginFederatedUser.

private static UserData _loginFederatedUser(UserData apiUserData, User user, UserSignUpSource signUpSource) throws InsufficientAccessException {
    // Do nothing if a user is already logged in
    if (!AccessTokenFilter.getAccessToken().getUserId().equals(0L))
        return getCurrentUser();
    // Note: Users having Facebook or Google Id can never be in GUEST or REFERRAL state.
    // Note: No action required for REGISTERED or ACTIVE users.
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    boolean isNew = false;
    if (user == null || user.getState() == UserState.DELETED) {
        if (apiUserData.getEmail() != null)
            user = dataAccessor.getUserByEmail(apiUserData.getEmail());
        AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), null, null);
        if (user == null || user.getState() == UserState.DELETED) {
            user = dataAccessor.newUser();
            auditLog.setAccessType(AccessType.USER_ADD);
            auditLog.setEventDataOld(user);
            user.setEmail(apiUserData.getEmail());
            // Counting on Facebook / Google for e-mail/user verification
            user.setState(UserState.ACTIVE);
            user.setSignUpDate(new Date());
            user.setSignUpSource(signUpSource);
            isNew = true;
        } else if (user.getState() == UserState.REFERRAL) {
            auditLog.setAccessType(AccessType.USER_ADD);
            auditLog.setEventDataOld(user);
            // Counting on Facebook / Google for e-mail/user verification
            user.setState(UserState.ACTIVE);
            user.setSignUpDate(new Date());
            user.setSignUpSource(signUpSource);
            isNew = true;
        } else if (user.getState() == UserState.REGISTERED) {
            auditLog.setAccessType(AccessType.USER_UPDATE);
            auditLog.setEventDataOld(user);
            // Counting on Facebook / google for e-mail/user verification
            user.setState(UserState.ACTIVE);
        } else {
            // user.getState() == UserState.ACTIVE || user.getState() == UserState.BLOCKED
            auditLog.setAccessType(AccessType.USER_UPDATE);
            auditLog.setEventDataOld(user);
        }
        if (apiUserData.getFacebookId() != null)
            user.setFacebookId(apiUserData.getFacebookId());
        if (apiUserData.getGoogleId() != null)
            user.setGoogleId(apiUserData.getGoogleId());
        user.setLastUpdated(new Date());
        user = dataAccessor.createOrUpdateUser(user, auditLog);
    }
    if (user.getState() == UserState.BLOCKED)
        throw new InsufficientAccessException(GenericRequest.ERR_ACCOUNT_BLOCKED);
    _loginUser(AccessTokenFilter.getAccessToken(), user);
    UserData userData = createUserData(user);
    if (isNew) {
        userData.setFirstName(apiUserData.getFirstName());
        userData.setLastName(apiUserData.getLastName());
        userData.setGender(apiUserData.getGender());
        userData.setDateOfBirth(apiUserData.getDateOfBirth());
    }
    return userData;
}
Also used : 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)

Aggregations

UserData (com.pratilipi.data.client.UserData)28 DataAccessor (com.pratilipi.data.DataAccessor)14 User (com.pratilipi.data.type.User)7 Date (java.util.Date)7 HashMap (java.util.HashMap)7 Post (com.pratilipi.api.annotation.Post)6 AuthorData (com.pratilipi.data.client.AuthorData)6 JsonObject (com.google.gson.JsonObject)5 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)5 Gson (com.google.gson.Gson)4 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)4 UserPratilipiData (com.pratilipi.data.client.UserPratilipiData)4 Author (com.pratilipi.data.type.Author)4 ArrayList (java.util.ArrayList)4 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)3 PratilipiData (com.pratilipi.data.client.PratilipiData)3 AuditLog (com.pratilipi.data.type.AuditLog)3 JsonElement (com.google.gson.JsonElement)2 GenericResponse (com.pratilipi.api.shared.GenericResponse)2 Language (com.pratilipi.common.type.Language)2