Search in sources :

Example 76 with JsonObject

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);
}
Also used : BatchProcessDoc(com.pratilipi.data.type.BatchProcessDoc) JsonElement(com.google.gson.JsonElement) DocAccessor(com.pratilipi.data.DocAccessor) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson)

Example 77 with JsonObject

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);
}
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 78 with JsonObject

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);
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonObject(com.google.gson.JsonObject)

Example 79 with JsonObject

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;
}
Also used : JsonObject(com.google.gson.JsonObject)

Example 80 with JsonObject

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;
}
Also used : UserIdView(org.ambraproject.rhino.view.user.UserIdView) JsonObject(com.google.gson.JsonObject)

Aggregations

JsonObject (com.google.gson.JsonObject)5111 JsonArray (com.google.gson.JsonArray)1162 JsonElement (com.google.gson.JsonElement)1084 JsonParser (com.google.gson.JsonParser)710 Test (org.junit.Test)491 IOException (java.io.IOException)471 JsonPrimitive (com.google.gson.JsonPrimitive)387 ArrayList (java.util.ArrayList)376 Gson (com.google.gson.Gson)369 HashMap (java.util.HashMap)324 Map (java.util.Map)269 Test (org.testng.annotations.Test)208 Test (org.junit.jupiter.api.Test)201 File (java.io.File)146 List (java.util.List)142 InputStreamReader (java.io.InputStreamReader)135 JsonParseException (com.google.gson.JsonParseException)121 GsonBuilder (com.google.gson.GsonBuilder)93 JsonSyntaxException (com.google.gson.JsonSyntaxException)88 Nonnull (javax.annotation.Nonnull)87