use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project pratilipi by Pratilipi.
the class BatchProcessDataUtil method _execStateInit.
private static void _execStateInit(BatchProcess batchProcess) throws UnexpectedServerException {
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
BatchProcessDoc bpDoc = docAccessor.newBatchProcessDoc();
JsonObject initDoc = new Gson().fromJson(batchProcess.getInitDoc(), JsonElement.class).getAsJsonObject();
for (Entry<String, JsonElement> entry : initDoc.entrySet()) bpDoc.setData(entry.getKey(), entry.getValue());
// Saving Doc
docAccessor.save(batchProcess.getId(), bpDoc);
batchProcess.setStateCompleted(BatchProcessState.INIT);
}
use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject 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);
}
use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project pratilipi by Pratilipi.
the class CommentDataUtil method _validateCommentDataForSave.
private static void _validateCommentDataForSave(CommentData commentData) throws InvalidArgumentException {
boolean isNew = commentData.getId() == null;
JsonObject errorMessages = new JsonObject();
// New comment must have a user id.
if (isNew && (commentData.getUserId() == null || commentData.getUserId() == 0L))
errorMessages.addProperty("userId", GenericRequest.ERR_USER_ID_REQUIRED);
// New comment must have a parent type.
if (isNew && commentData.getParentType() == null)
errorMessages.addProperty("parentType", GenericRequest.ERR_COMMENT_PARENT_TYPE_REQUIRED);
// New comment must have a parent id.
if (isNew && commentData.getParentId() == null)
errorMessages.addProperty("parentId", GenericRequest.ERR_COMMENT_PARENT_ID_REQUIRED);
// New comment must have content.
if (isNew && commentData.getContent() == null)
errorMessages.addProperty("content", GenericRequest.ERR_COMMENT_CONTENT_REQUIRED);
else // Content can not be unset or set to null for old comment
if (!isNew && commentData.hasContent() && commentData.getContent() == null)
errorMessages.addProperty("content", GenericRequest.ERR_COMMENT_CONTENT_REQUIRED);
if (errorMessages.entrySet().size() > 0)
throw new InvalidArgumentException(errorMessages);
}
use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project rhino by PLOS.
the class UserIdView method serialize.
@Override
public JsonElement serialize(JsonSerializationContext context) {
JsonObject serialized = new JsonObject();
/*
* Although the userProfileId's low-level representation is a long integer, in the API we expose this essentially
* non-numeric value as a string. This serves two purposes. First, it is general future-proofing in case we ever
* want to change the ID scheme to allow arbitrary strings. Second, it helps prevent loss of precision in case the
* client deserializes it to a floating-point value or smaller integer.
*/
String userId = String.valueOf(userProfileId);
serialized.addProperty("userId", userId);
return serialized;
}
use of org.apache.beam.vendor.grpc.v1p43p2.com.google.gson.JsonObject in project rhino by PLOS.
the class CommentOutputView method serializeBase.
static JsonObject serializeBase(JsonSerializationContext context, Comment comment, CompetingInterestStatement competingInterestStatement) {
JsonObject serialized = context.serialize(comment).getAsJsonObject();
serialized.remove("userProfileID");
serialized.add("creator", context.serialize(new UserIdView(comment.getUserProfileID())));
serialized.remove("articleID");
normalizeField(serialized, "title");
normalizeField(serialized, "body");
normalizeField(serialized, "highlightedText");
serialized.remove("competingInterestBody");
serialized.add("competingInterestStatement", context.serialize(competingInterestStatement));
return serialized;
}
Aggregations