Example usage for com.fasterxml.jackson.databind.node ObjectNode put

List of usage examples for com.fasterxml.jackson.databind.node ObjectNode put

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.node ObjectNode put.

Prototype

public ObjectNode put(String paramString1, String paramString2) 

Source Link

Usage

From source file:dao.ScriptFinderDAO.java

public static ObjectNode getPagedScripts(JsonNode filterOpt, int page, int size) {
    ObjectNode result = Json.newObject();

    javax.sql.DataSource ds = getJdbcTemplate().getDataSource();
    DataSourceTransactionManager tm = new DataSourceTransactionManager(ds);
    TransactionTemplate txTemplate = new TransactionTemplate(tm);
    String scriptName = null;// w w  w .  j  av  a 2  s  . c  om
    String scriptPath = null;
    String scriptType = null;
    String chainName = null;
    String jobName = null;
    String committerName = null;
    String committerEmail = null;
    if (filterOpt != null && (filterOpt.isContainerNode())) {
        if (filterOpt.has("scriptName")) {
            scriptName = filterOpt.get("scriptName").asText();
        }
        if (filterOpt.has("scriptPath")) {
            scriptPath = filterOpt.get("scriptPath").asText();
        }
        if (filterOpt.has("scriptType")) {
            scriptType = filterOpt.get("scriptType").asText();
        }
        if (filterOpt.has("chainName")) {
            chainName = filterOpt.get("chainName").asText();
        }
        if (filterOpt.has("jobName")) {
            jobName = filterOpt.get("jobName").asText();
        }
        if (filterOpt.has("committerName")) {
            committerName = filterOpt.get("committerName").asText();
        }
        if (filterOpt.has("committerEmail")) {
            committerEmail = filterOpt.get("committerEmail").asText();
        }
    }

    final String finalScriptName = scriptName;
    final String finalScriptPath = scriptPath;
    final String finalScriptType = scriptType;
    final String finalChainName = chainName;
    final String finalJobName = jobName;
    final String finalCommitterName = committerName;
    result = txTemplate.execute(new TransactionCallback<ObjectNode>() {
        public ObjectNode doInTransaction(TransactionStatus status) {

            List<Map<String, Object>> rows = null;
            String whereClause = "";
            boolean needAnd = false;
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(finalScriptName)) {
                if (StringUtils.isBlank(whereClause)) {
                    whereClause = " WHERE ";
                }
                if (needAnd) {
                    whereClause += " AND ";
                }
                whereClause += " script_name like :scriptname ";
                needAnd = true;
                params.put("scriptname", "%" + finalScriptName + "%");
            }
            if (StringUtils.isNotBlank(finalScriptPath)) {
                if (StringUtils.isBlank(whereClause)) {
                    whereClause = " WHERE ";
                }
                if (needAnd) {
                    whereClause += " AND ";
                }
                whereClause += " script_path like :scriptpath ";
                needAnd = true;
                params.put("scriptpath", "%" + finalScriptPath + "%");
            }
            if (StringUtils.isNotBlank(finalScriptType)) {
                if (StringUtils.isBlank(whereClause)) {
                    whereClause = " WHERE ";
                }
                if (needAnd) {
                    whereClause += " AND ";
                }
                whereClause += " script_type like :scripttype ";
                needAnd = true;
                params.put("scripttype", "%" + finalScriptType + "%");
            }
            if (StringUtils.isNotBlank(finalChainName)) {
                if (StringUtils.isBlank(whereClause)) {
                    whereClause = " WHERE ";
                }
                if (needAnd) {
                    whereClause += " AND ";
                }
                whereClause += " chain_name like :chainname ";
                needAnd = true;
                params.put("chainname", "%" + finalChainName + "%");
            }
            if (StringUtils.isNotBlank(finalJobName)) {
                if (StringUtils.isBlank(whereClause)) {
                    whereClause = " WHERE ";
                }
                if (needAnd) {
                    whereClause += " AND ";
                }
                whereClause += " job_name like :jobname ";
                needAnd = true;
                params.put("jobname", "%" + finalJobName + "%");
            }
            if (StringUtils.isNotBlank(finalCommitterName)) {
                if (StringUtils.isBlank(whereClause)) {
                    whereClause = " WHERE ";
                }
                if (needAnd) {
                    whereClause += " AND ";
                }
                whereClause += " ( committer_ldap like :committername or committer_name like :committername )";
                needAnd = true;
                params.put("committername", "%" + finalCommitterName + "%");
            }
            String query = GET_PAGED_SCRIPTS.replace("$WHERE_CLAUSE", whereClause);
            params.put("index", (page - 1) * size);
            params.put("size", size);
            NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(
                    getJdbcTemplate().getDataSource());
            rows = namedParameterJdbcTemplate.queryForList(query, params);
            long count = 0;
            try {
                count = getJdbcTemplate().queryForObject("SELECT FOUND_ROWS()", Long.class);
            } catch (EmptyResultDataAccessException e) {
                Logger.error("Exception = " + e.getMessage());
            }

            List<ScriptInfo> pagedScripts = new ArrayList<ScriptInfo>();
            for (Map row : rows) {
                int applicationID = (Integer) row.get(ScriptInfoRowMapper.APPLICATION_ID_COLUMN);
                int jobID = (Integer) row.get(ScriptInfoRowMapper.JOB_ID_COLUMN);
                String scriptUrl = (String) row.get(ScriptInfoRowMapper.SCRIPT_URL_COLUMN);
                String scriptPath = (String) row.get(ScriptInfoRowMapper.SCRIPT_PATH_COLUMN);
                String scriptType = (String) row.get(ScriptInfoRowMapper.SCRIPT_TYPE_COLUMN);
                String chainName = (String) row.get(ScriptInfoRowMapper.CHAIN_NAME_COLUMN);
                String jobName = (String) row.get(ScriptInfoRowMapper.JOB_NAME_COLUMN);
                String scriptName = (String) row.get(ScriptInfoRowMapper.SCRIPT_NAME_COLUMN);
                String committerName = (String) row.get(ScriptInfoRowMapper.COMMITTER_NAMES_COLUMN);
                String committerEmail = (String) row.get(ScriptInfoRowMapper.COMMITTER_EMAILS_COLUMN);
                ScriptInfo scriptInfo = new ScriptInfo();
                scriptInfo.applicationID = applicationID;
                scriptInfo.jobID = jobID;
                scriptInfo.scriptUrl = scriptUrl;
                scriptInfo.scriptPath = scriptPath;
                scriptInfo.scriptType = scriptType;
                scriptInfo.scriptName = scriptName;
                scriptInfo.chainName = chainName;
                scriptInfo.jobName = jobName;
                scriptInfo.committerName = committerName;
                scriptInfo.committerEmail = committerEmail;
                pagedScripts.add(scriptInfo);
            }

            ObjectNode resultNode = Json.newObject();
            resultNode.put("count", count);
            resultNode.put("page", page);
            resultNode.put("itemsPerPage", size);
            resultNode.put("totalPages", (int) Math.ceil(count / ((double) size)));
            resultNode.set("scripts", Json.toJson(pagedScripts));

            return resultNode;
        }
    });

    return result;
}

