Search in sources :

Example 1 with BatchProcess

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

the class BatchProcessDataUtil method exec.

public static boolean exec(Long batchProcessId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    BatchProcess batchProcess = dataAccessor.getBatchProcess(batchProcessId);
    if (batchProcess.getStartAt().after(new Date()))
        return false;
    if (batchProcess.getStateInProgress() != null && !batchProcess.getStateInProgress().isTimedOut(batchProcess.getLastUpdated()))
        return false;
    BatchProcessState currState = batchProcess.getType().getNextState(batchProcess.getStateCompleted());
    if (currState == null)
        throw new UnexpectedServerException();
    batchProcess.setStateInProgress(currState);
    batchProcess.setLastUpdated(new Date());
    batchProcess = dataAccessor.createOrUpdateBatchProcess(batchProcess);
    System.out.println("Process Id: " + batchProcess.getId());
    System.out.println("Process Type: " + batchProcess.getType());
    System.out.println("Process State: " + batchProcess.getStateInProgress());
    if (currState == BatchProcessState.INIT)
        _execStateInit(batchProcess);
    else if (currState == BatchProcessState.GET_USER_IDS_BY_AUTHOR_FILTER)
        _execStateGetUserIdsByAuthorFilter(batchProcess);
    else if (currState == BatchProcessState.CREATE_NOTIFICATIONS_FOR_USER_IDS)
        _execStateCreateNotificationsForUserIds(batchProcess);
    else if (currState == BatchProcessState.VALIDATE_NOTIFICATION_COUNT)
        _execStateValidateNotificationCount(batchProcess);
    else if (currState == BatchProcessState.COMPLETED)
        batchProcess.setStateCompleted(currState);
    batchProcess.setStateInProgress(null);
    batchProcess.setLastUpdated(new Date());
    // Saving Entity
    batchProcess = dataAccessor.createOrUpdateBatchProcess(batchProcess);
    return true;
}
Also used : BatchProcessState(com.pratilipi.common.type.BatchProcessState) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) DataAccessor(com.pratilipi.data.DataAccessor) BatchProcess(com.pratilipi.data.type.BatchProcess) Date(java.util.Date)

Example 2 with BatchProcess

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

the class BatchProcessDataUtil method createBatchProcess.

public static void createBatchProcess(BatchProcessType type, String initDoc, String execDoc, Language language) throws InsufficientAccessException, InvalidArgumentException {
    if (!hasAccessToCreateBatchProcess(language))
        throw new InsufficientAccessException();
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    BatchProcess batchProcess = dataAccessor.newBatchProcess();
    batchProcess.setType(type);
    batchProcess.setInitDoc(initDoc);
    batchProcess.setExecDoc(execDoc);
    batchProcess.setCreationDate(new Date());
    batchProcess = dataAccessor.createOrUpdateBatchProcess(batchProcess);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) BatchProcess(com.pratilipi.data.type.BatchProcess) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Date(java.util.Date)

Example 3 with BatchProcess

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

the class BatchProcessApi method get.

@Get
public GenericResponse get(GenericRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    List<BatchProcess> batchProcessList = dataAccessor.getIncompleteBatchProcessList();
    for (BatchProcess batchProcess : batchProcessList) if (BatchProcessDataUtil.exec(batchProcess.getId()))
        // Only one execution per iteration as next BatchProcess' state might change by the time it is picked up.
        break;
    return new GenericResponse();
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) BatchProcess(com.pratilipi.data.type.BatchProcess) Get(com.pratilipi.api.annotation.Get)

Example 4 with BatchProcess

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

the class NotificationDataUtil method createNotificationDataList.

