Example usage for io.vertx.core.eventbus Message body

List of usage examples for io.vertx.core.eventbus Message body

Introduction

In this page you can find the example usage for io.vertx.core.eventbus Message body.

Prototype

@CacheReturn
T body();

Source Link

Document

The body of the message.

Usage

From source file:org.entcore.feeder.dictionary.structures.GraphData.java

License:Open Source License

static void loadData(final Neo4j neo4j, final Handler<Message<JsonObject>> handler) {
    String query = "MATCH (s:Structure) " + "OPTIONAL MATCH s<-[:DEPENDS]-(g:Group) "
            + "OPTIONAL MATCH s<-[:BELONGS]-(c:Class) "
            + "return s, collect(distinct g.externalId) as groups, collect(distinct c.externalId) as classes ";
    neo4j.execute(query, new JsonObject(), new Handler<Message<JsonObject>>() {
        @Override/*from   ww  w  .  j av a  2s  . c o  m*/
        public void handle(Message<JsonObject> message) {
            String query = "MATCH (p:Profile) " + "OPTIONAL MATCH p<-[:COMPOSE]-(f:Function) "
                    + "return p, collect(distinct f.externalId) as functions ";
            final AtomicInteger count = new AtomicInteger(2);
            neo4j.execute(query, new JsonObject(), new Handler<Message<JsonObject>>() {
                @Override
                public void handle(Message<JsonObject> message) {
                    JsonArray res = message.body().getJsonArray("result");
                    if ("ok".equals(message.body().getString("status")) && res != null) {
                        for (Object o : res) {
                            if (!(o instanceof JsonObject))
                                continue;
                            JsonObject r = (JsonObject) o;
                            JsonObject p = r.getJsonObject("p", new JsonObject()).getJsonObject("data");
                            profiles.putIfAbsent(p.getString("externalId"),
                                    new Profile(p, r.getJsonArray("functions")));
                        }
                    }
                    if (handler != null && count.decrementAndGet() == 0) {
                        handler.handle(message);
                    }
                }
            });
            JsonArray res = message.body().getJsonArray("result");
            if ("ok".equals(message.body().getString("status")) && res != null) {
                for (Object o : res) {
                    if (!(o instanceof JsonObject))
                        continue;
                    JsonObject r = (JsonObject) o;
                    JsonObject s = r.getJsonObject("s", new JsonObject()).getJsonObject("data");
                    Structure structure = new Structure(s, r.getJsonArray("groups"), r.getJsonArray("classes"));
                    String externalId = s.getString("externalId");
                    structures.putIfAbsent(externalId, structure);
                    String UAI = s.getString("UAI");
                    if (UAI != null && !UAI.trim().isEmpty()) {
                        structuresByUAI.putIfAbsent(UAI, structure);
                    }
                    JsonArray joinKeys = s.getJsonArray("joinKey");
                    if (joinKeys != null && joinKeys.size() > 0) {
                        for (Object key : joinKeys) {
                            externalIdMapping.putIfAbsent(key.toString(), externalId);
                        }
                    }
                }
            }
            if (handler != null && count.decrementAndGet() == 0) {
                handler.handle(message);
            }
        }
    });
}

From source file:org.entcore.feeder.dictionary.structures.Importer.java

License:Open Source License

public void init(final Neo4j neo4j, final String source, String acceptLanguage,
        final Handler<Message<JsonObject>> handler) {
    this.neo4j = neo4j;
    this.currentSource = source;
    this.report = new Report(acceptLanguage);
    this.transactionHelper = new TransactionHelper(neo4j, 1000);
    GraphData.loadData(neo4j, new Handler<Message<JsonObject>>() {
        @Override/*from w w w  . j a  va2s .  c  o m*/
        public void handle(Message<JsonObject> event) {
            firstImport = GraphData.getStructures().isEmpty();
            structures = GraphData.getStructures();
            structuresByUAI = GraphData.getStructuresByUAI();
            externalIdMapping = GraphData.getExternalIdMapping();
            profiles = GraphData.getProfiles();
            persEducNat = new PersEducNat(transactionHelper, externalIdMapping, userImportedExternalId, report,
                    currentSource);
            if ("CSV".equals(source) && "ok".equals(event.body().getString("status"))) {
                loadFieldOfStudy(handler);
            } else {
                if (handler != null) {
                    handler.handle(event);
                }
            }
        }
    });
}

