Search in sources :

Example 1 with DataAccessor

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

the class UriAliasUtil method generateUriAlias.

public static String generateUriAlias(String oldUriAlias, String uriPrefix, String... keywords) {
    String uriAlias = uriPrefix;
    for (String keyword : keywords) if (keyword != null)
        uriAlias += keyword.trim().toLowerCase().replaceAll("[^a-z0-9]+", "-") + "-";
    uriAlias = uriAlias.replaceAll("[-]+", "-");
    if (uriAlias.length() > uriPrefix.length() + 1) {
        if (uriAlias.charAt(uriPrefix.length()) == '-')
            uriAlias = uriPrefix + uriAlias.substring(uriPrefix.length() + 1, uriAlias.length() - 1);
        else
            uriAlias = uriAlias.substring(0, uriAlias.length() - 1);
    } else {
        return null;
    }
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    for (int i = 0; ; i++) {
        String aUriAlias = i == 0 ? uriAlias : uriAlias + "-" + i;
        if (oldUriAlias != null && oldUriAlias.equals(aUriAlias))
            return aUriAlias;
        Page page = dataAccessor.getPage(aUriAlias);
        if (page == null)
            return aUriAlias;
    }
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) Page(com.pratilipi.data.type.Page)

Example 2 with DataAccessor

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

use of com.pratilipi.data.DataAccessor 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 4 with DataAccessor

use of com.pratilipi.data.DataAccessor 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 5 with DataAccessor

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

Aggregations

DataAccessor (com.pratilipi.data.DataAccessor)162 Date (java.util.Date)53 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)37 Pratilipi (com.pratilipi.data.type.Pratilipi)34 Author (com.pratilipi.data.type.Author)33 AuditLog (com.pratilipi.data.type.AuditLog)32 UserPratilipi (com.pratilipi.data.type.UserPratilipi)32 ArrayList (java.util.ArrayList)30 Page (com.pratilipi.data.type.Page)28 HashMap (java.util.HashMap)25 GenericResponse (com.pratilipi.api.shared.GenericResponse)24 DocAccessor (com.pratilipi.data.DocAccessor)22 User (com.pratilipi.data.type.User)22 Get (com.pratilipi.api.annotation.Get)20 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)19 PratilipiData (com.pratilipi.data.client.PratilipiData)15 UserData (com.pratilipi.data.client.UserData)14 BlobAccessor (com.pratilipi.data.BlobAccessor)13 Email (com.pratilipi.data.type.Email)13 JsonObject (com.google.gson.JsonObject)12