public static List<NotificationData> createNotificationDataList(List<Notification> notificationList, Language language, boolean plainText) throws UnexpectedServerException {
    // Pre-fetching required User and Pratilipi Entities
    List<Long> userIdList = new LinkedList<>();
    List<Long> pratilipiIdList = new LinkedList<>();
    for (Notification notification : notificationList) {
        userIdList.add(notification.getUserId());
        if (notification.getType() == NotificationType.PRATILIPI) {
            pratilipiIdList.add(notification.getSourceIdLong());
        } else if (notification.getType() == NotificationType.PRATILIPI_PUBLISHED_FOLLOWER) {
            pratilipiIdList.add(notification.getSourceIdLong());
        } else if (notification.getType() == NotificationType.AUTHOR_FOLLOW) {
            if (notification.getDataIds().size() <= 3)
                userIdList.addAll(notification.getDataIds());
            else
                userIdList.addAll(notification.getDataIds().subList(notification.getDataIds().size() - 3, notification.getDataIds().size()));
        }
    }
    Map<Long, UserData> users = UserDataUtil.createUserDataList(userIdList, true);
    List<PratilipiData> pratilipiDataList = PratilipiDataUtil.createPratilipiDataList(pratilipiIdList, true);
    Map<Long, PratilipiData> pratilipis = new HashMap<>(pratilipiDataList.size());
    for (PratilipiData pratilipiData : pratilipiDataList) pratilipis.put(pratilipiData.getId(), pratilipiData);
    // Creating Notification Data list
    List<NotificationData> notificationDataList = new ArrayList<>(notificationList.size());
    for (Notification notification : notificationList) {
        NotificationData notificationData = new NotificationData(notification.getId());
        notificationData.setUserId(notification.getUserId());
        Language notificationLanguage = language == null ? users.get(notification.getUserId()).getLanguage() : language;
        if (notificationLanguage == null)
            notificationLanguage = Language.ENGLISH;
        if (notification.getType() == NotificationType.PRATILIPI) {
            String createdBy = notification.getCreatedBy();
            if (createdBy.startsWith("BATCH_PROCESS::")) {
                Long batchProcessId = Long.parseLong(createdBy.substring(15));
                BatchProcess batchProcess = DataAccessorFactory.getDataAccessor().getBatchProcess(batchProcessId);
                JsonObject execDoc = new Gson().fromJson(batchProcess.getExecDoc(), JsonElement.class).getAsJsonObject();
                notificationData.setMessage(execDoc.get("message").getAsString());
            }
            PratilipiData pratilipiData = pratilipis.get(notification.getSourceIdLong());
            notificationData.setSourceUrl(pratilipiData.getPageUrl() + "?" + RequestParameter.NOTIFICATION_ID.getName() + "=" + notification.getId());
            notificationData.setSourceImageUrl(pratilipiData.getCoverImageUrl());
        } else if (notification.getType() == NotificationType.PRATILIPI_PUBLISHED_FOLLOWER) {
            PratilipiData pratilipiData = pratilipis.get(notification.getSourceIdLong());
            if (pratilipiData.getState() == PratilipiState.PUBLISHED) {
                notificationData.setMessage(createNotificationMessage(pratilipiData, notificationLanguage, plainText));
                notificationData.setSourceUrl(pratilipiData.getPageUrl() + "?" + RequestParameter.NOTIFICATION_ID.getName() + "=" + notification.getId());
                notificationData.setSourceImageUrl(pratilipiData.getCoverImageUrl());
                notificationData.setDisplayImageUrl(pratilipiData.getAuthor().getImageUrl());
            }
        } else if (notification.getType() == NotificationType.AUTHOR_FOLLOW) {
            notificationData.setMessage(createNotificationMessage(notification.getDataIds(), users, notificationLanguage, plainText));
            notificationData.setSourceUrl("/followers?" + RequestParameter.NOTIFICATION_ID.getName() + "=" + notification.getId());
            if (notification.getDataIds().size() != 0)
                notificationData.setDisplayImageUrl(users.get(notification.getDataIds().get(notification.getDataIds().size() - 1)).getProfileImageUrl());
        }
        notificationData.setSourceId(notification.getSourceId());
        notificationData.setState(notification.getState());
        notificationData.setNotificationType(notification.getType());
        notificationData.setLastUpdatedDate(notification.getLastUpdated());
        notificationDataList.add(notificationData);
    }
    return notificationDataList;
}
Also used : UserData(com.pratilipi.data.client.UserData) HashMap(java.util.HashMap) BatchProcess(com.pratilipi.data.type.BatchProcess) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) LinkedList(java.util.LinkedList) Notification(com.pratilipi.data.type.Notification) Language(com.pratilipi.common.type.Language) PratilipiData(com.pratilipi.data.client.PratilipiData) JsonElement(com.google.gson.JsonElement) NotificationData(com.pratilipi.data.client.NotificationData)

Aggregations

BatchProcess (com.pratilipi.data.type.BatchProcess)4 DataAccessor (com.pratilipi.data.DataAccessor)3 Date (java.util.Date)2 Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Get (com.pratilipi.api.annotation.Get)1 GenericResponse (com.pratilipi.api.shared.GenericResponse)1 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 BatchProcessState (com.pratilipi.common.type.BatchProcessState)1 Language (com.pratilipi.common.type.Language)1 NotificationData (com.pratilipi.data.client.NotificationData)1 PratilipiData (com.pratilipi.data.client.PratilipiData)1 UserData (com.pratilipi.data.client.UserData)1 Notification (com.pratilipi.data.type.Notification)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1