Search in sources :

Example 11 with DocAccessor

use of com.pratilipi.data.DocAccessor in project pratilipi by Pratilipi.

the class AuthorDataUtil method getRecommendedAuthorList.

public static DataListCursorTuple<AuthorData> getRecommendedAuthorList(Long userId, Language language, String cursorStr, Integer resultCount) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    UserFollowsDoc followsDoc = docAccessor.getUserFollowsDoc(userId);
    // Authors to ignore = Authors Following + Authors Ignored
    List<Long> authorIdsToIgnore = new ArrayList<Long>();
    if (followsDoc != null) {
        for (UserAuthorDoc userAuthorDoc : followsDoc.getFollows(UserFollowState.FOLLOWING)) authorIdsToIgnore.add(userAuthorDoc.getAuthorId());
        for (UserAuthorDoc userAuthorDoc : followsDoc.getFollows(UserFollowState.IGNORED)) if (new Date().getTime() - userAuthorDoc.getFollowDate().getTime() >= TimeUnit.DAYS.toMillis(30))
            authorIdsToIgnore.add(userAuthorDoc.getAuthorId());
    }
    // Get global list of recommended authors
    List<Long> recommendedList = _getRecommendAuthorGlobalList(language);
    // If cursor is passed, drop all items up till cursor
    Long cursor = cursorStr == null ? null : Long.parseLong(cursorStr);
    if (cursor != null && recommendedList.contains(cursor))
        while (!recommendedList.remove(0).equals(cursor)) continue;
    // Remove Author ids to be ignored
    recommendedList.removeAll(authorIdsToIgnore);
    // Drop items if recommendedList size is requested count
    if (resultCount != null)
        recommendedList = recommendedList.subList(0, Math.min(resultCount, recommendedList.size()));
    Map<Long, Author> authors = dataAccessor.getAuthors(recommendedList);
    Map<Long, Page> authorPages = dataAccessor.getPages(PageType.AUTHOR, recommendedList);
    List<AuthorData> recommendAuthorData = new ArrayList<>(recommendedList.size());
    for (Long authorId : recommendedList) recommendAuthorData.add(createAuthorData(authors.get(authorId), authorPages.get(authorId)));
    Collections.shuffle(recommendAuthorData);
    return new DataListCursorTuple<AuthorData>(recommendAuthorData, recommendedList.isEmpty() ? null : recommendedList.get(recommendedList.size() - 1).toString());
}
Also used : UserAuthorDoc(com.pratilipi.data.type.UserAuthorDoc) DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) ArrayList(java.util.ArrayList) Page(com.pratilipi.data.type.Page) Date(java.util.Date) DataListCursorTuple(com.pratilipi.data.DataListCursorTuple) AuthorData(com.pratilipi.data.client.AuthorData) Author(com.pratilipi.data.type.Author) UserFollowsDoc(com.pratilipi.data.type.UserFollowsDoc)

Example 12 with DocAccessor

use of com.pratilipi.data.DocAccessor in project pratilipi by Pratilipi.

the class PratilipiDataUtil method getPratilipiKeywords.

public static String getPratilipiKeywords(Long pratilipiId) throws UnexpectedServerException {
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    PratilipiMetaDoc pmDoc = docAccessor.getPratilipiMetaDoc(pratilipiId);
    if (pmDoc == null)
        return null;
    String keywords = "";
    for (String str : pmDoc.getWordCounts().keySet()) keywords += str + " ";
    keywords = keywords.trim();
    return keywords.isEmpty() ? null : keywords;
}
Also used : PratilipiMetaDoc(com.pratilipi.data.type.PratilipiMetaDoc) DocAccessor(com.pratilipi.data.DocAccessor)

Example 13 with DocAccessor

use of com.pratilipi.data.DocAccessor 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));
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc)

Example 14 with DocAccessor

