List of usage examples for io.vertx.core.json JsonArray getJsonArray
public JsonArray getJsonArray(int pos)
From source file:org.entcore.common.http.filter.AdmlResourcesProvider.java
License:Open Source License
protected void validateQueries(final HttpServerRequest request, final Handler<Boolean> handler, StatementsBuilder statementsBuilder) { request.pause();/*from w w w . java2 s. co m*/ final JsonArray statements = statementsBuilder.build(); neo4j.executeTransaction(statements, null, true, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> r) { request.resume(); JsonArray res = r.body().getJsonArray("results"); if (!"ok".equals(r.body().getString("status")) || res == null || res.size() != statements.size()) { handler.handle(false); return; } for (int i = 0; i < statements.size(); i++) { JsonArray j = res.getJsonArray(i); if (j.size() != 1 || !j.getJsonObject(0).getBoolean("exists", false)) { handler.handle(false); return; } } handler.handle(true); } }); }
From source file:org.entcore.common.neo4j.Neo4jResult.java
License:Open Source License
private static Either<String, JsonObject> validUniqueResult(int idx, Message<JsonObject> event) { Either<String, JsonArray> r = validResults(event); if (r.isRight()) { JsonArray results = r.right().getValue(); if (results == null || results.size() == 0) { return new Either.Right<>(new JsonObject()); } else {/*from w ww .j a v a 2 s. c o m*/ results = results.getJsonArray(idx); if (results.size() == 1 && (results.getValue(0) instanceof JsonObject)) { return new Either.Right<>(results.getJsonObject(0)); } } return new Either.Left<>("non.unique.result"); } else { return new Either.Left<>(r.left().getValue()); } }
From source file:org.entcore.common.sql.SqlResult.java
License:Open Source License
public static Long countResult(Message<JsonObject> res) { if ("ok".equals(res.body().getString("status"))) { JsonArray values = res.body().getJsonArray("results"); if (values != null && values.size() == 1) { JsonArray row = values.getJsonArray(0); if (row != null && row.size() == 1) { return row.getLong(0); }/*from ww w . ja v a 2 s .com*/ } } return null; }
From source file:org.entcore.conversation.controllers.ConversationController.java
License:Open Source License
private void deleteMessages(final HttpServerRequest request, final List<String> ids, final Boolean deleteAll, Handler<Either<String, JsonArray>> handler) { getUserInfos(eb, request, new Handler<UserInfos>() { @Override/* ww w.ja v a2 s . c om*/ public void handle(final UserInfos user) { if (user != null) { conversationService.delete(ids, deleteAll, user, new Handler<Either<String, JsonArray>>() { @Override public void handle(Either<String, JsonArray> event) { if (event.isLeft()) { if (handler != null) { handler.handle(event); } else { badRequest(request, event.left().getValue()); } return; } JsonArray results = event.right().getValue(); final long freeQuota = results.getJsonArray(0).getJsonObject(0).getLong("totalquota", 0L); updateUserQuota(user.getUserId(), -freeQuota, new Handler<Void>() { public void handle(Void event) { if (handler != null) { handler.handle(new Either.Right<>(new JsonArray())); } else { ok(request); } } }); } }); } else { unauthorized(request); } } }); }
From source file:org.entcore.conversation.controllers.ConversationController.java
License:Open Source License
private void deleteFolders(final HttpServerRequest request, final String folderId, final Boolean deleteAll, final Handler<Either<String, JsonArray>> handler) { Handler<UserInfos> userInfosHandler = new Handler<UserInfos>() { public void handle(final UserInfos user) { if (user == null) { unauthorized(request);/* w w w .j a v a 2s . c o m*/ return; } conversationService.deleteFolder(folderId, deleteAll, user, new Handler<Either<String, JsonArray>>() { public void handle(Either<String, JsonArray> event) { if (event.isLeft()) { if (handler != null) { handler.handle(event); } else { badRequest(request, event.left().getValue()); } return; } JsonArray results = event.right().getValue(); final long freeQuota = results.getJsonArray(0).getJsonObject(0) .getLong("totalquota", 0L); updateUserQuota(user.getUserId(), -freeQuota, new Handler<Void>() { public void handle(Void event) { if (handler != null) { handler.handle(new Either.Right<>(new JsonArray())); } else { ok(request); } } }); } }); } }; UserUtils.getUserInfos(eb, request, userInfosHandler); }
From source file:org.entcore.conversation.service.impl.ConversationRepositoryEvents.java
License:Open Source License
@Override public void deleteUsers(JsonArray users) { JsonArray userIds = new fr.wseduc.webutils.collections.JsonArray(); for (Object o : users) { if (!(o instanceof JsonObject)) continue; userIds.add(((JsonObject) o).getString("id")); }//from w ww .j a v a 2 s . c o m SqlStatementsBuilder builder = new SqlStatementsBuilder(); String unusedAttachments = "WITH unusedAtts AS (" + "SELECT DISTINCT attachment_id AS id FROM conversation.usermessagesattachments uma " + "GROUP BY attachment_id " + "HAVING every(user_id IN " + Sql.listPrepared(userIds.getList()) + ") " + ") SELECT " + "CASE WHEN COUNT(id) = 0 THEN '[]' ELSE json_agg(distinct id) END AS attachmentIds " + "FROM unusedAtts u"; builder.prepared(unusedAttachments, userIds); String deleteFolder = "DELETE FROM conversation.folders f " + "WHERE f.user_id IN " + Sql.listPrepared(userIds.getList()); builder.prepared(deleteFolder, userIds); String deleteUserMessages = "DELETE FROM conversation.usermessages um " + "WHERE um.user_id IN " + Sql.listPrepared(userIds.getList()); builder.prepared(deleteUserMessages, userIds); String setFrom = "UPDATE conversation.messages " + "SET " + "\"from\" = '', " + "\"fromName\" = ?, " + "\"displayNames\" = \"displayNames\" - (? || '$' || ? || '$ $ ') " + "WHERE \"from\" = ?"; String setTO = "UPDATE conversation.messages " + "SET " + "\"to\" = \"to\" - ?, " + "\"toName\" = COALESCE(\"toName\", '[]')::jsonb || (?)::jsonb, " + "\"displayNames\" = \"displayNames\" - (? || '$' || ? || '$ $ ') " + "WHERE \"to\" @> (?)::jsonb"; String setCC = "UPDATE conversation.messages " + "SET " + "\"cc\" = \"cc\" - ?, " + "\"ccName\" = COALESCE(\"ccName\", '[]')::jsonb || (?)::jsonb, " + "\"displayNames\" = \"displayNames\" - (? || '$' || ? || '$ $ ') " + "WHERE \"cc\" @> (?)::jsonb"; for (Object o : users) { if (!(o instanceof JsonObject)) continue; JsonObject user = (JsonObject) o; JsonArray paramsToCc = new fr.wseduc.webutils.collections.JsonArray(); JsonArray paramsFrom = new fr.wseduc.webutils.collections.JsonArray(); paramsToCc.add(user.getString("id", "")); paramsToCc.add(new fr.wseduc.webutils.collections.JsonArray().add(user.getString("displayName", "")) .toString()); paramsToCc.add(user.getString("id", "")); paramsToCc.add(user.getString("displayName", "")); paramsToCc.add(new fr.wseduc.webutils.collections.JsonArray().add(user.getString("id", "")).toString()); paramsFrom.add(user.getString("displayName", "")); paramsFrom.add(user.getString("id", "")); paramsFrom.add(user.getString("displayName", "")); paramsFrom.add(user.getString("id", "")); builder.prepared(setTO, paramsToCc); builder.prepared(setCC, paramsToCc); builder.prepared(setFrom, paramsFrom); } sql.transaction(builder.build(), new DeliveryOptions().setSendTimeout(timeout), SqlResult.validResultsHandler(new Handler<Either<String, JsonArray>>() { public void handle(Either<String, JsonArray> event) { if (event.isLeft()) { log.error("Error deleting conversation data : " + event.left().getValue()); return; } JsonArray results = event.right().getValue(); JsonArray attachmentIds = results.getJsonArray(0).size() > 0 ? new fr.wseduc.webutils.collections.JsonArray( results.getJsonArray(0).getJsonObject(0).getString("attachmentIds", "[]")) : new fr.wseduc.webutils.collections.JsonArray(); for (Object attachmentObj : attachmentIds) { final String attachmentId = (String) attachmentObj; storage.removeFile(attachmentId, new Handler<JsonObject>() { @Override public void handle(JsonObject event) { if (!"ok".equals(event.getString("status"))) { log.error("[" + ConversationRepositoryEvents.class.getSimpleName() + "] Error while tying to delete attachment file (_id: {" + attachmentId + "})"); } } }); } } }, "attachmentIds")); }
From source file:org.entcore.conversation.service.impl.SqlConversationService.java
License:Open Source License
@Override public void removeAttachment(String messageId, String attachmentId, UserInfos user, final Handler<Either<String, JsonObject>> result) { if (validationParamsError(user, result, messageId, attachmentId)) return;// w ww . j ava 2 s . co m SqlStatementsBuilder builder = new SqlStatementsBuilder(); JsonArray values = new fr.wseduc.webutils.collections.JsonArray().add(messageId).add(user.getUserId()) .add(attachmentId); String query1 = "SELECT att.* FROM " + attachmentTable + " att WHERE att.id = ?"; builder.prepared(query1, new fr.wseduc.webutils.collections.JsonArray().add(attachmentId)); String query2 = "SELECT (count(*) = 1) AS deletionCheck FROM " + attachmentTable + " att JOIN " + userMessageAttachmentTable + " uma ON uma.attachment_id = att.id " + "WHERE att.id = ? " + "GROUP BY att.id HAVING count(distinct uma.user_id) = 1 AND count(distinct uma.message_id) = 1"; builder.prepared(query2, new fr.wseduc.webutils.collections.JsonArray().add(attachmentId)); String query3 = "WITH attachment AS (" + query1 + ") " + "UPDATE " + userMessageTable + " AS um " + "SET total_quota = um.total_quota - (SELECT SUM(DISTINCT attachment.size) FROM attachment) " + "WHERE um.message_id = ? AND um.user_id = ?"; JsonArray values3 = new fr.wseduc.webutils.collections.JsonArray().add(attachmentId).add(messageId) .add(user.getUserId()); builder.prepared(query3, values3); String query4 = "DELETE FROM " + userMessageAttachmentTable + " WHERE " + "message_id = ? AND user_id = ? AND attachment_id = ?"; builder.prepared(query4, values); sql.transaction(builder.build(), SqlResult.validResultsHandler(new Handler<Either<String, JsonArray>>() { public void handle(Either<String, JsonArray> event) { if (event.isLeft()) { result.handle(new Either.Left<String, JsonObject>(event.left().getValue())); return; } else { JsonArray results = event.right().getValue(); JsonObject attachment = results.getJsonArray(0).getJsonObject(0); boolean deletionCheck = results.getJsonArray(1).size() > 0 ? results.getJsonArray(1).getJsonObject(0).getBoolean("deletioncheck", false) : false; JsonObject resultJson = new JsonObject().put("deletionCheck", deletionCheck) .put("fileId", attachment.getString("id")).put("fileSize", attachment.getLong("size")); result.handle(new Either.Right<String, JsonObject>(resultJson)); } } })); }
From source file:org.entcore.directory.security.RelativeStudentFilter.java
License:Open Source License
@Override public void authorize(final HttpServerRequest request, Binding binding, UserInfos user, final Handler<Boolean> handler) { Map<String, UserInfos.Function> functions = user.getFunctions(); if (functions == null || functions.isEmpty()) { handler.handle(false);//from w w w . j a v a2 s. c o m return; } if (functions.containsKey(SUPER_ADMIN)) { handler.handle(true); return; } final UserInfos.Function adminLocal = functions.get(DefaultFunctions.ADMIN_LOCAL); if (adminLocal == null || adminLocal.getScope() == null) { handler.handle(false); return; } final String studentId = request.params().get("studentId"); final String relativeId = request.params().get("relativeId"); String query = "MATCH (s)<-[:DEPENDS]-(:Group)<-[:IN]-(:User { id : {id}}) " + "WHERE (s:Structure OR s:Class) AND s.id IN {scope} " + "RETURN count(*) > 0 as exists "; JsonArray scope = new fr.wseduc.webutils.collections.JsonArray(adminLocal.getScope()); StatementsBuilder s = new StatementsBuilder() .add(query, new JsonObject().put("id", studentId).put("scope", scope)) .add(query, new JsonObject().put("id", relativeId).put("scope", scope)); request.pause(); Neo4j.getInstance().executeTransaction(s.build(), null, true, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> r) { request.resume(); JsonArray res = r.body().getJsonArray("results"); handler.handle("ok".equals(r.body().getString("status")) && res.size() == 2 && res.getJsonArray(0).getJsonObject(0).getBoolean("exists", false) && res.getJsonArray(1).getJsonObject(0).getBoolean("exists", false)); } }); }
From source file:org.entcore.directory.services.impl.DefaultUserBookService.java
License:Open Source License
@Override public void update(String userId, JsonObject userBook, final Handler<Either<String, JsonObject>> result) { JsonObject u = Utils.validAndGet(userBook, UPDATE_USERBOOK_FIELDS, Collections.<String>emptyList()); if (Utils.defaultValidationError(u, result, userId)) return;/*from w w w . j a va2 s. com*/ // OVERRIDE AVATAR URL Optional<String> pictureId = getPictureIdForUserbook(userBook); if (pictureId.isPresent()) { String fileId = avatarFileNameFromUserId(userId, Optional.empty()); u.put("picture", "/userbook/avatar/" + fileId); } StatementsBuilder b = new StatementsBuilder(); String query = "MATCH (u:`User` { id : {id}})-[:USERBOOK]->(ub:UserBook) WITH ub.picture as oldpic,ub,u SET " + nodeSetPropertiesFromJson("ub", u); query += " RETURN oldpic,ub.picture as picture"; boolean updateUserBook = u.size() > 0; if (updateUserBook) { b.add(query, u.put("id", userId)); } String q2 = "MATCH (u:`User` { id : {id}})-[:USERBOOK]->(ub:UserBook)" + "-[:PUBLIC|PRIVE]->(h:`Hobby` { category : {category}}) " + "SET h.values = {values} "; JsonArray hobbies = userBook.getJsonArray("hobbies"); if (hobbies != null) { for (Object o : hobbies) { if (!(o instanceof JsonObject)) continue; JsonObject j = (JsonObject) o; b.add(q2, j.put("id", userId)); } } neo.executeTransaction(b.build(), null, true, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> r) { if ("ok".equals(r.body().getString("status"))) { if (updateUserBook) { JsonArray results = r.body().getJsonArray("results", new JsonArray()); JsonArray firstStatement = results.getJsonArray(0); if (firstStatement != null && firstStatement.size() > 0) { JsonObject object = firstStatement.getJsonObject(0); String picture = object.getString("picture", ""); cacheAvatarFromUserBook(userId, pictureId, StringUtils.isEmpty(picture)) .setHandler(e -> { if (e.succeeded()) { result.handle(new Either.Right<String, JsonObject>(new JsonObject())); } else { result.handle(new Either.Left<String, JsonObject>( r.body().getString("message", "update.error"))); } }); } } else { result.handle(new Either.Right<String, JsonObject>(new JsonObject())); } } else { result.handle( new Either.Left<String, JsonObject>(r.body().getString("message", "update.error"))); } } }); }
From source file:org.entcore.directory.services.impl.DefaultUserService.java
License:Open Source License
@Override public void addFunction(String id, String functionCode, JsonArray scope, String inherit, Handler<Either<String, JsonObject>> result) { JsonObject action = new JsonObject().put("action", "manual-add-user-function").put("userId", id) .put("function", functionCode).put("inherit", inherit).put("scope", scope); eb.send(Directory.FEEDER, action, ar -> { if (ar.succeeded()) { JsonArray res = ((JsonObject) ar.result().body()).getJsonArray("results"); JsonObject json = new JsonObject(); if (res.size() == 2) { JsonArray r = res.getJsonArray(1); if (r.size() == 1) { json = r.getJsonObject(0); }/*w w w . j a v a2 s. c o m*/ } result.handle(new Either.Right<>(json)); } else { result.handle(new Either.Left<>(ar.cause().getMessage())); } }); }