Search in sources :

Example 1 with DataListCursorTuple

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

the class CommentDataUtil method getCommentList.

public static DataListCursorTuple<CommentData> getCommentList(CommentParentType parentType, String parentId, String cursor, Integer offset, Integer resultCount) throws UnexpectedServerException {
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    if (parentType == CommentParentType.REVIEW) {
        Long userId = Long.parseLong(parentId.substring(0, parentId.indexOf('-')));
        Long pratilipiId = Long.parseLong(parentId.substring(parentId.indexOf('-') + 1));
        PratilipiReviewsDoc reviewsDoc = docAccessor.getPratilipiReviewsDoc(pratilipiId);
        List<CommentDoc> commentDocList = null;
        for (UserPratilipiDoc reviewDoc : reviewsDoc.getReviews()) {
            if (reviewDoc.getUserId().equals(userId)) {
                commentDocList = reviewDoc.getComments();
                break;
            }
        }
        int fromIndex = (cursor == null ? 0 : Integer.parseInt(cursor)) + (offset == null ? 0 : offset);
        int toIndex = resultCount == null ? commentDocList.size() : fromIndex + resultCount;
        commentDocList = commentDocList.subList(Math.min(fromIndex, commentDocList.size()), Math.min(toIndex, commentDocList.size()));
        List<CommentData> commentDataList = createCommentDataList(commentDocList);
        return new DataListCursorTuple<CommentData>(commentDataList, toIndex + "", (long) (int) reviewsDoc.getReviews().size());
    }
    return null;
}
Also used : CommentDoc(com.pratilipi.data.type.CommentDoc) DataListCursorTuple(com.pratilipi.data.DataListCursorTuple) DocAccessor(com.pratilipi.data.DocAccessor) UserPratilipiDoc(com.pratilipi.data.type.UserPratilipiDoc) CommentData(com.pratilipi.data.client.CommentData) PratilipiReviewsDoc(com.pratilipi.data.type.PratilipiReviewsDoc)

Example 2 with DataListCursorTuple

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

the class NotificationDataUtil method getNotificationList.

