use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.
the class PratilipiDocUtil method getContent_v3.
public static Object getContent_v3(Long pratilipiId, Integer chapterNo, Integer pageNo) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
if (!PratilipiDataUtil.hasAccessToReadPratilipiContent(pratilipi))
throw new InsufficientAccessException();
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiId);
if (pcDoc == null)
return null;
else if (chapterNo == null)
return _processContent_v3(pratilipi, pcDoc);
Chapter chapter = pcDoc.getChapter(chapterNo);
if (chapter == null)
return null;
else if (pageNo == null)
return _processContent_v3(pratilipi, chapter);
PratilipiContentDoc.Page page = chapter.getPage(pageNo);
if (page == null)
return null;
else
return _processContent_v3(pratilipi, chapter.getPage(pageNo));
}
use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.
the class PratilipiDocUtil method updatePratilipiContent.
public static void updatePratilipiContent(Long pratilipiId) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
PratilipiContentDoc pcDoc = DataAccessorFactory.getDocAccessor().newPratilipiContentDoc();
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
if (!pratilipi.isOldContent()) {
return;
} else if (pratilipi.getContentType() == PratilipiContentType.PRATILIPI) {
BlobEntry blobEntry = blobAccessor.getBlob("pratilipi-content/pratilipi/" + pratilipiId);
if (blobEntry == null)
return;
String contentHtml = new String(blobEntry.getData(), Charset.forName("UTF-8"));
List<Object[]> pageletList = _createPageletList(pratilipi, Jsoup.parse(contentHtml).body());
if (pageletList.size() > 0) {
PratilipiContentDoc.Chapter chapter = null;
if (pageletList.get(0)[0] != PratilipiContentDoc.PageletType.HEAD)
chapter = pcDoc.addChapter(pratilipi.getTitle() == null ? pratilipi.getTitleEn() : pratilipi.getTitle());
for (Object[] pagelet : pageletList) {
if (pagelet[0] == PratilipiContentDoc.PageletType.HEAD) {
chapter = pcDoc.addChapter((String) pagelet[1]);
} else {
PratilipiContentDoc.Page page = chapter.getPage(1);
if (page == null)
page = chapter.addPage();
page.addPagelet((PratilipiContentDoc.PageletType) pagelet[0], pagelet[1], (PratilipiContentDoc.AlignmentType) pagelet[2]);
}
}
}
} else if (pratilipi.getContentType() == PratilipiContentType.IMAGE) {
for (int i = 1; i <= pratilipi.getPageCount(); i++) {
BlobEntry blobEntry = blobAccessor.getBlob("pratilipi/" + pratilipiId + "/images/" + i);
if (pratilipi.getId() == 5639838220943360L && i <= 5)
// Skipping first 5 pages as per Shally's request
continue;
else if (pratilipi.getId() == 5749258686824448L && i <= 4)
// Skipping first 4 pages as per Shally's request
continue;
else if (pratilipi.getId() == 5486454792781824L && i <= 1)
// Skipping first page as per Shally's request
continue;
else if (blobEntry == null && pratilipi.getId() == 5768181499035648L)
// Skipping missing pages as per Dileepan's request
continue;
JsonObject imgData = new JsonObject();
imgData.addProperty("name", i + "");
imgData.addProperty("height", ImageUtil.getHeight(blobEntry));
imgData.addProperty("width", ImageUtil.getWidth(blobEntry));
pcDoc.addChapter(null).addPage().addPagelet(PratilipiContentDoc.PageletType.IMAGE, imgData);
}
} else {
throw new UnexpectedServerException("ContentType " + pratilipi.getContentType() + " is not supported !");
}
docAccessor.save(pratilipiId, pcDoc);
}
use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.
the class AuditLogProcessApi method get.
@Get
public GenericResponse get(GenericRequest request) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
// Fetching AppProperty
String appPropertyId = "Api.AuditLogProcess";
AppProperty appProperty = dataAccessor.getAppProperty(appPropertyId);
if (appProperty == null)
appProperty = dataAccessor.newAppProperty(appPropertyId);
// Fetching list of audit logs
DataListCursorTuple<AuditLog> auditLogDataListCursorTuple = dataAccessor.getAuditLogList(// Tuesday, 1 August 2017 00:00:00 GMT+05:30
new Date(1501525800000L), (String) appProperty.getValue(), 5000);
// Make sets of PrimaryContent ids
Map<Long, Set<Long>> pratilipiUpdateIds = new HashMap<>();
Set<String> userPratilipiUpdateIds = new HashSet<>();
Set<String> userAuthorUpdateIds = new HashSet<>();
Map<String, UserAuthor> userAuthors = new HashMap<>();
Set<Long> commentUpdateIds = new HashSet<>();
Set<String> voteUpdateIds = new HashSet<>();
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new GsonLongDateAdapter()).create();
for (AuditLog auditLog : auditLogDataListCursorTuple.getDataList()) {
// TODO: Delete following condition as soon as 'legacy' module is removed
if (auditLog.getUserId() == null || auditLog.getPrimaryContentId() == null) {
continue;
}
if (auditLog.getUserId().equals(SystemProperty.SYSTEM_USER_ID)) {
continue;
}
if (auditLog.getAccessType() == AccessType.PRATILIPI_UPDATE) {
Pratilipi oldPratilipi = gson.fromJson(auditLog.getEventDataOld(), PratilipiEntity.class);
Pratilipi newPratilipi = gson.fromJson(auditLog.getEventDataNew(), PratilipiEntity.class);
if (oldPratilipi.getState() == PratilipiState.DRAFTED && newPratilipi.getState() == PratilipiState.PUBLISHED) {
Set<Long> userIdSet = pratilipiUpdateIds.get(auditLog.getPrimaryContentIdLong());
if (userIdSet == null) {
userIdSet = new HashSet<>();
pratilipiUpdateIds.put(auditLog.getPrimaryContentIdLong(), userIdSet);
}
userIdSet.add(auditLog.getUserId());
}
} else if (auditLog.getAccessType() == AccessType.USER_PRATILIPI_REVIEW) {
UserPratilipi oldUserPratilipi = gson.fromJson(auditLog.getEventDataOld(), UserPratilipiEntity.class);
UserPratilipi newUserPratilipi = gson.fromJson(auditLog.getEventDataNew(), UserPratilipiEntity.class);
if (oldUserPratilipi.getRating() == null && oldUserPratilipi.getReview() == null && (newUserPratilipi.getRating() != null || newUserPratilipi.getReview() != null))
userPratilipiUpdateIds.add(auditLog.getPrimaryContentId());
} else if (auditLog.getAccessType() == AccessType.USER_AUTHOR_FOLLOWING) {
UserAuthor oldUserAuthor = gson.fromJson(auditLog.getEventDataOld(), UserAuthorEntity.class);
UserAuthor newUserAuthor = gson.fromJson(auditLog.getEventDataNew(), UserAuthorEntity.class);
if (oldUserAuthor.getFollowState() == null && newUserAuthor.getFollowState() == UserFollowState.FOLLOWING) {
userAuthorUpdateIds.add(auditLog.getPrimaryContentId());
userAuthors.put(auditLog.getPrimaryContentId(), newUserAuthor);
}
} else if (auditLog.getAccessType() == AccessType.COMMENT_ADD) {
commentUpdateIds.add(auditLog.getPrimaryContentIdLong());
} else if (auditLog.getAccessType() == AccessType.VOTE) {
Vote newVote = gson.fromJson(auditLog.getEventDataNew(), VoteEntity.class);
if (newVote.getType() == VoteType.LIKE)
voteUpdateIds.add(auditLog.getPrimaryContentId());
}
}
// Batch get Vote entities
logger.log(Level.INFO, "Fetching " + voteUpdateIds.size() + " Vote Entities.");
Map<String, Vote> votes = dataAccessor.getVotes(voteUpdateIds);
// Batch get Comment and entities
Set<Long> commentIds = new HashSet<>(commentUpdateIds);
for (Vote vote : votes.values()) if (vote.getParentType() == VoteParentType.COMMENT)
commentIds.add(vote.getParentIdLong());
logger.log(Level.INFO, "Fetching " + commentIds.size() + " Comment Entities.");
Map<Long, Comment> comments = dataAccessor.getComments(commentIds);
// Batch get UserPratilipi entities
Set<String> userPratilipiIds = new HashSet<>(userPratilipiUpdateIds);
for (Comment comment : comments.values()) if (comment.getParentType() == CommentParentType.REVIEW)
userPratilipiIds.add(comment.getParentId());
for (Vote vote : votes.values()) if (vote.getParentType() == VoteParentType.REVIEW)
userPratilipiIds.add(vote.getParentId());
logger.log(Level.INFO, "Fetching " + userPratilipiIds.size() + " UserPratilipi Entities.");
Map<String, UserPratilipi> userPratilipis = dataAccessor.getUserPratilipis(userPratilipiIds);
// Batch get Pratilipi entities
Set<Long> pratilipiIds = new HashSet<>(pratilipiUpdateIds.keySet());
for (UserPratilipi userPratilipi : userPratilipis.values()) pratilipiIds.add(userPratilipi.getPratilipiId());
logger.log(Level.INFO, "Fetching " + pratilipiIds.size() + " Pratilipi Entities.");
Map<Long, Pratilipi> pratilipis = dataAccessor.getPratilipis(pratilipiIds);
// Batch get UserAuthor entities
logger.log(Level.INFO, "Fetching " + userAuthorUpdateIds.size() + " UserAuthor Entities.");
// Batch get Author entities
Set<Long> authorIds = new HashSet<>();
for (Pratilipi pratilipi : pratilipis.values()) authorIds.add(pratilipi.getAuthorId());
for (UserAuthor userAuthor : userAuthors.values()) authorIds.add(userAuthor.getAuthorId());
logger.log(Level.INFO, "Fetching " + authorIds.size() + " Author Entities.");
Map<Long, Author> authors = dataAccessor.getAuthors(authorIds);
List<Email> totalEmailList = new ArrayList<>();
// Get userIds of followers for authors who had published
List<Long> pratilipiUpdateAuthorIds = new ArrayList<>();
for (Long pratilipiId : pratilipiUpdateIds.keySet()) pratilipiUpdateAuthorIds.add(pratilipis.get(pratilipiId).getAuthorId());
Map<Long, List<Long>> authorsFollowersMap = _getUserAuthorFollowList(pratilipiUpdateAuthorIds);
// auditLog.getAccessType() == AccessType.PRATILIPI_UPDATE
for (Long pratilipiId : pratilipiUpdateIds.keySet()) {
Pratilipi pratilipi = pratilipis.get(pratilipiId);
Set<Long> followerUserIdList = new HashSet<>(authorsFollowersMap.get(pratilipi.getAuthorId()));
Email email = _createPratilipiPublishedEmail(pratilipi, authors.get(pratilipi.getAuthorId()));
if (email != null)
totalEmailList.add(email);
totalEmailList.addAll(_createPratilipiPublishedEmails(pratilipi, followerUserIdList));
// Send notification to all AEEs as well
// only if the content is self-published
List<Long> aeeUserIdList = _getAeeUserIdList(pratilipi.getLanguage());
Set<Long> userIdSet = pratilipiUpdateIds.get(pratilipiId);
for (Long userId : userIdSet) {
if (!aeeUserIdList.contains(userId)) {
followerUserIdList.addAll(aeeUserIdList);
break;
}
}
_createPratilipiPublishedNotification(pratilipi, authors.get(pratilipi.getAuthorId()));
_createPratilipiPublishedNotifications(pratilipi, followerUserIdList);
}
// auditLog.getAccessType() == AccessType.USER_PRATILIPI_REVIEW
for (String userPratilipiId : userPratilipiUpdateIds) {
UserPratilipi userPratilipi = userPratilipis.get(userPratilipiId);
if (userPratilipi.getReviewState() != UserReviewState.PUBLISHED)
continue;
Long pratilipiId = userPratilipi.getPratilipiId();
Email email = _createUserPratilipiReviewEmail(userPratilipi, authors.get(pratilipis.get(pratilipiId).getAuthorId()));
if (email != null)
totalEmailList.add(email);
}
// auditLog.getAccessType() == AccessType.USER_AUTHOR_FOLLOWING
for (String userAuthorId : userAuthorUpdateIds) {
UserAuthor userAuthor = userAuthors.get(userAuthorId);
_createUserAuthorFollowingNotifications(userAuthor, authors.get(userAuthor.getAuthorId()));
Email email = _createUserAuthorFollowingEmail(userAuthor, authors.get(userAuthor.getAuthorId()));
if (email != null)
totalEmailList.add(email);
}
// auditLog.getAccessType() == AccessType.COMMENT_ADD
for (Long commentId : commentUpdateIds) {
Comment comment = comments.get(commentId);
if (comment.getParentType() != CommentParentType.REVIEW)
continue;
UserPratilipi userPratilipi = userPratilipis.get(comment.getParentId());
Email email = _createCommentAddedReviewerEmail(userPratilipi, comment);
if (email != null)
totalEmailList.add(email);
/* // Business call - Not to send CommentAddedAuthorEmail.
Pratilipi pratilipi = pratilipis.get( userPratilipi.getPratilipiId() );
Author author = authors.get( pratilipi.getAuthorId() );
email = _createCommentAddedAuthorEmail( author, comment );
if( email != null )
totalEmailList.add( email );
*/
}
// auditLog.getAccessType() == AccessType.VOTE
for (String voteId : voteUpdateIds) {
Vote vote = votes.get(voteId);
if (vote.getParentType() == VoteParentType.REVIEW) {
UserPratilipi userPratilipi = userPratilipis.get(vote.getParentId());
// To the reviewer
Email email = _createVoteOnReviewReviewerEmail(userPratilipi, vote);
if (email != null)
totalEmailList.add(email);
} else if (vote.getParentType() == VoteParentType.COMMENT) {
// To the commentor
Email email = _createVoteOnCommentCommentorEmail(comments.get(vote.getParentIdLong()), vote);
if (email != null)
totalEmailList.add(email);
}
}
_updateEmailTable(totalEmailList);
// Updating AppProperty.
if (auditLogDataListCursorTuple.getDataList().size() > 0) {
appProperty.setValue(auditLogDataListCursorTuple.getCursor());
appProperty = dataAccessor.createOrUpdateAppProperty(appProperty);
}
return new GenericResponse();
}
use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.
the class PratilipiProcessApi method getPratilipiProcess.
@Get
public GenericResponse getPratilipiProcess(GenericRequest request) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
// START: Creating ValidateData Tasks
// Fetching AppProperty
String appPropertyId = "Api.PratilipiProcess.ValidateData";
AppProperty appProperty = dataAccessor.getAppProperty(appPropertyId);
if (appProperty == null) {
appProperty = dataAccessor.newAppProperty(appPropertyId);
appProperty.setValue(new Date(0));
}
// Fetching list of Pratilipi ids.
PratilipiFilter pratilipiFilter = new PratilipiFilter();
pratilipiFilter.setMinLastUpdate((Date) appProperty.getValue(), false);
List<Long> pratilipiIdList = dataAccessor.getPratilipiIdList(pratilipiFilter, null, 0, 10000).getDataList();
// Creating one task per Pratilipi id.
List<Task> taskList = new ArrayList<>(pratilipiIdList.size());
for (Long pratilipiId : pratilipiIdList) {
/*
Task task = TaskQueueFactory.newTask()
.setUrl( "/pratilipi/process" )
.addParam( "pratilipiId", pratilipiId.toString() )
.addParam( "validateData", "true" );
taskList.add( task );
*/
}
TaskQueueFactory.getPratilipiOfflineTaskQueue().addAll(taskList);
logger.log(Level.INFO, "Added " + taskList.size() + " ValidateData tasks.");
// Updating AppProperty.
if (pratilipiIdList.size() > 0) {
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiIdList.get(pratilipiIdList.size() - 1));
Date lastUpdatedDate = pratilipi.getLastUpdated();
logger.log(Level.INFO, "Processing all contents updated from " + (Date) appProperty.getValue() + " to " + lastUpdatedDate + ".");
logger.log(Level.INFO, "Last Updated PratilipiID : " + pratilipi.getId());
appProperty.setValue(lastUpdatedDate);
appProperty = dataAccessor.createOrUpdateAppProperty(appProperty);
}
return new GenericResponse();
}
use of com.pratilipi.data.type.Pratilipi 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);
return new GenericResponse();
}
Aggregations