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) 

Source Link

Document

Get the JsonArray value with the specified key

Usage

From source file:org.entcore.feeder.timetable.edt.EDTImporter.java

License:Open Source License

private void matchAndCreatePersEducNat(final Handler<AsyncResult<Void>> handler) {
    txXDT.commit(new Handler<Message<JsonObject>>() {
        @Override// w ww  . j  a  va2  s .  c o m
        public void handle(Message<JsonObject> event) {
            JsonArray res = event.body().getJsonArray("results");
            if ("ok".equals(event.body().getString("status")) && res != null) {
                for (Object o : res) {
                    setUsersId(o);
                }
                if (!notFoundPersEducNat.isEmpty()) {
                    try {
                        TransactionHelper tx = TransactionManager.getTransaction();
                        persEducNat.setTransactionHelper(tx);
                        for (JsonObject p : notFoundPersEducNat.values()) {
                            p.put("structures", new JsonArray().add(structureExternalId));
                            if ("Teacher".equals(p.getJsonArray("profiles").getString(0))) {
                                persEducNat.createOrUpdatePersonnel(p, TEACHER_PROFILE_EXTERNAL_ID, structure,
                                        null, null, true, true);
                            } else {
                                persEducNat.createOrUpdatePersonnel(p, PERSONNEL_PROFILE_EXTERNAL_ID, structure,
                                        null, null, true, true);
                            }
                        }
                        tx.commit(new Handler<Message<JsonObject>>() {
                            @Override
                            public void handle(Message<JsonObject> event) {
                                JsonArray res = event.body().getJsonArray("results");
                                if ("ok".equals(event.body().getString("status")) && res != null) {
                                    for (Object o : res) {
                                        setUsersId(o);
                                    }
                                    if (notFoundPersEducNat.isEmpty()) {
                                        handler.handle(new DefaultAsyncResult<>((Void) null));
                                    } else {
                                        for (Map.Entry<String, JsonObject> e : notFoundPersEducNat.entrySet()) {
                                            log.info(e.getKey() + " : " + e.getValue().encode());
                                        }
                                        handler.handle(new DefaultAsyncResult<Void>(
                                                new ValidationException("not.found.users.not.empty")));
                                    }
                                } else {
                                    handler.handle(new DefaultAsyncResult<Void>(
                                            new TransactionException(event.body().getString("message"))));
                                }
                            }
                        });
                    } catch (TransactionException e) {
                        handler.handle(new DefaultAsyncResult<Void>(e));
                    }
                } else {
                    handler.handle(new DefaultAsyncResult<>((Void) null));
                }
            } else {
                handler.handle(new DefaultAsyncResult<Void>(
                        new TransactionException(event.body().getString("message"))));
            }
        }

        private void setUsersId(Object o) {
            if ((o instanceof JsonArray) && ((JsonArray) o).size() > 0) {
                JsonObject j = ((JsonArray) o).getJsonObject(0);
                String idPronote = j.getString(IDPN);
                String id = j.getString("id");
                String profile = j.getString("profile");
                if (isNotEmpty(id) && isNotEmpty(idPronote) && isNotEmpty(profile)) {
                    notFoundPersEducNat.remove(idPronote);
                    if ("Teacher".equals(profile)) {
                        teachersMapping.put(idPronote, new String[] { id, getSource() });
                    } else {
                        String[] ident = idPronote.split("\\$");
                        if (ident.length == 2) {
                            personnels.put(ident[1], id);
                        }
                    }
                }
            }
        }
    });

}

From source file:org.entcore.feeder.timetable.edt.EDTImporter.java

License:Open Source License

void addEleve(JsonObject currentEntity) {
    final String sconetId = currentEntity.getString("IDSconet");
    if (isNotEmpty(sconetId)) {
        final JsonArray classes = currentEntity.getJsonArray("Classe");
        final JsonArray pcs = currentEntity.getJsonArray("PartieDeClasse");
        studentToGroups(sconetId, classes, this.classes);
        studentToGroups(sconetId, pcs, this.subClasses);
    }// w  ww .  j a va 2  s  .c  o  m
}

From source file:org.entcore.feeder.timetable.edt.EDTImporter.java

License:Open Source License

