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.directory.services.impl.DefaultUserService.java

License:Open Source License

@Override
public void mergeByKey(String userId, JsonObject body, Handler<Either<String, JsonObject>> handler) {
    if (Utils.defaultValidationParamsNull(handler, userId, body))
        return;//www.  j ava  2s  . c o m
    JsonObject action = new JsonObject().put("action", "merge-by-keys").put("originalUserId", userId)
            .put("mergeKeys", body.getJsonArray("mergeKeys"));
    eb.send(Directory.FEEDER, action, handlerToAsyncHandler(validUniqueResultHandler(5, handler)));
}

From source file:org.entcore.feeder.aaf.PersonnelImportProcessing.java

License:Open Source License

@Override
public void process(JsonObject object) {
    List<String> c = object.getJsonArray("classes") != null ? object.getJsonArray("classes").getList()
            : new LinkedList<>();
    createGroups(object.getJsonArray("groups"), c, null);
    createClasses(new fr.wseduc.webutils.collections.JsonArray(c));
    createFunctionGroups(object.getJsonArray("functions"), null);
    createHeadTeacherGroups(object.getJsonArray("headTeacher"), null);
    linkMef(object.getJsonArray("modules"));
    String profile = detectProfile(object);
    object.put("profiles", new fr.wseduc.webutils.collections.JsonArray()
            .add((TEACHER_PROFILE_EXTERNAL_ID.equals(profile) ? "Teacher" : "Personnel")));
    String email = object.getString("email");
    if (email != null && !email.trim().isEmpty()) {
        object.put("emailAcademy", email);
    }/*w  w  w. ja v a2s.  c o m*/
    importer.createOrUpdatePersonnel(object, profile, null, null, null, true, false);
}

From source file:org.entcore.feeder.aaf.PersonnelImportProcessing.java

License:Open Source License

protected String detectProfile(JsonObject object) {
    Boolean isTeacher = object.getBoolean("isTeacher");
    if (isTeacher != null) {
        return isTeacher ? TEACHER_PROFILE_EXTERNAL_ID : PERSONNEL_PROFILE_EXTERNAL_ID;
    } else {/*from ww  w  . ja va 2s.c  o  m*/
        JsonArray functions = object.getJsonArray("functions");
        if (object.getBoolean("teaches", false)) {
            return TEACHER_PROFILE_EXTERNAL_ID;
        } else if (functions != null && functions.size() > 0) {
            for (Object function : functions.getList()) {
                if (function != null
                        && (function.toString().contains("$DOC$") || function.toString().contains("$ENS$"))) {
                    return TEACHER_PROFILE_EXTERNAL_ID;
                }
            }
        }
        return PERSONNEL_PROFILE_EXTERNAL_ID;
    }
}

From source file:org.entcore.feeder.aaf.PersonnelImportProcessing2.java

License:Open Source License

@Override
public void process(JsonObject object) {
    List<String> c = object.getJsonArray("classes") != null ? object.getJsonArray("classes").getList()
            : new LinkedList<String>();
    final List<String[]> groups = new ArrayList<>();
    createGroups(object.getJsonArray("groups"), c, groups);
    String[][] classes = createClasses(new fr.wseduc.webutils.collections.JsonArray(c));
    JsonArray functions = object.getJsonArray("functions");
    JsonArray structuresByFunctions = null;
    if (functions != null) {
        Set<String> s = new HashSet<>();
        for (Object o : functions) {
            if (!(o instanceof String) || !o.toString().contains("$"))
                continue;
            s.add(o.toString().substring(0, o.toString().indexOf('$')));
        }//from w  w w.j a v a 2s.  co  m
        structuresByFunctions = new fr.wseduc.webutils.collections.JsonArray(new ArrayList<>(s));
    }
    createFunctionGroups(object.getJsonArray("functions"), groups);
    createHeadTeacherGroups(object.getJsonArray("headTeacher"), groups);
    importer.createOrUpdatePersonnel(object, detectProfile(object), structuresByFunctions, classes,
            groups.toArray(new String[][] {}), false, true);
}

From source file:org.entcore.feeder.aaf.StudentImportProcessing.java

