use of com.pratilipi.api.annotation.Post in project pratilipi by Pratilipi.
the class PratilipiProcessApi method postPratilipiProcess.
@Post
public GenericResponse postPratilipiProcess(PostPratilipiProcessRequest request) throws InvalidArgumentException, UnexpectedServerException, InsufficientAccessException {
List<Long> pratilipiIdList = request.getPratilipiIdList() != null ? request.getPratilipiIdList() : new ArrayList<Long>(1);
if (request.getPratilipiId() != null)
pratilipiIdList.add(request.getPratilipiId());
if (pratilipiIdList.size() == 0) {
logger.log(Level.SEVERE, "0 pratilipiId found.");
throw new InvalidArgumentException("'pratilipiId' or 'pratilipiIdList' must be set/non-empty.");
}
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
if (request.validateData()) {
for (Long pratilipiId : pratilipiIdList) {
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
Author author = dataAccessor.getAuthor(pratilipi.getAuthorId());
Page pratilipiPage = dataAccessor.getPage(PageType.PRATILIPI, pratilipiId);
if (pratilipi.getLanguage() == null)
throw new InvalidArgumentException("Language is null.");
if (pratilipi.getAuthorId() != null && author == null)
throw new InvalidArgumentException("Invalid Author id.");
if (pratilipi.getState() == PratilipiState.PUBLISHED && author != null && author.getState() == AuthorState.DELETED)
throw new InvalidArgumentException("Linked Author entity is DELETED.");
if (pratilipi.getState() == PratilipiState.DELETED && pratilipiPage != null)
throw new InvalidArgumentException("DELETED Pratilipi has non-deleted Page entity.");
if (pratilipi.getState() != PratilipiState.DELETED && pratilipiPage == null)
throw new InvalidArgumentException("Page entity is missing for the Pratilipi.");
if (pratilipi.getState() == PratilipiState.DELETED)
continue;
if (pratilipi.getTitle() == null && pratilipi.getTitleEn() == null)
throw new InvalidArgumentException("Title is missing for the Pratilipi.");
if ((pratilipi.getTitle() != null && pratilipi.getTitle().trim().isEmpty()) || (pratilipi.getTitleEn() != null && pratilipi.getTitleEn().trim().isEmpty()))
throw new InvalidArgumentException("Title has empty string.");
}
}
if (request.processData()) {
PratilipiDataUtil.updatePratilipiSearchIndex(pratilipiIdList);
PratilipiDataUtil.updateFacebookScrape(pratilipiIdList);
}
if (request.processContent()) {
for (Long pratilipiId : pratilipiIdList) {
PratilipiDocUtil.updatePratilipiContent(pratilipiId);
PratilipiDataUtil.updatePratilipiIndex(pratilipiId);
boolean changed = PratilipiDocUtil.updateMeta(pratilipiId);
if (changed)
PratilipiDataUtil.updatePratilipiSearchIndex(pratilipiId, null);
}
}
if (request.processContentDoc()) {
for (Long pratilipiId : pratilipiIdList) {
boolean changed = PratilipiDocUtil.updateMeta(pratilipiId);
if (changed)
PratilipiDataUtil.updatePratilipiSearchIndex(pratilipiId, null);
}
}
if (request.updateReviewsDoc()) {
for (long pratilipiId : pratilipiIdList) PratilipiDocUtil.updatePratilipiReviews(pratilipiId);
}
if (request.updateStats()) {
List<Task> taskList = new ArrayList<>(pratilipiIdList.size());
List<Pratilipi> pratilipiList = DataAccessorFactory.getDataAccessor().getPratilipiList(pratilipiIdList);
for (int i = 0; i < pratilipiIdList.size(); i++) {
Pratilipi pratilipi = pratilipiList.get(i);
if (pratilipi == null || pratilipi.getState() != PratilipiState.PUBLISHED) {
pratilipiList.remove(i);
pratilipiIdList.remove(i);
i--;
continue;
}
PratilipiDataUtil.updatePratilipiStats(pratilipi.getId());
if (pratilipi.getAuthorId() != null) {
// Creating tasks to update author entities
Task task = TaskQueueFactory.newTask().setUrl("/author/process").addParam("authorId", pratilipi.getAuthorId().toString()).addParam("updateStats", "true");
taskList.add(task);
}
}
PratilipiDataUtil.updatePratilipiSearchIndex(pratilipiIdList);
TaskQueueFactory.getAuthorOfflineTaskQueue().addAll(taskList);
}
if (request.updateUserPratilipiStats()) {
for (long pratilipiId : pratilipiIdList) PratilipiDataUtil.updateUserPratilipiStats(pratilipiId);
}
return new GenericResponse();
}
use of com.pratilipi.api.annotation.Post in project pratilipi by Pratilipi.
the class PratilipiStatsApi method post.
@Post
public GenericResponse post(Request request) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(request.pratilipiId);
if (pratilipi == null || pratilipi.getState() != PratilipiState.PUBLISHED)
return new GenericResponse();
if (request.fbLikeShareCount == null) {
Page page = dataAccessor.getPage(PageType.PRATILIPI, pratilipi.getId());
request.fbLikeShareCount = FacebookApi.getUrlShareCount("http://" + Website.ALL_LANGUAGE.getHostName() + page.getUri());
}
PratilipiDataUtil.updatePratilipiStats(request.pratilipiId, request.readCountOffset, request.readCount, request.fbLikeShareCountOffset, request.fbLikeShareCount);
PratilipiDataUtil.updatePratilipiSearchIndex(request.pratilipiId, null);
if (pratilipi.getAuthorId() != null && (request.readCountOffset != null || request.readCount != null)) {
Task task = TaskQueueFactory.newTask().setUrl("/author/process").addParam("authorId", pratilipi.getAuthorId().toString()).addParam("updateStats", "true");
TaskQueueFactory.getAuthorOfflineTaskQueue().add(task);
}
return new GenericResponse();
}
use of com.pratilipi.api.annotation.Post 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.api.annotation.Post in project pratilipi by Pratilipi.
the class UserRegisterApi method post.
@Post
public UserV1Api.Response post(PostRequest request) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
String firstName = request.name.trim();
String lastName = null;
String email = request.email.trim().toLowerCase();
if (firstName.lastIndexOf(' ') != -1) {
lastName = firstName.substring(firstName.lastIndexOf(' ') + 1);
firstName = firstName.substring(0, firstName.lastIndexOf(' '));
}
// Register the User.
UserData userData = UserDataUtil.registerUser(firstName, lastName, email, request.password, UserDataUtil.getUserSignUpSource(false, false));
// Create Author profile for the User.
Long authorId = AuthorDataUtil.createAuthorProfile(userData, request.language);
// Log-in the User and get update UserData
userData = UserDataUtil.loginUser(email, request.password);
Task task1 = TaskQueueFactory.newTask().setUrl("/user/email").addParam("userId", userData.getId().toString()).addParam("language", request.language.toString()).addParam("sendWelcomeMail", "true");
Task task2 = TaskQueueFactory.newTask().setUrl("/user/email").addParam("userId", userData.getId().toString()).addParam("language", request.language.toString()).addParam("sendEmailVerificationMail", "true");
Task task3 = TaskQueueFactory.newTask().setUrl("/author/process").addParam("authorId", authorId.toString()).addParam("processData", "true");
TaskQueueFactory.getUserTaskQueue().addAll(task1, task2);
TaskQueueFactory.getAuthorTaskQueue().add(task3);
return new UserV1Api.Response(userData, UserRegisterApi.class);
}
use of com.pratilipi.api.annotation.Post in project pratilipi by Pratilipi.
the class UserEmailApi method post.
@Post
public GenericResponse post(PostRequest request) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
JsonObject errorMessages = new JsonObject();
if ((request.sendWelcomeMail() || request.sendBirthdayMail()) && request.getUserId() == null)
errorMessages.addProperty("userId", GenericRequest.ERR_USER_ID_REQUIRED);
if (request.sendEmailVerificationMail() && request.getUserId() == null && (request.getEmail() == null || request.getEmail().trim().isEmpty()))
errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REQUIRED);
if (request.sendPasswordResetMail() && (request.getEmail() == null || request.getEmail().trim().isEmpty()))
errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REQUIRED);
if (errorMessages.entrySet().size() > 0)
throw new InvalidArgumentException(errorMessages);
Language language = request.getLanguage() == null ? // User facing requests
UxModeFilter.getDisplayLanguage() : // Offline requests
request.getLanguage();
if (request.sendWelcomeMail())
UserDataUtil.sendWelcomeMail(request.getUserId(), language);
if (request.sendEmailVerificationMail()) {
if (request.getUserId() != null)
UserDataUtil.sendEmailVerificationMail(request.getUserId(), language);
else
UserDataUtil.sendEmailVerificationMail(request.getEmail(), language);
}
if (request.sendPasswordResetMail())
UserDataUtil.sendPasswordResetMail(request.getEmail(), language);
if (request.sendBirthdayMail())
throw new InvalidArgumentException("Feature not yet supported !");
return new GenericResponse();
}
Aggregations