Example usage for io.vertx.core.json JsonObject getJsonArray

List of usage examples for io.vertx.core.json JsonObject getJsonArray

Introduction

In this page you can find the example usage for io.vertx.core.json JsonObject getJsonArray.

Prototype

public JsonArray getJsonArray(String key, JsonArray def) 

Source Link

Document

Like #getJsonArray(String) but specifying a default value to return if there is no entry.

Usage

From source file:org.entcore.workspace.service.impl.WorkspaceRepositoryEvents.java

License:Open Source License

private void exportFiles(final JsonObject alias, final String[] ids, String exportPath, String locale,
        final AtomicBoolean exported, final Handler<Boolean> handler) {
    createExportDirectory(exportPath, locale, new Handler<String>() {
        @Override/*w  w  w. j a v  a 2 s . co m*/
        public void handle(String path) {
            if (path != null) {
                if (ids.length == 0) {
                    handler.handle(true);
                    return;
                }
                storage.writeToFileSystem(ids, path, alias, new Handler<JsonObject>() {
                    @Override
                    public void handle(JsonObject event) {
                        if ("ok".equals(event.getString("status"))) {
                            exported.set(true);
                            handler.handle(exported.get());
                        } else {
                            JsonArray errors = event.getJsonArray("errors",
                                    new fr.wseduc.webutils.collections.JsonArray());
                            boolean ignoreErrors = errors.size() > 0;
                            for (Object o : errors) {
                                if (!(o instanceof JsonObject))
                                    continue;
                                if (((JsonObject) o).getString("message") == null || (!((JsonObject) o)
                                        .getString("message").contains("NoSuchFileException")
                                        && !((JsonObject) o).getString("message")
                                                .contains("FileAlreadyExistsException"))) {
                                    ignoreErrors = false;
                                    break;
                                }
                            }
                            if (ignoreErrors) {
                                exported.set(true);
                                handler.handle(exported.get());
                            } else {
                                log.error("Write to fs : "
                                        + new fr.wseduc.webutils.collections.JsonArray(Arrays.asList(ids))
                                                .encode()
                                        + " - " + event.encode());
                                handler.handle(exported.get());
                            }
                        }
                    }
                });
            } else {
                log.error("Create export directory error.");
                handler.handle(exported.get());
            }
        }
    });
}

From source file:org.entcore.workspace.service.impl.WorkspaceSearchingEvents.java

License:Open Source License

@SuppressWarnings("unchecked")
private JsonArray formatSearchResult(final JsonArray results, final JsonArray columnsHeader,
        final List<String> words, final String locale, final String userId,
        final Map<String, Map<String, String>> mapOwnerMapNameFolderId) {
    final List<String> aHeader = columnsHeader.getList();
    final JsonArray traity = new fr.wseduc.webutils.collections.JsonArray();

    for (int i = 0; i < results.size(); i++) {
        final JsonObject j = results.getJsonObject(i);
        final JsonObject jr = new JsonObject();

        if (j != null) {
            final String folder = j.getString("folder", "");

            Date modified = new Date();
            try {
                modified = DateUtils.parse(j.getString("modified"), PATTERN);
            } catch (ParseException e) {
                log.error("Can't parse date from modified", e);
            }/* w w w . ja va  2 s  .  co m*/
            final String owner = j.getString("owner", "");
            final Map<String, Object> map = formatDescription(
                    j.getJsonArray("comments", new fr.wseduc.webutils.collections.JsonArray()), words, modified,
                    locale);
            jr.put(aHeader.get(0), j.getString("name"));
            jr.put(aHeader.get(1), map.get("description").toString());
            jr.put(aHeader.get(2), new JsonObject().put("$date", ((Date) map.get("modified")).getTime()));
            jr.put(aHeader.get(3), j.getString("ownerName", ""));
            jr.put(aHeader.get(4), owner);
            //default front route (no folder and the file belongs to the owner)
            String resourceURI = "/workspace/workspace";

            if (userId.equals(owner)) {
                if (!StringUtils.isEmpty(folder)) {
                    resourceURI += "#/folder/" + mapOwnerMapNameFolderId.get(owner).get(folder);
                }
            } else {
                //if there is a folder on entry file and this folder is shared
                if (!StringUtils.isEmpty(folder) && mapOwnerMapNameFolderId.containsKey(owner)
                        && mapOwnerMapNameFolderId.get(owner).containsKey(folder)) {
                    resourceURI += "#/shared/folder/" + mapOwnerMapNameFolderId.get(owner).get(folder);
                } else {
                    //only the file is shared
                    resourceURI += "#/shared";
                }
            }
            jr.put(aHeader.get(5), resourceURI);
            traity.add(jr);
        }
    }
    return traity;
}

