Search in sources :

Example 1 with BatchProcessDoc

use of com.pratilipi.data.type.BatchProcessDoc 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 BatchProcessDoc

use of com.pratilipi.data.type.BatchProcessDoc 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 BatchProcessDoc

use of com.pratilipi.data.type.BatchProcessDoc 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 BatchProcessDoc

use of com.pratilipi.data.type.BatchProcessDoc in project pratilipi by Pratilipi.

the class BatchProcessDataUtil method _execStateGetUserIdsByAuthorFilter.

private static void _execStateGetUserIdsByAuthorFilter(BatchProcess batchProcess) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    BatchProcessDoc processDoc = docAccessor.getBatchProcessDoc(batchProcess.getId());
    AuthorFilter authorFilter = processDoc.getData(BatchProcessState.GET_USER_IDS_BY_AUTHOR_FILTER.getInputName(), AuthorFilter.class);
    Set<Long> userIdSet = new HashSet<>();
    List<Author> tempAuthorList = null;
    String cursor = null;
    int resultCount = 200;
    do {
        DataListCursorTuple<Author> authorListCusorTuple = dataAccessor.getAuthorList(authorFilter, cursor, resultCount);
        tempAuthorList = authorListCusorTuple.getDataList();
        for (Author author : tempAuthorList) if (author.getUserId() != null)
            userIdSet.add(author.getUserId());
        cursor = authorListCusorTuple.getCursor();
    } while (tempAuthorList.size() >= resultCount);
    processDoc.setData(BatchProcessState.GET_USER_IDS_BY_AUTHOR_FILTER.getOutputName(), userIdSet);
    // Saving Doc
    docAccessor.save(batchProcess.getId(), processDoc);
    batchProcess.setStateCompleted(BatchProcessState.GET_USER_IDS_BY_AUTHOR_FILTER);
}
Also used : BatchProcessDoc(com.pratilipi.data.type.BatchProcessDoc) DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) AuthorFilter(com.pratilipi.common.util.AuthorFilter) Author(com.pratilipi.data.type.Author) HashSet(java.util.HashSet)

Aggregations

DocAccessor (com.pratilipi.data.DocAccessor)4 BatchProcessDoc (com.pratilipi.data.type.BatchProcessDoc)4 DataAccessor (com.pratilipi.data.DataAccessor)3 Gson (com.google.gson.Gson)2 JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 TypeToken (com.google.gson.reflect.TypeToken)2 NotificationType (com.pratilipi.common.type.NotificationType)1 AuthorFilter (com.pratilipi.common.util.AuthorFilter)1 Author (com.pratilipi.data.type.Author)1 Notification (com.pratilipi.data.type.Notification)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1