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:mobile.service.GroupService.java

/**
 * /*from ww w .  j  av a 2s .  c o  m*/
 *
 * @param groupName  ??
 * @param industryId Id
 * @param groupInfo  ?
 * @param groupPriv  ??
 * @param skillsTags 
 * @return
 */
public static ServiceVOResult<CommonVO> createGroup(String groupName, Long industryId, String groupInfo,
        String groupPriv, List<String> skillsTags) {
    GroupPriv priv = GroupPriv.getByName(groupPriv);
    if (null == priv) {
        return ServiceVOResult.error("100005", "??" + groupPriv);
    }
    if (StringUtils.isBlank(groupName)) {
        return ServiceVOResult.error("100005", "???");
    }
    SkillTag tag = SkillTag.getTagById(industryId);
    if (null == tag || tag.getTagType() != SkillTag.TagType.CATEGORY) {
        return ServiceVOResult.error("100005", "Id" + industryId);
    }

    ObjectNode data = Json.newObject();
    data.put("groupName", groupName);
    data.put("industry", industryId);
    data.put("groupPriv", priv.ordinal());
    data.put("groupInfo", groupInfo);
    data.set("tags", Json.toJson(skillsTags));

    User me = User.getFromSession(Context.current().session());

    ObjectNodeResult objectNodeResult = Group.saveGroupByJson(me, data);

    if (!objectNodeResult.isSuccess()) {
        if ("900003".equals(objectNodeResult.getErrorCode())) {
            return ServiceVOResult.error("294001", objectNodeResult.getError());
        }
        Logger.error(objectNodeResult.getObjectNode().toString());
        return ServiceVOResult.error("100001", "");
    }

    CommonVO vo = CommonVO.create();
    vo.set("groupId", objectNodeResult.getObjectNode().path("groupId").asLong(-1));

    return ServiceVOResult.success(vo);
}

From source file:dao.SearchDAO.java

public static JsonNode elasticSearchDatasetByKeyword(String category, String keywords, String source, int page,
        int size) {
    ObjectNode queryNode = Json.newObject();
    queryNode.put("from", (page - 1) * size);
    queryNode.put("size", size);
    JsonNode responseNode = null;/*from  ww w.java  2  s.  co  m*/
    ObjectNode keywordNode = null;

    try {
        keywordNode = utils.Search.generateElasticSearchQueryString(category, source, keywords);
    } catch (Exception e) {
        Logger.error("Elastic search dataset input query is not JSON format. Error message :" + e.getMessage());
    }

    if (keywordNode != null) {
        ObjectNode funcScoreNodes = Json.newObject();

        ObjectNode fieldValueFactorNode = Json.newObject();
        fieldValueFactorNode.put("field", "static_boosting_score");
        fieldValueFactorNode.put("factor", 1);
        fieldValueFactorNode.put("modifier", "square");
        fieldValueFactorNode.put("missing", 1);

        funcScoreNodes.put("query", keywordNode);
        funcScoreNodes.put("field_value_factor", fieldValueFactorNode);

        ObjectNode funcScoreNodesWrapper = Json.newObject();
        funcScoreNodesWrapper.put("function_score", funcScoreNodes);

        queryNode.put("query", funcScoreNodesWrapper);

        Logger.debug("The query sent to Elastic Search is: " + queryNode.toString());

        Promise<WSResponse> responsePromise = WS
                .url(Play.application().configuration().getString(SearchDAO.ELASTICSEARCH_DATASET_URL_KEY))
                .post(queryNode);
        responseNode = responsePromise.get(1000).asJson();

        Logger.debug("The responseNode from Elastic Search is: " + responseNode.toString());

    }

    ObjectNode resultNode = Json.newObject();
    Long count = 0L;
    List<Dataset> pagedDatasets = new ArrayList<>();
    resultNode.put("page", page);
    resultNode.put("category", category);
    resultNode.put("source", source);
    resultNode.put("itemsPerPage", size);
    resultNode.put("keywords", keywords);

    if (responseNode != null && responseNode.isContainerNode() && responseNode.has("hits")) {
        JsonNode hitsNode = responseNode.get("hits");
        if (hitsNode != null) {
            if (hitsNode.has("total")) {
                count = hitsNode.get("total").asLong();
            }
            if (hitsNode.has("hits")) {
                JsonNode dataNode = hitsNode.get("hits");
                if (dataNode != null && dataNode.isArray()) {
                    Iterator<JsonNode> arrayIterator = dataNode.elements();
                    if (arrayIterator != null) {
                        while (arrayIterator.hasNext()) {
                            JsonNode node = arrayIterator.next();
                            if (node.isContainerNode() && node.has("_id")) {
                                Dataset dataset = new Dataset();
                                dataset.id = node.get("_id").asLong();
                                if (node.has("_source")) {
                                    JsonNode sourceNode = node.get("_source");
                                    if (sourceNode != null) {
                                        if (sourceNode.has("name")) {
                                            dataset.name = sourceNode.get("name").asText();
                                        }
                                        if (sourceNode.has("source")) {
                                            dataset.source = sourceNode.get("source").asText();
                                        }
                                        if (sourceNode.has("urn")) {
                                            dataset.urn = sourceNode.get("urn").asText();
                                        }
                                        if (sourceNode.has("schema")) {
                                            dataset.schema = sourceNode.get("schema").asText();
                                        }
                                    }
                                }
                                pagedDatasets.add(dataset);
                            }
                        }
                    }

                }
            }

        }
    }
    resultNode.put("count", count);
    resultNode.put("totalPages", (int) Math.ceil(count / ((double) size)));
    resultNode.set("data", Json.toJson(pagedDatasets));
    return resultNode;
}

