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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations