Search in sources :

Example 1 with ActionEventRequestBuilder

use of org.motechproject.tasks.contract.builder.ActionEventRequestBuilder in project motech by motech.

the class ActionEventRequestDeserializer method deserialize.

@Override
public ActionEventRequest deserialize(JsonElement element, Type type, JsonDeserializationContext context) {
    ActionEventRequest actionEvent = null;
    if (element.isJsonObject()) {
        JsonObject jsonObject = element.getAsJsonObject();
        actionEvent = new ActionEventRequestBuilder().setDisplayName(getValue(jsonObject, DISPLAY_NAME_FIELD)).setSubject(getValue(jsonObject, SUBJECT_FIELD)).setDescription(getValue(jsonObject, DESCRIPTION_FIELD)).setServiceInterface(getValue(jsonObject, SERVICE_INTERFACE_FIELD)).setServiceMethod(getValue(jsonObject, SERVICE_METHOD_FIELD)).setServiceMethodCallManner(getValue(jsonObject, SERVICE_METHOD_CALL_MANNER_FIELD)).setName(getValue(jsonObject, NAME_FIELD)).createActionEventRequest();
        if (jsonObject.has(ACTION_PARAMETERS_FIELD)) {
            JsonArray jsonArray = jsonObject.getAsJsonArray(ACTION_PARAMETERS_FIELD);
            for (int i = 0; i < jsonArray.size(); ++i) {
                ActionParameterRequest parameter = context.deserialize(jsonArray.get(i), ActionParameterRequest.class);
                boolean changeOrder = parameter.getOrder() == null;
                actionEvent.addParameter(parameter, changeOrder);
            }
        }
        if (jsonObject.has(POST_ACTION_PARAMETERS_FIELD)) {
            JsonArray jsonArray = jsonObject.getAsJsonArray(POST_ACTION_PARAMETERS_FIELD);
            for (int i = 0; i < jsonArray.size(); ++i) {
                ActionParameterRequest parameter = context.deserialize(jsonArray.get(i), ActionParameterRequest.class);
                boolean changeOrder = parameter.getOrder() == null;
                actionEvent.addPostActionParameter(parameter, changeOrder);
            }
        }
        if (!actionEvent.isValid()) {
            throw new JsonParseException("Channel action must contain subject and/or serviceInterface and serviceMethod");
        }
    }
    return actionEvent;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) ActionParameterRequest(org.motechproject.tasks.contract.ActionParameterRequest) JsonParseException(com.google.gson.JsonParseException) ActionEventRequest(org.motechproject.tasks.contract.ActionEventRequest) ActionEventRequestBuilder(org.motechproject.tasks.contract.builder.ActionEventRequestBuilder)

Aggregations

JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 ActionEventRequest (org.motechproject.tasks.contract.ActionEventRequest)1 ActionParameterRequest (org.motechproject.tasks.contract.ActionParameterRequest)1 ActionEventRequestBuilder (org.motechproject.tasks.contract.builder.ActionEventRequestBuilder)1