License:Open Source License

@Override
public void process(JsonObject object) {
    createClasses(object.getJsonArray("classes"));
    createGroups(object.getJsonArray("groups"));
    JsonArray r = parseRelativeField(object.getJsonArray("relative"));
    if (r != null) {
        resp.addAll(r.getList());/*  w w  w.j a  va2  s  .  co m*/
    }
    object.put("profiles", new fr.wseduc.webutils.collections.JsonArray().add("Student"));
    importer.createOrUpdateStudent(object, DefaultProfiles.STUDENT_PROFILE_EXTERNAL_ID, null, null, null, null,
            null, true, false);
}

From source file:org.entcore.feeder.aaf.StudentImportProcessing2.java

License:Open Source License

@Override
public void process(JsonObject object) {
    String[][] classes = createClasses(object.getJsonArray("classes"));
    String[][] groups = createGroups(object.getJsonArray("groups"));
    JsonArray relative = parseRelativeField(object.getJsonArray("relative"));
    importer.createOrUpdateStudent(object, DefaultProfiles.STUDENT_PROFILE_EXTERNAL_ID, null, null, classes,
            groups, relative, false, true);
}

From source file:org.entcore.feeder.aaf1d.PersonnelImportProcessing1d.java

License:Open Source License

@Override
protected String detectProfile(JsonObject object) {
    Boolean isTeacher = object.getBoolean("isTeacher");

    if (isTeacher != null) {
        return isTeacher ? TEACHER_PROFILE_EXTERNAL_ID : PERSONNEL_PROFILE_EXTERNAL_ID;
    } else {/*from  w  ww . j  a  v a  2 s .  co m*/
        JsonArray functions = object.getJsonArray("functions");
        if (functions != null && functions.size() > 0) {
            for (Object f : functions) {
                if (!(f instanceof String))
                    continue;
                String function = (String) f;
                if (function.contains("ENS") || function.contains("DOC")) {
                    return TEACHER_PROFILE_EXTERNAL_ID;
                }
            }
        }
        return PERSONNEL_PROFILE_EXTERNAL_ID;
    }
}

From source file:org.entcore.feeder.aaf1d.PersonnelImportProcessing1d.java

License:Open Source License

@Override
public void process(JsonObject object) {
    String profile = detectProfile(object);
    object.put("profiles", new fr.wseduc.webutils.collections.JsonArray()
            .add((TEACHER_PROFILE_EXTERNAL_ID.equals(profile) ? "Teacher" : "Personnel")));
    String email = object.getString("email");
    if (email != null && !email.trim().isEmpty()) {
        object.put("emailAcademy", email);
    }/*from   w  ww  . j a va 2 s. c om*/
    JsonArray functions = object.getJsonArray("functions");
    JsonArray structuresByFunctions = null;
    if (functions != null) {
        Set<String> s = new HashSet<>();
        for (Object o : functions) {
            if (!(o instanceof String) || !o.toString().contains("$"))
                continue;
            s.add(o.toString().substring(0, o.toString().indexOf('$')));
        }
        structuresByFunctions = new fr.wseduc.webutils.collections.JsonArray(new ArrayList<>(s));
    }
    importer.createOrUpdatePersonnel(object, profile, structuresByFunctions, null, null, true, true);
}

From source file:org.entcore.feeder.csv.CsvFeeder.java

License:Open Source License