From source file:org.entcore.workspace.service.WorkspaceService.java

License:Open Source License

private void notifyComment(final HttpServerRequest request, final String id, final UserInfos user,
        final boolean isFolder) {
    final JsonObject params = new JsonObject()
            .put("userUri", "/userbook/annuaire#" + user.getUserId() + "#" + user.getType())
            .put("userName", user.getUsername()).put("appPrefix", pathPrefix + "/workspace");

    final String notifyName = WORKSPACE_NAME.toLowerCase() + "." + (isFolder ? "comment-folder" : "comment");

    //Retrieve the document from DB
    mongo.findOne(DocumentDao.DOCUMENTS_COLLECTION, new JsonObject().put("_id", id),
            new Handler<Message<JsonObject>>() {
                @Override//from  ww w .  j av  a  2 s.  co m
                public void handle(Message<JsonObject> event) {
                    if ("ok".equals(event.body().getString("status"))
                            && event.body().getJsonObject("result") != null) {
                        final JsonObject document = event.body().getJsonObject("result");
                        params.put("resourceName", document.getString("name", ""));

                        //Handle the parent folder id (null if document is at root level)
                        final Handler<String> folderIdHandler = new Handler<String>() {
                            public void handle(final String folderId) {

                                //Send the notification to the shared network
                                Handler<List<String>> shareNotificationHandler = new Handler<List<String>>() {
                                    public void handle(List<String> recipients) {
                                        JsonObject sharedNotifParams = params.copy();

                                        if (folderId != null) {
                                            sharedNotifParams.put("resourceUri",
                                                    pathPrefix + "/workspace#/shared/folder/" + folderId);
                                        } else {
                                            sharedNotifParams.put("resourceUri",
                                                    pathPrefix + "/workspace#/shared");
                                        }

                                        // don't send comment with share uri at owner
                                        final String o = document.getString("owner");
                                        if (o != null && recipients.contains(o)) {
                                            recipients.remove(o);
                                        }
                                        notification.notifyTimeline(request, notifyName, user, recipients, id,
                                                sharedNotifParams);
                                    }
                                };

                                //'Flatten' the share users & group into a user id list (excluding the current user)
                                flattenShareIds(
                                        document.getJsonArray("shared",
                                                new fr.wseduc.webutils.collections.JsonArray()),
                                        user, shareNotificationHandler);

                                //If the user commenting is not the owner, send a notification to the owner
                                if (!document.getString("owner").equals(user.getUserId())) {
                                    JsonObject ownerNotif = params.copy();
                                    ArrayList<String> ownerList = new ArrayList<>();
                                    ownerList.add(document.getString("owner"));

                                    if (folderId != null) {
                                        ownerNotif.put("resourceUri",
                                                pathPrefix + "/workspace#/folder/" + folderId);
                                    } else {
                                        ownerNotif.put("resourceUri", pathPrefix + "/workspace");
                                    }
                                    notification.notifyTimeline(request, notifyName, user, ownerList, id, null,
                                            ownerNotif, true);
                                }

                            }
                        };

                        //Handle the parent folder result from DB
                        Handler<Message<JsonObject>> folderHandler = new Handler<Message<JsonObject>>() {
                            public void handle(Message<JsonObject> event) {
                                if (!"ok".equals(event.body().getString("status"))
                                        || event.body().getJsonObject("result") == null) {
                                    log.error(
                                            "Unable to send timeline notification : invalid parent folder on resource "
                                                    + id);
                                    return;
                                }

                                final JsonObject folder = event.body().getJsonObject("result");
                                final String folderId = folder.getString("_id");

                                folderIdHandler.handle(folderId);
                            }
                        };

                        //If the document does not have a parent folder
                        if (!isFolder && !document.containsKey("folder") || isFolder
                                && document.getString("folder").equals(document.getString("name"))) {
                            folderIdHandler.handle(null);
                        } else {
                            //If the document has a parent folder
                            String parentFolderPath = document.getString("folder");
                            if (isFolder) {
                                parentFolderPath = parentFolderPath.substring(0,
                                        parentFolderPath.lastIndexOf("_"));
                            }

                            QueryBuilder query = QueryBuilder.start("folder").is(parentFolderPath).and("file")
                                    .exists(false).and("owner").is(document.getString("owner"));

                            //Retrieve the parent folder from DB
                            mongo.findOne(DocumentDao.DOCUMENTS_COLLECTION, MongoQueryBuilder.build(query),
                                    folderHandler);
                        }

                    } else {
                        log.error("Unable to send timeline notification : missing name on resource " + id);
                    }
                }
            });
}