From source file:org.entcore.feeder.dictionary.structures.Importer.java

License:Open Source License

private void loadFieldOfStudy(final Handler<Message<JsonObject>> handler) {
    Neo4j.getInstance().execute("MATCH (f:FieldOfStudy) return f.externalId as externalId, f.name as name",
            new JsonObject(), new Handler<Message<JsonObject>>() {
                @Override// ww w.j a  va 2 s  .  c o  m
                public void handle(Message<JsonObject> event) {
                    final JsonArray res = event.body().getJsonArray("result");
                    if ("ok".equals(event.body().getString("status")) && res != null) {
                        for (Object o : res) {
                            if (!(o instanceof JsonObject))
                                continue;
                            final JsonObject j = (JsonObject) o;
                            fieldOfStudy.putIfAbsent(j.getString("name"), j.getString("externalId"));
                        }
                    }
                    if (handler != null) {
                        handler.handle(event);
                    }
                }
            });
}

From source file:org.entcore.feeder.dictionary.structures.Importer.java

License:Open Source License

public static void markMissingUsers(String structureExternalId, String currentSource,
        final Set<String> userImportedExternalId, final TransactionHelper transactionHelper, String prefix,
        final Handler<Void> handler) {
    String query;// ww  w . j a v  a  2s  . c  om
    JsonObject params = new JsonObject().put("currentSource", currentSource);
    String filter = "";
    if (isNotEmpty(prefix)) {
        filter = "AND u.externalId STARTS WITH {prefix} ";
        params.put("prefix", prefix);
    }
    if (structureExternalId != null) {
        query = "MATCH (:Structure {externalId : {externalId}})<-[:DEPENDS]-(:ProfileGroup)<-[:IN]-(u:User) "
                + "WHERE u.source = {currentSource} " + filter + "RETURN u.externalId as externalId";
        params.put("externalId", structureExternalId);
    } else {
        query = "MATCH (u:User) WHERE u.source = {currentSource} " + filter
                + "RETURN u.externalId as externalId";
    }
    TransactionManager.getNeo4jHelper().execute(query, params, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> message) {
            JsonArray res = message.body().getJsonArray("result");
            if ("ok".equals(message.body().getString("status")) && res != null) {
                Set<String> existingUser = new TreeSet<>();
                for (Object o : res) {
                    if (!(o instanceof JsonObject))
                        continue;
                    String externalId = ((JsonObject) o).getString("externalId");
                    if (externalId != null) {
                        existingUser.add(externalId);
                    }
                }
                existingUser.removeAll(userImportedExternalId); // set difference
                String q = // mark missing users
                        "START u=node:node_auto_index(externalId={externalId}) "
                                + "WHERE NOT(HAS(u.disappearanceDate)) " + "SET u.disappearanceDate = {date} ";
                JsonObject p = new JsonObject().put("date", System.currentTimeMillis());
                for (String eId : existingUser) {
                    transactionHelper.add(q, p.copy().put("externalId", eId));
                }
                String q2 = // remove mark of imported users
                        "START u=node:node_auto_index(externalId={externalId}) "
                                + "WHERE HAS(u.disappearanceDate) " + "REMOVE u.disappearanceDate ";
                for (String eId : userImportedExternalId) {
                    transactionHelper.add(q2, new JsonObject().put("externalId", eId));
                }
            }
            handler.handle(null);
        }
    });
}

From source file:org.entcore.feeder.dictionary.structures.PostImport.java

License:Open Source License

private void storeImportedEvent() {
    String countQuery = "MATCH (:User) WITH count(*) as nbUsers "
            + "MATCH (:Structure) WITH count(*) as nbStructures, nbUsers "
            + "MATCH (:Class) RETURN nbUsers, nbStructures, count(*) as nbClasses ";
    neo4j.execute(countQuery, (JsonObject) null, new Handler<Message<JsonObject>>() {
        @Override/*from ww w  .j  av a2  s .com*/
        public void handle(Message<JsonObject> event) {
            JsonArray res = event.body().getJsonArray("result");
            if ("ok".equals(event.body().getString("status")) && res != null && res.size() == 1) {
                eventStore.createAndStoreEvent(Feeder.FeederEvent.IMPORT.name(), (UserInfos) null,
                        res.getJsonObject(0));
            } else {
                logger.error(event.body().getString("message"));
            }
        }
    });
}

