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);
}
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(" ".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 = " ";
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();
}
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);
}
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);
}
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;
}
Aggregations