use of com.pratilipi.data.type.gae.AuthorEntity in project pratilipi by Pratilipi.
the class UserProcessApi method post.
@Post
public GenericResponse post(PostRequest request) throws InvalidArgumentException, UnexpectedServerException {
if (request.validateData != null && request.validateData) {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
User user = dataAccessor.getUser(request.userId);
// DELETED User entity can not have a non-DELETED Author entity linked.
if (user.getState() == UserState.DELETED) {
Author author = dataAccessor.getAuthorByUserId(user.getId());
if (author != null && author.getState() != AuthorState.DELETED)
throw new InvalidArgumentException("DELETED User entity has a non-DELETED Author entity linked.");
// TODO: DELETED User entity can not have non-DELETED UserAuthor entities.
return new GenericResponse();
}
// Either of two - email and facebook id - must be present.
if (user.getEmail() == null && user.getFacebookId() == null)
throw new InvalidArgumentException("Neither email nor facebook id is set.");
// Email, if present, must not be empty.
if (user.getEmail() != null && user.getEmail().trim().isEmpty())
throw new InvalidArgumentException("Email is empty.");
// Facebook id, if present, must not be empty.
if (user.getFacebookId() != null && user.getFacebookId().trim().isEmpty())
throw new InvalidArgumentException("Facebook Id is empty.");
// Email, if present, must be trimmed and converted to lower case.
if (user.getEmail() != null && !user.getEmail().equals(user.getEmail().trim().toLowerCase()))
throw new InvalidArgumentException("Email is either not trimmed or not converted to lower case.");
// Only one non-DELETED User entity can exist per email id.
if (user.getEmail() != null) {
Query<UserEntity> query = ObjectifyService.ofy().load().type(UserEntity.class).filter("EMAIL", user.getEmail()).filter("STATE !=", UserState.DELETED).order("STATE").order("SIGN_UP_DATE");
List<UserEntity> list = query.list();
if (list.size() != 1)
throw new InvalidArgumentException(list.size() + " non-DELETED User entities found for email " + user.getEmail() + " .");
}
// Only one non-DELETED User entity can exist per facebook id.
if (user.getFacebookId() != null) {
Query<UserEntity> query = ObjectifyService.ofy().load().type(UserEntity.class).filter("FACEBOOK_ID", user.getFacebookId()).filter("STATE !=", UserState.DELETED).order("STATE").order("SIGN_UP_DATE");
List<UserEntity> list = query.list();
if (list.size() != 1)
throw new InvalidArgumentException(list.size() + " non-DELETED User entities found for Facebook Id " + user.getFacebookId() + " .");
}
// Author profile for the user.
Query<AuthorEntity> query = ObjectifyService.ofy().load().type(AuthorEntity.class).filter("USER_ID", user.getId()).filter("STATE !=", AuthorState.DELETED).order("STATE").order("REGISTRATION_DATE");
List<AuthorEntity> authorList = query.list();
if (authorList.size() == 0) {
if (user.getState() != UserState.REFERRAL || user.getSignUpSource() != UserSignUpSource.PRE_LAUNCH_WEBSITE)
throw new InvalidArgumentException("Could not find an Author entity linked.");
} else if (authorList.size() == 1) {
// Do Nothing.
} else {
throw new InvalidArgumentException("User has " + authorList.size() + " non-DELETED Author entities linked.");
}
}
if (request.updateFollowsDoc != null && request.updateFollowsDoc) {
UserDocUtil.updateUserFollows(request.userId);
}
if (request.updateUserAuthorStats != null && request.updateUserAuthorStats) {
UserDataUtil.updateUserAuthorStats(request.userId);
}
return new GenericResponse();
}
use of com.pratilipi.data.type.gae.AuthorEntity in project pratilipi by Pratilipi.
the class DataAccessorMockImpl method createOrUpdateAuthor.
@Override
public Author createOrUpdateAuthor(Author author) {
if (author.getId() != null)
return author;
long authorId = 0L;
for (Author aAuthor : AUTHOR_TABLE) if (authorId <= aAuthor.getId())
authorId = aAuthor.getId() + 1;
((AuthorEntity) author).setId(authorId);
AUTHOR_TABLE.add(author);
return author;
}
use of com.pratilipi.data.type.gae.AuthorEntity in project pratilipi by Pratilipi.
the class DataStoreCleanupUtil method delete.
public static void delete(User user, boolean preview) {
System.out.println();
System.out.println("User id: " + user.getId() + ", state: " + user.getState() + ", signUpDate:" + user.getSignUpDate() + ", signUpSource:" + user.getSignUpSource());
if (!preview && user.getState() != UserState.DELETED && user.getState() != UserState.BLOCKED) {
user.setState(UserState.DELETED);
// Save
ObjectifyService.ofy().save().entity(user).now();
}
// ACCESS_TOKEN Table
List<AccessTokenEntity> accessTokenList = ObjectifyService.ofy().load().type(AccessTokenEntity.class).filter("USER_ID", user.getId()).filter("EXPIRY >", new Date()).list();
System.out.println("AccessTokenEntity # " + accessTokenList.size());
if (!preview) {
for (AccessToken accessToken : accessTokenList) {
accessToken.setExpiry(new Date());
// Save
ObjectifyService.ofy().save().entity(accessToken).now();
}
}
// USER_PRATILIPI Table
List<UserPratilipiEntity> userPratilipiList = ObjectifyService.ofy().load().type(UserPratilipiEntity.class).filter("USER_ID", user.getId()).list();
System.out.println("UserPratilipiEntity # " + userPratilipiList.size());
int reviewCount = 0;
int ratingCount = 0;
for (UserPratilipi userPratilipi : userPratilipiList) {
if (userPratilipi.getReview() != null && userPratilipi.getReviewState() != UserReviewState.DELETED && userPratilipi.getReviewState() != UserReviewState.BLOCKED)
reviewCount++;
if (userPratilipi.getRating() != null && userPratilipi.getRating() != 0)
ratingCount++;
}
System.out.println("Review ## " + reviewCount);
System.out.println("Rating ## " + ratingCount);
if (!preview) {
for (UserPratilipi userPratilipi : userPratilipiList) {
boolean save = false;
if (userPratilipi.getReviewState() != UserReviewState.DELETED && userPratilipi.getReviewState() != UserReviewState.BLOCKED) {
userPratilipi.setReviewState(UserReviewState.DELETED);
save = true;
}
if (userPratilipi.getRating() != null && userPratilipi.getRating() != 0) {
userPratilipi.setRating(null);
save = true;
}
if (save) {
// Save
ObjectifyService.ofy().save().entity(userPratilipi).now();
Task task = TaskQueueFactory.newTask().setUrl("/pratilipi/process").addParam("pratilipiId", userPratilipi.getPratilipiId().toString()).addParam("updateReviewsDoc", "true").addParam("updateUserPratilipiStats", "true");
TaskQueueFactory.getPratilipiTaskQueue().add(task);
}
}
}
// USER_AUTHOR Table
List<UserAuthorEntity> userAuthorList = ObjectifyService.ofy().load().type(UserAuthorEntity.class).filter("USER_ID", user.getId()).list();
System.out.println("UserAuthorEntity # " + userAuthorList.size());
int followCount = 0;
for (UserAuthor userAuthor : userAuthorList) if (userAuthor.getFollowState() == UserFollowState.FOLLOWING)
followCount++;
System.out.println("Follow ## " + followCount);
if (!preview) {
for (UserAuthor userAuthor : userAuthorList) {
if (userAuthor.getFollowState() != null) {
userAuthor.setFollowState(null);
// Save
ObjectifyService.ofy().save().entity(userAuthor).now();
Task task = TaskQueueFactory.newTask().setUrl("/author/process").addParam("authorId", userAuthor.getAuthorId().toString()).addParam("updateUserAuthorStats", "true");
TaskQueueFactory.getUserAuthorTaskQueue().add(task);
}
}
Task task = TaskQueueFactory.newTask().setUrl("/user/process").addParam("userId", user.getId().toString()).addParam("updateFollowsDoc", "true").addParam("updateUserAuthorStats", "true");
TaskQueueFactory.getUserAuthorTaskQueue().add(task);
}
// COMMENT Table
List<CommentEntity> commentList = ObjectifyService.ofy().load().type(CommentEntity.class).filter("USER_ID", user.getId()).list();
System.out.println("CommentEntity # " + commentList.size());
int commentCount = 0;
for (Comment comment : commentList) if (comment.getState() == CommentState.ACTIVE)
commentCount++;
System.out.println("Comment ## " + commentCount);
if (!preview) {
for (Comment comment : commentList) {
if (comment.getState() == CommentState.ACTIVE) {
comment.setState(CommentState.DELETED);
// Save
ObjectifyService.ofy().save().entity(comment).now();
if (comment.getReferenceType() == ReferenceType.PRATILIPI) {
Task task = TaskQueueFactory.newTask().setUrl("/pratilipi/process").addParam("pratilipiId", comment.getReferenceId().toString()).addParam("updateReviewsDoc", "true").addParam("updateUserPratilipiStats", "true");
TaskQueueFactory.getPratilipiTaskQueue().add(task);
}
}
}
}
// VOTE Table
List<VoteEntity> voteList = ObjectifyService.ofy().load().type(VoteEntity.class).filter("USER_ID", user.getId()).list();
System.out.println("VoteEntity # " + voteList.size());
int voteCount = 0;
for (Vote vote : voteList) if (vote.getType() != VoteType.NONE)
voteCount++;
System.out.println("Vote ## " + voteCount);
if (!preview) {
for (Vote vote : voteList) {
// Delete
ObjectifyService.ofy().delete().entity(vote).now();
if (vote.getReferenceType() == ReferenceType.PRATILIPI) {
Task task = TaskQueueFactory.newTask().setUrl("/pratilipi/process").addParam("pratilipiId", vote.getReferenceId().toString()).addParam("updateReviewsDoc", "true").addParam("updateUserPratilipiStats", "true");
TaskQueueFactory.getPratilipiTaskQueue().add(task);
}
}
}
// AUTHOR Table
List<AuthorEntity> authorList = ObjectifyService.ofy().load().type(AuthorEntity.class).filter("USER_ID", user.getId()).list();
System.out.println();
System.out.println("AuthorEntity # " + authorList.size());
if (authorList.size() == 0)
return;
for (Author author : authorList) delete(author, preview);
}
use of com.pratilipi.data.type.gae.AuthorEntity in project pratilipi by Pratilipi.
the class DataStoreBackupUtil method csvAuthor.
public static void csvAuthor() throws UnexpectedServerException {
String CSV_HEADER = "AuthorId,UserId," + "FirstName,LastName,PenName,FirstNameEN,LastNameEN,PenNameEN," + "Language,HasSummary,ContentsPublished,FollowCount,RegistrationDate";
String CSV_SEPARATOR = ",";
String LINE_SEPARATOR = "\n";
BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessorBackup();
DateFormat csvDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
csvDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
StringBuilder csv = new StringBuilder(CSV_HEADER + LINE_SEPARATOR);
int count = 0;
QueryResultIterator<AuthorEntity> itr = ObjectifyService.ofy().cache(false).load().type(AuthorEntity.class).chunk(1000).iterator();
while (itr.hasNext()) {
Author author = itr.next();
csv.append("'" + author.getId()).append(CSV_SEPARATOR).append(author.getUserId() == null ? "" : "'" + author.getUserId()).append(CSV_SEPARATOR).append(author.getFirstName() == null ? "" : author.getFirstName()).append(CSV_SEPARATOR).append(author.getLastName() == null ? "" : author.getLastName()).append(CSV_SEPARATOR).append(author.getPenName() == null ? "" : author.getPenName()).append(CSV_SEPARATOR).append(author.getFirstNameEn() == null ? "" : author.getFirstNameEn()).append(CSV_SEPARATOR).append(author.getLastNameEn() == null ? "" : author.getLastNameEn()).append(CSV_SEPARATOR).append(author.getPenNameEn() == null ? "" : author.getPenNameEn()).append(CSV_SEPARATOR).append(author.getLanguage()).append(CSV_SEPARATOR).append(author.getSummary() != null && author.getSummary().trim().length() != 0).append(CSV_SEPARATOR).append(author.getContentPublished()).append(CSV_SEPARATOR).append(author.getFollowCount()).append(CSV_SEPARATOR).append(csvDateFormat.format(author.getRegistrationDate())).append(LINE_SEPARATOR);
count++;
if (count % 1000 == 0)
System.out.println(count + " ...");
}
System.out.println(count + " ...");
BlobEntry authorCsvEntry = blobAccessor.newBlob("datastore/author.csv", csv.toString().getBytes(Charset.forName("UTF-8")), "text/csv");
blobAccessor.createOrUpdateBlob(authorCsvEntry);
}
Aggregations