From source file:org.apache.streams.data.util.JsonUtil.java

/**
 * Creates an empty array if missing/*  ww w. jav a2 s .  co m*/
 * @param node objectnode to create the object within
 * @param field location to create the object
 * @return the Map representing the extensions property
 */
public static ObjectNode ensureObject(ObjectNode node, String field) {
    String[] path = Lists.newArrayList(Splitter.on('.').split(field)).toArray(new String[0]);
    ObjectNode current = node;
    ObjectNode result = null;
    for (int i = 0; i < path.length; i++) {
        if (node.get(field) == null)
            node.put(field, mapper.createObjectNode());
        current = (ObjectNode) node.get(field);
    }
    result = ensureObject((ObjectNode) node.get(path[path.length]),
            Joiner.on('.').join(Arrays.copyOfRange(path, 1, path.length)));
    return result;
}

From source file:controllers.api.v1.Tracking.java

public static Result addTrackingEvent() {
    ObjectNode result = Json.newObject();
    String username = session("user");
    ObjectNode json = Json.newObject();//from   w w w  .j a v a  2s. c o  m
    ArrayNode res = json.arrayNode();
    JsonNode requestNode = request().body().asJson();

    if (StringUtils.isNotBlank(username)) {
        String message = TrackingDAO.addTrackingEvent(requestNode, username);
        if (StringUtils.isBlank(message)) {
            result.put("status", "success");
            return ok(result);
        } else {
            result.put("status", "failed");
            result.put("message", message);
            return badRequest(result);
        }
    } else {
        result.put("status", "failed");
        result.put("message", "User is not authenticated");
        return unauthorized(result);
    }
}

