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;
}
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());
}
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());
}
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());
}
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());
}
Aggregations