From source file:mobile.service.GroupService.java

/**
 * ?//from w ww  . ja va  2 s  .  c o m
 *
 * @param groupId    Id
 * @param avatarFile ?length>0
 * @return
 */
public static ServiceVOResult<CommonVO> updateGroupAvatar(Long groupId, File avatarFile) {
    if (null == groupId || null == avatarFile || avatarFile.length() <= 0) {
        throw new IllegalArgumentException(
                "groupId?avatarFile is null or avatarFile.length() <= 0. groupId = " + groupId
                        + ", avatarFile = " + avatarFile);
    }

    User me = User.getFromSession(Context.current().session());
    Group group = Group.queryGroupById(groupId);
    if (null != group && group.getType() != Group.Type.NORMAL) {
        return ServiceVOResult.error("100005", "??");
    }
    if (null == group) {
        return ServiceVOResult.error("2001", "?");
    } else if (!Objects.equals(group.getOwner().getUserId(), me.getId())) {
        return ServiceVOResult.error("2002", "??");
    }

    ObjectNodeResult nodeResult = new ObjectNodeResult();
    try {
        UserGroupsApp.save(avatarFile, nodeResult, me);
    } catch (AvatarException e) {
        LOGGER.error("?", e);
        return ServiceVOResult.error("100001", "?");
    }

    String headUrl = nodeResult.getObjectNode().path("avatar_190_source").asText();
    ObjectNode data = Json.newObject();
    data.put("headUrl", headUrl);
    data.put("id", groupId);

    ObjectNodeResult objectNodeResult = Group.saveGroupByJson(me, data);

    if (!objectNodeResult.isSuccess()) {
        Logger.error(objectNodeResult.getObjectNode().toString());
        return ServiceVOResult.error("100001", "");
    }

    CommonVO vo = CommonVO.create();
    vo.set("headUrl", Assets.at(headUrl));

    ServiceVOResult<CommonVO> result = ServiceVOResult.success();
    result.setVo(vo);

    return result;
}

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

public static Result getFavorites() {
    ObjectNode result = Json.newObject();
    String username = session("user");
    result.put("status", "ok");
    result.set("data", DatasetsDAO.getFavorites(username));
    return ok(result);
}

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

public static Result getDatasetByID(int id) {
    String username = session("user");
    models.Dataset dataset = DatasetsDAO.getDatasetByID(id, username);

    ObjectNode result = Json.newObject();

    if (dataset != null) {
        result.put("status", "ok");
        result.set("dataset", Json.toJson(dataset));
    } else {// w w w .jav a2s  . c om
        result.put("status", "error");
        result.put("message", "record not found");
    }

    return ok(result);
}

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

public static Result getDatasetPropertiesByID(int id) {
    JsonNode properties = DatasetsDAO.getDatasetPropertiesByID(id);

    ObjectNode result = Json.newObject();

    if (properties != null) {
        result.put("status", "ok");
        result.set("properties", properties);
    } else {/*  w ww.  ja va2s.c  o m*/
        result.put("status", "error");
        result.put("message", "record not found");
    }

    return ok(result);
}

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

public static Result getDatasetSampleDataByID(int id) {
    JsonNode sampleData = DatasetsDAO.getDatasetSampleDataByID(id);

    ObjectNode result = Json.newObject();

    if (sampleData != null) {
        result.put("status", "ok");
        result.set("sampleData", sampleData);
    } else {//from w  ww  .  j  ava2s  . co m
        result.put("status", "error");
        result.put("message", "record not found");
    }

    return ok(result);
}

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

public static Result unwatchDataset(int id, int watchId) {
    ObjectNode result = Json.newObject();
    if (DatasetsDAO.unwatch(watchId)) {
        result.put("status", "success");
    } else {/*from   www. ja va  2 s. co  m*/
        result.put("status", "failed");
    }

    return ok(result);
}

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

public static Result unwatchURN(int watchId) {
    ObjectNode result = Json.newObject();
    if (DatasetsDAO.unwatch(watchId)) {
        result.put("status", "success");
    } else {/*  w  ww  .j a  v  a2s . com*/
        result.put("status", "failed");
    }

    return ok(result);
}

From source file:com.github.fge.jsonschema.process.JsonPatch.java

private static JsonNode buildResult(final String rawPatch, final String rawData) throws IOException {
    final ObjectNode ret = JsonNodeFactory.instance.objectNode();

    final boolean invalidSchema = fillWithData(ret, INPUT, INVALID_INPUT, rawPatch);
    final boolean invalidData = fillWithData(ret, INPUT2, INVALID_INPUT2, rawData);

    final JsonNode patchNode = ret.remove(INPUT);
    final JsonNode data = ret.remove(INPUT2);

    if (invalidSchema || invalidData)
        return ret;

    final JsonPatchInput input = new JsonPatchInput(patchNode, data);

    final ProcessingReport report = new ListProcessingReport();
    final ProcessingResult<ValueHolder<JsonNode>> result = ProcessingResult.uncheckedResult(PROCESSOR, report,
            input);/*from   ww  w  .  j  av a  2s . co m*/

    final boolean success = result.isSuccess();
    ret.put(VALID, success);
    final JsonNode node = result.isSuccess() ? result.getResult().getValue() : buildReport(result.getReport());
    ret.put(RESULTS, JacksonUtils.prettyPrint(node));
    return ret;
}