Search in sources :

Example 1 with Notification

use of com.pratilipi.data.type.Notification 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 2 with Notification

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

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

the class NotificationDataUtil method saveNotificationState.

public static void saveNotificationState(Long notificationId, NotificationState state) throws InsufficientAccessException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Notification notification = dataAccessor.getNotification(notificationId);
    if (notification.getState() == state)
        return;
    if (!hasAccessToUpdateData(notification))
        throw new InsufficientAccessException();
    notification.setState(state);
    notification = dataAccessor.createOrUpdateNotification(notification);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Notification(com.pratilipi.data.type.Notification)

Example 4 with Notification

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

the class NotificationFilter method doFilter.

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
    String notificationIdStr = ((HttpServletRequest) req).getParameter(RequestParameter.NOTIFICATION_ID.getName());
    if (notificationIdStr != null && !notificationIdStr.trim().isEmpty()) {
        DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
        Notification notification = dataAccessor.getNotification(Long.parseLong(notificationIdStr.trim()));
        if (notification != null && notification.getState() == NotificationState.UNREAD && AccessTokenFilter.getAccessToken().getUserId().equals(notification.getUserId())) {
            notification.setState(NotificationState.READ);
            notification = dataAccessor.createOrUpdateNotification(notification);
        }
    }
    chain.doFilter(req, resp);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) DataAccessor(com.pratilipi.data.DataAccessor) Notification(com.pratilipi.data.type.Notification)

Example 5 with Notification

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

the class AuditLogProcessApi method _createPratilipiPublishedNotifications.

private void _createPratilipiPublishedNotifications(Pratilipi pratilipi, Set<Long> followersSet) {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    List<Long> followers = new ArrayList<Long>(followersSet);
    List<Notification> existingNotificationList = dataAccessor.getNotificationListIterator(null, NotificationType.PRATILIPI_PUBLISHED_FOLLOWER, pratilipi.getId(), null, null).list();
    for (Notification notification : existingNotificationList) followers.remove(notification.getUserId());
    List<Notification> notificationList = new ArrayList<>(followers.size());
    for (Long followerUserId : followers) notificationList.add(dataAccessor.newNotification(followerUserId, NotificationType.PRATILIPI_PUBLISHED_FOLLOWER, pratilipi.getId()));
    notificationList = dataAccessor.createOrUpdateNotificationList(notificationList);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) Notification(com.pratilipi.data.type.Notification)

Aggregations

Notification (com.pratilipi.data.type.Notification)11 DataAccessor (com.pratilipi.data.DataAccessor)10 ArrayList (java.util.ArrayList)4 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)3 HashMap (java.util.HashMap)3 Gson (com.google.gson.Gson)2 JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 NotificationData (com.pratilipi.data.client.NotificationData)2 LinkedList (java.util.LinkedList)2 TypeToken (com.google.gson.reflect.TypeToken)1 Get (com.pratilipi.api.annotation.Get)1 GenericResponse (com.pratilipi.api.shared.GenericResponse)1 Language (com.pratilipi.common.type.Language)1 NotificationState (com.pratilipi.common.type.NotificationState)1 NotificationType (com.pratilipi.common.type.NotificationType)1 Async (com.pratilipi.common.util.Async)1 DataListCursorTuple (com.pratilipi.data.DataListCursorTuple)1 DocAccessor (com.pratilipi.data.DocAccessor)1 PratilipiData (com.pratilipi.data.client.PratilipiData)1