From source file:org.entcore.feeder.dictionary.structures.PostImport.java

License:Open Source License

private void applyComRules(final Handler<Void> handler) {
    if (config.getBoolean("apply-communication-rules", false)) {
        String q = "MATCH (s:Structure) return COLLECT(s.id) as ids";
        neo4j.execute(q, new JsonObject(), new Handler<Message<JsonObject>>() {
            @Override//from   w  w  w . j a v  a 2s . c om
            public void handle(Message<JsonObject> message) {
                JsonArray ids = message.body().getJsonArray("result",
                        new fr.wseduc.webutils.collections.JsonArray());
                if ("ok".equals(message.body().getString("status")) && ids != null && ids.size() == 1) {
                    JsonObject j = new JsonObject().put("action", "initAndApplyDefaultCommunicationRules")
                            .put("schoolIds", (ids.getJsonObject(0)).getJsonArray("ids",
                                    new fr.wseduc.webutils.collections.JsonArray()));
                    eb.send("wse.communication", j, new DeliveryOptions().setSendTimeout(3600 * 1000l),
                            handlerToAsyncHandler(new Handler<Message<JsonObject>>() {
                                @Override
                                public void handle(Message<JsonObject> event) {
                                    if (!"ok".equals(event.body().getString("status"))) {
                                        logger.error("Init rules error : " + event.body().getString("message"));
                                    } else {
                                        logger.info("Communication rules applied.");
                                    }
                                    handler.handle(null);
                                }
                            }));
                } else {
                    logger.error(message.body().getString("message"));
                    handler.handle(null);
                }
            }
        });
    } else {
        handler.handle(null);
    }
}

From source file:org.entcore.feeder.dictionary.structures.Structure.java

License:Open Source License

public void transition(final Handler<Message<JsonObject>> handler) {
    final TransactionHelper tx = TransactionManager.getInstance().getTransaction("GraphDataUpdate");
    String query = "MATCH (s:Structure {id : {id}})<-[:BELONGS]-(c:Class)"
            + "<-[:DEPENDS]-(cpg:Group)<-[:IN]-(u:User) " + "OPTIONAL MATCH s<-[:DEPENDS]-(fg:FunctionalGroup) "
            + "RETURN collect(distinct u.id) as users, collect(distinct cpg.id) as profileGroups, "
            + "collect(distinct fg.id) as functionalGroups";
    JsonObject params = new JsonObject().put("id", id);
    tx.getNeo4j().execute(query, params, new Handler<Message<JsonObject>>() {
        @Override//from w w w .jav  a  2 s  .c o  m
        public void handle(Message<JsonObject> event) {
            JsonArray r = event.body().getJsonArray("result");
            if ("ok".equals(event.body().getString("status")) && r != null && r.size() == 1) {
                final JsonObject res = r.getJsonObject(0);
                usersInGroups(new Handler<Message<JsonObject>>() {

                    @Override
                    public void handle(Message<JsonObject> event) {
                        if ("ok".equals(event.body().getString("status"))) {
                            for (Object u : res.getJsonArray("users")) {
                                User.backupRelationship(u.toString(), tx);
                                User.transition(u.toString(), tx);
                            }
                            transitionClassGroup();
                            handler.handle(event);
                        } else {
                            log.error("Structure " + id + " transition error - useringroups.");
                            log.error(event.body().encode());
                            handler.handle(event);
                        }
                    }
                });
            } else {
                log.error("Structure " + id + " transition error.");
                log.error(event.body().encode());
                handler.handle(event);
            }
        }
    });
}

From source file:org.entcore.feeder.dictionary.structures.Transition.java

License:Open Source License

public void launch(final String structureExternalId, final Handler<Message<JsonObject>> handler) {
    if (GraphData.isReady()) {
        GraphData.loadData(TransactionManager.getInstance().getNeo4j(), new Handler<Message<JsonObject>>() {
            @Override//  w ww. j a v  a 2s  . c o m
            public void handle(Message<JsonObject> res) {
                if (!"ok".equals(res.body().getString("status"))) {
                    log.error(res.body().getString("message"));
                    if (handler != null) {
                        handler.handle(res);
                    }
                } else if (structureExternalId != null && !structureExternalId.trim().isEmpty()) {
                    transitionStructure(structureExternalId, handler);
                } else {
                    transitionStructures(handler);
                }
            }
        });
    } else {
        if (handler != null) {
            handler.handle(new ResultMessage().error("Concurrent graph migration"));
        }
    }
}