private void studentToGroups(String sconetId, JsonArray classes, Map<String, JsonObject> ref) {
    if (classes != null) {
        for (Object o : classes) {
            if (o instanceof JsonObject) {
                final String inDate = ((JsonObject) o).getString("DateEntree");
                final String outDate = ((JsonObject) o).getString("DateSortie");
                final String ident = ((JsonObject) o).getString(IDENT);
                if (inDate == null || ident == null || outDate == null || DateTime.parse(inDate).isAfterNow())
                    continue;
                final JsonObject j = ref.get(ident);
                if (j != null) {
                    JsonArray groups = j.getJsonArray("groups");
                    if (groups != null) {
                        for (Object o2 : groups) {
                            JsonObject group = this.groups.get(o2.toString());
                            if (group != null) {
                                String name = group.getString("Nom");
                                txXDT.add(STUDENTS_TO_GROUPS, new JsonObject().put("idSconet", sconetId)
                                        .put("externalId", structureExternalId + "$" + name).put("source", EDT)
                                        .put("inDate", DateTime.parse(inDate).getMillis())
                                        .put("outDate", DateTime.parse(outDate).getMillis())
                                        .put("now", importTimestamp));
                            }/*from  w  w  w  .j  a  va  2  s. c  o m*/
                        }
                    }
                }
            }
        }
    }
}

From source file:org.entcore.feeder.timetable.edt.EDTImporter.java

License:Open Source License

void addCourse(JsonObject currentEntity) {
    final List<Long> weeks = new ArrayList<>();
    final List<JsonObject> items = new ArrayList<>();

    for (String attr : currentEntity.fieldNames()) {
        if (!ignoreAttributes.contains(attr) && currentEntity.getValue(attr) instanceof JsonArray) {
            for (Object o : currentEntity.getJsonArray(attr)) {
                if (!(o instanceof JsonObject))
                    continue;
                final JsonObject j = (JsonObject) o;
                j.put("itemType", attr);
                final String week = j.getString("Semaines");
                if (week != null) {
                    weeks.add(Long.valueOf(week));
                    items.add(j);/*from   ww w  .  ja  v  a  2  s.co  m*/
                }
            }
        }
    }

    if (log.isDebugEnabled() && currentEntity.containsKey("SemainesAnnulation")) {
        log.debug(currentEntity.encode());
    }
    final Long cancelWeek = (currentEntity.getString("SemainesAnnulation") != null)
            ? Long.valueOf(currentEntity.getString("SemainesAnnulation"))
            : null;
    BitSet lastWeek = new BitSet(weeks.size());
    int startCourseWeek = 0;
    for (int i = 1; i < 53; i++) {
        final BitSet currentWeek = new BitSet(weeks.size());
        boolean enabledCurrentWeek = false;
        for (int j = 0; j < weeks.size(); j++) {
            if (cancelWeek != null && ((1L << i) & cancelWeek) != 0) {
                currentWeek.set(j, false);
            } else {
                final Long week = weeks.get(j);
                currentWeek.set(j, ((1L << i) & week) != 0);
            }
            enabledCurrentWeek = enabledCurrentWeek | currentWeek.get(j);
        }
        if (!currentWeek.equals(lastWeek)) {
            if (startCourseWeek > 0) {
                persistCourse(generateCourse(startCourseWeek, i - 1, lastWeek, items, currentEntity));
            }
            startCourseWeek = enabledCurrentWeek ? i : 0;
            lastWeek = currentWeek;
        }
    }
}

From source file:org.entcore.feeder.timetable.edt.EDTImporter.java

License:Open Source License

