Search in sources :

Example 11 with UserPratilipi

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

the class UserPratilipiDataUtil method hasAccessToUpdateUserPratilipiData.

public static boolean hasAccessToUpdateUserPratilipiData(UserPratilipi userPratilipi, AccessType accessType) {
    AccessToken accessToken = AccessTokenFilter.getAccessToken();
    if (!userPratilipi.getUserId().equals(accessToken.getUserId()))
        return false;
    if (!UserAccessUtil.hasUserAccess(accessToken.getUserId(), null, accessType))
        return false;
    // Review can not be created for content pieces created by the user
    if (accessType == AccessType.USER_PRATILIPI_REVIEW) {
        if (userPratilipi.getReviewState() == UserReviewState.BLOCKED)
            return false;
        DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
        Pratilipi pratilipi = dataAccessor.getPratilipi(userPratilipi.getPratilipiId());
        Author author = pratilipi.getAuthorId() == null ? null : dataAccessor.getAuthor(pratilipi.getAuthorId());
        if (author != null && userPratilipi.getUserId().equals(author.getUserId()))
            return false;
    }
    return true;
}
Also used : AccessToken(com.pratilipi.data.type.AccessToken) DataAccessor(com.pratilipi.data.DataAccessor) Author(com.pratilipi.data.type.Author) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi)

Example 12 with UserPratilipi

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

the class UserPratilipiDataUtil method saveUserPratilipiReview.

public static UserPratilipiData saveUserPratilipiReview(Long userId, Long pratilipiId, Integer rating, String review, UserReviewState reviewState) 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 (!hasAccessToUpdateUserPratilipiData(userPratilipi, AccessType.USER_PRATILIPI_REVIEW))
        throw new InsufficientAccessException();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_PRATILIPI_REVIEW, userPratilipi);
    if (rating != null && !rating.equals(userPratilipi.getRating()) && !(rating == 0 && userPratilipi.getRating() == null)) {
        userPratilipi.setRating(rating);
        userPratilipi.setRatingDate(new Date());
    }
    if (review != null) {
        userPratilipi.setReviewTitle(null);
        userPratilipi.setReview(review);
        if (userPratilipi.getReviewState() != UserReviewState.PUBLISHED) {
            // NOTE: As review comment and likes are counted only if they
            // were created after review date, do NOT update reviewDate if
            // review is already published.
            userPratilipi.setReviewState(UserReviewState.PUBLISHED);
            userPratilipi.setReviewDate(new Date());
        }
    }
    // review is already published.
    if (reviewState != null && reviewState != userPratilipi.getReviewState()) {
        userPratilipi.setReviewState(reviewState);
        userPratilipi.setReviewDate(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 13 with UserPratilipi

use of com.pratilipi.data.type.UserPratilipi 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 14 with UserPratilipi

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

the class UserPratilipiBackfillApi method updateUserPratilipi.

private UserPratilipiData updateUserPratilipi(Long userId, Long pratilipiId, String lastPageOpened, String lastOpenedDate) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(userId, pratilipiId);
    if (userPratilipi == null) {
        // CREATE USER PRATILIPI
        userPratilipi = dataAccessor.newUserPratilipi();
        userPratilipi.setUserId(userId);
        userPratilipi.setPratilipiId(pratilipiId);
    }
    // Creating Auditlog entity
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_PRATILIPI_REVIEW, userPratilipi);
    // update lastPageOpened
    userPratilipi.setLastOpenedPage(lastPageOpened);
    try {
        // update lastPageOpenedDate
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        formatter.setTimeZone(TimeZone.getTimeZone("IST"));
        Date date = (Date) formatter.parse(lastOpenedDate);
        userPratilipi.setLastOpenedDate(date);
        userPratilipi = dataAccessor.createOrUpdateUserPratilipi(userPratilipi, auditLog);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        logger.log(Level.SEVERE, "Event Date : " + lastOpenedDate);
        throw new UnexpectedServerException("Error while parsing date.");
    }
    return UserPratilipiDataUtil.createUserPratilipiData(userPratilipi);
}
Also used : UserPratilipi(com.pratilipi.data.type.UserPratilipi) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) DataAccessor(com.pratilipi.data.DataAccessor) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Aggregations

UserPratilipi (com.pratilipi.data.type.UserPratilipi)14 DataAccessor (com.pratilipi.data.DataAccessor)11 Date (java.util.Date)7 AuditLog (com.pratilipi.data.type.AuditLog)6 Comment (com.pratilipi.data.type.Comment)5 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)4 Vote (com.pratilipi.data.type.Vote)4 ArrayList (java.util.ArrayList)4 Author (com.pratilipi.data.type.Author)3 Pratilipi (com.pratilipi.data.type.Pratilipi)3 LinkedList (java.util.LinkedList)3 AccessToken (com.pratilipi.data.type.AccessToken)2 Page (com.pratilipi.data.type.Page)2 UserAuthor (com.pratilipi.data.type.UserAuthor)2 UserPratilipiEntity (com.pratilipi.data.type.gae.UserPratilipiEntity)2 Task (com.pratilipi.taskqueue.Task)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Get (com.pratilipi.api.annotation.Get)1 GenericResponse (com.pratilipi.api.shared.GenericResponse)1