Search in sources :

Example 11 with AuditLog

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

the class PratilipiDataUtil method savePratilipiCover.

public static String savePratilipiCover(Long pratilipiId, BlobEntry blobEntry) throws InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (!hasAccessToUpdatePratilipiData(pratilipi, null))
        throw new InsufficientAccessException();
    String coverImageName = new Date().getTime() + "";
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    blobEntry.setName("pratilipi/" + pratilipiId + "/images/" + coverImageName);
    blobAccessor.createOrUpdateBlob(blobEntry);
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
    pratilipi.setCoverImage(coverImageName);
    pratilipi.setLastUpdated(new Date());
    pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
    return createPratilipiCoverUrl(pratilipi);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) BlobAccessor(com.pratilipi.data.BlobAccessor) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Date(java.util.Date) AuditLog(com.pratilipi.data.type.AuditLog)

Example 12 with AuditLog

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

the class PratilipiDataUtil method updatePratilipiContent.

public static int updatePratilipiContent(long pratilipiId, int pageNo, PratilipiContentType contentType, Object pageContent, boolean insertNew) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (!hasAccessToUpdatePratilipiContent(pratilipi))
        throw new InsufficientAccessException();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    if (contentType == PratilipiContentType.PRATILIPI) {
        BlobEntry blobEntry = blobAccessor.getBlob(CONTENT_FOLDER + "/" + pratilipiId);
        if (blobEntry == null) {
            blobEntry = blobAccessor.newBlob(CONTENT_FOLDER + "/" + pratilipiId);
            blobEntry.setData("&nbsp".getBytes(Charset.forName("UTF-8")));
            blobEntry.setMimeType("text/html");
        }
        String content = new String(blobEntry.getData(), Charset.forName("UTF-8"));
        PratilipiContentUtil pratilipiContentUtil = new PratilipiContentUtil(content);
        content = pratilipiContentUtil.updateContent(pageNo, (String) pageContent, insertNew);
        int pageCount = pratilipiContentUtil.getPageCount();
        if (content.isEmpty()) {
            content = "&nbsp";
            pageCount = 1;
        }
        blobEntry.setData(content.getBytes(Charset.forName("UTF-8")));
        blobAccessor.createOrUpdateBlob(blobEntry);
        pratilipi.setPageCount(pageCount);
        if (insertNew)
            auditLog.setEventComment("Added new page " + pageNo + " in Pratilpi content.");
        else if (!((String) pageContent).isEmpty())
            auditLog.setEventComment("Updated page " + pageNo + " in Pratilpi content.");
        else
            auditLog.setEventComment("Deleted page " + pageNo + " in Pratilpi content.");
    } else if (contentType == PratilipiContentType.IMAGE) {
        BlobEntry blobEntry = (BlobEntry) pageContent;
        blobEntry.setName(IMAGE_CONTENT_FOLDER + "/" + pratilipiId + "/" + pageNo);
        blobAccessor.createOrUpdateBlob(blobEntry);
        if (pageNo > (int) pratilipi.getPageCount())
            pratilipi.setPageCount(pageNo);
        auditLog.setEventComment("Uploaded page " + pageNo + " in Image content.");
    } else {
        throw new InvalidArgumentException(contentType + " content type is not yet supported.");
    }
    pratilipi.setLastUpdated(new Date());
    pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
    return pratilipi.getPageCount();
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) BlobAccessor(com.pratilipi.data.BlobAccessor) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 13 with AuditLog

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

the class UserDataUtil method updateUserPassword.

public static void updateUserPassword(String email, String verificationToken, String newPassword) throws InvalidArgumentException, InsufficientAccessException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUserByEmail(email.toLowerCase());
    JsonObject errorMessages = new JsonObject();
    if (user == null || user.getState() == UserState.REFERRAL || user.getState() == UserState.DELETED) {
        errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_NOT_REGISTERED);
        throw new InvalidArgumentException(errorMessages);
    } else if (user.getState() == UserState.BLOCKED) {
        throw new InvalidArgumentException(GenericRequest.ERR_ACCOUNT_BLOCKED);
    }
    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.setPassword(PasswordUtil.getSaltedHash(newPassword));
    // 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) JsonObject(com.google.gson.JsonObject) AuditLog(com.pratilipi.data.type.AuditLog)

Example 14 with AuditLog

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

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

the class UserDataUtil method saveUserData.

public static UserData saveUserData(UserData userData) throws InvalidArgumentException, InsufficientAccessException {
    _validateUserDataForSave(userData);
    boolean isNew = userData.getId() == null;
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = isNew ? dataAccessor.newUser() : dataAccessor.getUser(userData.getId());
    if (isNew && !hasAccessToAddUserData(userData))
        throw new InsufficientAccessException();
    if (!isNew && !hasAccessToUpdateUserData(user, userData))
        throw new InsufficientAccessException();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), isNew ? AccessType.USER_ADD : AccessType.USER_UPDATE, user);
    if (userData.hasEmail() && !userData.getEmail().equals(user.getEmail())) {
        user.setEmail(userData.getEmail());
        if (user.getState() == UserState.ACTIVE)
            user.setState(UserState.REGISTERED);
        user.setVerificationToken(null);
    }
    if (userData.hasPhone())
        user.setPhone(userData.getPhone());
    if (userData.hasLanguage())
        user.setLanguage(userData.getLanguage());
    if (isNew) {
        // Assuming only AEEs have USER_ADD access.
        user.setState(UserState.REFERRAL);
        user.setCampaign(UserCampaign.AEE_TEAM);
        user.setReferrer(AccessTokenFilter.getAccessToken().getUserId().toString());
        user.setSignUpDate(new Date());
        user.setSignUpSource(getUserSignUpSource(false, false));
    }
    user.setLastUpdated(new Date());
    user = dataAccessor.createOrUpdateUser(user, auditLog);
    if (isNew) {
        String firstName = userData.getFirstName();
        String lastName = userData.getLastName();
        userData = createUserData(user, null);
        userData.setFirstName(firstName);
        userData.setLastName(lastName);
    } else {
        userData = createUserData(user);
    }
    return userData;
}
Also used : User(com.pratilipi.data.type.User) DataAccessor(com.pratilipi.data.DataAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) 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