private JsonObject generateCourse(int startCourseWeek, int endCourseWeek, BitSet enabledItems,
        List<JsonObject> items, JsonObject entity) {
    final int day = Integer.parseInt(entity.getString("Jour"));
    final int startPlace = Integer.parseInt(entity.getString("NumeroPlaceDebut"));
    final int placesNumber = Integer.parseInt(entity.getString("NombrePlaces"));
    DateTime startDate = startDateWeek1.plusWeeks(startCourseWeek - 1).plusDays(day - 1);
    DateTime endDate = startDate.plusWeeks(endCourseWeek - startCourseWeek);
    startDate = startDate.plusSeconds(slots.get(entity.getString("NumeroPlaceDebut")).getStart());
    endDate = endDate.plusSeconds(slots.get(String.valueOf((startPlace + placesNumber - 1))).getEnd());
    final JsonObject c = new JsonObject().put("structureId", structureId)
            .put("subjectId", subjects.get(entity.getJsonArray("Matiere").getJsonObject(0).getString(IDENT)))
            .put("startDate", startDate.toString()).put("endDate", endDate.toString())
            .put("dayOfWeek", startDate.getDayOfWeek());

    for (int i = 0; i < enabledItems.size(); i++) {
        if (enabledItems.get(i)) {
            final JsonObject item = items.get(i);
            final String ident = item.getString(IDENT);
            switch (item.getString("itemType")) {
            case "Professeur":
                JsonArray teachersArray = c.getJsonArray("teacherIds");
                if (teachersArray == null) {
                    teachersArray = new fr.wseduc.webutils.collections.JsonArray();
                    c.put("teacherIds", teachersArray);
                }/*from w ww  . j a  va2  s  .  c  om*/
                final String tId = teachers.get(ident);
                if (isNotEmpty(tId)) {
                    teachersArray.add(tId);
                }
                break;
            case "Classe":
                JsonArray classesArray = c.getJsonArray("classes");
                if (classesArray == null) {
                    classesArray = new fr.wseduc.webutils.collections.JsonArray();
                    c.put("classes", classesArray);
                }
                JsonObject ci = classes.get(ident);
                if (ci != null) {
                    classesArray.add(ci.getString("className"));
                }
                break;
            case "Groupe":
                JsonArray groupsArray = c.getJsonArray("groups");
                if (groupsArray == null) {
                    groupsArray = new fr.wseduc.webutils.collections.JsonArray();
                    c.put("groups", groupsArray);
                }
                JsonObject g = groups.get(ident);
                if (g != null) {
                    groupsArray.add(g.getString("Nom"));
                }
                break;
            case "Materiel":
                JsonArray equipmentsArray = c.getJsonArray("equipmentLabels");
                if (equipmentsArray == null) {
                    equipmentsArray = new fr.wseduc.webutils.collections.JsonArray();
                    c.put("equipmentLabels", equipmentsArray);
                }
                final String eId = equipments.get(ident);
                if (isNotEmpty(eId)) {
                    equipmentsArray.add(eId);
                }

                break;
            case "Salle":
                JsonArray roomsArray = c.getJsonArray("roomLabels");
                if (roomsArray == null) {
                    roomsArray = new fr.wseduc.webutils.collections.JsonArray();
                    c.put("roomLabels", roomsArray);
                }
                final String rId = rooms.get(ident);
                if (isNotEmpty(rId)) {
                    roomsArray.add(rId);
                }
                break;
            case "Personnel":
                JsonArray personnelsArray = c.getJsonArray("personnelIds");
                if (personnelsArray == null) {
                    personnelsArray = new fr.wseduc.webutils.collections.JsonArray();
                    c.put("personnelIds", personnelsArray);
                }
                final String pId = personnels.get(ident);
                if (isNotEmpty(pId)) {
                    personnelsArray.add(pId);
                }
                break;
            }
        }
    }
    try {
        c.put("_id", JsonUtil.checksum(c));
    } catch (NoSuchAlgorithmException e) {
        log.error("Error generating course checksum", e);
    }
    return c;
}

From source file:org.entcore.feeder.timetable.udt.UDTImporter.java

License:Open Source License

void initPeriods(JsonObject currentEntity) {
    JsonArray weeks = currentEntity.getJsonArray("semaine");
    if (weeks != null && weeks.size() > 0) {
        int oldWeek = startDateWeek1.getWeekOfWeekyear();
        for (Object o : weeks) {
            if (o instanceof JsonObject) {
                int week = Integer.parseInt(((JsonObject) o).getString("num"));
                if (week != oldWeek) {
                    periods.put(oldWeek, (week == 1 ? maxYearWeek : week - 1));
                    oldWeek = week;//from  w  ww .j  a  v  a 2  s .  com
                }
            }
        }
        periods.put(oldWeek, new DateTime(endStudents).getWeekOfWeekyear());
        for (int i = new DateTime(endStudents).getWeekOfWeekyear() + 1; i < startDateWeek1
                .getWeekOfWeekyear(); i++) {
            holidaysWeeks.add(i);
        }
    }
}

From source file:org.entcore.feeder.timetable.udt.UDTImporter.java

License:Open Source License

void addGroup2(JsonObject currentEntity) {
    final String codeGroup = currentEntity.getString("code_gpe");
    final String name = currentEntity.getString("nom");
    if (isNotEmpty(codeGroup)) {
        final String groupId = currentEntity.getString("code_div") + codeGroup;
        JsonObject group = groups.get(groupId);
        if (group == null) {
            log.warn("addGroup2 : unknown.group.mapping");
            return;
        }//w  ww .  j  ava2 s  .  c  om
        JsonArray groups = group.getJsonArray("groups");
        if (groups == null) {
            groups = new fr.wseduc.webutils.collections.JsonArray();
            group.put("groups", groups);
        }
        groups.add(name);
    } else {
        final String classId = currentEntity.getString("code_div");
        JsonObject classe = classes.get(classId);
        if (classe == null) {
            log.warn("addGroup2 : unknown.class.mapping");
            return;
        }
        JsonArray groups = classe.getJsonArray("groups");
        if (groups == null) {
            groups = new fr.wseduc.webutils.collections.JsonArray();
            classe.put("groups", groups);
        }
        groups.add(name);
    }
    regroup.put(currentEntity.getString(CODE), name);
    txXDT.add(CREATE_GROUPS + "SET fg.idrgpmt = {idrgpmt} ",
            new JsonObject().put("structureExternalId", structureExternalId).put("name", name)
                    .put("displayNameSearchField", Validator.sanitize(name))
                    .put("externalId", structureExternalId + "$" + name).put("id", UUID.randomUUID().toString())
                    .put("source", getSource()).put("idrgpmt", currentEntity.getString("id")));
}