From source file:automenta.knowtention.Core.java

/** adapter for JSONPatch+ */
static JsonPatch getPatch(ArrayNode patch) throws IOException {

    //System.out.println("min: " + patch.toString());

    /*/*from  ww  w .  ja  v a 2 s . co  m*/
            
    JSONPatch+
            
    [ [ <op>, <param1>[, <param2> ], .... ]
            
    + add
    - remove
    * copy
    / move
    = replace
    ? test
            
    [
      { "op": "test", "path": "/a/b/c", "value": "foo" },
      { "op": "remove", "path": "/a/b/c" },
      { "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] },
      { "op": "replace", "path": "/a/b/c", "value": 42 },
      { "op": "move", "from": "/a/b/c", "path": "/a/b/d" },
      { "op": "copy", "from": "/a/b/d", "path": "/a/b/e" }
    ]
    */

    int i = 0;
    for (JsonNode k : patch) {
        JsonNode nextOp = k;
        ObjectNode m = null;
        if (k.isArray()) {
            String op = k.get(0).textValue();
            switch (op.charAt(0)) {
            case '+':
                m = new ObjectNode(newJson);
                m.put("op", "add");
                m.put("path", k.get(1));
                m.put("value", k.get(2));
                break;
            case '-':
                m = new ObjectNode(newJson);
                m.put("op", "remove");
                m.put("path", k.get(1));
                break;
            case '*':
                m = new ObjectNode(newJson);
                m.put("op", "copy");
                m.put("from", k.get(1));
                m.put("path", k.get(2));
                break;
            case '/':
                m = new ObjectNode(newJson);
                m.put("op", "move");
                m.put("from", k.get(1));
                m.put("path", k.get(2));
                break;
            case '?':
                m = new ObjectNode(newJson);
                m.put("op", "test");
                m.put("path", k.get(1));
                m.put("value", k.get(2));
                break;
            case '=':
                m = new ObjectNode(newJson);
                m.put("op", "replace");
                m.put("path", k.get(1));
                m.put("value", k.get(2));
                break;
            }
        }
        if (m != null)
            patch.set(i++, m);
    }

    //System.out.println("standard: " + patch.toString());

    return JsonPatch.fromJson(patch);
}

From source file:org.apache.streams.data.util.PropertyUtil.java

public static ObjectNode unflattenObjectNode(ObjectNode flatObject, char seperator) {
    ObjectNode root = mapper.createObjectNode();
    Iterator<Map.Entry<String, JsonNode>> iter = flatObject.fields();
    while (iter.hasNext()) {
        Map.Entry<String, JsonNode> item = iter.next();
        String fullKey = item.getKey();
        if (!fullKey.contains(Character.valueOf(seperator).toString())) {
            root.put(item.getKey(), item.getValue());
        } else {/*from  w ww.  j  a  v a2  s.  c  o  m*/
            ObjectNode currentNode = root;
            List<String> keyParts = Splitter.on(seperator).splitToList(item.getKey());
            Iterator<String> keyPartIterator = Iterables
                    .limit(Splitter.on(seperator).split(item.getKey()), keyParts.size() - 1).iterator();
            while (keyPartIterator.hasNext()) {
                String part = keyPartIterator.next();
                if (currentNode.has(part) && currentNode.get(part).isObject()) {
                    currentNode = (ObjectNode) currentNode.get(part);
                } else {
                    ObjectNode newNode = mapper.createObjectNode();
                    currentNode.put(part, newNode);
                    currentNode = newNode;
                }
            }
            ;
            currentNode.put(keyParts.get(keyParts.size() - 1), item.getValue());

        }
    }
    return root;
}

From source file:com.auditbucket.client.AbRestClient.java

/**
 * Converts the strings to a simple JSON representation
 *
 * @param headerRow - keys/*  w w  w .  j av a  2 s .  c om*/
 * @param line      - values
 * @return JSON Object
 * @throws JsonProcessingException
 */
public static String convertToJson(String[] headerRow, String[] line) throws JsonProcessingException {
    ObjectNode node = mapper.createObjectNode();
    for (int i = 0; i < headerRow.length; i++) {
        node.put(headerRow[i], line[i].trim());
    }
    return node.toString();
}

From source file:json_cmp.Comparer.java

private static void changeMetrics(JsonNode mediaObj) {
    if (mediaObj.path("metrics").isMissingNode())
        return;//  w w  w .jav a  2s. c  o m
    ObjectNode metricsObj = (ObjectNode) mediaObj.path("metrics");
    //if(metricsObj.path("plidl").isMissingNode())
    metricsObj.put("plidl", "");
    //if(metricsObj.path("isrc").isMissingNode())
    metricsObj.put("isrc", "");
    metricsObj.put("pspid", "");
    //metricsObj.put("beacons", "");
}

From source file:com.squarespace.template.plugins.platform.CommerceUtils.java

public static ArrayNode getItemVariantOptions(JsonNode item) {
    JsonNode structuredContent = item.path("structuredContent");
    JsonNode variants = structuredContent.path("variants");
    if (variants.size() <= 1) {
        return EMPTY_ARRAY;
    }// w ww  .  j  a v  a  2  s . c  om

    ArrayNode userDefinedOptions = JsonUtils.createArrayNode();
    JsonNode ordering = structuredContent.path("variantOptionOrdering");
    for (int i = 0; i < ordering.size(); i++) {
        String optionName = ordering.path(i).asText();
        ObjectNode option = JsonUtils.createObjectNode();
        option.put("name", optionName);
        option.put("values", JsonUtils.createArrayNode());
        userDefinedOptions.add(option);
    }

    for (int i = 0; i < variants.size(); i++) {
        JsonNode variant = variants.path(i);
        JsonNode attributes = variant.get("attributes");
        if (attributes == null) {
            continue;
        }
        Iterator<String> fields = attributes.fieldNames();

        while (fields.hasNext()) {
            String field = fields.next();

            String variantOptionValue = attributes.get(field).asText();
            ObjectNode userDefinedOption = null;

            for (int j = 0; j < userDefinedOptions.size(); j++) {
                ObjectNode current = (ObjectNode) userDefinedOptions.get(j);
                if (current.get("name").asText().equals(field)) {
                    userDefinedOption = current;
                }
            }

            if (userDefinedOption != null) {
                boolean hasOptionValue = false;
                ArrayNode optionValues = (ArrayNode) userDefinedOption.get("values");
                for (int k = 0; k < optionValues.size(); k++) {
                    String optionValue = optionValues.get(k).asText();
                    if (optionValue.equals(variantOptionValue)) {
                        hasOptionValue = true;
                        break;
                    }
                }

                if (!hasOptionValue) {
                    optionValues.add(variantOptionValue);
                }
            }
        }
    }
    return userDefinedOptions;
}

From source file:org.waarp.common.json.JsonHandler.java

/**
 * //from  w ww.j  a va 2s.c om
 * @param node
 * @param field
 * @param value
 */
public final static void setValue(ObjectNode node, String field, int value) {
    node.put(field, value);
}

From source file:org.waarp.common.json.JsonHandler.java

/**
 * /*from  w w  w.j a  v  a 2s .c  o  m*/
 * @param node
 * @param field
 * @param value
 */
public final static void setValue(ObjectNode node, String field, long value) {
    node.put(field, value);
}