From source file:org.entcore.feeder.dictionary.structures.Transition.java

License:Open Source License

private void transitionStructures(final Handler<Message<JsonObject>> handler) {
    getTransaction(handler);/*  w  ww .j a  va 2s.co m*/
    Set<String> s = GraphData.getStructures().keySet();
    final String[] structuresExternalId = s.toArray(new String[s.size()]);
    final Handler[] handlers = new Handler[structuresExternalId.length + 1];
    handlers[handlers.length - 1] = commitHandler(handler, null, null);

    for (int i = structuresExternalId.length - 1; i >= 0; i--) {
        final int j = i;
        handlers[i] = new Handler<Message<JsonObject>>() {
            @Override
            public void handle(Message<JsonObject> m) {
                if ("ok".equals(m.body().getString("status"))) {

                    Structure s = GraphData.getStructures().get(structuresExternalId[j]);
                    if (s == null) {
                        log.error("Missing structure with externalId : " + structuresExternalId[j]);
                        if (handler != null) {
                            handler.handle(new ResultMessage()
                                    .error("Missing structure with externalId : " + structuresExternalId[j]));
                        }
                        return;
                    }
                    s.transition(commitHandler(handler, handlers[j + 1], s.getStruct()));
                } else {
                    TransactionManager.getInstance().rollback(GRAPH_DATA_UPDATE);
                    log.error("Transition error");
                    log.error(m.body().encode());
                    if (handler != null) {
                        handler.handle(m);
                    }
                }
            }
        };
    }
    handlers[0].handle(new ResultMessage().put("result", new fr.wseduc.webutils.collections.JsonArray()));
}

From source file:org.entcore.feeder.dictionary.structures.Transition.java

License:Open Source License

private Handler<Message<JsonObject>> commitHandler(final Handler<Message<JsonObject>> resHandler,
        final Handler<Message<JsonObject>> handler, final JsonObject structure) {
    return new Handler<Message<JsonObject>>() {
        @Override//from w  w w .  j a v  a  2  s.co  m
        public void handle(Message<JsonObject> m) {
            if ("ok".equals(m.body().getString("status"))) {
                try {
                    TransactionManager.getInstance().persist(GRAPH_DATA_UPDATE, false,
                            new Handler<Message<JsonObject>>() {
                                @Override
                                public void handle(Message<JsonObject> event) {
                                    JsonArray results = m.body().getJsonArray("results");
                                    if ("ok".equals(event.body().getString("status")) && results != null
                                            && results.size() == 2) {
                                        JsonArray r = getOrElse(results.getJsonArray(0),
                                                new fr.wseduc.webutils.collections.JsonArray());
                                        publishTransition(structure, results.getJsonArray(1));
                                        if (r.size() > 0) {
                                            publishDeleteGroups(vertx.eventBus(), log, r);
                                            if (handler != null) {
                                                vertx.setTimer(delayBetweenStructure, eventTimer -> next(r));
                                            } else {
                                                next(r);
                                            }
                                        } else {
                                            next(r);
                                        }
                                    } else {
                                        log.error("Transition commit error");
                                        log.error(event.body().encode());
                                        if (resHandler != null) {
                                            resHandler.handle(event);
                                        }
                                    }
                                }
                            });
                } catch (Exception e) {
                    log.error("Transition commit error");
                    log.error(e.getMessage(), e);
                    if (resHandler != null) {
                        resHandler.handle(new ResultMessage().error(e.getMessage()));
                    }
                }
            } else {
                TransactionManager.getInstance().rollback(GRAPH_DATA_UPDATE);
                log.error("Transition error");
                log.error(m.body().encode());
                if (resHandler != null) {
                    resHandler.handle(m);
                }
            }
        }

        protected void next(JsonArray r) {
            if (handler != null) {
                if (getTransaction(resHandler) == null)
                    return;
            }
            if (handler != null) {
                handler.handle(new ResultMessage());
            } else {
                resHandler.handle(new ResultMessage().put("result", r));
            }
        }
    };
}