From source file:org.entcore.feeder.timetable.udt.UDTImporter.java

License:Open Source License

void addEleve(JsonObject currentEntity) {
    if ("0".equals(currentEntity.getString("theorique"))) {
        return;/*from   w  w w.  j a  v a 2s  . co  m*/
    }
    final String ele = currentEntity.getString("ele");
    if (isEmpty(ele)) {
        report.addErrorWithParams("invalid.epj", currentEntity.encode());
        return;
    }
    JsonObject eleve = eleves.get(ele);
    if (eleve == null) {
        report.addErrorWithParams("missing.student", currentEntity.encode());
        return;
    }
    final String codeGroup = currentEntity.getString("gpe");
    final String codeDiv = currentEntity.getString("code_div");
    JsonArray groups;
    if (isNotEmpty(codeGroup)) {
        JsonObject group = this.groups.get(codeDiv + codeGroup);
        if (group == null) {
            log.warn("addEleve : unknown.group.mapping");
            return;
        }
        final String name = group.getString("code_div") + " Gr " + group.getString(CODE);
        txXDT.add(STUDENTS_TO_GROUPS,
                new JsonObject().put("firstName", eleve.getString("prenom", "").toLowerCase())
                        .put("lastName", eleve.getString("nom", "").toLowerCase())
                        .put("birthDate", StringValidation.convertDate(eleve.getString("naissance", "")))
                        .put("externalId", structureExternalId + "$" + name)
                        .put("structureExternalId", structureExternalId).put("source", UDT)
                        .put("inDate", importTimestamp).put("outDate", endStudents)
                        .put("now", importTimestamp));
        groups = group.getJsonArray("groups");

    } else {
        JsonObject classe = classes.get(codeDiv);
        if (classe == null) {
            log.warn("addEleve : unknown.class.mapping");
            return;
        }
        groups = classe.getJsonArray("groups");
    }
    if (groups != null) {
        for (Object o2 : groups) {
            txXDT.add(STUDENTS_TO_GROUPS,
                    new JsonObject().put("firstName", eleve.getString("prenom", "").toLowerCase())
                            .put("lastName", eleve.getString("nom", "").toLowerCase())
                            .put("birthDate", StringValidation.convertDate(eleve.getString("naissance", "")))
                            .put("externalId", structureExternalId + "$" + o2.toString())
                            .put("structureExternalId", structureExternalId).put("source", UDT)
                            .put("inDate", importTimestamp).put("outDate", endStudents)
                            .put("now", importTimestamp));
        }
    }
}

From source file:org.entcore.feeder.utils.Report.java

License:Open Source License

public void addSoftErrorByFile(String file, String key, String... errors) {
    JsonObject softErrors = result.getJsonObject("softErrors");
    if (softErrors == null) {
        softErrors = new JsonObject();
        result.put("softErrors", softErrors);
    }//w w w  .  ja  va2s.c  om
    JsonArray f = softErrors.getJsonArray(file);
    if (f == null) {
        f = new fr.wseduc.webutils.collections.JsonArray();
        softErrors.put(file, f);
    }
    String error = i18n.translate(key, I18n.DEFAULT_DOMAIN, acceptLanguage, errors);
    f.add(error);
    log.error(error);
}

From source file:org.entcore.feeder.utils.Report.java

License:Open Source License

private void emailReport(final Vertx vertx, final EmailFactory emailFactory, final JsonObject sendReport,
        final JsonObject result) {
    final JsonObject reqParams = new JsonObject().put("headers",
            new JsonObject().put("Accept-Language", acceptLanguage));
    emailFactory.getSender().sendEmail(new JsonHttpServerRequest(reqParams),
            sendReport.getJsonArray("to").getList(),
            sendReport.getJsonArray("cc") != null ? sendReport.getJsonArray("cc").getList() : null,
            sendReport.getJsonArray("bcc") != null ? sendReport.getJsonArray("bcc").getList() : null,
            sendReport.getString("project", "")
                    + i18n.translate("import.report", I18n.DEFAULT_DOMAIN, acceptLanguage) + " - "
                    + DateTime.now().toString(DateTimeFormat.forPattern("yyyy-MM-dd")),
            "email/report.html", result, false, ar -> {
                if (ar.failed()) {
                    log.error("Error sending report email.", ar.cause());
                }//from w  w w.j  av  a2s . c o  m
            });
}