All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.weaverplatform.protocol.WriteOperationParser Maven / Gradle / Ivy

There is a newer version: 3.27.0
Show newest version
package com.weaverplatform.protocol;

import com.google.gson.*;
import com.google.gson.internal.bind.TreeTypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.weaverplatform.protocol.model.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weaverplatform.protocol.model.WriteOperation.WriteOperationAction.*;

/**
 * @author Mohamad Alamili
 */
public class WriteOperationParser {

  protected static final Gson gson;

  static {
    WriteOperationDeserializer deserializer = new WriteOperationDeserializer();

    deserializer.registerAction(CREATE_NODE,                CreateNodeOperation.class);
    deserializer.registerAction(CREATE_ATTRIBUTE,           CreateAttributeOperation.class);
    deserializer.registerAction(CREATE_RELATION,            CreateRelationOperation.class);
    deserializer.registerAction(REMOVE_NODE,                RemoveNodeOperation.class);
    deserializer.registerAction(REMOVE_NODE_UNRECOVERABLE,  RemoveNodeUnrecoverableOperation.class);
    deserializer.registerAction(REMOVE_ATTRIBUTE,           RemoveAttributeOperation.class);
    deserializer.registerAction(REMOVE_RELATION,            RemoveRelationOperation.class);
    deserializer.registerAction(TRUNCATE_GRAPH,             TruncateGraphOperation.class);

    gson = new GsonBuilder()
      .registerTypeAdapter(WriteOperation.class, deserializer)
      .registerTypeAdapter(AttributeValue.class, new AttributeValue.AttributeValueDeserializer())
      .create();
  }

  protected JsonReader reader = null;

  public static List parse(String writeOperationsJson) {
    return gson.fromJson(writeOperationsJson, new TypeToken>(){}.getType());
  }

  public List parseNext(InputStream stream, int chunkSize) throws IOException {
    List list = new ArrayList<>();

    if(reader == null) {
      reader = new JsonReader(new InputStreamReader(stream, "UTF-8"));
      reader.beginArray();
    }

    int count = 0;
    while(reader.hasNext() && count++ < chunkSize) {
      list.add(gson.fromJson(reader, new TypeToken(){}.getType()));
    }
    return list;
  }

  public static List parse(String writeOperationsJson, String creator) {
    List operations = parse(writeOperationsJson);

    // Set creator in bulk
    if (creator != null){
      operations.forEach(operation -> operation.setUser(creator));
    }

    return operations;
  }



  static class WriteOperationDeserializer implements JsonDeserializer {
    Map> actionRegistry = new HashMap<>();

    void registerAction(WriteOperation.WriteOperationAction action, Class javaType) {
      actionRegistry.put(action.getValue(), javaType);
    }

    @Override
    public WriteOperation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
      JsonObject jsonObject = json.getAsJsonObject();
      String action = jsonObject.get("action").getAsString();

      if(!actionRegistry.containsKey(action)) {
        throw new WeaverError(WeaverError.WRITE_OPERATION_NOT_EXISTS, "This action was not found: " + action);
      }

      Class operationType = actionRegistry.get(action);

      return context.deserialize(jsonObject, operationType);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy