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