Search in sources :

Example 1 with RtdbAccessor

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

the class PratilipiDocUtil method updateFirebaseIndex.

public static void updateFirebaseIndex(Long pratilipiId, PratilipiContentDoc pcDoc) throws UnexpectedServerException {
    RtdbAccessor rtdbAccessor = DataAccessorFactory.getRtdbAccessor();
    JsonObject chapters = new JsonObject();
    for (Chapter chapter : pcDoc.getChapterList()) {
        JsonObject meta = new JsonObject();
        meta.addProperty("lastUpdated", chapter.getLastUpdated());
        meta.addProperty("lastUpdatedBy", chapter.getLastUpdatedBy().toString());
        chapters.add(chapter.getId(), meta);
    }
    JsonObject jsonObject = new JsonObject();
    jsonObject.add("index", chapters);
    rtdbAccessor.setPratilipiIndex(pratilipiId, jsonObject);
}
Also used : RtdbAccessor(com.pratilipi.data.RtdbAccessor) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) JsonObject(com.google.gson.JsonObject)

Example 2 with RtdbAccessor

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

the class EmailProcessApi method get.

@Get
public GenericResponse get(GenericRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    RtdbAccessor rtdbAccessor = DataAccessorFactory.getRtdbAccessor();
    Map<Long, UserPreferenceRtdb> userPreferences = null;
    List<Email> emailList = new ArrayList<>();
    DataListIterator<Email> it = dataAccessor.getEmailListIteratorForStatePending(null, false);
    Set<Long> userIds = new HashSet<>();
    while (it.hasNext()) {
        Email email = it.next();
        emailList.add(email);
        userIds.add(email.getUserId());
    }
    userPreferences = rtdbAccessor.getUserPreferences(userIds);
    Map<Long, User> users = dataAccessor.getUsers(userPreferences.keySet());
    // Re-scheduling for all Emails
    DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss z");
    dateFormat.setTimeZone(TimeZone.getTimeZone("IST"));
    for (Email email : emailList) {
        UserPreferenceRtdb preference = userPreferences.get(email.getUserId());
        User user = users.get(email.getUserId());
        if (email.getScheduledDate().before(new Date()))
            continue;
        if (email.getScheduledDate().equals(preference.getEmailFrequency().getNextSchedule(user.getLastEmailedDate())))
            continue;
        email.setScheduledDate(preference.getEmailFrequency().getNextSchedule(user.getLastEmailedDate()));
        email.setLastUpdated(new Date());
        if (email.getScheduledDate() == null) {
            // User changed setting to NEVER -> Scheduled Date will be null
            email.setState(EmailState.DROPPED);
        } else {
            logger.log(Level.INFO, "Rescheduling email: " + email.getId() + " from " + dateFormat.format(email.getScheduledDate()) + " to " + dateFormat.format(preference.getEmailFrequency().getNextSchedule(user.getLastEmailedDate())));
        }
    }
    // Creating Tasks
    List<Task> taskList = new ArrayList<>();
    Set<Long> userIdTaskSet = new HashSet<>();
    for (Email email : emailList) {
        if (// User changed setting to NEVER -> Email Dropped
        email.getState() != EmailState.PENDING)
            continue;
        if (email.getScheduledDate().after(new Date()))
            continue;
        UserPreferenceRtdb preference = userPreferences.get(email.getUserId());
        if (preference.getEmailFrequency() == EmailFrequency.IMMEDIATELY)
            taskList.add(TaskQueueFactory.newTask().setUrl("/email/process").addParam("emailId", email.getId().toString()));
        else
            userIdTaskSet.add(email.getUserId());
        email.setState(EmailState.IN_PROGRESS);
        email.setLastUpdated(new Date());
    }
    for (Long userId : userIdTaskSet) taskList.add(TaskQueueFactory.newTask().setUrl("/email/process").addParam("userId", userId.toString()));
    TaskQueueFactory.getEmailHpTaskQueue().addAll(taskList);
    emailList = dataAccessor.createOrUpdateEmailList(emailList);
    return new GenericResponse();
}
Also used : Task(com.pratilipi.taskqueue.Task) Email(com.pratilipi.data.type.Email) User(com.pratilipi.data.type.User) GenericResponse(com.pratilipi.api.shared.GenericResponse) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) Date(java.util.Date) RtdbAccessor(com.pratilipi.data.RtdbAccessor) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) UserPreferenceRtdb(com.pratilipi.data.type.UserPreferenceRtdb) SimpleDateFormat(java.text.SimpleDateFormat) HashSet(java.util.HashSet) Get(com.pratilipi.api.annotation.Get)

Aggregations

RtdbAccessor (com.pratilipi.data.RtdbAccessor)2 JsonObject (com.google.gson.JsonObject)1 Get (com.pratilipi.api.annotation.Get)1 GenericResponse (com.pratilipi.api.shared.GenericResponse)1 DataAccessor (com.pratilipi.data.DataAccessor)1 Email (com.pratilipi.data.type.Email)1 Chapter (com.pratilipi.data.type.PratilipiContentDoc.Chapter)1 User (com.pratilipi.data.type.User)1 UserPreferenceRtdb (com.pratilipi.data.type.UserPreferenceRtdb)1 Task (com.pratilipi.taskqueue.Task)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1