use of com.pratilipi.data.DocAccessor 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);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) JsonObject(com.google.gson.JsonObject) Page(com.pratilipi.data.type.Page) AlignmentType(com.pratilipi.data.type.PratilipiContentDoc.AlignmentType) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) PageletType(com.pratilipi.data.type.PratilipiContentDoc.PageletType) BlobAccessor(com.pratilipi.data.BlobAccessor) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) JsonObject(com.google.gson.JsonObject) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc)

Example 15 with DocAccessor

use of com.pratilipi.data.DocAccessor in project pratilipi by Pratilipi.

the class PratilipiDocUtil method updatePratilipiReviews.

public static void updatePratilipiReviews(Long pratilipiId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    List<Comment> commentList = dataAccessor.getCommentListByReference(ReferenceType.PRATILIPI, pratilipiId);
    Map<String, List<Comment>> reviewIdCommentListMap = new HashMap<>();
    for (Comment comment : commentList) {
        if (comment.getParentType() != CommentParentType.REVIEW)
            continue;
        List<Comment> reviewCommentList = reviewIdCommentListMap.get(comment.getParentId());
        if (reviewCommentList == null) {
            reviewCommentList = new LinkedList<>();
            reviewIdCommentListMap.put(comment.getParentId(), reviewCommentList);
        }
        reviewCommentList.add(comment);
    }
    List<Vote> voteList = dataAccessor.getVoteListByReference(ReferenceType.PRATILIPI, pratilipiId);
    Map<String, List<Vote>> reviewIdLikeVotesMap = new HashMap<>();
    Map<String, List<Long>> commentIdLikedByUserIdsMap = new HashMap<>();
    for (Vote vote : voteList) {
        if (vote.getType() == VoteType.NONE) {
            continue;
        } else if (vote.getParentType() == VoteParentType.REVIEW) {
            List<Vote> reviewLikeVoteList = reviewIdLikeVotesMap.get(vote.getParentId());
            if (reviewLikeVoteList == null) {
                reviewLikeVoteList = new LinkedList<>();
                reviewIdLikeVotesMap.put(vote.getParentId(), reviewLikeVoteList);
            }
            reviewLikeVoteList.add(vote);
        } else if (vote.getParentType() == VoteParentType.COMMENT) {
            List<Long> userIdList = commentIdLikedByUserIdsMap.get(vote.getParentId());
            if (userIdList == null) {
                userIdList = new LinkedList<>();
                commentIdLikedByUserIdsMap.put(vote.getParentId(), userIdList);
            }
            userIdList.add(vote.getUserId());
        }
    }
    List<UserPratilipi> userPratilipiList = dataAccessor.getUserPratilipiList(null, pratilipiId, null, null, true).getDataList();
    long ratingCount = 0;
    long totalRating = 0;
    List<UserPratilipiDoc> reviewDocList = new ArrayList<>();
    for (UserPratilipi userPratilipi : userPratilipiList) {
        if (userPratilipi.getRating() != null && userPratilipi.getRating() > 0) {
            ratingCount++;
            totalRating += userPratilipi.getRating();
        }
        if (userPratilipi.getReviewState() != UserReviewState.PUBLISHED)
            continue;
        if ((userPratilipi.getReviewTitle() == null || userPratilipi.getReviewTitle().trim().isEmpty()) && (userPratilipi.getReview() == null || userPratilipi.getReview().trim().isEmpty()))
            continue;
        UserPratilipiDoc reviewDoc = docAccessor.newUserPratilipiDoc();
        reviewDoc.setId(userPratilipi.getId());
        reviewDoc.setUserId(userPratilipi.getUserId());
        reviewDoc.setRating(userPratilipi.getRating());
        reviewDoc.setReviewTitle(userPratilipi.getReviewTitle() == null || userPratilipi.getReviewTitle().trim().isEmpty() ? null : userPratilipi.getReviewTitle().trim());
        reviewDoc.setReview(userPratilipi.getReview() == null || userPratilipi.getReview().trim().isEmpty() ? null : userPratilipi.getReview().trim());
        reviewDoc.setReviewDate(userPratilipi.getReviewDate());
        reviewDocList.add(reviewDoc);
        List<Vote> reviewLikeVoteList = reviewIdLikeVotesMap.get(userPratilipi.getId());
        if (reviewLikeVoteList != null) {
            List<Long> userIdList = new ArrayList<>(reviewLikeVoteList.size());
            for (Vote vote : reviewLikeVoteList) {
                if (vote.getLastUpdated().before(userPratilipi.getReviewDate()))
                    continue;
                userIdList.add(vote.getUserId());
            }
            reviewDoc.setLikedByUserIds(userIdList);
        }
        List<Comment> reviewCommentList = reviewIdCommentListMap.get(userPratilipi.getId());
        if (reviewCommentList != null) {
            List<CommentDoc> commentDocList = new ArrayList<>(reviewCommentList.size());
            for (Comment comment : reviewCommentList) {
                if (comment.getState() == CommentState.DELETED)
                    continue;
                if (comment.getCreationDate().before(userPratilipi.getReviewDate()))
                    continue;
                CommentDoc commentDoc = docAccessor.newCommentDoc();
                commentDoc.setId(comment.getId());
                commentDoc.setUserId(comment.getUserId());
                commentDoc.setContent(comment.getContent());
                commentDoc.setCreationDate(comment.getCreationDate());
                commentDoc.setLastUpdated(comment.getLastUpdated());
                commentDoc.setLikedByUserIds(commentIdLikedByUserIdsMap.get(comment.getId().toString()));
                commentDocList.add(commentDoc);
            }
            reviewDoc.setComments(commentDocList);
        }
    }
    PratilipiReviewsDoc reviewsDoc = docAccessor.newPratilipiReviewsDoc();
    reviewsDoc.setRatingCount(ratingCount);
    reviewsDoc.setTotalRating(totalRating);
    reviewsDoc.setReviews(reviewDocList);
    docAccessor.save(pratilipiId, reviewsDoc);
}
Also used : HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) ArrayList(java.util.ArrayList) CommentDoc(com.pratilipi.data.type.CommentDoc) UserPratilipi(com.pratilipi.data.type.UserPratilipi) UserPratilipiDoc(com.pratilipi.data.type.UserPratilipiDoc) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Comment(com.pratilipi.data.type.Comment) Vote(com.pratilipi.data.type.Vote) LinkedList(java.util.LinkedList) PratilipiReviewsDoc(com.pratilipi.data.type.PratilipiReviewsDoc)

Aggregations

DocAccessor (com.pratilipi.data.DocAccessor)28 DataAccessor (com.pratilipi.data.DataAccessor)22 Pratilipi (com.pratilipi.data.type.Pratilipi)12 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)12 UserPratilipi (com.pratilipi.data.type.UserPratilipi)12 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)7 Chapter (com.pratilipi.data.type.PratilipiContentDoc.Chapter)6 ArrayList (java.util.ArrayList)6 PratilipiReviewsDoc (com.pratilipi.data.type.PratilipiReviewsDoc)5 BatchProcessDoc (com.pratilipi.data.type.BatchProcessDoc)4 UserPratilipiDoc (com.pratilipi.data.type.UserPratilipiDoc)4 JsonElement (com.google.gson.JsonElement)3 JsonObject (com.google.gson.JsonObject)3 Get (com.pratilipi.api.annotation.Get)3 GenericResponse (com.pratilipi.api.shared.GenericResponse)3 DataListCursorTuple (com.pratilipi.data.DataListCursorTuple)3 PratilipiData (com.pratilipi.data.client.PratilipiData)3 AuditLog (com.pratilipi.data.type.AuditLog)3 Author (com.pratilipi.data.type.Author)3 Page (com.pratilipi.data.type.Page)3