From source file:org.sfs.metadata.Metadata.java

License:Apache License

public Metadata withJsonObject(JsonArray metadataArray) {
    for (Object o : metadataArray) {
        JsonObject jsonObject = (JsonObject) o;
        String name = jsonObject.getString("name");
        JsonArray valueArray = jsonObject.getJsonArray("values", new JsonArray());
        for (Object p : valueArray) {
            String metaValue = (String) p;
            put(name, metaValue);/*from  w ww  .j a  v  a2 s .  c  o m*/
        }

    }
    return this;
}

From source file:org.sfs.vo.Account.java

License:Apache License

public T merge(JsonObject document) {

    JsonArray metadataJsonObject = document.getJsonArray("metadata", new JsonArray());
    metadata.withJsonObject(metadataJsonObject);

    setNodeId(document.getString("node_id"));

    String createTimestamp = document.getString("create_ts");
    String updateTimestamp = document.getString("update_ts");

    if (createTimestamp != null) {
        setCreateTs(fromDateTimeString(createTimestamp));
    }/*from w  w w  . j  av  a  2s  .  c o  m*/
    if (updateTimestamp != null) {
        setUpdateTs(fromDateTimeString(updateTimestamp));
    }

    return (T) this;
}

From source file:org.sfs.vo.Container.java

License:Apache License

public T merge(JsonObject document) {

    checkState(getParent().getId().equals(document.getString("account_id")));

    setOwnerGuid(document.getString("owner_guid"));

    JsonArray metadataJsonObject = document.getJsonArray("metadata", new JsonArray());
    getMetadata().withJsonObject(metadataJsonObject);

    setNodeId(document.getString("node_id"));

    String createTimestamp = document.getString("create_ts");
    String updateTimestamp = document.getString("update_ts");

    if (createTimestamp != null) {
        setCreateTs(fromDateTimeString(createTimestamp));
    }/*  w w  w  .j ava2  s  . c o m*/
    if (updateTimestamp != null) {
        setUpdateTs(fromDateTimeString(updateTimestamp));
    }

    setObjectReplicas(
            document.containsKey("object_replicas") ? document.getInteger("object_replicas") : NOT_SET);

    return (T) this;
}

From source file:org.sfs.vo.XVersion.java

License:Apache License

public T merge(JsonObject document) {
    setDeleted(document.getBoolean("deleted"));
    setDeleteMarker(document.getBoolean("delete_marker"));

    setContentDisposition(document.getString("content_disposition"));
    setContentType(document.getString("content_type"));
    setContentEncoding(document.getString("content_encoding"));
    setContentLength(document.getLong("content_length"));
    setEtag(document.getBinary("etag"));
    setContentMd5(document.getBinary("content_md5"));
    setContentSha512(document.getBinary("content_sha512"));
    setDeleteAt(document.getLong("delete_at"));
    setServerSideEncryption(document.getBoolean("server_side_encryption"));
    setObjectManifest(document.getString("object_manifest"));
    setStaticLargeObject(document.getBoolean("static_large_object"));

    JsonArray metadataJsonObject = document.getJsonArray("metadata", new JsonArray());
    metadata.withJsonObject(metadataJsonObject);

    this.segments.clear();
    JsonArray jsonSegments = document.getJsonArray("segments", new JsonArray());
    for (Object o : jsonSegments) {
        JsonObject segmentDocument = (JsonObject) o;
        Long segmentId = segmentDocument.getLong("id");
        checkNotNull(segmentId, "Segment id cannot be null");
        TransientSegment transientSegment = new TransientSegment(this, segmentId).merge(segmentDocument);
        segments.add(transientSegment);/*from www.  j  a v a 2  s.  c  o m*/
    }

    String createTimestamp = document.getString("create_ts");
    String updateTimestamp = document.getString("update_ts");

    if (createTimestamp != null) {
        setCreateTs(fromDateTimeString(createTimestamp));
    }
    if (updateTimestamp != null) {
        setUpdateTs(fromDateTimeString(updateTimestamp));
    }
    return (T) this;
}