public void start(final String profile, final Structure structure, String file, String charset,
        final Importer importer, final Handler<Message<JsonObject>> handler) {
    importer.getReport().addProfile(profile);
    importer.createOrUpdateProfile(STUDENT_PROFILE);
    importer.createOrUpdateProfile(RELATIVE_PROFILE);
    importer.createOrUpdateProfile(PERSONNEL_PROFILE);
    importer.createOrUpdateProfile(TEACHER_PROFILE);
    importer.createOrUpdateProfile(GUEST_PROFILE);
    DefaultFunctions.createOrUpdateFunctions(importer);

    final Validator validator = ManualFeeder.profiles.get(profile);
    //      final Structure structure = importer.getStructure(structureExternalId);
    //      if (structure == null) {
    //         handler.handle(new ResultMessage().error("invalid.structure"));
    //         return;
    //      }// www. j  a  v a 2 s  .  c om

    try {
        CSVReader csvParser = getCsvReader(file, charset);
        final List<String> columns = new ArrayList<>();
        int nbColumns = 0;
        String[] strings;
        int i = 0;
        csvParserWhile: while ((strings = csvParser.readNext()) != null) {
            if (i == 0) {
                columnsMapper.getColumsNames(strings, columns, profile, handler);
                nbColumns = columns.size();
            } else if (!columns.isEmpty()) {
                if (emptyLine(strings)) {
                    i++;
                    continue;
                }
                if (strings.length > nbColumns) {
                    strings = Arrays.asList(strings).subList(0, nbColumns).toArray(new String[nbColumns]);
                }
                JsonObject user = new JsonObject();
                user.put("structures",
                        new fr.wseduc.webutils.collections.JsonArray().add(structure.getExternalId()));
                user.put("profiles", new fr.wseduc.webutils.collections.JsonArray().add(profile));
                List<String[]> classes = new ArrayList<>();

                // Class Admin
                if (isNotEmpty(structure.getOverrideClass())) {
                    String eId = structure.getOverrideClass(); // structure.getExternalId() + '$' + structure.getOverrideClass();
                    //structure.createClassIfAbsent(eId, structure.getOverrideClass());
                    final String[] classId = new String[3];
                    classId[0] = structure.getExternalId();
                    classId[1] = eId;
                    classId[2] = "";
                    classes.add(classId);
                }

                for (int j = 0; j < strings.length; j++) {
                    final String c = columns.get(j);
                    final String v = strings[j].trim();
                    if ((v.isEmpty() && !c.startsWith("child"))
                            || ("classes".equals(c) && isNotEmpty(structure.getOverrideClass())))
                        continue;
                    switch (validator.getType(c)) {
                    case "string":
                        if ("birthDate".equals(c)) {
                            Matcher m = frenchDatePatter.matcher(v);
                            if (m.find()) {
                                user.put(c, m.group(3) + "-" + m.group(2) + "-" + m.group(1));
                            } else {
                                user.put(c, v);
                            }
                        } else {
                            user.put(c, v);
                        }
                        break;
                    case "array-string":
                        JsonArray a = user.getJsonArray(c);
                        if (a == null) {
                            a = new fr.wseduc.webutils.collections.JsonArray();
                            user.put(c, a);
                        }
                        if (("classes".equals(c) || "subjectTaught".equals(c) || "functions".equals(c))
                                && !v.startsWith(structure.getExternalId() + "$")) {
                            a.add(structure.getExternalId() + "$" + v);
                        } else {
                            a.add(v);
                        }
                        break;
                    case "boolean":
                        user.put(c, "true".equals(v.toLowerCase()));
                        break;
                    default:
                        Object o = user.getValue(c);
                        final String v2;
                        if ("childClasses".equals(c) && !v.startsWith(structure.getExternalId() + "$")) {
                            v2 = structure.getExternalId() + "$" + v;
                        } else {
                            v2 = v;
                        }
                        if (o != null) {
                            if (o instanceof JsonArray) {
                                ((JsonArray) o).add(v2);
                            } else {
                                JsonArray array = new fr.wseduc.webutils.collections.JsonArray();
                                array.add(o).add(v2);
                                user.put(c, array);
                            }
                        } else {
                            user.put(c, v2);
                        }
                    }
                    if ("classes".equals(c)) {
                        String[] cc = v.split("\\$");
                        if (cc.length == 2 && !cc[1].matches("[0-9]+")) {
                            final String fosEId = importer.getFieldOfStudy().get(cc[1]);
                            if (fosEId != null) {
                                cc[1] = fosEId;
                            }
                        }
                        String eId = structure.getExternalId() + '$' + cc[0];
                        structure.createClassIfAbsent(eId, cc[0]);
                        final String[] classId = new String[3];
                        classId[0] = structure.getExternalId();
                        classId[1] = eId;
                        classId[2] = (cc.length == 2) ? cc[1] : "";
                        classes.add(classId);
                    }
                }
                String ca;
                long seed;
                JsonArray classesA;
                Object co = user.getValue("classes");
                if (co != null && co instanceof JsonArray) {
                    classesA = (JsonArray) co;
                } else if (co instanceof String) {
                    classesA = new fr.wseduc.webutils.collections.JsonArray().add(co);
                } else {
                    classesA = null;
                }
                if ("Student".equals(profile) && classesA != null && classesA.size() == 1) {
                    seed = defaultStudentSeed;
                    ca = classesA.getString(0);
                } else {
                    ca = String.valueOf(i);
                    seed = System.currentTimeMillis();
                }
                generateUserExternalId(user, ca, structure, seed);
                if ("Student".equals(profile)) {
                    studentExternalIdMapping.put(getHashMapping(user, ca, structure, seed),
                            user.getString("externalId"));
                }
                switch (profile) {
                case "Teacher":
                    importer.createOrUpdatePersonnel(user, TEACHER_PROFILE_EXTERNAL_ID,
                            user.getJsonArray("structures"), classes.toArray(new String[classes.size()][2]),
                            null, true, true);
                    break;
                case "Personnel":
                    importer.createOrUpdatePersonnel(user, PERSONNEL_PROFILE_EXTERNAL_ID,
                            user.getJsonArray("structures"), classes.toArray(new String[classes.size()][2]),
                            null, true, true);
                    break;
                case "Student":
                    importer.createOrUpdateStudent(user, STUDENT_PROFILE_EXTERNAL_ID, null, null,
                            classes.toArray(new String[classes.size()][2]), null, null, true, true);
                    break;
                case "Relative":
                    if (("Intitul".equals(strings[0]) && "Adresse Organisme".equals(strings[1]))
                            || ("".equals(strings[0]) && "Intitul".equals(strings[1])
                                    && "Adresse Organisme".equals(strings[2]))) {
                        break csvParserWhile;
                    }
                    JsonArray linkStudents = new fr.wseduc.webutils.collections.JsonArray();
                    for (String attr : user.fieldNames()) {
                        if ("childExternalId".equals(attr)) {
                            Object o = user.getValue(attr);
                            if (o instanceof JsonArray) {
                                for (Object c : (JsonArray) o) {
                                    if (c instanceof String && !((String) c).trim().isEmpty()) {
                                        linkStudents.add(c);
                                    }
                                }
                            } else if (o instanceof String && !((String) o).trim().isEmpty()) {
                                linkStudents.add(o);
                            }
                        } else if ("childUsername".equals(attr)) {
                            Object childUsername = user.getValue(attr);
                            Object childLastName = user.getValue("childLastName");
                            Object childFirstName = user.getValue("childFirstName");
                            Object childClasses;
                            if (isNotEmpty(structure.getOverrideClass())) {
                                childClasses = structure.getOverrideClass();
                            } else {
                                childClasses = user.getValue("childClasses");
                            }
                            if (childUsername instanceof JsonArray && childLastName instanceof JsonArray
                                    && childFirstName instanceof JsonArray && childClasses instanceof JsonArray
                                    && ((JsonArray) childClasses).size() == ((JsonArray) childLastName).size()
                                    && ((JsonArray) childFirstName).size() == ((JsonArray) childLastName)
                                            .size()) {
                                for (int j = 0; j < ((JsonArray) childUsername).size(); j++) {
                                    String mapping = structure.getExternalId()
                                            + ((JsonArray) childUsername).getString(j).trim()
                                            + ((JsonArray) childLastName).getString(j).trim()
                                            + ((JsonArray) childFirstName).getString(j).trim()
                                            + ((JsonArray) childClasses).getString(j).trim()
                                            + defaultStudentSeed;
                                    relativeStudentMapping(linkStudents, mapping);
                                }
                            } else if (childUsername instanceof String && childLastName instanceof String
                                    && childFirstName instanceof String && childClasses instanceof String) {
                                String mapping = structure.getExternalId() + childLastName.toString().trim()
                                        + childFirstName.toString().trim() + childClasses.toString().trim()
                                        + defaultStudentSeed;
                                relativeStudentMapping(linkStudents, mapping);
                            } else {
                                handler.handle(new ResultMessage().error("invalid.child.mapping"));
                                return;
                            }
                        } else if ("childLastName".equals(attr)
                                && !user.fieldNames().contains("childUsername")) {
                            Object childLastName = user.getValue(attr);
                            Object childFirstName = user.getValue("childFirstName");
                            Object childClasses;
                            if (isNotEmpty(structure.getOverrideClass())) {
                                childClasses = structure.getOverrideClass();
                            } else {
                                childClasses = user.getValue("childClasses");
                            }
                            if (childLastName instanceof JsonArray && childFirstName instanceof JsonArray
                                    && childClasses instanceof JsonArray
                                    && ((JsonArray) childClasses).size() == ((JsonArray) childLastName).size()
                                    && ((JsonArray) childFirstName).size() == ((JsonArray) childLastName)
                                            .size()) {
                                for (int j = 0; j < ((JsonArray) childLastName).size(); j++) {
                                    String mapping = structure.getExternalId()
                                            + ((JsonArray) childLastName).getString(j).trim()
                                            + ((JsonArray) childFirstName).getString(j).trim()
                                            + ((JsonArray) childClasses).getString(j).trim()
                                            + defaultStudentSeed;
                                    relativeStudentMapping(linkStudents, mapping);
                                }
                            } else if (childLastName instanceof String && childFirstName instanceof String
                                    && childClasses instanceof String) {
                                String mapping = structure.getExternalId() + childLastName.toString().trim()
                                        + childFirstName.toString().trim() + childClasses.toString().trim()
                                        + defaultStudentSeed;
                                relativeStudentMapping(linkStudents, mapping);
                            } else {
                                handler.handle(new ResultMessage().error("invalid.child.mapping"));
                                return;
                            }
                        }
                    }
                    importer.createOrUpdateUser(user, linkStudents, true);
                    break;
                case "Guest":
                    importer.createOrUpdateGuest(user, classes.toArray(new String[classes.size()][2]));
                    break;
                }
            }
            i++;
        }

        switch (profile) {
        case "Teacher":
            importer.getPersEducNat().createAndLinkSubjects(structure.getExternalId());
            break;
        case "Relative":
            importer.linkRelativeToClass(RELATIVE_PROFILE_EXTERNAL_ID, null, structure.getExternalId());
            importer.linkRelativeToStructure(RELATIVE_PROFILE_EXTERNAL_ID, null, structure.getExternalId());
            importer.addRelativeProperties(getSource());
            break;
        }
    } catch (Exception e) {
        handler.handle(new ResultMessage().error("csv.exception"));
        log.error("csv.exception", e);
    }
    //      importer.markMissingUsers(structure.getExternalId(), new Handler<Void>() {
    //         @Override
    //         public void handle(Void event) {
    //            importer.persist(handler);
    //         }
    //      });
    //   importer.persist(handler);
}

From source file:org.entcore.feeder.csv.CsvValidator.java

License:Open Source License

private void findUsers(final String path, final String profile, List<String> columns, int eii,
        final String charset, final Handler<JsonObject> handler) {
    mappingFinder.findExternalIds(structureId, path, profile, columns, eii, charset, new Handler<JsonArray>() {
        @Override/*from w  ww .  j a va 2 s.  co m*/
        public void handle(JsonArray errors) {
            if (errors.size() > 0) {
                for (Object o : errors) {
                    if (!(o instanceof JsonObject))
                        continue;
                    JsonObject j = (JsonObject) o;
                    JsonArray p = j.getJsonArray("params");
                    log.info(j.encode());
                    if (p != null && p.size() > 0) {
                        addErrorByFile(profile, j.getString("key"), p.encode()
                                .substring(1, p.encode().length() - 1).replaceAll("\"", "").split(","));
                    } else if (j.getString("key") != null) {
                        addError(profile, j.getString("key"));
                    } else {
                        addError(profile, "mapping.unknown.error");
                    }
                }
                handler.handle(result);
            } else {
                //validateFile(path, profile, columns, null, handler);
                checkFile(path, profile, charset, handler);
            }
        }
    });
}