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);
}
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();
}
Aggregations