Search in sources :

Example 1 with DocAccessor

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

the class BatchProcessDataUtil method _execStateInit.

private static void _execStateInit(BatchProcess batchProcess) throws UnexpectedServerException {
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    BatchProcessDoc bpDoc = docAccessor.newBatchProcessDoc();
    JsonObject initDoc = new Gson().fromJson(batchProcess.getInitDoc(), JsonElement.class).getAsJsonObject();
    for (Entry<String, JsonElement> entry : initDoc.entrySet()) bpDoc.setData(entry.getKey(), entry.getValue());
    // Saving Doc
    docAccessor.save(batchProcess.getId(), bpDoc);
    batchProcess.setStateCompleted(BatchProcessState.INIT);
}
Also used : BatchProcessDoc(com.pratilipi.data.type.BatchProcessDoc) JsonElement(com.google.gson.JsonElement) DocAccessor(com.pratilipi.data.DocAccessor) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson)

Example 2 with DocAccessor

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

the class BatchProcessDataUtil method _execStateValidateNotificationCount.

private static void _execStateValidateNotificationCount(BatchProcess batchProcess) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    BatchProcessDoc processDoc = docAccessor.getBatchProcessDoc(batchProcess.getId());
    String createdBy = "BATCH_PROCESS::" + batchProcess.getId();
    Map<Long, Long> userIdNotifIdMap = processDoc.getData(BatchProcessState.VALIDATE_NOTIFICATION_COUNT.getInputName(), new TypeToken<Map<Long, Long>>() {
    }.getType());
    int notifCount = dataAccessor.getNotificationCout(createdBy);
    if (userIdNotifIdMap.size() == notifCount)
        batchProcess.setStateCompleted(BatchProcessState.VALIDATE_NOTIFICATION_COUNT);
    else
        logger.log(Level.INFO, "Validation failed !");
}
Also used : BatchProcessDoc(com.pratilipi.data.type.BatchProcessDoc) DataAccessor(com.pratilipi.data.DataAccessor) TypeToken(com.google.gson.reflect.TypeToken) DocAccessor(com.pratilipi.data.DocAccessor)

Example 3 with DocAccessor

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

the class BatchProcessDataUtil method _execStateCreateNotificationsForUserIds.

private static void _execStateCreateNotificationsForUserIds(BatchProcess batchProcess) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    BatchProcessDoc processDoc = docAccessor.getBatchProcessDoc(batchProcess.getId());
    Set<Long> userIdSet = processDoc.getData(BatchProcessState.CREATE_NOTIFICATIONS_FOR_USER_IDS.getInputName(), new TypeToken<Set<Long>>() {
    }.getType());
    Map<Long, Long> userIdNotifIdMap = processDoc.getData(BatchProcessState.CREATE_NOTIFICATIONS_FOR_USER_IDS.getOutputName(), new TypeToken<Map<Long, Long>>() {
    }.getType());
    JsonObject execDoc = new Gson().fromJson(batchProcess.getExecDoc(), JsonElement.class).getAsJsonObject();
    NotificationType type = NotificationType.valueOf(execDoc.get("type").getAsString());
    String sourceId = execDoc.get("sourceId").getAsString();
    String createdBy = "BATCH_PROCESS::" + batchProcess.getId();
    if (userIdNotifIdMap == null) {
        // First attempt on this state
        userIdNotifIdMap = new HashMap<>();
    } else {
        for (Entry<Long, Long> entry : userIdNotifIdMap.entrySet()) {
            if (entry.getValue() != 0L) {
                userIdSet.remove(entry.getKey());
                continue;
            }
            Notification notification = dataAccessor.getNotification(entry.getKey(), type, sourceId, createdBy);
            if (notification != null) {
                entry.setValue(notification.getId());
                userIdSet.remove(entry.getKey());
            }
        }
    }
    while (!userIdSet.isEmpty()) {
        List<Long> userIdList = new ArrayList<>(100);
        for (Long userId : userIdSet) {
            // Can't put null (instead of 0) here because Gson ignores keys with null values
            userIdNotifIdMap.put(userId, 0L);
            userIdList.add(userId);
            if (// Limiting to 100 users per run
            userIdList.size() == 100)
                break;
        }
        userIdSet.removeAll(userIdList);
        processDoc.setData(BatchProcessState.CREATE_NOTIFICATIONS_FOR_USER_IDS.getOutputName(), userIdNotifIdMap);
        // Saving Doc
        docAccessor.save(batchProcess.getId(), processDoc);
        List<Notification> notifList = new ArrayList<>(userIdList.size());
        for (Long userId : userIdList) notifList.add(dataAccessor.newNotification(userId, type, sourceId, createdBy));
        notifList = dataAccessor.createOrUpdateNotificationList(notifList);
        for (Notification notif : notifList) userIdNotifIdMap.put(notif.getUserId(), notif.getId());
    }
    processDoc.setData(BatchProcessState.CREATE_NOTIFICATIONS_FOR_USER_IDS.getOutputName(), userIdNotifIdMap);
    // Saving Doc
    docAccessor.save(batchProcess.getId(), processDoc);
    batchProcess.setStateCompleted(BatchProcessState.CREATE_NOTIFICATIONS_FOR_USER_IDS);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) Notification(com.pratilipi.data.type.Notification) BatchProcessDoc(com.pratilipi.data.type.BatchProcessDoc) TypeToken(com.google.gson.reflect.TypeToken) JsonElement(com.google.gson.JsonElement) NotificationType(com.pratilipi.common.type.NotificationType)

Example 4 with DocAccessor

use of com.pratilipi.data.DocAccessor 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 5 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)

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