Search in sources :

Example 36 with JsonStructure

use of javax.json.JsonStructure in project iaf by ibissource.

the class SlotIdRecord method getResult.

@Override
protected PipeRunResult getResult(ResultSet resultset, Object blobSessionVar, Object clobSessionVar, HttpServletResponse response, String contentType, String contentDisposition, PipeLineSession session, IForwardTarget next) throws JdbcException, SQLException, IOException {
    JsonArrayBuilder types = Json.createArrayBuilder();
    String previousType = null;
    JsonObjectBuilder typeBuilder = null;
    JsonArrayBuilder slotsBuilder = null;
    String previousSlot = null;
    JsonObjectBuilder slotBuilder = null;
    JsonArrayBuilder datesBuilder = null;
    int typeslotcount = 0;
    int typedatecount = 0;
    int typemsgcount = 0;
    int slotdatecount = 0;
    int slotmsgcount = 0;
    while (resultset.next()) {
        String type = resultset.getString("type");
        String slotid = resultset.getString("slotid");
        String date = resultset.getString("msgdate");
        int count = resultset.getInt("msgcount");
        if (type == null) {
            type = "";
        }
        if (slotid == null) {
            slotid = "";
        }
        if (!type.equals(previousType)) {
            if (typeBuilder != null) {
                slotBuilder.add("datecount", slotdatecount);
                slotBuilder.add("msgcount", slotmsgcount);
                slotBuilder.add("dates", datesBuilder.build());
                slotsBuilder.add(slotBuilder.build());
                slotdatecount = 0;
                slotmsgcount = 0;
                typeBuilder.add("slotcount", typeslotcount);
                typeBuilder.add("datecount", typedatecount);
                typeBuilder.add("msgcount", typemsgcount);
                typeBuilder.add("slots", slotsBuilder.build());
                types.add(typeBuilder.build());
                typeslotcount = 0;
                typedatecount = 0;
                typemsgcount = 0;
                previousSlot = null;
                slotBuilder = null;
            }
            typeBuilder = Json.createObjectBuilder();
            typeBuilder.add("type", type);
            if (type.equalsIgnoreCase("E")) {
                typeBuilder.add("name", "errorlog");
            } else {
                typeBuilder.add("name", "messagelog");
            }
            slotsBuilder = Json.createArrayBuilder();
            previousType = type;
        }
        if (!slotid.equals(previousSlot)) {
            if (slotBuilder != null) {
                slotBuilder.add("datecount", slotdatecount);
                slotBuilder.add("msgcount", slotmsgcount);
                slotBuilder.add("dates", datesBuilder.build());
                slotsBuilder.add(slotBuilder.build());
                slotdatecount = 0;
                slotmsgcount = 0;
            }
            slotBuilder = Json.createObjectBuilder();
            datesBuilder = Json.createArrayBuilder();
            slotBuilder.add("id", slotid);
            if (StringUtils.isNotEmpty(slotid)) {
                SlotIdRecord sir = (SlotIdRecord) slotmap.get(type + "/" + slotid);
                if (sir != null) {
                    slotBuilder.add("adapter", sir.adapterName);
                    if (StringUtils.isNotEmpty(sir.receiverName)) {
                        slotBuilder.add("receiver", sir.receiverName);
                    }
                    if (StringUtils.isNotEmpty(sir.pipeName)) {
                        slotBuilder.add("pipe", sir.pipeName);
                    }
                }
            }
            previousSlot = slotid;
            typeslotcount++;
        }
        typemsgcount += count;
        typedatecount++;
        slotmsgcount += count;
        slotdatecount++;
        datesBuilder.add(Json.createObjectBuilder().add("id", date).add("count", count).build());
    }
    if (slotBuilder != null) {
        slotBuilder.add("datecount", slotdatecount);
        slotBuilder.add("msgcount", slotmsgcount);
        slotBuilder.add("dates", datesBuilder.build());
        slotsBuilder.add(slotBuilder.build());
    }
    if (typeBuilder != null) {
        typeBuilder.add("slotcount", typeslotcount);
        typeBuilder.add("datecount", typedatecount);
        typeBuilder.add("msgcount", typemsgcount);
        typeBuilder.add("slots", slotsBuilder.build());
        types.add(typeBuilder.build());
    }
    JsonStructure result = types.build();
    return new PipeRunResult(null, new Message(result.toString()));
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) Message(nl.nn.adapterframework.stream.Message) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) JsonStructure(javax.json.JsonStructure)

Example 37 with JsonStructure

use of javax.json.JsonStructure in project java-driver by datastax.

the class Jsr353JsonFunction method selectToJson.

// Retrieving JSON payloads from table columns of arbitrary types,
// using toJson() function
private static void selectToJson(Session session) {
    Statement stmt = select().column("id").toJson("user").as("user").toJson("scores").as("scores").from("examples", "json_jsr353_function").where(in("id", 1, 2));
    ResultSet rows = session.execute(stmt);
    for (Row row : rows) {
        int id = row.getInt("id");
        // retrieve the JSON payload and convert it to a JsonObject instance
        // note that the codec requires that the type passed to the get() method
        // be always JsonStructure, and not a subclass of it, such as JsonObject,
        // hence the need to downcast to JsonObject manually
        JsonObject user = (JsonObject) row.get("user", JsonStructure.class);
        // it is also possible to retrieve the raw JSON payload
        String userJson = row.getString("user");
        // retrieve the JSON payload and convert it to a JsonObject instance
        JsonObject scores = (JsonObject) row.get("scores", JsonStructure.class);
        // it is also possible to retrieve the raw JSON payload
        String scoresJson = row.getString("scores");
        System.out.printf("Retrieved row:%n" + "id           %d%n" + "user         %s%n" + "user (raw)   %s%n" + "scores       %s%n" + "scores (raw) %s%n%n", id, user, userJson, scores, scoresJson);
    }
}
Also used : JsonObject(javax.json.JsonObject) JsonStructure(javax.json.JsonStructure)

Aggregations

JsonStructure (javax.json.JsonStructure)37 JsonObject (javax.json.JsonObject)14 StringReader (java.io.StringReader)7 JsonObjectBuilder (javax.json.JsonObjectBuilder)5 Test (org.junit.Test)5 StringWriter (java.io.StringWriter)4 URL (java.net.URL)4 JsonArrayBuilder (javax.json.JsonArrayBuilder)4 IOException (java.io.IOException)3 Map (java.util.Map)3 JsonReader (javax.json.JsonReader)3 JsonString (javax.json.JsonString)3 PipeForward (nl.nn.adapterframework.core.PipeForward)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HashMap (java.util.HashMap)2 List (java.util.List)2 JsonArray (javax.json.JsonArray)2 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)2 Message (nl.nn.adapterframework.stream.Message)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1