public static DataListCursorTuple<NotificationData> getNotificationList(Long userId, Language language, String cursor, Integer resultCount) throws InsufficientAccessException, UnexpectedServerException {
    if (!hasAccessToListData(userId))
        throw new InsufficientAccessException();
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    // Fetching Notification Entities
    DataListIterator<Notification> notifListIterator = dataAccessor.getNotificationListIterator(userId, null, (String) null, cursor, null);
    List<Notification> notifList = resultCount == null ? new ArrayList<Notification>() : new ArrayList<Notification>(resultCount);
    while (notifListIterator.hasNext()) {
        Notification notif = notifListIterator.next();
        if (notif.getType().isValid(notif))
            notifList.add(notif);
        if (resultCount != null && notifList.size() == resultCount)
            break;
    }
    // Return
    return new DataListCursorTuple<>(createNotificationDataList(notifList, language, false), notifListIterator.getCursor());
}
Also used : DataListCursorTuple(com.pratilipi.data.DataListCursorTuple) DataAccessor(com.pratilipi.data.DataAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Notification(com.pratilipi.data.type.Notification)

Example 3 with DataListCursorTuple

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

the class UserPratilipiDataUtil method getPratilipiReviewList.

public static DataListCursorTuple<UserPratilipiData> getPratilipiReviewList(Long pratilipiId, String cursor, Integer offset, Integer resultCount) throws UnexpectedServerException {
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    PratilipiReviewsDoc reviewsDoc = docAccessor.getPratilipiReviewsDoc(pratilipiId);
    List<UserPratilipiDoc> reviewDocList = reviewsDoc.getReviews();
    // Reverse Sort
    Collections.reverse(reviewDocList);
    // Using next UserPratilipiId as the cursor
    int fromIndex = 0;
    if (cursor != null && !cursor.equals("-1")) {
        for (int i = 0; i < reviewDocList.size(); i++) {
            if (reviewDocList.get(i).getId().equals(cursor)) {
                fromIndex = i;
                break;
            }
        }
        if (offset != null)
            fromIndex = fromIndex + offset;
    }
    if (fromIndex >= reviewDocList.size() || (cursor != null && cursor.equals("-1"))) {
        return new DataListCursorTuple<UserPratilipiData>(new ArrayList<UserPratilipiData>(), "-1", (long) (int) reviewsDoc.getReviews().size());
    }
    int toIndex = resultCount == null ? reviewDocList.size() : Math.min(fromIndex + resultCount, reviewDocList.size());
    // Next cursor
    cursor = toIndex == reviewDocList.size() ? "-1" : reviewDocList.get(toIndex).getId();
    reviewDocList = reviewDocList.subList(fromIndex, toIndex);
    List<UserPratilipiData> userPratilipiDataList = new ArrayList<>(reviewDocList.size());
    for (UserPratilipiDoc review : reviewDocList) userPratilipiDataList.add(createUserPratilipiData(review));
    return new DataListCursorTuple<UserPratilipiData>(userPratilipiDataList, cursor, (long) (int) reviewsDoc.getReviews().size());
}
Also used : DataListCursorTuple(com.pratilipi.data.DataListCursorTuple) DocAccessor(com.pratilipi.data.DocAccessor) UserPratilipiDoc(com.pratilipi.data.type.UserPratilipiDoc) ArrayList(java.util.ArrayList) UserPratilipiData(com.pratilipi.data.client.UserPratilipiData) PratilipiReviewsDoc(com.pratilipi.data.type.PratilipiReviewsDoc)

Example 4 with DataListCursorTuple

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

the class UserAuthorDataUtil method getUserFollowList.

public static DataListCursorTuple<AuthorData> getUserFollowList(Long userId, String cursor, Integer offset, Integer resultCount) {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUser(userId);
    if (user.getFollowCount() == 0L)
        return new DataListCursorTuple<>(new ArrayList<AuthorData>(0), null, 0L);
    DataListCursorTuple<Long> authorIdListCursorTuple = dataAccessor.getUserAuthorFollowList(userId, null, cursor, offset, resultCount);
    List<Long> authorIdList = authorIdListCursorTuple.getDataList();
    List<AuthorData> authorDataList = AuthorDataUtil.createAuthorDataList(authorIdList, true);
    // Setting AuthorData.isFollowing flag
    if (userId.equals(AccessTokenFilter.getAccessToken().getUserId())) {
        for (AuthorData authorData : authorDataList) authorData.setFollowing(true);
    } else {
        List<UserAuthor> userAuthorList = dataAccessor.getUserAuthorList(AccessTokenFilter.getAccessToken().getUserId(), authorIdList);
        if (userAuthorList.size() != 0)
            for (int i = 0; i < authorIdList.size(); i++) if (userAuthorList.get(i) != null && userAuthorList.get(i).getFollowState() == UserFollowState.FOLLOWING)
                authorDataList.get(i).setFollowing(true);
    }
    // Setting UserData.isFollowing flag
    Author author = dataAccessor.getAuthorByUserId(AccessTokenFilter.getAccessToken().getUserId());
    if (author != null) {
        List<Long> userIdList = new ArrayList<>(authorDataList.size());
        List<UserData> userDataList = new ArrayList<>(authorDataList.size());
        for (AuthorData authorData : authorDataList) {
            if (authorData.getUser() != null && authorData.getUser().getId() != null) {
                userIdList.add(authorData.getUser().getId());
                userDataList.add(authorData.getUser());
            }
        }
        List<UserAuthor> userAuthorList = dataAccessor.getUserAuthorList(userIdList, author.getId());
        if (userAuthorList != null) {
            for (UserAuthor userAuthor : userAuthorList) if (userAuthor != null && userAuthor.getFollowState() == UserFollowState.FOLLOWING)
                userDataList.get(userIdList.indexOf(userAuthor.getUserId())).setFollowing(true);
        }
    }
    return new DataListCursorTuple<>(authorDataList, authorIdListCursorTuple.getCursor(), user.getFollowCount());
}
Also used : User(com.pratilipi.data.type.User) UserData(com.pratilipi.data.client.UserData) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) DataListCursorTuple(com.pratilipi.data.DataListCursorTuple) UserAuthorData(com.pratilipi.data.client.UserAuthorData) AuthorData(com.pratilipi.data.client.AuthorData) UserAuthor(com.pratilipi.data.type.UserAuthor) UserAuthor(com.pratilipi.data.type.UserAuthor) Author(com.pratilipi.data.type.Author)

Example 5 with DataListCursorTuple

use of com.pratilipi.data.DataListCursorTuple 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 : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) DataListCursorTuple(com.pratilipi.data.DataListCursorTuple) AuthorData(com.pratilipi.data.client.AuthorData)

Aggregations

DataListCursorTuple (com.pratilipi.data.DataListCursorTuple)11 DataAccessor (com.pratilipi.data.DataAccessor)7 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)3 DocAccessor (com.pratilipi.data.DocAccessor)3 AuthorData (com.pratilipi.data.client.AuthorData)3 Author (com.pratilipi.data.type.Author)3 ArrayList (java.util.ArrayList)3 UserData (com.pratilipi.data.client.UserData)2 PratilipiReviewsDoc (com.pratilipi.data.type.PratilipiReviewsDoc)2 UserAuthor (com.pratilipi.data.type.UserAuthor)2 UserPratilipiDoc (com.pratilipi.data.type.UserPratilipiDoc)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 AuthorByReadCountData (com.pratilipi.data.client.AuthorByReadCountData)1 CommentData (com.pratilipi.data.client.CommentData)1 PratilipiData (com.pratilipi.data.client.PratilipiData)1 UserAuthorData (com.pratilipi.data.client.UserAuthorData)1 UserPratilipiData (com.pratilipi.data.client.UserPratilipiData)1 CommentDoc (com.pratilipi.data.type.CommentDoc)1 Notification (com.pratilipi.data.type.Notification)1